Define theme/ui text style settings in theme crate

This commit is contained in:
Max Brunsfeld 2023-05-17 14:44:55 -07:00
parent 5c729c0e56
commit 67a25126d4
86 changed files with 530 additions and 637 deletions

View file

@ -2,10 +2,10 @@ use fs::Fs;
use fuzzy::{match_strings, StringMatch, StringMatchCandidate};
use gpui::{actions, elements::*, AnyElement, AppContext, Element, MouseState, ViewContext};
use picker::{Picker, PickerDelegate, PickerEvent};
use settings::{update_settings_file, Settings};
use settings::{update_settings_file, SettingsStore};
use staff_mode::StaffMode;
use std::sync::Arc;
use theme::{Theme, ThemeMeta, ThemeRegistry};
use theme::{Theme, ThemeMeta, ThemeRegistry, ThemeSettings};
use util::ResultExt;
use workspace::Workspace;
@ -18,17 +18,17 @@ pub fn init(cx: &mut AppContext) {
pub fn toggle(workspace: &mut Workspace, _: &Toggle, cx: &mut ViewContext<Workspace>) {
workspace.toggle_modal(cx, |workspace, cx| {
let themes = workspace.app_state().themes.clone();
let fs = workspace.app_state().fs.clone();
cx.add_view(|cx| ThemeSelector::new(ThemeSelectorDelegate::new(fs, themes, cx), cx))
cx.add_view(|cx| ThemeSelector::new(ThemeSelectorDelegate::new(fs, cx), cx))
});
}
#[cfg(debug_assertions)]
pub fn reload(themes: Arc<ThemeRegistry>, cx: &mut AppContext) {
let current_theme_name = cx.global::<Settings>().theme.meta.name.clone();
themes.clear();
match themes.get(&current_theme_name) {
pub fn reload(cx: &mut AppContext) {
let current_theme_name = theme::current(cx).meta.name.clone();
let registry = cx.global::<Arc<ThemeRegistry>>();
registry.clear();
match registry.get(&current_theme_name) {
Ok(theme) => {
ThemeSelectorDelegate::set_theme(theme, cx);
log::info!("reloaded theme {}", current_theme_name);
@ -43,7 +43,6 @@ pub type ThemeSelector = Picker<ThemeSelectorDelegate>;
pub struct ThemeSelectorDelegate {
fs: Arc<dyn Fs>,
registry: Arc<ThemeRegistry>,
theme_data: Vec<ThemeMeta>,
matches: Vec<StringMatch>,
original_theme: Arc<Theme>,
@ -52,18 +51,12 @@ pub struct ThemeSelectorDelegate {
}
impl ThemeSelectorDelegate {
fn new(
fs: Arc<dyn Fs>,
registry: Arc<ThemeRegistry>,
cx: &mut ViewContext<ThemeSelector>,
) -> Self {
let settings = cx.global::<Settings>();
fn new(fs: Arc<dyn Fs>, cx: &mut ViewContext<ThemeSelector>) -> Self {
let original_theme = theme::current(cx).clone();
let original_theme = settings.theme.clone();
let mut theme_names = registry
.list(**cx.default_global::<StaffMode>())
.collect::<Vec<_>>();
let staff_mode = **cx.default_global::<StaffMode>();
let registry = cx.global::<Arc<ThemeRegistry>>();
let mut theme_names = registry.list(staff_mode).collect::<Vec<_>>();
theme_names.sort_unstable_by(|a, b| a.is_light.cmp(&b.is_light).then(a.name.cmp(&b.name)));
let matches = theme_names
.iter()
@ -76,7 +69,6 @@ impl ThemeSelectorDelegate {
.collect();
let mut this = Self {
fs,
registry,
theme_data: theme_names,
matches,
original_theme: original_theme.clone(),
@ -89,7 +81,8 @@ impl ThemeSelectorDelegate {
fn show_selected_theme(&mut self, cx: &mut ViewContext<ThemeSelector>) {
if let Some(mat) = self.matches.get(self.selected_index) {
match self.registry.get(&mat.string) {
let registry = cx.global::<Arc<ThemeRegistry>>();
match registry.get(&mat.string) {
Ok(theme) => {
Self::set_theme(theme, cx);
}
@ -109,8 +102,10 @@ impl ThemeSelectorDelegate {
}
fn set_theme(theme: Arc<Theme>, cx: &mut AppContext) {
cx.update_global::<Settings, _, _>(|settings, cx| {
settings.theme = theme;
cx.update_global::<SettingsStore, _, _>(|store, cx| {
let mut theme_settings = store.get::<ThemeSettings>(None).clone();
theme_settings.theme = theme;
store.override_global(theme_settings);
cx.refresh_windows();
});
}
@ -128,9 +123,9 @@ impl PickerDelegate for ThemeSelectorDelegate {
fn confirm(&mut self, cx: &mut ViewContext<ThemeSelector>) {
self.selection_completed = true;
let theme_name = cx.global::<Settings>().theme.meta.name.clone();
update_settings_file::<Settings>(self.fs.clone(), cx, |settings_content| {
settings_content.theme = Some(theme_name);
let theme_name = theme::current(cx).meta.name.clone();
update_settings_file::<ThemeSettings>(self.fs.clone(), cx, |settings| {
settings.theme = Some(theme_name);
});
cx.emit(PickerEvent::Dismiss);
@ -212,11 +207,10 @@ impl PickerDelegate for ThemeSelectorDelegate {
selected: bool,
cx: &AppContext,
) -> AnyElement<Picker<Self>> {
let settings = cx.global::<Settings>();
let theme = &settings.theme;
let theme_match = &self.matches[ix];
let theme = theme::current(cx);
let style = theme.picker.item.style_for(mouse_state, selected);
let theme_match = &self.matches[ix];
Label::new(theme_match.string.clone(), style.label.clone())
.with_highlights(theme_match.positions.clone())
.contained()