1use crate::{
2 defaults::window::*,
3 error::{ConfigError, Result},
4};
5use serde::{Deserialize, Serialize};
6
7#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct WindowDimensions {
9 pub width: u32,
10 pub height: u32,
11}
12
13impl WindowDimensions {
14 pub fn new(width: u32, height: u32) -> Result<Self> {
15 if width < MIN_WIDTH {
16 return Err(ConfigError::ValidationError(format!(
17 "Window width must be at least {}",
18 MIN_WIDTH
19 )));
20 }
21 if height < MIN_HEIGHT {
22 return Err(ConfigError::ValidationError(format!(
23 "Window height must be at least {}",
24 MIN_HEIGHT
25 )));
26 }
27 Ok(Self {
28 width,
29 height,
30 })
31 }
32}
33
34#[derive(Debug, Clone, Serialize, Deserialize)]
35pub struct WindowConfig {
36 pub dimensions: Option<WindowDimensions>,
37 pub borderless: bool,
38}
39
40impl Default for WindowConfig {
41 fn default() -> Self {
42 Self {
43 dimensions: None, borderless: BORDERLESS
44 }
45 }
46}
47
48impl WindowConfig {
49 pub fn validate(&self) -> Result<()> {
50 if let Some(ref dims) = self.dimensions {
51 if dims.width < MIN_WIDTH {
52 return Err(ConfigError::ValidationError(format!(
53 "Window width must be at least {}",
54 MIN_WIDTH
55 )));
56 }
57 if dims.height < MIN_HEIGHT {
58 return Err(ConfigError::ValidationError(format!(
59 "Window height must be at least {}",
60 MIN_HEIGHT
61 )));
62 }
63 }
64 Ok(())
65 }
66
67 pub fn with_dimensions(width: u32, height: u32) -> Result<Self> {
68 Ok(Self {
69 dimensions: Some(WindowDimensions::new(width, height)?),
70 ..Self::default()
71 })
72 }
73}