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 /// Creates a new drag drop source configuration
9 ///
10 /// # Arguments
11 /// * `name` - Identifier for this drag source (must match target name)
12 ///
13 /// # Example
14 /// ```no_run
15 /// # use dear_imgui_rs::*;
16 /// # let mut ctx = Context::create();
17 /// # let ui = ctx.frame();
18 /// ui.button("Drag me!");
19 /// if let Some(source) = ui.drag_drop_source_config("MY_DATA")
20 /// .flags(DragDropSourceFlags::NO_PREVIEW_TOOLTIP)
21 /// .begin() {
22 /// ui.text("Custom drag tooltip");
23 /// source.end();
24 /// }
25 /// ```
26 pub fn drag_drop_source_config<T: AsRef<str>>(&self, name: T) -> DragDropSource<'_, T> {
27 DragDropSource {
28 name,
29 flags: DragDropSourceFlags::NONE,
30 cond: DragDropPayloadCond::Always,
31 ui: self,
32 }
33 }
34
35 /// Creates a drag drop target for the last item
36 ///
37 /// Returns `Some(DragDropTarget)` if the last item can accept drops,
38 /// `None` otherwise.
39 ///
40 /// # Example
41 /// ```no_run
42 /// # use dear_imgui_rs::*;
43 /// # let mut ctx = Context::create();
44 /// # let ui = ctx.frame();
45 /// ui.button("Drop target");
46 /// if let Some(target) = ui.drag_drop_target() {
47 /// if target.accept_payload_empty("MY_DATA", DragDropTargetFlags::NONE).is_some() {
48 /// println!("Received drop!");
49 /// }
50 /// target.pop();
51 /// }
52 /// ```
53 #[doc(alias = "BeginDragDropTarget")]
54 pub fn drag_drop_target(&self) -> Option<DragDropTarget<'_>> {
55 let should_begin = self.run_with_bound_context(|| unsafe { sys::igBeginDragDropTarget() });
56 if should_begin {
57 Some(DragDropTarget(
58 self,
59 self.begin_native_scope(
60 crate::scope::NativeScopePop::EndDragDropTarget,
61 "DragDropTarget",
62 ),
63 ))
64 } else {
65 None
66 }
67 }
68
69 /// Returns the current drag and drop payload, if any.
70 ///
71 /// This is a convenience wrapper over `ImGui::GetDragDropPayload`.
72 ///
73 /// The returned payload is owned and managed by Dear ImGui and may become invalid
74 /// after the drag operation completes. Do not cache it beyond the current frame.
75 #[doc(alias = "GetDragDropPayload")]
76 pub fn drag_drop_payload(&self) -> Option<DragDropPayload> {
77 self.run_with_bound_context(|| unsafe {
78 let ptr = sys::igGetDragDropPayload();
79 if ptr.is_null() {
80 return None;
81 }
82 Some(DragDropPayload::from_raw(*ptr))
83 })
84 }
85}