diff --git a/crates/theme_selector/src/theme_selector.rs b/crates/theme_selector/src/theme_selector.rs index 8c48f295dd..ad960a77ca 100644 --- a/crates/theme_selector/src/theme_selector.rs +++ b/crates/theme_selector/src/theme_selector.rs @@ -7,9 +7,11 @@ use gpui::{ Window, actions, }; use picker::{Picker, PickerDelegate}; -use settings::{SettingsStore, update_settings_file}; +use settings::{Settings as _, SettingsStore, update_settings_file}; use std::sync::Arc; -use theme::{Appearance, Theme, ThemeMeta, ThemeRegistry, ThemeSettings}; +use theme::{ + Appearance, Theme, ThemeMeta, ThemeMode, ThemeRegistry, ThemeSelection, ThemeSettings, +}; use ui::{ListItem, ListItemSpacing, prelude::*, v_flex}; use util::ResultExt; use workspace::{ModalView, Workspace, ui::HighlightedLabel, with_active_or_new_workspace}; @@ -38,6 +40,11 @@ pub fn init(cx: &mut App) { toggle_icon_theme_selector(workspace, &action, window, cx); }); }); + cx.on_action(|_: &zed_actions::theme_selector::ToggleMode, cx| { + with_active_or_new_workspace(cx, |workspace, window, cx| { + toggle_theme_mode(workspace, window, cx); + }); + }); } fn toggle_theme_selector( @@ -76,6 +83,35 @@ fn toggle_icon_theme_selector( }); } +fn toggle_theme_mode(workspace: &mut Workspace, _window: &mut Window, cx: &mut Context) { + let current_settings = ThemeSettings::get_global(cx); + let current_selection = current_settings.theme_selection.as_ref(); + + let new_mode = match current_selection { + Some(ThemeSelection::Dynamic { mode, .. }) => match mode { + ThemeMode::Light => ThemeMode::Dark, + ThemeMode::Dark => ThemeMode::Light, + ThemeMode::System => { + if cx.theme().appearance().is_light() { + ThemeMode::Dark + } else { + ThemeMode::Light + } + } + }, + Some(ThemeSelection::Static(_)) => ThemeMode::Light, + None => ThemeMode::Light, + }; + + let fs = workspace.app_state().fs.clone(); + + update_settings_file::(fs, cx, move |settings, _| { + settings.set_mode(new_mode); + }); + + ThemeSettings::reload_current_theme(cx); +} + impl ModalView for ThemeSelector {} struct ThemeSelector { diff --git a/crates/zed/src/zed/app_menus.rs b/crates/zed/src/zed/app_menus.rs index 6c7ab0b374..a5528b53b4 100644 --- a/crates/zed/src/zed/app_menus.rs +++ b/crates/zed/src/zed/app_menus.rs @@ -32,6 +32,10 @@ pub fn app_menus() -> Vec { "Select Theme...", zed_actions::theme_selector::Toggle::default(), ), + MenuItem::action( + "Toggle Theme Mode", + zed_actions::theme_selector::ToggleMode, + ), ], }), MenuItem::separator(), diff --git a/crates/zed_actions/src/lib.rs b/crates/zed_actions/src/lib.rs index 8f4c42ca49..5efb35365f 100644 --- a/crates/zed_actions/src/lib.rs +++ b/crates/zed_actions/src/lib.rs @@ -246,6 +246,12 @@ pub mod theme_selector { /// A list of theme names to filter the theme selector down to. pub themes_filter: Option>, } + + /// Toggles between light and dark theme modes. + #[derive(PartialEq, Clone, Default, Debug, Deserialize, JsonSchema, Action)] + #[action(namespace = theme_selector)] + #[serde(deny_unknown_fields)] + pub struct ToggleMode; } pub mod icon_theme_selector {