Skip to main content

dear_imgui_rs/ui/
navigation.rs

1use super::*;
2
3impl Ui {
4    /// Returns the currently desired mouse cursor type
5    ///
6    /// Returns `None` if no cursor should be displayed
7    #[doc(alias = "GetMouseCursor")]
8    pub fn mouse_cursor(&self) -> Option<MouseCursor> {
9        unsafe {
10            match sys::igGetMouseCursor() {
11                sys::ImGuiMouseCursor_Arrow => Some(MouseCursor::Arrow),
12                sys::ImGuiMouseCursor_TextInput => Some(MouseCursor::TextInput),
13                sys::ImGuiMouseCursor_ResizeAll => Some(MouseCursor::ResizeAll),
14                sys::ImGuiMouseCursor_ResizeNS => Some(MouseCursor::ResizeNS),
15                sys::ImGuiMouseCursor_ResizeEW => Some(MouseCursor::ResizeEW),
16                sys::ImGuiMouseCursor_ResizeNESW => Some(MouseCursor::ResizeNESW),
17                sys::ImGuiMouseCursor_ResizeNWSE => Some(MouseCursor::ResizeNWSE),
18                sys::ImGuiMouseCursor_Hand => Some(MouseCursor::Hand),
19                sys::ImGuiMouseCursor_NotAllowed => Some(MouseCursor::NotAllowed),
20                _ => None,
21            }
22        }
23    }
24
25    /// Sets the desired mouse cursor type
26    ///
27    /// Passing `None` hides the mouse cursor
28    #[doc(alias = "SetMouseCursor")]
29    pub fn set_mouse_cursor(&self, cursor_type: Option<MouseCursor>) {
30        self.run_with_bound_context(|| unsafe {
31            let val: sys::ImGuiMouseCursor = cursor_type
32                .map(|x| x as sys::ImGuiMouseCursor)
33                .unwrap_or(sys::ImGuiMouseCursor_None);
34            sys::igSetMouseCursor(val);
35        });
36    }
37
38    /// Controls whether Dear ImGui renders its own mouse cursor for this Context.
39    ///
40    /// This is the frame-scoped alternative to mutably borrowing [`crate::Io`] while a `Ui` is
41    /// live. Platform backends should hide the OS cursor while this value is enabled.
42    pub fn set_mouse_draw_cursor(&self, draw: bool) {
43        self.run_with_bound_context(|| unsafe {
44            let io = sys::igGetIO_Nil();
45            assert!(
46                !io.is_null(),
47                "Ui::set_mouse_draw_cursor() requires an active ImGui context"
48            );
49            (*io).MouseDrawCursor = draw;
50        });
51    }
52
53    // ============================================================================
54    // Focus and Navigation
55    // ============================================================================
56
57    /// Focuses keyboard on the next widget.
58    ///
59    /// This is the equivalent to [set_keyboard_focus_here_with_offset](Self::set_keyboard_focus_here_with_offset)
60    /// with `offset` set to 0.
61    #[doc(alias = "SetKeyboardFocusHere")]
62    pub fn set_keyboard_focus_here(&self) {
63        self.set_keyboard_focus_here_with_offset(0);
64    }
65
66    /// Focuses keyboard on a widget relative to current position.
67    ///
68    /// Use positive offset to focus on next widgets, negative offset to focus on previous widgets.
69    #[doc(alias = "SetKeyboardFocusHere")]
70    pub fn set_keyboard_focus_here_with_offset(&self, offset: i32) {
71        unsafe {
72            sys::igSetKeyboardFocusHere(offset);
73        }
74    }
75
76    /// Shows or hides the navigation cursor (a small marker indicating nav focus).
77    #[doc(alias = "SetNavCursorVisible")]
78    pub fn set_nav_cursor_visible(&self, visible: bool) {
79        self.run_with_bound_context(|| unsafe { sys::igSetNavCursorVisible(visible) });
80    }
81}