//! The development switches of the GPUI front end, read from the //! environment. They are the Slint front end's (docs/ui.md, "Development //! switches and screenshots") where the GPUI one has the same thing to //! switch, so that one recipe serves both. //! //! - `VOELIN_WINDOW_SIZE=1440x960`: the window's size. /// What the environment switched. #[derive(Clone, Debug, Default, PartialEq)] pub struct Switches { /// The window's size in pixels, when the switch names one. pub window: Option<(f32, f32)>, } impl Switches { /// The switches of this process's environment. pub fn from_env() -> Self { Self::parse(|name| std::env::var(name).ok()) } /// The switches `get` answers for; what it does not understand is logged /// and left out. pub fn parse(get: impl Fn(&str) -> Option) -> Self { let mut switches = Self::default(); if let Some(text) = get("VOELIN_WINDOW_SIZE") { match parse_size(&text) { Some(size) => switches.window = Some(size), None => tracing::warn!("VOELIN_WINDOW_SIZE={text}: WIDTHxHEIGHT, such as 1440x960"), } } switches } } /// `WIDTHxHEIGHT` in pixels, at least 320 by 240 and at most 16384 each way. fn parse_size(text: &str) -> Option<(f32, f32)> { let (w, h) = text.trim().split_once('x')?; let (w, h): (u32, u32) = (w.parse().ok()?, h.parse().ok()?); ((320..=16384).contains(&w) && (240..=16384).contains(&h)).then_some((w as f32, h as f32)) } #[cfg(test)] mod tests { use super::*; fn size_of(text: &str) -> Switches { let text = text.to_owned(); Switches::parse(|name| (name == "VOELIN_WINDOW_SIZE").then(|| text.clone())) } #[test] fn the_window_size_is_read() { assert_eq!(size_of("1440x960").window, Some((1440.0, 960.0))); assert_eq!(Switches::parse(|_| None), Switches::default()); } #[test] fn what_makes_no_sense_is_left_out() { for text in ["10x10", "1440", "axb", "99999x99999", ""] { assert_eq!(size_of(text), Switches::default(), "{text:?}"); } } }