diff --git a/crates/voelin-ui-vm/src/settings.rs b/crates/voelin-ui-vm/src/settings.rs index 56c66dd..3139cb1 100644 --- a/crates/voelin-ui-vm/src/settings.rs +++ b/crates/voelin-ui-vm/src/settings.rs @@ -113,6 +113,64 @@ pub static UI_VOICE_COMPACT: Key = Key::new( || false, ); +/// Where the main window was and how large, as the last run left it: size and +/// position in logical pixels, and whether it was maximized (then the size is +/// the one it returns to). The position is left out where the platform does +/// not tell it (Wayland). A front end keeps it in [`UI_WINDOW`]. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct WindowState { + /// Width in logical pixels. + pub width: u32, + /// Height in logical pixels. + pub height: u32, + /// Distance of the window's left edge from the screen's, when known. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub x: Option, + /// Distance of the window's top edge from the screen's, when known. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub y: Option, + /// The window was maximized. + #[serde(default)] + pub maximized: bool, +} + +impl WindowState { + /// The smallest size a window can have been left at. + pub const MIN_SIZE: (u32, u32) = (320, 240); + /// The largest size and the farthest position taken as real: a value + /// beyond it is a damaged setting, not a screen. + pub const LIMIT: u32 = 16_384; + + /// Whether the values can be a window: a size within [`Self::MIN_SIZE`] + /// and [`Self::LIMIT`], a position within it either way, and a position + /// that is given whole or not at all. + pub fn is_sane(&self) -> bool { + let limit = i64::from(Self::LIMIT); + (Self::MIN_SIZE.0..=Self::LIMIT).contains(&self.width) + && (Self::MIN_SIZE.1..=Self::LIMIT).contains(&self.height) + && self.x.is_some() == self.y.is_some() + && [self.x, self.y].into_iter().flatten().all(|at| i64::from(at).abs() <= limit) + } +} + +fn valid_window(state: &Option) -> Result<(), String> { + match state { + Some(state) if !state.is_sane() => Err("a window of 320x240 to 16384x16384".into()), + _ => Ok(()), + } +} + +/// `ui.window`: the main window's size and position as the last run left them +/// (none until a run has). The GPUI front end writes it; the Slint one does +/// not read it. +pub static UI_WINDOW: Key> = Key::new( + "ui.window", + Kind::Json, + "The main window's size and position as the last run left them (none: not stored yet).", + || None, +) +.validated(valid_window); + /// The UI's keys besides [`UI`] and [`CLIENT_PLAYBACK`], for registering. pub fn appearance_keys() -> [&'static dyn voelin_core::settings::Setting; 6] { [ @@ -616,6 +674,47 @@ mod tests { assert!(settings.set_json("ui.voice_compact", "small".into()).is_err()); } + /// The window's state is stored as the setting `ui.window`, comes back as it + /// went, and a damaged value is refused in words. + #[test] + fn window_state_roundtrip() { + let state = + WindowState { width: 1440, height: 960, x: Some(-20), y: Some(35), maximized: true }; + let settings = voelin_core::settings::Settings::in_memory(); + assert_eq!(settings.get(&UI_WINDOW), None, "none until a run left one"); + settings.set(&UI_WINDOW, Some(state)).unwrap(); + assert_eq!(settings.get(&UI_WINDOW), Some(state)); + assert_eq!( + settings.get_json("ui.window"), + Some( + serde_json::json!({"width": 1440, "height": 960, "x": -20, "y": 35, "maximized": true}) + ) + ); + // A platform that does not tell the position leaves it out. + let no_position = WindowState { x: None, y: None, maximized: false, ..state }; + let json = serde_json::to_value(no_position).unwrap(); + assert_eq!(json, serde_json::json!({"width": 1440, "height": 960, "maximized": false})); + assert_eq!(serde_json::from_value::(json).unwrap(), no_position); + let older: WindowState = serde_json::from_str(r#"{"width":800,"height":600}"#).unwrap(); + assert_eq!( + older, + WindowState { width: 800, height: 600, x: None, y: None, maximized: false } + ); + for damaged in [ + WindowState { width: 0, ..state }, + WindowState { height: 100_000, ..state }, + WindowState { x: Some(1_000_000), ..state }, + WindowState { y: None, ..state }, + ] { + assert!(!damaged.is_sane(), "{damaged:?}"); + assert!(settings.set(&UI_WINDOW, Some(damaged)).is_err(), "{damaged:?}"); + } + assert!(settings.set_json("ui.window", "big".into()).is_err()); + assert_eq!(settings.get(&UI_WINDOW), Some(state), "a refused value changes nothing"); + settings.set(&UI_WINDOW, None).unwrap(); + assert_eq!(settings.get(&UI_WINDOW), None); + } + #[test] fn ui_settings_defaults() { let parsed: UiSettings = serde_json::from_str(r#"{"openh264":true}"#).unwrap();