use crate::persistence::error::PersistenceError;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct WindowState {
pub width: u32,
pub height: u32,
#[serde(skip_serializing_if = "Option::is_none")]
pub x: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub y: Option<i32>,
pub maximized: bool,
}
impl WindowState {
pub fn with_defaults(width: u32, height: u32) -> Self {
Self {
width,
height,
x: None,
y: None,
maximized: false,
}
}
pub fn size(&self) -> iced::Size {
iced::Size::new(self.width as f32, self.height as f32)
}
pub fn position(&self) -> Option<iced::Point> {
match (self.x, self.y) {
(Some(x), Some(y)) => Some(iced::Point::new(x as f32, y as f32)),
_ => None,
}
}
pub fn validate(&self) -> Result<(), PersistenceError> {
if self.width < 100 || self.height < 100 {
return Err(PersistenceError::InvalidState {
reason: format!(
"Window dimensions {}x{} are below minimum 100x100",
self.width, self.height
),
});
}
if self.width > 16384 || self.height > 16384 {
return Err(PersistenceError::InvalidState {
reason: format!(
"Window dimensions {}x{} exceed maximum 16384x16384",
self.width, self.height
),
});
}
Ok(())
}
}