Remove cx from ThemeSettings (#38836)
Before this change the active theme and icon theme were retrofitted onto the ThemeSettings. Now they're in their own new global (GlobalTheme::theme(cx) and GlobalTheme::icon_theme(cx)) This lets us remove cx from the settings traits, and tidy up a few other things along the way. Release Notes: - N/A
This commit is contained in:
@@ -7,7 +7,10 @@ use gpui::{
|
||||
use picker::{Picker, PickerDelegate};
|
||||
use settings::{Settings as _, SettingsStore, update_settings_file};
|
||||
use std::sync::Arc;
|
||||
use theme::{Appearance, IconTheme, ThemeMeta, ThemeRegistry, ThemeSettings};
|
||||
use theme::{
|
||||
Appearance, IconThemeName, IconThemeSelection, SystemAppearance, ThemeMeta, ThemeRegistry,
|
||||
ThemeSettings,
|
||||
};
|
||||
use ui::{ListItem, ListItemSpacing, prelude::*, v_flex};
|
||||
use util::ResultExt;
|
||||
use workspace::{ModalView, ui::HighlightedLabel};
|
||||
@@ -51,9 +54,9 @@ pub(crate) struct IconThemeSelectorDelegate {
|
||||
fs: Arc<dyn Fs>,
|
||||
themes: Vec<ThemeMeta>,
|
||||
matches: Vec<StringMatch>,
|
||||
original_theme: Arc<IconTheme>,
|
||||
original_theme: IconThemeName,
|
||||
selection_completed: bool,
|
||||
selected_theme: Option<Arc<IconTheme>>,
|
||||
selected_theme: Option<IconThemeName>,
|
||||
selected_index: usize,
|
||||
selector: WeakEntity<IconThemeSelector>,
|
||||
}
|
||||
@@ -66,7 +69,9 @@ impl IconThemeSelectorDelegate {
|
||||
cx: &mut Context<IconThemeSelector>,
|
||||
) -> Self {
|
||||
let theme_settings = ThemeSettings::get_global(cx);
|
||||
let original_theme = theme_settings.active_icon_theme.clone();
|
||||
let original_theme = theme_settings
|
||||
.icon_theme
|
||||
.name(SystemAppearance::global(cx).0);
|
||||
|
||||
let registry = ThemeRegistry::global(cx);
|
||||
let mut themes = registry
|
||||
@@ -107,29 +112,18 @@ impl IconThemeSelectorDelegate {
|
||||
selector,
|
||||
};
|
||||
|
||||
this.select_if_matching(&original_theme.name);
|
||||
this.select_if_matching(&original_theme.0);
|
||||
this
|
||||
}
|
||||
|
||||
fn show_selected_theme(
|
||||
&mut self,
|
||||
cx: &mut Context<Picker<IconThemeSelectorDelegate>>,
|
||||
) -> Option<Arc<IconTheme>> {
|
||||
if let Some(mat) = self.matches.get(self.selected_index) {
|
||||
let registry = ThemeRegistry::global(cx);
|
||||
match registry.get_icon_theme(&mat.string) {
|
||||
Ok(theme) => {
|
||||
Self::set_icon_theme(theme.clone(), cx);
|
||||
Some(theme)
|
||||
}
|
||||
Err(err) => {
|
||||
log::error!("error loading icon theme {}: {err}", mat.string);
|
||||
None
|
||||
}
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
) -> Option<IconThemeName> {
|
||||
let mat = self.matches.get(self.selected_index)?;
|
||||
let name = IconThemeName(mat.string.clone().into());
|
||||
Self::set_icon_theme(name.clone(), cx);
|
||||
Some(name)
|
||||
}
|
||||
|
||||
fn select_if_matching(&mut self, theme_name: &str) {
|
||||
@@ -140,12 +134,11 @@ impl IconThemeSelectorDelegate {
|
||||
.unwrap_or(self.selected_index);
|
||||
}
|
||||
|
||||
fn set_icon_theme(theme: Arc<IconTheme>, cx: &mut App) {
|
||||
SettingsStore::update_global(cx, |store, cx| {
|
||||
fn set_icon_theme(name: IconThemeName, cx: &mut App) {
|
||||
SettingsStore::update_global(cx, |store, _| {
|
||||
let mut theme_settings = store.get::<ThemeSettings>(None).clone();
|
||||
theme_settings.active_icon_theme = theme;
|
||||
theme_settings.icon_theme = IconThemeSelection::Static(name);
|
||||
store.override_global(theme_settings);
|
||||
cx.refresh_windows();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -170,7 +163,9 @@ impl PickerDelegate for IconThemeSelectorDelegate {
|
||||
self.selection_completed = true;
|
||||
|
||||
let theme_settings = ThemeSettings::get_global(cx);
|
||||
let theme_name = theme_settings.active_icon_theme.name.clone();
|
||||
let theme_name = theme_settings
|
||||
.icon_theme
|
||||
.name(SystemAppearance::global(cx).0);
|
||||
|
||||
telemetry::event!(
|
||||
"Settings Changed",
|
||||
@@ -181,7 +176,7 @@ impl PickerDelegate for IconThemeSelectorDelegate {
|
||||
let appearance = Appearance::from(window.appearance());
|
||||
|
||||
update_settings_file(self.fs.clone(), cx, move |settings, _| {
|
||||
theme::set_icon_theme(settings, theme_name.to_string(), appearance);
|
||||
theme::set_icon_theme(settings, theme_name, appearance);
|
||||
});
|
||||
|
||||
self.selector
|
||||
@@ -268,7 +263,7 @@ impl PickerDelegate for IconThemeSelectorDelegate {
|
||||
.matches
|
||||
.iter()
|
||||
.enumerate()
|
||||
.find(|(_, mtch)| mtch.string == selected.name)
|
||||
.find(|(_, mtch)| mtch.string.as_str() == selected.0.as_ref())
|
||||
.map(|(ix, _)| ix)
|
||||
.unwrap_or_default();
|
||||
} else {
|
||||
|
||||
@@ -203,12 +203,11 @@ impl ThemeSelectorDelegate {
|
||||
}
|
||||
|
||||
fn set_theme(theme: Arc<Theme>, cx: &mut App) {
|
||||
SettingsStore::update_global(cx, |store, cx| {
|
||||
SettingsStore::update_global(cx, |store, _| {
|
||||
let mut theme_settings = store.get::<ThemeSettings>(None).clone();
|
||||
theme_settings.active_theme = theme;
|
||||
theme_settings.apply_theme_overrides();
|
||||
let name = theme.as_ref().name.clone().into();
|
||||
theme_settings.theme = theme::ThemeSelection::Static(theme::ThemeName(name));
|
||||
store.override_global(theme_settings);
|
||||
cx.refresh_windows();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user