Sync with system appearance.

This commit is contained in:
Jason Lee 2024-06-29 09:05:49 +08:00
parent 3e8421c495
commit a3c14e1515
3 changed files with 20 additions and 3 deletions

View file

@ -4,7 +4,6 @@ use gpui::{
RenderOnce, SharedString, StyleRefinement, Styled, Svg, TextStyle, TextStyleRefinement,
WindowContext,
};
use windows::Win32::Foundation::NOERROR;
#[derive(IntoElement)]
pub enum IconName {

View file

@ -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) {

View file

@ -28,6 +28,11 @@ impl Workspace {
_parent: Option<WeakView<Self>>,
cx: &mut ViewContext<Self>,
) -> Self {
cx.observe_window_appearance(|_workspace, cx| {
Theme::sync_system_appearance(cx);
})
.detach();
Workspace {
stories: Stories::view(cx),
notifications: Default::default(),