use super::flags::{DragDropPayloadCond, DragDropSourceFlags, validate_drag_drop_source_flags};
use super::payload::{TypedPayload, make_typed_payload};
use super::validation::validate_payload_submission;
use crate::{Ui, sys};
use std::ffi;
#[derive(Debug)]
pub struct DragDropSource<'ui, T> {
pub(super) name: T,
pub(super) flags: DragDropSourceFlags,
pub(super) cond: DragDropPayloadCond,
pub(super) ui: &'ui Ui,
}
impl<'ui, T: AsRef<str>> DragDropSource<'ui, T> {
#[inline]
pub fn flags(mut self, flags: DragDropSourceFlags) -> Self {
validate_drag_drop_source_flags("DragDropSource::flags()", flags);
self.flags = flags;
self
}
#[inline]
pub fn condition(mut self, cond: DragDropPayloadCond) -> Self {
self.cond = cond;
self
}
#[inline]
pub fn begin(self) -> Option<DragDropSourceTooltip<'ui>> {
self.begin_payload(())
}
#[inline]
pub fn begin_payload<P: Copy + 'static>(
self,
payload: P,
) -> Option<DragDropSourceTooltip<'ui>> {
unsafe {
let payload_size = std::mem::size_of::<TypedPayload<P>>();
assert!(
payload_size <= i32::MAX as usize,
"DragDropSource::begin_payload() payload size exceeds Dear ImGui's i32 payload range"
);
let payload = make_typed_payload(payload);
self.begin_payload_unchecked(&payload as *const _ as *const ffi::c_void, payload_size)
}
}
pub unsafe fn begin_payload_unchecked(
&self,
ptr: *const ffi::c_void,
size: usize,
) -> Option<DragDropSourceTooltip<'ui>> {
validate_payload_submission(
self.name.as_ref(),
ptr,
size,
"DragDropSource::begin_payload_unchecked()",
);
validate_drag_drop_source_flags("DragDropSource::begin_payload_unchecked()", self.flags);
unsafe {
let should_begin = sys::igBeginDragDropSource(self.flags.bits() as i32);
if should_begin {
sys::igSetDragDropPayload(
self.ui.scratch_txt(self.name.as_ref()),
ptr,
size,
self.cond as i32,
);
Some(DragDropSourceTooltip::new(self.ui))
} else {
None
}
}
}
}
#[derive(Debug)]
pub struct DragDropSourceTooltip<'ui> {
_ui: &'ui Ui,
}
impl<'ui> DragDropSourceTooltip<'ui> {
fn new(ui: &'ui Ui) -> Self {
Self { _ui: ui }
}
pub fn end(self) {
}
}
impl Drop for DragDropSourceTooltip<'_> {
fn drop(&mut self) {
unsafe {
sys::igEndDragDropSource();
}
}
}