From a3c14e15156fe256ce43b2188a5bb0b92bf790b0 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Sat, 29 Jun 2024 09:05:49 +0800 Subject: [PATCH] Sync with system appearance. --- crates/ui/src/icon.rs | 1 - crates/ui/src/theme.rs | 17 +++++++++++++++-- crates/workspace/src/lib.rs | 5 +++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/crates/ui/src/icon.rs b/crates/ui/src/icon.rs index 4f5038f7..36415a19 100644 --- a/crates/ui/src/icon.rs +++ b/crates/ui/src/icon.rs @@ -4,7 +4,6 @@ use gpui::{ RenderOnce, SharedString, StyleRefinement, Styled, Svg, TextStyle, TextStyleRefinement, WindowContext, }; -use windows::Win32::Foundation::NOERROR; #[derive(IntoElement)] pub enum IconName { diff --git a/crates/ui/src/theme.rs b/crates/ui/src/theme.rs index 3e733273..66a441d9 100644 --- a/crates/ui/src/theme.rs +++ b/crates/ui/src/theme.rs @@ -1,4 +1,4 @@ -use gpui::{hsla, AppContext, Global, Hsla}; +use gpui::{hsla, AppContext, Global, Hsla, WindowAppearance}; pub trait ActiveTheme { fn theme(&self) -> &Theme; @@ -293,7 +293,20 @@ impl Theme { } pub fn init(cx: &mut AppContext) { - cx.set_global(Theme::new()) + cx.set_global(Theme::new()); + Self::sync_system_appearance(cx) + } + + /// Sync the theme with the system appearance + pub fn sync_system_appearance(cx: &mut AppContext) { + match cx.window_appearance() { + WindowAppearance::Dark | WindowAppearance::VibrantDark => { + Self::change(ThemeMode::Dark, cx) + } + WindowAppearance::Light | WindowAppearance::VibrantLight => { + Self::change(ThemeMode::Light, cx) + } + } } pub fn change(mode: ThemeMode, cx: &mut AppContext) { diff --git a/crates/workspace/src/lib.rs b/crates/workspace/src/lib.rs index 760e748a..3f9e1e33 100644 --- a/crates/workspace/src/lib.rs +++ b/crates/workspace/src/lib.rs @@ -28,6 +28,11 @@ impl Workspace { _parent: Option>, cx: &mut ViewContext, ) -> Self { + cx.observe_window_appearance(|_workspace, cx| { + Theme::sync_system_appearance(cx); + }) + .detach(); + Workspace { stories: Stories::view(cx), notifications: Default::default(),