Sorted themes correctly

This commit is contained in:
Mikayla Maki 2022-09-08 14:11:48 -07:00
parent a1889ad236
commit bdf655d757
5 changed files with 34 additions and 23 deletions

View file

@ -15,7 +15,7 @@ pub use theme_registry::*;
#[derive(Deserialize, Default)] #[derive(Deserialize, Default)]
pub struct Theme { pub struct Theme {
#[serde(default)] #[serde(default)]
pub name: String, pub meta: ThemeMeta,
pub workspace: Workspace, pub workspace: Workspace,
pub context_menu: ContextMenu, pub context_menu: ContextMenu,
pub chat_panel: ChatPanel, pub chat_panel: ChatPanel,
@ -34,6 +34,12 @@ pub struct Theme {
pub terminal: TerminalStyle, pub terminal: TerminalStyle,
} }
#[derive(Deserialize, Default, Clone)]
pub struct ThemeMeta {
pub name: String,
pub is_light: bool,
}
#[derive(Deserialize, Default)] #[derive(Deserialize, Default)]
pub struct Workspace { pub struct Workspace {
pub background: Color, pub background: Color,

View file

@ -1,4 +1,4 @@
use crate::Theme; use crate::{Theme, ThemeMeta};
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use gpui::{fonts, AssetSource, FontCache}; use gpui::{fonts, AssetSource, FontCache};
use parking_lot::Mutex; use parking_lot::Mutex;
@ -22,11 +22,11 @@ impl ThemeRegistry {
}) })
} }
pub fn list(&self) -> impl Iterator<Item = String> { pub fn list(&self) -> impl Iterator<Item = ThemeMeta> + '_ {
self.assets.list("themes/").into_iter().filter_map(|path| { self.assets.list("themes/").into_iter().filter_map(|path| {
let filename = path.strip_prefix("themes/")?; let filename = path.strip_prefix("themes/")?;
let theme_name = filename.strip_suffix(".json")?; let theme_name = filename.strip_suffix(".json")?;
Some(theme_name.to_string()) self.get(theme_name).ok().map(|theme| theme.meta.clone())
}) })
} }
@ -50,7 +50,7 @@ impl ThemeRegistry {
serde_path_to_error::deserialize(&mut serde_json::Deserializer::from_slice(&theme_json)) serde_path_to_error::deserialize(&mut serde_json::Deserializer::from_slice(&theme_json))
})?; })?;
theme.name = name.into(); theme.meta.name = name.into();
let theme = Arc::new(theme); let theme = Arc::new(theme);
self.themes.lock().insert(name.to_string(), theme.clone()); self.themes.lock().insert(name.to_string(), theme.clone());
Ok(theme) Ok(theme)

View file

