Skip to main content

dear_imgui_rs/input/
ui.rs

1use super::{Key, KeyChord, MouseButton, NextItemShortcutOptions, ShortcutOptions};
2use crate::sys;
3
4// TODO: Add NavInput enum once we have proper constants in sys crate
5
6impl crate::Ui {
7    /// Check if a key is being held down
8    #[doc(alias = "IsKeyDown")]
9    pub fn is_key_down(&self, key: Key) -> bool {
10        unsafe { sys::igIsKeyDown_Nil(key as sys::ImGuiKey) }
11    }
12
13    /// Check if a key was pressed (went from !Down to Down)
14    #[doc(alias = "IsKeyPressed")]
15    pub fn is_key_pressed(&self, key: Key) -> bool {
16        unsafe { sys::igIsKeyPressed_Bool(key as sys::ImGuiKey, true) }
17    }
18
19    /// Check if a key was pressed (went from !Down to Down), with repeat
20    #[doc(alias = "IsKeyPressed")]
21    pub fn is_key_pressed_with_repeat(&self, key: Key, repeat: bool) -> bool {
22        unsafe { sys::igIsKeyPressed_Bool(key as sys::ImGuiKey, repeat) }
23    }
24
25    /// Check if a key was released (went from Down to !Down)
26    #[doc(alias = "IsKeyReleased")]
27    pub fn is_key_released(&self, key: Key) -> bool {
28        unsafe { sys::igIsKeyReleased_Nil(key as sys::ImGuiKey) }
29    }
30
31    /// Check if a key chord was pressed (e.g. `Ctrl+S`).
32    #[doc(alias = "IsKeyChordPressed")]
33    pub fn is_key_chord_pressed(&self, key_chord: KeyChord) -> bool {
34        unsafe { sys::igIsKeyChordPressed_Nil(key_chord.raw()) }
35    }
36
37    /// Call ImGui shortcut routing with default flags.
38    #[doc(alias = "Shortcut")]
39    pub fn shortcut(&self, key_chord: KeyChord) -> bool {
40        self.shortcut_with_flags(key_chord, ShortcutOptions::new())
41    }
42
43    /// Call ImGui shortcut routing with explicit input options.
44    #[doc(alias = "Shortcut")]
45    pub fn shortcut_with_flags(
46        &self,
47        key_chord: KeyChord,
48        flags: impl Into<ShortcutOptions>,
49    ) -> bool {
50        unsafe { sys::igShortcut_Nil(key_chord.raw(), flags.into().raw()) }
51    }
52
53    /// Set the next item's shortcut with default flags.
54    #[doc(alias = "SetNextItemShortcut")]
55    pub fn set_next_item_shortcut(&self, key_chord: KeyChord) {
56        self.set_next_item_shortcut_with_flags(key_chord, NextItemShortcutOptions::new());
57    }
58
59    /// Set the next item's shortcut with explicit options.
60    #[doc(alias = "SetNextItemShortcut")]
61    pub fn set_next_item_shortcut_with_flags(
62        &self,
63        key_chord: KeyChord,
64        flags: impl Into<NextItemShortcutOptions>,
65    ) {
66        let flags = flags.into();
67        unsafe { sys::igSetNextItemShortcut(key_chord.raw(), flags.raw()) }
68    }
69
70    /// Overrides `io.WantCaptureKeyboard` for the next frame.
71    #[doc(alias = "SetNextFrameWantCaptureKeyboard")]
72    pub fn set_next_frame_want_capture_keyboard(&self, want_capture_keyboard: bool) {
73        unsafe { sys::igSetNextFrameWantCaptureKeyboard(want_capture_keyboard) }
74    }
75
76    /// Overrides `io.WantCaptureMouse` for the next frame.
77    #[doc(alias = "SetNextFrameWantCaptureMouse")]
78    pub fn set_next_frame_want_capture_mouse(&self, want_capture_mouse: bool) {
79        unsafe { sys::igSetNextFrameWantCaptureMouse(want_capture_mouse) }
80    }
81
82    /// Check if a mouse button is being held down
83    #[doc(alias = "IsMouseDown")]
84    pub fn is_mouse_down(&self, button: MouseButton) -> bool {
85        unsafe { sys::igIsMouseDown_Nil(button.into()) }
86    }
87
88    /// Check if a mouse button was clicked (went from !Down to Down)
89    #[doc(alias = "IsMouseClicked")]
90    pub fn is_mouse_clicked(&self, button: MouseButton) -> bool {
91        unsafe { sys::igIsMouseClicked_Bool(button.into(), false) }
92    }
93
94    /// Check if a mouse button was clicked, with repeat
95    #[doc(alias = "IsMouseClicked")]
96    pub fn is_mouse_clicked_with_repeat(&self, button: MouseButton, repeat: bool) -> bool {
97        unsafe { sys::igIsMouseClicked_Bool(button.into(), repeat) }
98    }
99
100    /// Check if a mouse button was released (went from Down to !Down)
101    #[doc(alias = "IsMouseReleased")]
102    pub fn is_mouse_released(&self, button: MouseButton) -> bool {
103        unsafe { sys::igIsMouseReleased_Nil(button.into()) }
104    }
105
106    /// Check if a mouse button was double-clicked
107    #[doc(alias = "IsMouseDoubleClicked")]
108    pub fn is_mouse_double_clicked(&self, button: MouseButton) -> bool {
109        unsafe { sys::igIsMouseDoubleClicked_Nil(button.into()) }
110    }
111
112    /// Returns `true` if the mouse position is valid (not NaN).
113    ///
114    /// This checks the current mouse position as known by Dear ImGui.
115    #[doc(alias = "IsMousePosValid")]
116    pub fn is_mouse_pos_valid(&self) -> bool {
117        unsafe { sys::igIsMousePosValid(std::ptr::null()) }
118    }
119
120    /// Returns `true` if the provided mouse position is valid (not NaN).
121    #[doc(alias = "IsMousePosValid")]
122    pub fn is_mouse_pos_valid_at(&self, pos: [f32; 2]) -> bool {
123        let v = sys::ImVec2_c {
124            x: pos[0],
125            y: pos[1],
126        };
127        unsafe { sys::igIsMousePosValid(&v as *const sys::ImVec2_c) }
128    }
129
130    /// Returns `true` if the mouse button was released and the given delay has passed.
131    #[doc(alias = "IsMouseReleasedWithDelay")]
132    pub fn is_mouse_released_with_delay(&self, button: MouseButton, delay: f32) -> bool {
133        unsafe { sys::igIsMouseReleasedWithDelay(button.into(), delay) }
134    }
135
136    /// Get mouse position in screen coordinates
137    #[doc(alias = "GetMousePos")]
138    pub fn mouse_pos(&self) -> [f32; 2] {
139        let pos = unsafe { sys::igGetMousePos() };
140        [pos.x, pos.y]
141    }
142
143    /// Get mouse position when a specific button was clicked
144    #[doc(alias = "GetMousePosOnOpeningCurrentPopup")]
145    pub fn mouse_pos_on_opening_current_popup(&self) -> [f32; 2] {
146        let pos = unsafe { sys::igGetMousePosOnOpeningCurrentPopup() };
147        [pos.x, pos.y]
148    }
149
150    /// Check if mouse is hovering given rectangle
151    #[doc(alias = "IsMouseHoveringRect")]
152    pub fn is_mouse_hovering_rect(&self, r_min: [f32; 2], r_max: [f32; 2]) -> bool {
153        unsafe {
154            sys::igIsMouseHoveringRect(
155                sys::ImVec2::new(r_min[0], r_min[1]),
156                sys::ImVec2::new(r_max[0], r_max[1]),
157                true,
158            )
159        }
160    }
161
162    /// Check if mouse is hovering given rectangle (with clipping test)
163    #[doc(alias = "IsMouseHoveringRect")]
164    pub fn is_mouse_hovering_rect_with_clip(
165        &self,
166        r_min: [f32; 2],
167        r_max: [f32; 2],
168        clip: bool,
169    ) -> bool {
170        unsafe {
171            sys::igIsMouseHoveringRect(
172                sys::ImVec2::new(r_min[0], r_min[1]),
173                sys::ImVec2::new(r_max[0], r_max[1]),
174                clip,
175            )
176        }
177    }
178
179    /// Check if mouse is dragging
180    #[doc(alias = "IsMouseDragging")]
181    pub fn is_mouse_dragging(&self, button: MouseButton) -> bool {
182        unsafe { sys::igIsMouseDragging(button as i32, -1.0) }
183    }
184
185    /// Check if mouse is dragging with threshold
186    #[doc(alias = "IsMouseDragging")]
187    pub fn is_mouse_dragging_with_threshold(
188        &self,
189        button: MouseButton,
190        lock_threshold: f32,
191    ) -> bool {
192        unsafe { sys::igIsMouseDragging(button as i32, lock_threshold) }
193    }
194
195    /// Get mouse drag delta
196    #[doc(alias = "GetMouseDragDelta")]
197    pub fn mouse_drag_delta(&self, button: MouseButton) -> [f32; 2] {
198        let delta = unsafe { sys::igGetMouseDragDelta(button as i32, -1.0) };
199        [delta.x, delta.y]
200    }
201
202    /// Get mouse drag delta with threshold
203    #[doc(alias = "GetMouseDragDelta")]
204    pub fn mouse_drag_delta_with_threshold(
205        &self,
206        button: MouseButton,
207        lock_threshold: f32,
208    ) -> [f32; 2] {
209        let delta = unsafe { sys::igGetMouseDragDelta(button as i32, lock_threshold) };
210        [delta.x, delta.y]
211    }
212
213    /// Reset mouse drag delta for a specific button
214    #[doc(alias = "ResetMouseDragDelta")]
215    pub fn reset_mouse_drag_delta(&self, button: MouseButton) {
216        unsafe { sys::igResetMouseDragDelta(button as i32) }
217    }
218
219    /// Returns true if the last item toggled its selection state in a multi-select scope.
220    ///
221    /// This only makes sense when used between `BeginMultiSelect()` /
222    /// `EndMultiSelect()` (or helpers built on top of them).
223    #[doc(alias = "IsItemToggledSelection")]
224    pub fn is_item_toggled_selection(&self) -> bool {
225        unsafe { sys::igIsItemToggledSelection() }
226    }
227}