dear_imgui/
io.rs

1#![allow(
2    clippy::cast_possible_truncation,
3    clippy::cast_sign_loss,
4    clippy::as_conversions
5)]
6use bitflags::bitflags;
7
8use crate::sys;
9
10bitflags! {
11    /// Configuration flags
12    #[repr(transparent)]
13    pub struct ConfigFlags: i32 {
14        /// Master keyboard navigation enable flag.
15        const NAV_ENABLE_KEYBOARD = sys::ImGuiConfigFlags_NavEnableKeyboard as i32;
16        /// Master gamepad navigation enable flag.
17        const NAV_ENABLE_GAMEPAD = sys::ImGuiConfigFlags_NavEnableGamepad as i32;
18        /// Instruction imgui-rs to clear mouse position/buttons in `frame()`.
19        const NO_MOUSE = sys::ImGuiConfigFlags_NoMouse as i32;
20        /// Instruction backend to not alter mouse cursor shape and visibility.
21        const NO_MOUSE_CURSOR_CHANGE = sys::ImGuiConfigFlags_NoMouseCursorChange as i32;
22        /// Application is SRGB-aware.
23        const IS_SRGB = sys::ImGuiConfigFlags_IsSRGB as i32;
24        /// Application is using a touch screen instead of a mouse.
25        const IS_TOUCH_SCREEN = sys::ImGuiConfigFlags_IsTouchScreen as i32;
26
27        const DOCKING_ENABLE = sys::ImGuiConfigFlags_DockingEnable as i32;
28
29        const VIEWPORTS_ENABLE = sys::ImGuiConfigFlags_ViewportsEnable as i32;
30    }
31}
32
33bitflags! {
34    /// Backend capabilities
35    #[repr(transparent)]
36    pub struct BackendFlags: i32 {
37        /// Backend supports gamepad and currently has one connected
38        const HAS_GAMEPAD = sys::ImGuiBackendFlags_HasGamepad as i32;
39        /// Backend supports honoring `get_mouse_cursor` value to change the OS cursor shape
40        const HAS_MOUSE_CURSORS = sys::ImGuiBackendFlags_HasMouseCursors as i32;
41        /// Backend supports `io.want_set_mouse_pos` requests to reposition the OS mouse position.
42        const HAS_SET_MOUSE_POS = sys::ImGuiBackendFlags_HasSetMousePos as i32;
43        /// Backend renderer supports DrawCmd::vtx_offset.
44        const RENDERER_HAS_VTX_OFFSET = sys::ImGuiBackendFlags_RendererHasVtxOffset as i32;
45        /// Backend renderer supports ImTextureData requests to create/update/destroy textures.
46        const RENDERER_HAS_TEXTURES = sys::ImGuiBackendFlags_RendererHasTextures as i32;
47
48        #[cfg(feature = "multi-viewport")]
49        /// Set if the platform backend supports viewports.
50        const PLATFORM_HAS_VIEWPORTS = sys::ImGuiBackendFlags_PlatformHasViewports as i32;
51        #[cfg(feature = "multi-viewport")]
52        /// Set if the renderer backend supports viewports.
53        const RENDERER_HAS_VIEWPORTS = sys::ImGuiBackendFlags_RendererHasViewports as i32;
54    }
55}
56
57#[cfg(feature = "multi-viewport")]
58bitflags! {
59    /// Viewport flags for multi-viewport support
60    #[repr(transparent)]
61    pub struct ViewportFlags: i32 {
62        /// No flags
63        const NONE = 0;
64        /// Represent a Platform Window
65        const IS_PLATFORM_WINDOW = sys::ImGuiViewportFlags_IsPlatformWindow as i32;
66        /// Represent a Platform Monitor (unused in our implementation)
67        const IS_PLATFORM_MONITOR = sys::ImGuiViewportFlags_IsPlatformMonitor as i32;
68        /// Platform Window: is created/managed by the application (rather than a dear imgui backend)
69        const OWNED_BY_APP = sys::ImGuiViewportFlags_OwnedByApp as i32;
70        /// Platform Window: Disable platform decorations: title bar, borders, etc.
71        const NO_DECORATION = sys::ImGuiViewportFlags_NoDecoration as i32;
72        /// Platform Window: Disable platform task bar icon (generally set on popups/tooltips, or all windows if ImGuiConfigFlags_ViewportsNoTaskBarIcon is set)
73        const NO_TASK_BAR_ICON = sys::ImGuiViewportFlags_NoTaskBarIcon as i32;
74        /// Platform Window: Don't take focus when created.
75        const NO_FOCUS_ON_APPEARING = sys::ImGuiViewportFlags_NoFocusOnAppearing as i32;
76        /// Platform Window: Don't take focus when clicked on.
77        const NO_FOCUS_ON_CLICK = sys::ImGuiViewportFlags_NoFocusOnClick as i32;
78        /// Platform Window: Make mouse pass through so we can drag this window while peaking behind it.
79        const NO_INPUTS = sys::ImGuiViewportFlags_NoInputs as i32;
80        /// Platform Window: Renderer doesn't need to clear the framebuffer ahead (because we will fill it entirely).
81        const NO_RENDERER_CLEAR = sys::ImGuiViewportFlags_NoRendererClear as i32;
82        /// Platform Window: Avoid merging this window into another host window. This can only be set via ImGuiWindowClass viewport flags override (because we need to now ahead if we are going to create a viewport in the first place!).
83        const NO_AUTO_MERGE = sys::ImGuiViewportFlags_NoAutoMerge as i32;
84        /// Platform Window: Display on top (for tooltips only).
85        const TOP_MOST = sys::ImGuiViewportFlags_TopMost as i32;
86        /// Viewport can host multiple imgui windows (secondary viewports are associated to a single window).
87        const CAN_HOST_OTHER_WINDOWS = sys::ImGuiViewportFlags_CanHostOtherWindows as i32;
88        /// Platform Window: Window is minimized, can skip render. When minimized we tend to avoid using the viewport pos/size for clipping rectangle computation.
89        const IS_MINIMIZED = sys::ImGuiViewportFlags_IsMinimized as i32;
90        /// Platform Window: Window is focused (last call to Platform_GetWindowFocus() returned true)
91        const IS_FOCUSED = sys::ImGuiViewportFlags_IsFocused as i32;
92    }
93}
94
95/// Settings and inputs/outputs for imgui-rs
96/// This is a transparent wrapper around ImGuiIO
97#[repr(transparent)]
98pub struct Io(sys::ImGuiIO);
99
100impl Io {
101    /// Creates a new Io instance from the current context
102    pub(crate) fn from_raw() -> &'static mut Self {
103        unsafe {
104            // Note: our bindings expose igGetIO_Nil which resolves against the
105            // current context. Keep using _Nil variant until regular symbol is exported.
106            let io_ptr = sys::igGetIO_Nil();
107            &mut *(io_ptr as *mut Self)
108        }
109    }
110
111    /// Main display size in pixels
112    pub fn display_size(&self) -> [f32; 2] {
113        [self.0.DisplaySize.x, self.0.DisplaySize.y]
114    }
115
116    /// Set main display size in pixels
117    pub fn set_display_size(&mut self, size: [f32; 2]) {
118        self.0.DisplaySize.x = size[0];
119        self.0.DisplaySize.y = size[1];
120    }
121
122    /// Time elapsed since last frame, in seconds
123    pub fn delta_time(&self) -> f32 {
124        self.0.DeltaTime
125    }
126
127    /// Set time elapsed since last frame, in seconds
128    pub fn set_delta_time(&mut self, delta_time: f32) {
129        self.0.DeltaTime = delta_time;
130    }
131
132    /// Mouse position, in pixels
133    pub fn mouse_pos(&self) -> [f32; 2] {
134        [self.0.MousePos.x, self.0.MousePos.y]
135    }
136
137    /// Set mouse position, in pixels
138    pub fn set_mouse_pos(&mut self, pos: [f32; 2]) {
139        self.0.MousePos.x = pos[0];
140        self.0.MousePos.y = pos[1];
141    }
142
143    /// Mouse wheel vertical scrolling
144    pub fn mouse_wheel(&self) -> f32 {
145        self.0.MouseWheel
146    }
147
148    /// Set mouse wheel vertical scrolling
149    pub fn set_mouse_wheel(&mut self, wheel: f32) {
150        self.0.MouseWheel = wheel;
151    }
152
153    /// Mouse wheel horizontal scrolling
154    pub fn mouse_wheel_h(&self) -> f32 {
155        self.0.MouseWheelH
156    }
157
158    /// Set mouse wheel horizontal scrolling
159    pub fn set_mouse_wheel_h(&mut self, wheel_h: f32) {
160        self.0.MouseWheelH = wheel_h;
161    }
162
163    /// Check if a mouse button is down
164    pub fn mouse_down(&self, button: usize) -> bool {
165        if button < 5 {
166            self.0.MouseDown[button]
167        } else {
168            false
169        }
170    }
171
172    /// Set mouse button state
173    pub fn set_mouse_down(&mut self, button: usize, down: bool) {
174        if button < 5 {
175            self.0.MouseDown[button] = down;
176        }
177    }
178
179    /// Check if imgui wants to capture mouse input
180    pub fn want_capture_mouse(&self) -> bool {
181        self.0.WantCaptureMouse
182    }
183
184    /// Check if imgui wants to capture keyboard input
185    pub fn want_capture_keyboard(&self) -> bool {
186        self.0.WantCaptureKeyboard
187    }
188
189    /// Check if imgui wants to use text input
190    pub fn want_text_input(&self) -> bool {
191        self.0.WantTextInput
192    }
193
194    /// Check if imgui wants to set mouse position
195    pub fn want_set_mouse_pos(&self) -> bool {
196        self.0.WantSetMousePos
197    }
198    /// Whether ImGui requests software-drawn mouse cursor
199    pub fn mouse_draw_cursor(&self) -> bool {
200        self.0.MouseDrawCursor
201    }
202    /// Enable or disable software-drawn mouse cursor
203    pub fn set_mouse_draw_cursor(&mut self, draw: bool) {
204        self.0.MouseDrawCursor = draw;
205    }
206
207    /// Check if imgui wants to save ini settings
208    pub fn want_save_ini_settings(&self) -> bool {
209        self.0.WantSaveIniSettings
210    }
211
212    /// Framerate estimation, in frames per second
213    pub fn framerate(&self) -> f32 {
214        self.0.Framerate
215    }
216
217    /// Vertices output during last call to render
218    pub fn metrics_render_vertices(&self) -> i32 {
219        self.0.MetricsRenderVertices
220    }
221
222    /// Indices output during last call to render
223    pub fn metrics_render_indices(&self) -> i32 {
224        self.0.MetricsRenderIndices
225    }
226
227    /// Number of visible windows
228    pub fn metrics_render_windows(&self) -> i32 {
229        self.0.MetricsRenderWindows
230    }
231
232    /// Number of active windows
233    pub fn metrics_active_windows(&self) -> i32 {
234        self.0.MetricsActiveWindows
235    }
236
237    /// Configuration flags
238    pub fn config_flags(&self) -> ConfigFlags {
239        ConfigFlags::from_bits_truncate(self.0.ConfigFlags)
240    }
241
242    /// Set configuration flags
243    pub fn set_config_flags(&mut self, flags: ConfigFlags) {
244        self.0.ConfigFlags = flags.bits();
245    }
246
247    /// Backend flags
248    pub fn backend_flags(&self) -> BackendFlags {
249        BackendFlags::from_bits_truncate(self.0.BackendFlags)
250    }
251
252    /// Set backend flags
253    pub fn set_backend_flags(&mut self, flags: BackendFlags) {
254        self.0.BackendFlags = flags.bits();
255    }
256
257    /// Add a key event to the input queue
258    pub fn add_key_event(&mut self, key: crate::Key, down: bool) {
259        unsafe {
260            sys::ImGuiIO_AddKeyEvent(&mut self.0 as *mut _, key.into(), down);
261        }
262    }
263
264    /// Add a character input event to the input queue
265    pub fn add_input_character(&mut self, character: char) {
266        unsafe {
267            sys::ImGuiIO_AddInputCharacter(&mut self.0 as *mut _, character as u32);
268        }
269    }
270
271    /// Add a mouse position event to the input queue
272    pub fn add_mouse_pos_event(&mut self, pos: [f32; 2]) {
273        unsafe {
274            sys::ImGuiIO_AddMousePosEvent(&mut self.0 as *mut _, pos[0], pos[1]);
275        }
276    }
277
278    /// Add a mouse button event to the input queue
279    pub fn add_mouse_button_event(&mut self, button: crate::input::MouseButton, down: bool) {
280        unsafe {
281            sys::ImGuiIO_AddMouseButtonEvent(&mut self.0 as *mut _, button.into(), down);
282        }
283    }
284
285    /// Add a mouse wheel event to the input queue
286    pub fn add_mouse_wheel_event(&mut self, wheel: [f32; 2]) {
287        unsafe {
288            sys::ImGuiIO_AddMouseWheelEvent(&mut self.0 as *mut _, wheel[0], wheel[1]);
289        }
290    }
291
292    /// Notify Dear ImGui that the application window gained or lost focus
293    /// This mirrors `io.AddFocusEvent()` in Dear ImGui and is used by platform backends.
294    pub fn add_focus_event(&mut self, focused: bool) {
295        unsafe {
296            sys::ImGuiIO_AddFocusEvent(&mut self.0 as *mut _, focused);
297        }
298    }
299
300    /// Get the global font scale (not available in current Dear ImGui version)
301    /// Compatibility shim: maps to style.FontScaleMain (Dear ImGui 1.92+)
302    pub fn font_global_scale(&self) -> f32 {
303        unsafe { (*sys::igGetStyle()).FontScaleMain }
304    }
305
306    /// Set the global font scale (not available in current Dear ImGui version)
307    /// Compatibility shim: maps to style.FontScaleMain (Dear ImGui 1.92+)
308    pub fn set_font_global_scale(&mut self, _scale: f32) {
309        unsafe {
310            (*sys::igGetStyle()).FontScaleMain = _scale;
311        }
312    }
313
314    /// Get the display framebuffer scale
315    pub fn display_framebuffer_scale(&self) -> [f32; 2] {
316        let scale = self.0.DisplayFramebufferScale;
317        [scale.x, scale.y]
318    }
319
320    /// Set the display framebuffer scale
321    /// This is important for HiDPI displays to ensure proper rendering
322    pub fn set_display_framebuffer_scale(&mut self, scale: [f32; 2]) {
323        self.0.DisplayFramebufferScale.x = scale[0];
324        self.0.DisplayFramebufferScale.y = scale[1];
325    }
326}