Skip to main content

azul_layout/
window_state.rs

1//! Window state types for azul-layout
2//!
3//! These types are defined here (rather than azul-core) because CallbackInfo
4//! needs to reference them, and CallbackInfo must live in azul-layout (since
5//! it requires LayoutWindow).
6
7use alloc::collections::BTreeMap;
8
9use azul_core::{
10    callbacks::LayoutCallback,
11    dom::DomId,
12    window::{
13        DebugState, ImePosition, KeyboardState, Monitor, MouseState, PlatformSpecificOptions,
14        RendererOptions, TouchState, WindowFlags, WindowPosition, WindowSize, WindowTheme,
15    },
16};
17use azul_css::{
18    corety::OptionU32, impl_option, impl_option_inner, impl_vec, impl_vec_clone, impl_vec_debug,
19    impl_vec_mut, impl_vec_partialeq, props::basic::OptionColorU, AzString,
20};
21
22use crate::callbacks::OptionCallback;
23
24/// Options for creating a new window
25#[derive(Debug, Clone, PartialEq)]
26#[repr(C)]
27pub struct WindowCreateOptions {
28    /// Initial state for the new window
29    pub window_state: FullWindowState,
30    /// Optional callback invoked after the window is created
31    pub create_callback: OptionCallback,
32    /// Optional renderer configuration (e.g., `VSync`, SRGB)
33    pub renderer: azul_core::window::OptionRendererOptions,
34    /// Optional window theme override (light/dark)
35    pub theme: azul_core::window::OptionWindowTheme,
36    /// If true, the window is resized to fit its content after the first layout
37    pub size_to_content: bool,
38    /// If true, enables hot-reloading of CSS and resources
39    pub hot_reload: bool,
40    /// Parent window's platform id (the window-registry key: X Window id on X11,
41    /// `wl_surface` ptr on Wayland, HWND on Windows, `NSWindow` ptr on macOS), or 0
42    /// for a top-level window with no parent. Child windows (menus, dropdowns,
43    /// dialogs) set this so the backend can position them relative to the parent
44    /// and, on X11, reuse the parent's display connection for the single shared
45    /// event pump. 0 = no parent.
46    pub parent_window_id: u64,
47}
48
49impl Default for WindowCreateOptions {
50    fn default() -> Self {
51        Self {
52            window_state: FullWindowState::default(),
53            create_callback: OptionCallback::None,
54            renderer: azul_core::window::OptionRendererOptions::None,
55            theme: azul_core::window::OptionWindowTheme::None,
56            size_to_content: false,
57            hot_reload: false,
58            parent_window_id: 0,
59        }
60    }
61}
62
63impl WindowCreateOptions {
64    /// Create a new `WindowCreateOptions` with a layout callback
65    pub fn create(layout_callback: impl Into<LayoutCallback>) -> Self {
66        let mut options = Self::default();
67        options.window_state.layout_callback = layout_callback.into();
68        options
69    }
70}
71
72impl_option!(WindowCreateOptions, OptionWindowCreateOptions, copy = false, [Debug, Clone, PartialEq]);
73impl_vec!(WindowCreateOptions, WindowCreateOptionsVec, WindowCreateOptionsVecDestructor, WindowCreateOptionsVecDestructorType, WindowCreateOptionsVecSlice, OptionWindowCreateOptions);
74impl_vec_clone!(
75    WindowCreateOptions,
76    WindowCreateOptionsVec,
77    WindowCreateOptionsVecDestructor
78);
79impl_vec_partialeq!(WindowCreateOptions, WindowCreateOptionsVec);
80impl_vec_debug!(WindowCreateOptions, WindowCreateOptionsVec);
81impl_vec_mut!(WindowCreateOptions, WindowCreateOptionsVec);
82
83/// Full window state including internal fields not exposed to callbacks
84#[derive(Debug, Clone, PartialEq)]
85#[repr(C)]
86pub struct FullWindowState {
87    /// Platform-specific window options
88    pub platform_specific_options: PlatformSpecificOptions,
89    /// Current keyboard state (pressed keys, modifiers)
90    pub keyboard_state: KeyboardState,
91    /// Semantic window identifier for multi-window debugging.
92    /// Can be set by the user to identify specific windows (e.g., "main", "settings", "popup-1")
93    pub window_id: AzString,
94    /// Window title bar text
95    pub title: AzString,
96    /// Optional callback invoked when the user requests the window to close
97    pub close_callback: OptionCallback,
98    /// Callback that returns the DOM for this window
99    pub layout_callback: LayoutCallback,
100    /// Window position on screen
101    pub position: WindowPosition,
102    /// Current touch/gesture input state
103    pub touch_state: TouchState,
104    /// Window dimensions (logical and physical)
105    pub size: WindowSize,
106    /// Window flags (minimized, maximized, fullscreen, etc.)
107    pub flags: WindowFlags,
108    /// Current mouse cursor state (position, buttons)
109    pub mouse_state: MouseState,
110    /// Active window theme (light/dark)
111    pub theme: WindowTheme,
112    /// Position of the IME candidate window
113    pub ime_position: ImePosition,
114    /// GPU renderer options (`VSync`, SRGB, hardware acceleration)
115    pub renderer_options: RendererOptions,
116    /// Monitor ID (not the full Monitor struct - just the identifier)
117    pub monitor_id: OptionU32,
118    /// Debug visualization state (layout borders, repaints, etc.)
119    pub debug_state: DebugState,
120    /// Window background color. If None, uses system window background color.
121    pub background_color: OptionColorU,
122    /// Whether this window currently has input focus
123    pub window_focused: bool,
124    /// Active route match (pattern + extracted parameters).
125    /// Set by `CallbackInfo::switch_route()` or by the web server on URL match.
126    /// Layout callbacks read this via `LayoutCallbackInfo::get_route_param()`.
127    pub active_route: azul_core::resources::OptionRouteMatch,
128}
129
130impl_option!(
131    FullWindowState,
132    OptionFullWindowState,
133    copy = false,
134    [Debug, Clone, PartialEq]
135);
136
137// C-ABI vec so that `CallbackInfo::queue_window_state_sequence()` can be
138// exposed over FFI (a `Vec<FullWindowState>` is not repr(C)).
139impl_vec!(
140    FullWindowState,
141    FullWindowStateVec,
142    FullWindowStateVecDestructor,
143    FullWindowStateVecDestructorType,
144    FullWindowStateVecSlice,
145    OptionFullWindowState
146);
147impl_vec_clone!(
148    FullWindowState,
149    FullWindowStateVec,
150    FullWindowStateVecDestructor
151);
152impl_vec_partialeq!(FullWindowState, FullWindowStateVec);
153impl_vec_debug!(FullWindowState, FullWindowStateVec);
154
155impl Default for FullWindowState {
156    fn default() -> Self {
157        Self {
158            platform_specific_options: PlatformSpecificOptions::default(),
159            keyboard_state: KeyboardState::default(),
160            window_id: AzString::from_const_str("azul-window"),
161            title: AzString::from_const_str("Azul Window"),
162            close_callback: OptionCallback::None,
163            layout_callback: LayoutCallback::default(),
164            position: WindowPosition::default(),
165            touch_state: TouchState::default(),
166            size: WindowSize::default(),
167            flags: WindowFlags::default(),
168            mouse_state: MouseState::default(),
169            theme: WindowTheme::default(),
170            ime_position: ImePosition::default(),
171            renderer_options: RendererOptions::default(),
172            monitor_id: OptionU32::None,
173            debug_state: DebugState::default(),
174            background_color: OptionColorU::None,
175            window_focused: true,
176            active_route: azul_core::resources::OptionRouteMatch::None,
177        }
178    }
179}