Skip to main content

dear_imgui_rs/io/
flags.rs

1use crate::sys;
2use bitflags::bitflags;
3#[cfg(feature = "serde")]
4use serde::{Deserialize, Serialize};
5
6#[cfg(feature = "serde")]
7impl Serialize for ConfigFlags {
8    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
9    where
10        S: serde::Serializer,
11    {
12        serializer.serialize_i32(self.bits())
13    }
14}
15
16#[cfg(feature = "serde")]
17impl<'de> Deserialize<'de> for ConfigFlags {
18    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
19    where
20        D: serde::Deserializer<'de>,
21    {
22        let bits = i32::deserialize(deserializer)?;
23        Ok(ConfigFlags::from_bits_retain(bits))
24    }
25}
26
27#[cfg(feature = "serde")]
28impl Serialize for BackendFlags {
29    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
30    where
31        S: serde::Serializer,
32    {
33        serializer.serialize_i32(self.bits())
34    }
35}
36
37#[cfg(feature = "serde")]
38impl<'de> Deserialize<'de> for BackendFlags {
39    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
40    where
41        D: serde::Deserializer<'de>,
42    {
43        let bits = i32::deserialize(deserializer)?;
44        Ok(BackendFlags::from_bits_retain(bits))
45    }
46}
47
48#[cfg(feature = "serde")]
49impl Serialize for ViewportFlags {
50    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
51    where
52        S: serde::Serializer,
53    {
54        serializer.serialize_i32(self.bits())
55    }
56}
57
58#[cfg(feature = "serde")]
59impl<'de> Deserialize<'de> for ViewportFlags {
60    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
61    where
62        D: serde::Deserializer<'de>,
63    {
64        let bits = i32::deserialize(deserializer)?;
65        Ok(ViewportFlags::from_bits_retain(bits))
66    }
67}
68
69#[cfg(feature = "serde")]
70impl Serialize for WindowClassViewportFlags {
71    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
72    where
73        S: serde::Serializer,
74    {
75        serializer.serialize_i32(self.bits())
76    }
77}
78
79#[cfg(feature = "serde")]
80impl<'de> Deserialize<'de> for WindowClassViewportFlags {
81    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
82    where
83        D: serde::Deserializer<'de>,
84    {
85        let bits = i32::deserialize(deserializer)?;
86        Ok(WindowClassViewportFlags::from_bits_retain(bits))
87    }
88}
89
90bitflags! {
91    /// Configuration flags
92    #[repr(transparent)]
93    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
94    pub struct ConfigFlags: i32 {
95        /// Master keyboard navigation enable flag.
96        const NAV_ENABLE_KEYBOARD = sys::ImGuiConfigFlags_NavEnableKeyboard as i32;
97        /// Master gamepad navigation enable flag.
98        const NAV_ENABLE_GAMEPAD = sys::ImGuiConfigFlags_NavEnableGamepad as i32;
99        /// Instruction imgui-rs to clear mouse position/buttons in `frame()`.
100        const NO_MOUSE = sys::ImGuiConfigFlags_NoMouse as i32;
101        /// Instruction backend to not alter mouse cursor shape and visibility.
102        const NO_MOUSE_CURSOR_CHANGE = sys::ImGuiConfigFlags_NoMouseCursorChange as i32;
103        /// Disable keyboard inputs and interactions.
104        const NO_KEYBOARD = sys::ImGuiConfigFlags_NoKeyboard as i32;
105        /// Application is SRGB-aware.
106        const IS_SRGB = sys::ImGuiConfigFlags_IsSRGB as i32;
107        /// Application is using a touch screen instead of a mouse.
108        const IS_TOUCH_SCREEN = sys::ImGuiConfigFlags_IsTouchScreen as i32;
109
110        const DOCKING_ENABLE = sys::ImGuiConfigFlags_DockingEnable as i32;
111
112        const VIEWPORTS_ENABLE = sys::ImGuiConfigFlags_ViewportsEnable as i32;
113    }
114}
115
116bitflags! {
117    /// Viewport policy bits that an application may override through [`crate::WindowClass`].
118    ///
119    /// Identity, topology, host capability, and platform output status remain owned by Dear
120    /// ImGui and its backends, so they are intentionally absent from this type.
121    #[repr(transparent)]
122    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
123    pub struct WindowClassViewportFlags: i32 {
124        /// Disable platform decorations such as title bars and borders.
125        const NO_DECORATION = sys::ImGuiViewportFlags_NoDecoration as i32;
126        /// Omit the platform task bar icon.
127        const NO_TASK_BAR_ICON = sys::ImGuiViewportFlags_NoTaskBarIcon as i32;
128        /// Do not activate the platform window when it appears.
129        const NO_FOCUS_ON_APPEARING = sys::ImGuiViewportFlags_NoFocusOnAppearing as i32;
130        /// Do not activate the platform window when it is clicked.
131        const NO_FOCUS_ON_CLICK = sys::ImGuiViewportFlags_NoFocusOnClick as i32;
132        /// Make pointer input pass through the platform window.
133        const NO_INPUTS = sys::ImGuiViewportFlags_NoInputs as i32;
134        /// Tell the renderer that the viewport framebuffer does not need to be cleared.
135        const NO_RENDERER_CLEAR = sys::ImGuiViewportFlags_NoRendererClear as i32;
136        /// Prevent this viewport from automatically merging into another host viewport.
137        const NO_AUTO_MERGE = sys::ImGuiViewportFlags_NoAutoMerge as i32;
138        /// Keep the platform window above ordinary windows.
139        const TOP_MOST = sys::ImGuiViewportFlags_TopMost as i32;
140    }
141}
142
143bitflags! {
144    /// Backend capabilities
145    #[repr(transparent)]
146    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
147    pub struct BackendFlags: i32 {
148        /// Backend supports gamepad and currently has one connected
149        const HAS_GAMEPAD = sys::ImGuiBackendFlags_HasGamepad as i32;
150        /// Backend supports honoring `get_mouse_cursor` value to change the OS cursor shape
151        const HAS_MOUSE_CURSORS = sys::ImGuiBackendFlags_HasMouseCursors as i32;
152        /// Backend supports `io.want_set_mouse_pos` requests to reposition the OS mouse position.
153        const HAS_SET_MOUSE_POS = sys::ImGuiBackendFlags_HasSetMousePos as i32;
154        /// Backend can report which viewport the OS mouse is hovering via `add_mouse_viewport_event`
155        const HAS_MOUSE_HOVERED_VIEWPORT =
156            sys::ImGuiBackendFlags_HasMouseHoveredViewport as i32;
157        /// Backend platform can honor viewport parent/child relationships.
158        const HAS_PARENT_VIEWPORT = sys::ImGuiBackendFlags_HasParentViewport as i32;
159        /// Backend renderer supports DrawCmd::vtx_offset.
160        const RENDERER_HAS_VTX_OFFSET = sys::ImGuiBackendFlags_RendererHasVtxOffset as i32;
161        /// Backend renderer supports ImTextureData requests to create/update/destroy textures.
162        const RENDERER_HAS_TEXTURES = sys::ImGuiBackendFlags_RendererHasTextures as i32;
163
164        #[cfg(feature = "multi-viewport")]
165        /// Set if the platform backend supports viewports.
166        const PLATFORM_HAS_VIEWPORTS = sys::ImGuiBackendFlags_PlatformHasViewports as i32;
167        #[cfg(feature = "multi-viewport")]
168        /// Set if the renderer backend supports viewports.
169        const RENDERER_HAS_VIEWPORTS = sys::ImGuiBackendFlags_RendererHasViewports as i32;
170    }
171}
172
173bitflags! {
174    /// Viewport flags for multi-viewport support
175    #[repr(transparent)]
176    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
177    pub struct ViewportFlags: i32 {
178        /// No flags
179        const NONE = 0;
180        /// Represent a Platform Window
181        const IS_PLATFORM_WINDOW = sys::ImGuiViewportFlags_IsPlatformWindow as i32;
182        /// Represent a Platform Monitor (unused in our implementation)
183        const IS_PLATFORM_MONITOR = sys::ImGuiViewportFlags_IsPlatformMonitor as i32;
184        /// Platform Window: is created/managed by the application (rather than a dear imgui backend)
185        const OWNED_BY_APP = sys::ImGuiViewportFlags_OwnedByApp as i32;
186        /// Platform Window: Disable platform decorations: title bar, borders, etc.
187        const NO_DECORATION = sys::ImGuiViewportFlags_NoDecoration as i32;
188        /// Platform Window: Disable platform task bar icon (generally set on popups/tooltips, or all windows if ImGuiConfigFlags_ViewportsNoTaskBarIcon is set)
189        const NO_TASK_BAR_ICON = sys::ImGuiViewportFlags_NoTaskBarIcon as i32;
190        /// Platform Window: Don't take focus when created.
191        const NO_FOCUS_ON_APPEARING = sys::ImGuiViewportFlags_NoFocusOnAppearing as i32;
192        /// Platform Window: Don't take focus when clicked on.
193        const NO_FOCUS_ON_CLICK = sys::ImGuiViewportFlags_NoFocusOnClick as i32;
194        /// Platform Window: Make mouse pass through so we can drag this window while peaking behind it.
195        const NO_INPUTS = sys::ImGuiViewportFlags_NoInputs as i32;
196        /// Platform Window: Renderer doesn't need to clear the framebuffer ahead (because we will fill it entirely).
197        const NO_RENDERER_CLEAR = sys::ImGuiViewportFlags_NoRendererClear as i32;
198        /// 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!).
199        const NO_AUTO_MERGE = sys::ImGuiViewportFlags_NoAutoMerge as i32;
200        /// Platform Window: Display on top (for tooltips only).
201        const TOP_MOST = sys::ImGuiViewportFlags_TopMost as i32;
202        /// Viewport can host multiple imgui windows (secondary viewports are associated to a single window).
203        const CAN_HOST_OTHER_WINDOWS = sys::ImGuiViewportFlags_CanHostOtherWindows as i32;
204        /// Platform Window: Window is minimized, can skip render. When minimized we tend to avoid using the viewport pos/size for clipping rectangle computation.
205        const IS_MINIMIZED = sys::ImGuiViewportFlags_IsMinimized as i32;
206        /// Platform Window: Window is focused (last call to Platform_GetWindowFocus() returned true)
207        const IS_FOCUSED = sys::ImGuiViewportFlags_IsFocused as i32;
208    }
209}
210
211pub(crate) fn validate_config_flags(caller: &str, flags: ConfigFlags) {
212    let unsupported = flags.bits() & !ConfigFlags::all().bits();
213    assert!(
214        unsupported == 0,
215        "{caller} received unsupported ImGuiConfigFlags bits: 0x{unsupported:X}"
216    );
217}
218
219pub(crate) fn validate_backend_flags(caller: &str, flags: BackendFlags) {
220    let unsupported = flags.bits() & !BackendFlags::all().bits();
221    assert!(
222        unsupported == 0,
223        "{caller} received unsupported ImGuiBackendFlags bits: 0x{unsupported:X}"
224    );
225}