dear_imgui_rs/widget/
misc.rs

1//! Miscellaneous widgets
2//!
3//! Small convenience widgets that don’t fit elsewhere (e.g. bullets, help
4//! markers). See functions on `Ui` for details.
5//!
6#![allow(
7    clippy::cast_possible_truncation,
8    clippy::cast_sign_loss,
9    clippy::as_conversions
10)]
11use crate::Ui;
12use crate::sys;
13
14bitflags::bitflags! {
15    /// Flags for invisible buttons
16    #[repr(transparent)]
17    #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
18    pub struct ButtonFlags: i32 {
19        /// No flags
20        const NONE = 0;
21        /// React on left mouse button
22        const MOUSE_BUTTON_LEFT = sys::ImGuiButtonFlags_MouseButtonLeft as i32;
23        /// React on right mouse button
24        const MOUSE_BUTTON_RIGHT = sys::ImGuiButtonFlags_MouseButtonRight as i32;
25        /// React on middle mouse button
26        const MOUSE_BUTTON_MIDDLE = sys::ImGuiButtonFlags_MouseButtonMiddle as i32;
27    }
28}
29
30/// Direction for arrow buttons (alias for Direction)
31pub use crate::Direction as ArrowDirection;
32
33impl Ui {
34    /// Creates a bullet point
35    #[doc(alias = "Bullet")]
36    pub fn bullet(&self) {
37        unsafe {
38            sys::igBullet();
39        }
40    }
41
42    /// Creates a bullet point with text
43    #[doc(alias = "BulletText")]
44    pub fn bullet_text(&self, text: impl AsRef<str>) {
45        let text_ptr = self.scratch_txt(text);
46        unsafe {
47            sys::igBulletText(text_ptr);
48        }
49    }
50}
51
52impl Ui {
53    /// Creates a small button
54    #[doc(alias = "SmallButton")]
55    pub fn small_button(&self, label: impl AsRef<str>) -> bool {
56        let label_ptr = self.scratch_txt(label);
57        unsafe { sys::igSmallButton(label_ptr) }
58    }
59
60    /// Creates an invisible button
61    #[doc(alias = "InvisibleButton")]
62    pub fn invisible_button(&self, str_id: impl AsRef<str>, size: impl Into<[f32; 2]>) -> bool {
63        self.invisible_button_flags(str_id, size, crate::widget::ButtonFlags::NONE)
64    }
65
66    /// Creates an invisible button with flags
67    #[doc(alias = "InvisibleButton")]
68    pub fn invisible_button_flags(
69        &self,
70        str_id: impl AsRef<str>,
71        size: impl Into<[f32; 2]>,
72        flags: crate::widget::ButtonFlags,
73    ) -> bool {
74        let id_ptr = self.scratch_txt(str_id);
75        let size_vec: sys::ImVec2 = size.into().into();
76        unsafe { sys::igInvisibleButton(id_ptr, size_vec, flags.bits()) }
77    }
78
79    /// Creates an arrow button
80    #[doc(alias = "ArrowButton")]
81    pub fn arrow_button(&self, str_id: impl AsRef<str>, dir: crate::Direction) -> bool {
82        let id_ptr = self.scratch_txt(str_id);
83        unsafe { sys::igArrowButton(id_ptr, dir as i32) }
84    }
85}
86
87// ============================================================================
88// Disabled scope (RAII)
89// ============================================================================
90
91/// Tracks a disabled scope begun with [`Ui::begin_disabled`] and ended on drop.
92#[must_use]
93pub struct DisabledToken<'ui> {
94    _ui: &'ui Ui,
95}
96
97impl<'ui> DisabledToken<'ui> {
98    fn new(ui: &'ui Ui) -> Self {
99        DisabledToken { _ui: ui }
100    }
101
102    /// Ends the disabled scope explicitly.
103    pub fn end(self) {
104        // Drop will call EndDisabled
105    }
106}
107
108impl<'ui> Drop for DisabledToken<'ui> {
109    fn drop(&mut self) {
110        unsafe { sys::igEndDisabled() }
111    }
112}
113
114impl Ui {
115    /// Begin a disabled scope for subsequent items.
116    ///
117    /// All following widgets will be disabled (grayed out and non-interactive)
118    /// until the returned token is dropped.
119    #[doc(alias = "BeginDisabled")]
120    pub fn begin_disabled(&self) -> DisabledToken<'_> {
121        unsafe { sys::igBeginDisabled(true) }
122        DisabledToken::new(self)
123    }
124
125    /// Begin a conditionally disabled scope for subsequent items.
126    ///
127    /// If `disabled` is false, this still needs to be paired with the returned
128    /// token being dropped to correctly balance the internal stack.
129    #[doc(alias = "BeginDisabled")]
130    pub fn begin_disabled_with_cond(&self, disabled: bool) -> DisabledToken<'_> {
131        unsafe { sys::igBeginDisabled(disabled) }
132        DisabledToken::new(self)
133    }
134}
135
136// ============================================================================
137// Button repeat (convenience over item flag)
138// ============================================================================
139
140impl Ui {
141    /// Enable/disable repeating behavior for subsequent buttons.
142    ///
143    /// Internally uses `PushItemFlag(ImGuiItemFlags_ButtonRepeat, repeat)`.
144    #[doc(alias = "PushButtonRepeat")]
145    pub fn push_button_repeat(&self, repeat: bool) {
146        unsafe { sys::igPushItemFlag(sys::ImGuiItemFlags_ButtonRepeat as i32, repeat) }
147    }
148
149    /// Pop the button repeat item flag.
150    #[doc(alias = "PopButtonRepeat")]
151    pub fn pop_button_repeat(&self) {
152        unsafe { sys::igPopItemFlag() }
153    }
154}
155
156// ============================================================================
157// Item key ownership
158// ============================================================================
159
160impl Ui {
161    /// Set the key owner for the last item, without flags.
162    #[doc(alias = "SetItemKeyOwner")]
163    pub fn set_item_key_owner(&self, key: crate::input::Key) {
164        let k: sys::ImGuiKey = key as sys::ImGuiKey;
165        unsafe { sys::igSetItemKeyOwner_Nil(k) }
166    }
167
168    /// Set the key owner for the last item with input flags.
169    /// Pass a combination of `ImGuiInputFlags_*` from `dear_imgui_sys`.
170    #[doc(alias = "SetItemKeyOwner")]
171    pub fn set_item_key_owner_with_flags(
172        &self,
173        key: crate::input::Key,
174        flags: sys::ImGuiInputFlags,
175    ) {
176        let k: sys::ImGuiKey = key as sys::ImGuiKey;
177        unsafe { sys::igSetItemKeyOwner_InputFlags(k, flags) }
178    }
179}