Skip to main content

dear_imgui_rs/drag_drop/
ui.rs

1use super::flags::{DragDropPayloadCond, DragDropSourceFlags};
2use super::payload::DragDropPayload;
3use super::source::DragDropSource;
4use super::target::DragDropTarget;
5use crate::{Ui, sys};
6
7impl Ui {
8    pub(super) fn drag_drop_scope_available(&self) -> bool {
9        // Dear ImGui has one pair of source/target flags for the entire context.
10        unsafe { !(*self.ctx).DragDropWithinSource && !(*self.ctx).DragDropWithinTarget }
11    }
12
13    /// Creates a new drag drop source configuration
14    ///
15    /// # Arguments
16    /// * `name` - Identifier for this drag source (must match target name)
17    ///
18    /// # Example
19    /// ```no_run
20    /// # use dear_imgui_rs::*;
21    /// # let mut ctx = Context::create();
22    /// # let ui = ctx.frame();
23    /// ui.button("Drag me!");
24    /// if let Some(source) = ui.drag_drop_source_config("MY_DATA")
25    ///     .flags(DragDropSourceFlags::NO_PREVIEW_TOOLTIP)
26    ///     .begin() {
27    ///     ui.text("Custom drag tooltip");
28    ///     source.end();
29    /// }
30    /// ```
31    pub fn drag_drop_source_config<T: AsRef<str>>(&self, name: T) -> DragDropSource<'_, T> {
32        DragDropSource {
33            name,
34            flags: DragDropSourceFlags::NONE,
35            cond: DragDropPayloadCond::Always,
36            ui: self,
37        }
38    }
39
40    /// Creates a drag drop target for the last item
41    ///
42    /// Returns `Some(DragDropTarget)` if the last item can accept drops. While a source or
43    /// target is open in this context, further attempts return `None`.
44    ///
45    /// # Example
46    /// ```no_run
47    /// # use dear_imgui_rs::*;
48    /// # let mut ctx = Context::create();
49    /// # let ui = ctx.frame();
50    /// ui.button("Drop target");
51    /// if let Some(target) = ui.drag_drop_target() {
52    ///     if target.accept_payload_empty("MY_DATA", DragDropTargetFlags::NONE).is_some() {
53    ///         println!("Received drop!");
54    ///     }
55    ///     target.pop();
56    /// }
57    /// ```
58    #[doc(alias = "BeginDragDropTarget")]
59    pub fn drag_drop_target(&self) -> Option<DragDropTarget<'_>> {
60        let should_begin = self.run_with_bound_context(|| {
61            self.drag_drop_scope_available() && unsafe { sys::igBeginDragDropTarget() }
62        });
63        if should_begin {
64            Some(DragDropTarget(
65                self,
66                self.begin_native_scope(
67                    crate::scope::NativeScopePop::EndDragDropTarget,
68                    "DragDropTarget",
69                ),
70            ))
71        } else {
72            None
73        }
74    }
75
76    /// Returns the current drag and drop payload, if any.
77    ///
78    /// This is a convenience wrapper over `ImGui::GetDragDropPayload`.
79    ///
80    /// The metadata is copied. Dear ImGui owns the data pointer, which can be invalidated
81    /// by another submission or by ending the target after delivery in the same frame.
82    #[doc(alias = "GetDragDropPayload")]
83    pub fn drag_drop_payload(&self) -> Option<DragDropPayload> {
84        self.run_with_bound_context(|| unsafe {
85            let ptr = sys::igGetDragDropPayload();
86            if ptr.is_null() {
87                return None;
88            }
89            Some(DragDropPayload::from_raw(*ptr))
90        })
91    }
92}