//! 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; the rest are new. //! //! - `VOELIN_OPEN=swatch`: the swatch page (every theme token, a few //! components, the icons) in place of the shell. The Slint front end's //! other pages do not exist here yet; they are named in a warning and //! skipped. //! - `VOELIN_OPEN=voice` (`voice:compact`): the voice channel page on screen //! once voice is connected, in place of the server page; `voice:compact` //! with the smaller stage, which is not stored. //! - `VOELIN_DEMO_UI=1`: the Slint front end's sample servers, shown through //! the app's own events, with their pictures as files of a folder in the //! temporary directory (removed when the window closes); nothing is sent to //! the engine or kept in the store. //! - `VOELIN_DEMO_SERVER=2`: with `VOELIN_DEMO_UI`, the sample server on //! screen, counting from 0 (the first, which is connected and populated, is //! the default; the second is observed, the third not connected). //! - `VOELIN_DEMO_TALK=10`: with `VOELIN_DEMO_UI`, one of the sample people //! starts and stops talking this many times a second, through the app's own //! events (what the meter measures while someone talks). //! - `VOELIN_DEV_THEME=dark|light|system`: the theme mode, in place of the //! one the settings give (`ui.theme`). //! - `VOELIN_FONT_SCALE=1.25`: the text size factor, in place of the //! settings' (`ui.font_scale`). //! - `VOELIN_WINDOW_SIZE=1440x960`: the window's size, in place of the //! remembered one (`ui.window`), which it then leaves alone. //! - `VOELIN_METER=1`: the cost meter, one log line a second //! ([`crate::meter`]). use crate::theme::Choice; /// A page the window can be opened on. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Page { /// The desktop shell. Shell, /// Every theme token, a few components and the icons. Swatch, } /// What the environment switched. #[derive(Clone, Debug, PartialEq)] pub struct Switches { /// The page to open on. pub open: Page, /// The voice channel page is asked for (`VOELIN_OPEN=voice`): it shows /// once voice is connected. pub voice: bool, /// With the voice page, the smaller stage, as the switch says (it is not /// stored, as the Slint front end's `voice:compact`). pub voice_compact: bool, /// Show the sample servers instead of the stored ones. pub demo_ui: bool, /// Write the sample servers' pictures to a folder of the temporary /// directory and deliver them: what the binary does with the sample /// servers, and what a test asks for by name (a test that does not leaves /// nothing behind). pub demo_pictures: bool, /// The sample server to show, by its place in the list, when the switch /// names one. pub demo_server: Option, /// How many times a second a sample person starts or stops talking (with /// the sample servers), when the switch names a rate. pub demo_talk: Option, /// The theme mode, when the switch names one. pub theme: Option, /// The text size factor, when the switch names one. pub font_scale: Option, /// The window's size in pixels, when the switch names one. pub window: Option<(f32, f32)>, /// Log the cost meter. pub meter: bool, } impl Default for Switches { fn default() -> Self { Self { open: Page::Shell, voice: false, voice_compact: false, demo_ui: false, demo_pictures: false, demo_server: None, demo_talk: None, theme: None, font_scale: None, window: None, meter: false, } } } 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(list) = get("VOELIN_OPEN") { for what in list.split(',').map(str::trim).filter(|w| !w.is_empty()) { match what { "swatch" => switches.open = Page::Swatch, "voice" => switches.voice = true, "voice:compact" => (switches.voice, switches.voice_compact) = (true, true), other => tracing::warn!( "VOELIN_OPEN={other}: this page is not in the GPUI front end yet (swatch and voice are)" ), } } } if let Some(name) = get("VOELIN_DEV_THEME") { match Choice::parse(name.trim()) { Some(choice) => switches.theme = Some(choice), None => tracing::warn!("VOELIN_DEV_THEME={name}: dark, light or system"), } } if let Some(text) = get("VOELIN_FONT_SCALE") { match text.trim().parse::() { Ok(scale) if (0.5..=3.0).contains(&scale) => switches.font_scale = Some(scale), _ => tracing::warn!("VOELIN_FONT_SCALE={text}: a number from 0.5 to 3"), } } 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"), } } if let Some(text) = get("VOELIN_DEMO_SERVER") { match text.trim().parse::() { Ok(place) if place < 3 => switches.demo_server = Some(place), _ => tracing::warn!("VOELIN_DEMO_SERVER={text}: 0, 1 or 2 (a sample server)"), } } if let Some(text) = get("VOELIN_DEMO_TALK") { match text.trim().parse::() { Ok(rate) if (0.1..=100.0).contains(&rate) => switches.demo_talk = Some(rate), _ => tracing::warn!("VOELIN_DEMO_TALK={text}: a rate from 0.1 to 100 a second"), } } let flag = |name: &str| get(name).is_some_and(|v| !matches!(v.trim(), "" | "0")); switches.demo_ui = flag("VOELIN_DEMO_UI"); switches.demo_pictures = switches.demo_ui; switches.meter = flag("VOELIN_METER"); 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 std::collections::HashMap; use super::*; fn env(pairs: &[(&str, &str)]) -> Switches { let map: HashMap = pairs.iter().map(|(k, v)| ((*k).to_owned(), (*v).to_owned())).collect(); Switches::parse(|name| map.get(name).cloned()) } #[test] fn nothing_switched_is_the_shell_in_the_settings_theme() { assert_eq!(env(&[]), Switches::default()); } #[test] fn every_switch_is_read() { let s = env(&[ ("VOELIN_OPEN", "swatch,voice:compact"), ("VOELIN_DEMO_UI", "1"), ("VOELIN_DEMO_SERVER", "2"), ("VOELIN_DEMO_TALK", "10"), ("VOELIN_DEV_THEME", "light"), ("VOELIN_FONT_SCALE", "1.25"), ("VOELIN_WINDOW_SIZE", "1440x960"), ("VOELIN_METER", "1"), ]); assert_eq!(s.open, Page::Swatch); assert!(s.voice && s.voice_compact, "the voice page, with the smaller stage"); assert!(s.demo_ui && s.demo_pictures, "the sample servers come with their pictures"); assert_eq!(s.demo_server, Some(2)); assert_eq!(s.demo_talk, Some(10.0)); assert_eq!(s.theme, Some(Choice::Light)); assert_eq!(s.font_scale, Some(1.25)); assert_eq!(s.window, Some((1440.0, 960.0))); assert!(s.meter); } #[test] fn the_voice_page_alone_has_the_larger_stage() { let s = env(&[("VOELIN_OPEN", "voice")]); assert_eq!((s.open, s.voice, s.voice_compact), (Page::Shell, true, false)); } #[test] fn what_makes_no_sense_is_left_out() { let s = env(&[ ("VOELIN_OPEN", "settings:voice,home"), ("VOELIN_DEV_THEME", "blue"), ("VOELIN_FONT_SCALE", "9"), ("VOELIN_WINDOW_SIZE", "10x10"), ("VOELIN_DEMO_UI", "0"), ("VOELIN_DEMO_SERVER", "7"), ("VOELIN_DEMO_TALK", "fast"), ("VOELIN_METER", "0"), ]); assert_eq!(s, Switches::default()); } }