Skip to main content

euv_core/window/config/
impl.rs

1use super::*;
2
3/// Default display info (`1280x800`, `1.0x` scale, `60Hz`, `sRGB`).
4impl Default for DisplayInfo {
5    /// Creates standard desktop fallback display metadata.
6    ///
7    /// # Returns
8    ///
9    /// - `Self` - Default `DisplayInfo`.
10    fn default() -> Self {
11        Self::new(
12            DEFAULT_WINDOW_WIDTH,
13            DEFAULT_WINDOW_HEIGHT,
14            1.0,
15            DEFAULT_REFRESH_RATE_HZ,
16            ColorSpace::Srgb,
17        )
18    }
19}
20
21/// Default `WindowConfig` with cross-platform sensible defaults.
22impl Default for WindowConfig {
23    /// Creates a standard resizable `1280x800` decorated window configuration.
24    ///
25    /// # Returns
26    ///
27    /// - `Self` - Default `WindowConfig`.
28    fn default() -> Self {
29        Self {
30            title: DEFAULT_WINDOW_TITLE.to_string(),
31            width: DEFAULT_WINDOW_WIDTH,
32            height: DEFAULT_WINDOW_HEIGHT,
33            min_size: None,
34            max_size: None,
35            position: None,
36            resizable: true,
37            decorations: true,
38            transparent: false,
39            always_on_top: false,
40            fullscreen: FullscreenMode::Windowed,
41            immersive: false,
42            visible: true,
43            render: RenderConfig::default(),
44            orientation: OrientationLock::Auto,
45            status_bar: StatusBarStyle::Auto,
46            preferred_theme: None,
47            icon: None,
48            icon_path: None,
49        }
50    }
51}
52
53/// Conversion and helper methods for `SafeAreaInsets`.
54impl SafeAreaInsets {
55    /// Converts safe-area insets directly into layout `Edges`.
56    ///
57    /// # Arguments
58    ///
59    /// - `&Self` - Safe-area insets.
60    ///
61    /// # Returns
62    ///
63    /// - `Edges` - Equivalent padding edges.
64    #[must_use]
65    pub fn to_edges(&self) -> Edges {
66        Edges::new(
67            self.get_top(),
68            self.get_right(),
69            self.get_bottom(),
70            self.get_left(),
71        )
72    }
73}
74
75/// Convenience setters and safe optional accessors for `WindowConfig`.
76impl WindowConfig {
77    /// Sets initial logical `(width, height)` clamped to at least `1x1`.
78    ///
79    /// # Arguments
80    ///
81    /// - `&Self` - Mutable window config.
82    /// - `u32` - Logical width.
83    /// - `u32` - Logical height.
84    ///
85    /// # Returns
86    ///
87    /// - `&mut Self` - Mutable reference for fluent setter chaining.
88    pub fn set_size(&mut self, width: u32, height: u32) -> &mut Self {
89        self.set_width(width.max(1)).set_height(height.max(1))
90    }
91
92    /// Returns the optional minimum size tuple.
93    ///
94    /// # Arguments
95    ///
96    /// - `&Self` - Window configuration.
97    ///
98    /// # Returns
99    ///
100    /// - `Option<(u32, u32)>` - Minimum logical dimensions if set.
101    #[must_use]
102    pub fn min_size_value(&self) -> Option<(u32, u32)> {
103        let Self { min_size, .. } = self;
104        *min_size
105    }
106
107    /// Returns the optional maximum size tuple.
108    ///
109    /// # Arguments
110    ///
111    /// - `&Self` - Window configuration.
112    ///
113    /// # Returns
114    ///
115    /// - `Option<(u32, u32)>` - Maximum logical dimensions if set.
116    #[must_use]
117    pub fn max_size_value(&self) -> Option<(u32, u32)> {
118        let Self { max_size, .. } = self;
119        *max_size
120    }
121
122    /// Returns the optional initial window position.
123    ///
124    /// # Arguments
125    ///
126    /// - `&Self` - Window configuration.
127    ///
128    /// # Returns
129    ///
130    /// - `Option<(i32, i32)>` - Initial screen position if set.
131    #[must_use]
132    pub fn position_value(&self) -> Option<(i32, i32)> {
133        let Self { position, .. } = self;
134        *position
135    }
136
137    /// Returns the optional preferred theme override.
138    ///
139    /// # Arguments
140    ///
141    /// - `&Self` - Window configuration.
142    ///
143    /// # Returns
144    ///
145    /// - `Option<ThemeMode>` - Preferred theme mode if set.
146    #[must_use]
147    pub fn preferred_theme_value(&self) -> Option<ThemeMode> {
148        let Self {
149            preferred_theme, ..
150        } = self;
151        *preferred_theme
152    }
153
154    /// Returns the optional decoded RGBA8 application icon bitmap.
155    ///
156    /// # Arguments
157    ///
158    /// - `&Self` - Window configuration.
159    ///
160    /// # Returns
161    ///
162    /// - `Option<ImageBuffer>` - Configured RGBA8 icon buffer if set.
163    #[must_use]
164    pub fn icon_value(&self) -> Option<ImageBuffer> {
165        let Self { icon, .. } = self;
166        icon.clone()
167    }
168
169    /// Returns the optional application icon asset path.
170    ///
171    /// # Arguments
172    ///
173    /// - `&Self` - Window configuration.
174    ///
175    /// # Returns
176    ///
177    /// - `Option<String>` - Configured icon asset path if set.
178    #[must_use]
179    pub fn icon_path_value(&self) -> Option<String> {
180        let Self { icon_path, .. } = self;
181        icon_path.clone()
182    }
183}
184
185/// State construction and geometry queries for `WindowState`.
186impl WindowState {
187    /// Initializes a `WindowState` from a `WindowId`, `WindowConfig`, and DPI scale factor.
188    ///
189    /// # Arguments
190    ///
191    /// - `WindowId` - Assigned window handle.
192    /// - `&WindowConfig` - Initial window configuration.
193    /// - `f32` - Active display scale factor.
194    ///
195    /// # Returns
196    ///
197    /// - `Self` - Initialized live window state.
198    #[must_use]
199    pub fn from_config(id: WindowId, config: &WindowConfig, scale_factor: f32) -> Self {
200        let safe_scale: f32 = scale_factor.max(0.5);
201        let logical_w: f32 = config.get_width().max(1) as f32;
202        let logical_h: f32 = config.get_height().max(1) as f32;
203        let phys_w: u32 = ((logical_w * safe_scale).round() as u32).max(1);
204        let phys_h: u32 = ((logical_h * safe_scale).round() as u32).max(1);
205        let display: DisplayInfo = DisplayInfo::new(
206            phys_w,
207            phys_h,
208            safe_scale,
209            DEFAULT_REFRESH_RATE_HZ,
210            config.get_render().get_color_space(),
211        );
212        Self {
213            id,
214            logical_width: logical_w,
215            logical_height: logical_h,
216            physical_width: phys_w,
217            physical_height: phys_h,
218            scale_factor: safe_scale,
219            safe_area: SafeAreaInsets::default(),
220            display,
221            focused: true,
222            minimized: false,
223            maximized: false,
224            fullscreen: config.get_fullscreen(),
225            system_theme: config.preferred_theme_value().unwrap_or(ThemeMode::Light),
226        }
227    }
228
229    /// Returns the logical viewport `Size`.
230    ///
231    /// # Arguments
232    ///
233    /// - `&Self` - Window state.
234    ///
235    /// # Returns
236    ///
237    /// - `Size` - Logical width and height.
238    #[must_use]
239    pub fn logical_size(&self) -> Size {
240        Size::new(self.get_logical_width(), self.get_logical_height())
241    }
242
243    /// Returns the responsive `Breakpoint` for the current logical width.
244    ///
245    /// # Arguments
246    ///
247    /// - `&Self` - Window state.
248    ///
249    /// # Returns
250    ///
251    /// - `Breakpoint` - Active viewport breakpoint.
252    #[must_use]
253    pub fn breakpoint(&self) -> Breakpoint {
254        Breakpoint::from_width(self.get_logical_width())
255    }
256}