diff --git a/launcher/src/theme.rs b/launcher/src/theme.rs index 60b796709..9d495a598 100644 --- a/launcher/src/theme.rs +++ b/launcher/src/theme.rs @@ -74,22 +74,33 @@ fn c(hex: u32) -> Hsla { rgb(hex).into() } +/// Whether the launcher is pinned dark (`Some(true)`) or light, or follows the system (`None`): +/// `HORIZON_LAUNCHER_APPEARANCE=light` or `dark` (for screenshots and tests), else the player's +/// choice in Settings. +pub fn pinned_dark(choice: Appearance) -> Option { + match std::env::var("HORIZON_LAUNCHER_APPEARANCE").as_deref() { + Ok("light") => Some(false), + Ok("dark") => Some(true), + _ => match choice { + Appearance::Dark => Some(true), + Appearance::Light => Some(false), + Appearance::System => None, + }, + } +} + impl Theme { - /// The player's choice in Settings (dark unless they picked another), the system's when they - /// asked to follow it. `HORIZON_LAUNCHER_APPEARANCE=light` or `dark` overrides both (for - /// screenshots and tests). + /// The pinned look (`pinned_dark`: dark unless the player picked another), the system's when + /// they asked to follow it. pub fn for_appearance(choice: Appearance, system: WindowAppearance) -> Self { - match std::env::var("HORIZON_LAUNCHER_APPEARANCE").as_deref() { - Ok("light") => return Self::light(), - Ok("dark") => return Self::dark(), - _ => {} - } - match (choice, system) { - (Appearance::Light, _) => Self::light(), - (Appearance::System, WindowAppearance::Light | WindowAppearance::VibrantLight) => { - Self::light() - } - _ => Self::dark(), + let system_dark = !matches!( + system, + WindowAppearance::Light | WindowAppearance::VibrantLight + ); + if pinned_dark(choice).unwrap_or(system_dark) { + Self::dark() + } else { + Self::light() } } diff --git a/launcher/src/titlebar.rs b/launcher/src/titlebar.rs index 2e6445091..a023f7d7d 100644 --- a/launcher/src/titlebar.rs +++ b/launcher/src/titlebar.rs @@ -22,9 +22,8 @@ pub fn render(_theme: &Theme, _window: &Window) -> Option
{ /// macOS keeps the window's appearance on the system setting, so a launcher set to Dark still /// draws a light title bar on a light desktop. Pin the NSWindow's appearance to the choice -/// instead (`System` clears the pin, handing the chrome back to the system), honouring the same -/// `HORIZON_LAUNCHER_APPEARANCE` override the theme does. Other platforms' chrome already -/// follows the setting (ours is drawn, Windows and Linux follow the system either way). +/// instead (`theme::pinned_dark`: `System` clears the pin, handing the chrome back to the +/// system). Elsewhere the chrome is ours (Windows) or the system's (Linux). #[cfg(target_os = "macos")] pub fn apply_appearance(window: &Window, choice: Appearance) { use objc2_app_kit::{ @@ -33,17 +32,11 @@ pub fn apply_appearance(window: &Window, choice: Appearance) { }; use raw_window_handle::{HasWindowHandle, RawWindowHandle}; - // Reading AppKit's name constants is unsafe to rustc only (`use of extern static`). - let name: Option<&'static NSAppearanceName> = unsafe { - match std::env::var("HORIZON_LAUNCHER_APPEARANCE").as_deref() { - Ok("light") => Some(NSAppearanceNameAqua), - Ok("dark") => Some(NSAppearanceNameDarkAqua), - _ => match choice { - Appearance::Dark => Some(NSAppearanceNameDarkAqua), - Appearance::Light => Some(NSAppearanceNameAqua), - Appearance::System => None, - }, - } + // AppKit's name constants are extern statics: reading them is `unsafe` to rustc only. + let name: Option<&'static NSAppearanceName> = match crate::theme::pinned_dark(choice) { + Some(true) => Some(unsafe { NSAppearanceNameDarkAqua }), + Some(false) => Some(unsafe { NSAppearanceNameAqua }), + None => None, }; // The trait, not `Window::window_handle` (which is gpui's own `AnyWindowHandle`). let Ok(handle) = HasWindowHandle::window_handle(window) else {