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 = unsafe { sys::igBeginDragDropTarget() };
56 if should_begin {
57 Some(DragDropTarget(self))
58 } else {
59 None
60 }
61 }
62
63 /// Returns the current drag and drop payload, if any.
64 ///
65 /// This is a convenience wrapper over `ImGui::GetDragDropPayload`.
66 ///
67 /// The returned payload is owned and managed by Dear ImGui and may become invalid
68 /// after the drag operation completes. Do not cache it beyond the current frame.
69 #[doc(alias = "GetDragDropPayload")]
70 pub fn drag_drop_payload(&self) -> Option<DragDropPayload> {
71 unsafe {
72 let ptr = sys::igGetDragDropPayload();
73 if ptr.is_null() {
74 return None;
75 }
76 Some(DragDropPayload::from_raw(*ptr))
77 }
78 }
79}