From 9388b88f59b3b505800b0659f81d9e9eaeeb2375 Mon Sep 17 00:00:00 2001 From: Floyd Wang Date: Thu, 12 Sep 2024 16:14:41 +0800 Subject: [PATCH] theme: Simplify init (#237) --- crates/app/src/story_workspace.rs | 1 - crates/ui/src/lib.rs | 1 + crates/ui/src/theme.rs | 18 +++++++----------- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/crates/app/src/story_workspace.rs b/crates/app/src/story_workspace.rs index 4062c8bb..150e4b78 100644 --- a/crates/app/src/story_workspace.rs +++ b/crates/app/src/story_workspace.rs @@ -31,7 +31,6 @@ actions!(workspace, [Open, CloseWindow]); pub fn init(_app_state: Arc, cx: &mut AppContext) { cx.on_action(|_action: &Open, _cx: &mut AppContext| {}); - Theme::init(cx); ui::init(cx); story::init(cx); } diff --git a/crates/ui/src/lib.rs b/crates/ui/src/lib.rs index afaddde9..dae38e92 100644 --- a/crates/ui/src/lib.rs +++ b/crates/ui/src/lib.rs @@ -58,6 +58,7 @@ pub use svg_img::*; /// Initialize the UI module. pub fn init(cx: &mut gpui::AppContext) { + theme::init(cx); context_menu::init(cx); date_picker::init(cx); dock::init(cx); diff --git a/crates/ui/src/theme.rs b/crates/ui/src/theme.rs index fb4999f6..f9be1479 100644 --- a/crates/ui/src/theme.rs +++ b/crates/ui/src/theme.rs @@ -5,6 +5,10 @@ use gpui::{ ViewContext, WindowAppearance, WindowContext, }; +pub fn init(cx: &mut AppContext) { + Theme::sync_system_appearance(cx) +} + pub trait ActiveTheme { fn theme(&self) -> &Theme; } @@ -328,7 +332,7 @@ impl Theme { impl From for Theme { fn from(colors: Colors) -> Self { Theme { - mode: ThemeMode::Dark, + mode: ThemeMode::default(), transparent: Hsla::transparent_black(), font_size: 14.0, font_family: if cfg!(target_os = "macos") { @@ -397,9 +401,10 @@ impl From for Theme { } } -#[derive(Debug, PartialEq, PartialOrd, Eq)] +#[derive(Debug, Default, PartialEq, PartialOrd, Eq)] pub enum ThemeMode { Light, + #[default] Dark, } @@ -410,15 +415,6 @@ impl ThemeMode { } impl Theme { - fn new() -> Self { - Self::from(Colors::dark()) - } - - pub fn init(cx: &mut AppContext) { - 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() {