From 34d2a7589474b2920a489317fc0d1f44437383ff Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Mon, 1 Sep 2025 11:39:01 +0800 Subject: [PATCH] theme: Add `theme_name` to get current active theme name. (#1190) And fix theme menu selected item in example when theme mode changed. --- crates/story/src/themes.rs | 7 ++++++- crates/ui/src/theme/mod.rs | 9 +++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/crates/story/src/themes.rs b/crates/story/src/themes.rs index 110c92e5..e6a91fda 100644 --- a/crates/story/src/themes.rs +++ b/crates/story/src/themes.rs @@ -6,7 +6,7 @@ use gpui::{ use gpui_component::{ button::{Button, ButtonVariants}, popup_menu::PopupMenuExt, - IconName, Sizable, Theme, ThemeRegistry, + ActiveTheme, IconName, Sizable, Theme, ThemeRegistry, }; use serde::{Deserialize, Serialize}; @@ -38,6 +38,11 @@ pub fn init(cx: &mut App) { }) { tracing::error!("Failed to watch themes directory: {}", err); } + + cx.observe_global::(|cx| { + AppState::global_mut(cx).theme_name = Some(cx.theme().theme_name().into()); + }) + .detach(); } #[derive(Action, Clone, PartialEq)] diff --git a/crates/ui/src/theme/mod.rs b/crates/ui/src/theme/mod.rs index 6e576c65..61646c83 100644 --- a/crates/ui/src/theme/mod.rs +++ b/crates/ui/src/theme/mod.rs @@ -101,6 +101,15 @@ impl Theme { self.mode.is_dark() } + /// Returns the current theme name. + pub fn theme_name(&self) -> &SharedString { + if self.is_dark() { + &self.dark_theme.name + } else { + &self.light_theme.name + } + } + // /// Sets the theme to default light. // pub fn set_default_light(&mut self) { // self.light_theme = ThemeColor::light();