@ -6,12 +6,12 @@ use gpui::{
use picker::{Picker, PickerDelegate}; use picker::{Picker, PickerDelegate};
use settings::Settings; use settings::Settings;
use std::sync::Arc; use std::sync::Arc;
use theme::{Theme, ThemeRegistry}; use theme::{Theme, ThemeMeta, ThemeRegistry};
use workspace::{AppState, Workspace}; use workspace::{AppState, Workspace};
pub struct ThemeSelector { pub struct ThemeSelector {
registry: Arc<ThemeRegistry>, registry: Arc<ThemeRegistry>,
theme_names: Vec<String>, theme_data: Vec<ThemeMeta>,
matches: Vec<StringMatch>, matches: Vec<StringMatch>,
original_theme: Arc<Theme>, original_theme: Arc<Theme>,
picker: ViewHandle<Picker<Self>>, picker: ViewHandle<Picker<Self>>,
@ -42,29 +42,30 @@ impl ThemeSelector {
let original_theme = cx.global::<Settings>().theme.clone(); let original_theme = cx.global::<Settings>().theme.clone();
let mut theme_names = registry.list().collect::<Vec<_>>(); let mut theme_names = registry.list().collect::<Vec<_>>();
theme_names.sort_unstable_by(|a, b| { theme_names.sort_unstable_by(|a, b| {
a.ends_with("dark") a.is_light.cmp(&b.is_light).reverse()
.cmp(&b.ends_with("dark")) // a.ends_with("dark")
.then_with(|| a.cmp(b)) // .cmp(&b.ends_with("dark"))
// .then_with(|| a.cmp(b))
}); });
let matches = theme_names let matches = theme_names
.iter() .iter()
.map(|name| StringMatch { .map(|meta| StringMatch {
candidate_id: 0, candidate_id: 0,
score: 0.0, score: 0.0,
positions: Default::default(), positions: Default::default(),
string: name.clone(), string: meta.name.clone(),
}) })
.collect(); .collect();
let mut this = Self { let mut this = Self {
registry, registry,
theme_names, theme_data: theme_names,
matches, matches,
picker, picker,
original_theme: original_theme.clone(), original_theme: original_theme.clone(),
selected_index: 0, selected_index: 0,
selection_completed: false, selection_completed: false,
}; };
this.select_if_matching(&original_theme.name); this.select_if_matching(&original_theme.meta.name);
this this
} }
@ -82,7 +83,7 @@ impl ThemeSelector {
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
pub fn reload(themes: Arc<ThemeRegistry>, cx: &mut MutableAppContext) { pub fn reload(themes: Arc<ThemeRegistry>, cx: &mut MutableAppContext) {
let current_theme_name = cx.global::<Settings>().theme.name.clone(); let current_theme_name = cx.global::<Settings>().theme.meta.name.clone();
themes.clear(); themes.clear();
match themes.get(&current_theme_name) { match themes.get(&current_theme_name) {
Ok(theme) => { Ok(theme) => {
@ -165,13 +166,13 @@ impl PickerDelegate for ThemeSelector {
fn update_matches(&mut self, query: String, cx: &mut ViewContext<Self>) -> gpui::Task<()> { fn update_matches(&mut self, query: String, cx: &mut ViewContext<Self>) -> gpui::Task<()> {
let background = cx.background().clone(); let background = cx.background().clone();
let candidates = self let candidates = self
.theme_names .theme_data
.iter() .iter()
.enumerate() .enumerate()
.map(|(id, name)| StringMatchCandidate { .map(|(id, meta)| StringMatchCandidate {
id, id,
char_bag: name.as_str().into(), char_bag: meta.name.as_str().into(),
string: name.clone(), string: meta.name.clone(),
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();

View file

@ -244,7 +244,7 @@ pub fn initialize_workspace(
cx.emit(workspace::Event::PaneAdded(workspace.active_pane().clone())); cx.emit(workspace::Event::PaneAdded(workspace.active_pane().clone()));
let theme_names = app_state.themes.list().collect(); let theme_names = app_state.themes.list().map(|meta| meta.name).collect();
let language_names = &languages::LANGUAGE_NAMES; let language_names = &languages::LANGUAGE_NAMES;
workspace.project().update(cx, |project, cx| { workspace.project().update(cx, |project, cx| {
@ -1668,12 +1668,12 @@ mod tests {
let settings = Settings::defaults(Assets, cx.font_cache(), &themes); let settings = Settings::defaults(Assets, cx.font_cache(), &themes);
let mut has_default_theme = false; let mut has_default_theme = false;
for theme_name in themes.list() { for theme_name in themes.list().map(|meta| meta.name) {
let theme = themes.get(&theme_name).unwrap(); let theme = themes.get(&theme_name).unwrap();
if theme.name == settings.theme.name { if theme.meta.name == settings.theme.meta.name {
has_default_theme = true; has_default_theme = true;
} }
assert_eq!(theme.name, theme_name); assert_eq!(theme.meta.name, theme_name);
} }
assert!(has_default_theme); assert!(has_default_theme);
} }

View file

@ -22,6 +22,10 @@ export const panel = {
export default function app(theme: Theme): Object { export default function app(theme: Theme): Object {
return { return {
meta: {
name: theme.name,
isLight: theme.isLight
},
picker: picker(theme), picker: picker(theme),
workspace: workspace(theme), workspace: workspace(theme),
contextMenu: contextMenu(theme), contextMenu: contextMenu(theme),