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}
41
42impl Default for WindowCreateOptions {
43    fn default() -> Self {
44        Self {
45            window_state: FullWindowState::default(),
46            create_callback: OptionCallback::None,
47            renderer: azul_core::window::OptionRendererOptions::None,
48            theme: azul_core::window::OptionWindowTheme::None,
49            size_to_content: false,
50            hot_reload: false,
51        }
52    }
53}
54
55impl WindowCreateOptions {
56    /// Create a new WindowCreateOptions with a layout callback
57    pub fn create(layout_callback: impl Into<azul_core::callbacks::LayoutCallback>) -> Self {
58        let mut options = Self::default();
59        options.window_state.layout_callback = layout_callback.into();
60        options
61    }
62}
63
64impl_option!(WindowCreateOptions, OptionWindowCreateOptions, copy = false, [Debug, Clone, PartialEq]);
65impl_vec!(WindowCreateOptions, WindowCreateOptionsVec, WindowCreateOptionsVecDestructor, WindowCreateOptionsVecDestructorType, WindowCreateOptionsVecSlice, OptionWindowCreateOptions);
66impl_vec_clone!(
67    WindowCreateOptions,
68    WindowCreateOptionsVec,
69    WindowCreateOptionsVecDestructor
70);
71impl_vec_partialeq!(WindowCreateOptions, WindowCreateOptionsVec);
72impl_vec_debug!(WindowCreateOptions, WindowCreateOptionsVec);
73impl_vec_mut!(WindowCreateOptions, WindowCreateOptionsVec);
74
75/// Full window state including internal fields not exposed to callbacks
76#[derive(Debug, Clone, PartialEq)]
77#[repr(C)]
78pub struct FullWindowState {
79    /// Platform-specific window options
80    pub platform_specific_options: PlatformSpecificOptions,
81    /// Current keyboard state (pressed keys, modifiers)
82    pub keyboard_state: KeyboardState,
83    /// Semantic window identifier for multi-window debugging.
84    /// Can be set by the user to identify specific windows (e.g., "main", "settings", "popup-1")
85    pub window_id: AzString,
86    /// Window title bar text
87    pub title: AzString,
88    /// Optional callback invoked when the user requests the window to close
89    pub close_callback: OptionCallback,
90    /// Callback that returns the DOM for this window
91    pub layout_callback: LayoutCallback,
92    /// Window position on screen
93    pub position: WindowPosition,
94    /// Current touch/gesture input state
95    pub touch_state: TouchState,
96    /// Window dimensions (logical and physical)
97    pub size: WindowSize,
98    /// Window flags (minimized, maximized, fullscreen, etc.)
99    pub flags: WindowFlags,
100    /// Current mouse cursor state (position, buttons)
101    pub mouse_state: MouseState,
102    /// Active window theme (light/dark)
103    pub theme: WindowTheme,
104    /// Position of the IME candidate window
105    pub ime_position: ImePosition,
106    /// GPU renderer options (VSync, SRGB, hardware acceleration)
107    pub renderer_options: RendererOptions,
108    /// Monitor ID (not the full Monitor struct - just the identifier)
109    pub monitor_id: OptionU32,
110    /// Debug visualization state (layout borders, repaints, etc.)
111    pub debug_state: DebugState,
112    /// Window background color. If None, uses system window background color.
113    pub background_color: OptionColorU,
114    /// Whether this window currently has input focus
115    pub window_focused: bool,
116    /// Active route match (pattern + extracted parameters).
117    /// Set by `CallbackInfo::switch_route()` or by the web server on URL match.
118    /// Layout callbacks read this via `LayoutCallbackInfo::get_route_param()`.
119    pub active_route: azul_core::resources::OptionRouteMatch,
120}
121
122impl_option!(
123    FullWindowState,
124    OptionFullWindowState,
125    copy = false,
126    [Debug, Clone, PartialEq]
127);
128
129impl Default for FullWindowState {
130    fn default() -> Self {
131        Self {
132            platform_specific_options: PlatformSpecificOptions::default(),
133            keyboard_state: KeyboardState::default(),
134            window_id: AzString::from_const_str("azul-window"),
135            title: AzString::from_const_str("Azul Window"),
136            close_callback: OptionCallback::None,
137            layout_callback: LayoutCallback::default(),
138            position: WindowPosition::default(),
139            touch_state: TouchState::default(),
140            size: WindowSize::default(),
141            flags: WindowFlags::default(),
142            mouse_state: MouseState::default(),
143            theme: WindowTheme::default(),
144            ime_position: ImePosition::default(),
145            renderer_options: RendererOptions::default(),
146            monitor_id: OptionU32::None,
147            debug_state: DebugState::default(),
148            background_color: OptionColorU::None,
149            window_focused: true,
150            active_route: azul_core::resources::OptionRouteMatch::None,
151        }
152    }
153}