2024-01-02 22:02:53 +00:00
|
|
|
use client::{telemetry::Telemetry, TelemetrySettings};
|
2023-08-26 00:00:53 +00:00
|
|
|
use feature_flags::FeatureFlagAppExt;
|
2023-05-10 01:14:42 +00:00
|
|
|
use fs::Fs;
|
2021-10-04 23:45:05 +00:00
|
|
|
use fuzzy::{match_strings, StringMatch, StringMatchCandidate};
|
2023-04-21 19:04:03 +00:00
|
|
|
use gpui::{actions, elements::*, AnyElement, AppContext, Element, MouseState, ViewContext};
|
2023-04-20 13:25:48 +00:00
|
|
|
use picker::{Picker, PickerDelegate, PickerEvent};
|
2023-05-17 21:44:55 +00:00
|
|
|
use settings::{update_settings_file, SettingsStore};
|
2022-04-14 23:51:14 +00:00
|
|
|
use std::sync::Arc;
|
2023-05-17 21:44:55 +00:00
|
|
|
use theme::{Theme, ThemeMeta, ThemeRegistry, ThemeSettings};
|
2023-04-18 12:58:57 +00:00
|
|
|
use util::ResultExt;
|
2023-05-01 13:48:41 +00:00
|
|
|
use workspace::Workspace;
|
2021-10-05 13:38:25 +00:00
|
|
|
|
2022-04-08 22:32:56 +00:00
|
|
|
actions!(theme_selector, [Toggle, Reload]);
|
2022-04-07 23:20:49 +00:00
|
|
|
|
2023-05-01 13:48:41 +00:00
|
|
|
pub fn init(cx: &mut AppContext) {
|
|
|
|
cx.add_action(toggle);
|
2023-04-20 13:25:48 +00:00
|
|
|
ThemeSelector::init(cx);
|
2021-08-02 21:55:27 +00:00
|
|
|
}
|
|
|
|
|
2023-05-01 13:48:41 +00:00
|
|
|
pub fn toggle(workspace: &mut Workspace, _: &Toggle, cx: &mut ViewContext<Workspace>) {
|
|
|
|
workspace.toggle_modal(cx, |workspace, cx| {
|
2023-05-10 01:14:42 +00:00
|
|
|
let fs = workspace.app_state().fs.clone();
|
2024-01-02 22:02:53 +00:00
|
|
|
let telemetry = workspace.client().telemetry().clone();
|
|
|
|
cx.add_view(|cx| ThemeSelector::new(ThemeSelectorDelegate::new(fs, telemetry, cx), cx))
|
2023-04-20 13:25:48 +00:00
|
|
|
});
|
2021-08-02 21:55:27 +00:00
|
|
|
}
|
|
|
|
|
2023-04-20 13:25:48 +00:00
|
|
|
#[cfg(debug_assertions)]
|
2023-05-17 21:44:55 +00:00
|
|
|
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(¤t_theme_name) {
|
2023-04-20 13:25:48 +00:00
|
|
|
Ok(theme) => {
|
|
|
|
ThemeSelectorDelegate::set_theme(theme, cx);
|
|
|
|
log::info!("reloaded theme {}", current_theme_name);
|
|
|
|
}
|
|
|
|
Err(error) => {
|
|
|
|
log::error!("failed to load theme {}: {:?}", current_theme_name, error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type ThemeSelector = Picker<ThemeSelectorDelegate>;
|
|
|
|
|
|
|
|
pub struct ThemeSelectorDelegate {
|
2023-05-10 01:14:42 +00:00
|
|
|
fs: Arc<dyn Fs>,
|
2023-04-20 13:25:48 +00:00
|
|
|
theme_data: Vec<ThemeMeta>,
|
|
|
|
matches: Vec<StringMatch>,
|
|
|
|
original_theme: Arc<Theme>,
|
|
|
|
selection_completed: bool,
|
|
|
|
selected_index: usize,
|
2024-01-02 22:02:53 +00:00
|
|
|
telemetry: Arc<Telemetry>,
|
2023-04-20 13:25:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ThemeSelectorDelegate {
|
2024-01-02 22:02:53 +00:00
|
|
|
fn new(
|
|
|
|
fs: Arc<dyn Fs>,
|
|
|
|
telemetry: Arc<Telemetry>,
|
|
|
|
cx: &mut ViewContext<ThemeSelector>,
|
|
|
|
) -> Self {
|
2023-05-17 21:44:55 +00:00
|
|
|
let original_theme = theme::current(cx).clone();
|
2022-09-08 22:47:27 +00:00
|
|
|
|
2023-08-26 00:00:53 +00:00
|
|
|
let staff_mode = cx.is_staff();
|
2023-05-17 21:44:55 +00:00
|
|
|
let registry = cx.global::<Arc<ThemeRegistry>>();
|
|
|
|
let mut theme_names = registry.list(staff_mode).collect::<Vec<_>>();
|
2023-03-07 20:19:51 +00:00
|
|
|
theme_names.sort_unstable_by(|a, b| a.is_light.cmp(&b.is_light).then(a.name.cmp(&b.name)));
|
2022-04-14 23:51:14 +00:00
|
|
|
let matches = theme_names
|
|
|
|
.iter()
|
2022-09-08 21:11:48 +00:00
|
|
|
.map(|meta| StringMatch {
|
2022-04-14 23:51:14 +00:00
|
|
|
candidate_id: 0,
|
|
|
|
score: 0.0,
|
|
|
|
positions: Default::default(),
|
2022-09-08 21:11:48 +00:00
|
|
|
string: meta.name.clone(),
|
2022-04-14 23:51:14 +00:00
|
|
|
})
|
|
|
|
.collect();
|
2021-08-02 21:55:27 +00:00
|
|
|
let mut this = Self {
|
2023-05-10 01:14:42 +00:00
|
|
|
fs,
|
2022-09-08 21:11:48 +00:00
|
|
|
theme_data: theme_names,
|
2022-04-14 23:51:14 +00:00
|
|
|
matches,
|
2022-03-09 18:34:52 +00:00
|
|
|
original_theme: original_theme.clone(),
|
2022-04-14 23:51:14 +00:00
|
|
|
selected_index: 0,
|
2022-03-09 18:34:52 +00:00
|
|
|
selection_completed: false,
|
2024-01-02 22:02:53 +00:00
|
|
|
telemetry,
|
2021-08-02 21:55:27 +00:00
|
|
|
};
|
2022-09-08 21:11:48 +00:00
|
|
|
this.select_if_matching(&original_theme.meta.name);
|
2021-08-02 21:55:27 +00:00
|
|
|
this
|
|
|
|
}
|
|
|
|
|
2023-04-20 13:25:48 +00:00
|
|
|
fn show_selected_theme(&mut self, cx: &mut ViewContext<ThemeSelector>) {
|
2022-03-09 02:42:31 +00:00
|
|
|
if let Some(mat) = self.matches.get(self.selected_index) {
|
2023-05-17 21:44:55 +00:00
|
|
|
let registry = cx.global::<Arc<ThemeRegistry>>();
|
|
|
|
match registry.get(&mat.string) {
|
2022-10-11 00:10:42 +00:00
|
|
|
Ok(theme) => {
|
|
|
|
Self::set_theme(theme, cx);
|
|
|
|
}
|
2022-03-09 02:42:31 +00:00
|
|
|
Err(error) => {
|
|
|
|
log::error!("error loading theme {}: {}", mat.string, error)
|
2022-03-09 18:34:52 +00:00
|
|
|
}
|
2022-03-09 02:42:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-09 18:34:52 +00:00
|
|
|
fn select_if_matching(&mut self, theme_name: &str) {
|
|
|
|
self.selected_index = self
|
|
|
|
.matches
|
|
|
|
.iter()
|
2022-03-09 02:42:31 +00:00
|
|
|
.position(|mat| mat.string == theme_name)
|
|
|
|
.unwrap_or(self.selected_index);
|
|
|
|
}
|
|
|
|
|
2023-04-06 21:49:03 +00:00
|
|
|
fn set_theme(theme: Arc<Theme>, cx: &mut AppContext) {
|
2023-05-17 21:44:55 +00:00
|
|
|
cx.update_global::<SettingsStore, _, _>(|store, cx| {
|
|
|
|
let mut theme_settings = store.get::<ThemeSettings>(None).clone();
|
|
|
|
theme_settings.theme = theme;
|
|
|
|
store.override_global(theme_settings);
|
2022-04-14 23:51:14 +00:00
|
|
|
cx.refresh_windows();
|
|
|
|
});
|
2021-08-02 21:55:27 +00:00
|
|
|
}
|
2022-04-14 23:51:14 +00:00
|
|
|
}
|
2021-08-02 21:55:27 +00:00
|
|
|
|
2023-04-20 13:25:48 +00:00
|
|
|
impl PickerDelegate for ThemeSelectorDelegate {
|
|
|
|
fn placeholder_text(&self) -> Arc<str> {
|
|
|
|
"Select Theme...".into()
|
|
|
|
}
|
|
|
|
|
2022-04-14 23:51:14 +00:00
|
|
|
fn match_count(&self) -> usize {
|
|
|
|
self.matches.len()
|
|
|
|
}
|
|
|
|
|
2023-07-14 19:41:23 +00:00
|
|
|
fn confirm(&mut self, _: bool, cx: &mut ViewContext<ThemeSelector>) {
|
2022-04-14 23:51:14 +00:00
|
|
|
self.selection_completed = true;
|
2022-10-12 02:18:29 +00:00
|
|
|
|
2023-05-17 21:44:55 +00:00
|
|
|
let theme_name = theme::current(cx).meta.name.clone();
|
2024-01-02 22:02:53 +00:00
|
|
|
|
|
|
|
let telemetry_settings = *settings::get::<TelemetrySettings>(cx);
|
|
|
|
self.telemetry
|
|
|
|
.report_setting_event(telemetry_settings, "theme", theme_name.to_string());
|
|
|
|
|
2023-05-17 21:44:55 +00:00
|
|
|
update_settings_file::<ThemeSettings>(self.fs.clone(), cx, |settings| {
|
|
|
|
settings.theme = Some(theme_name);
|
2022-10-13 00:05:23 +00:00
|
|
|
});
|
2022-10-12 02:18:29 +00:00
|
|
|
|
2023-04-20 13:25:48 +00:00
|
|
|
cx.emit(PickerEvent::Dismiss);
|
2022-04-14 23:51:14 +00:00
|
|
|
}
|
|
|
|
|
2023-04-20 13:25:48 +00:00
|
|
|
fn dismissed(&mut self, cx: &mut ViewContext<ThemeSelector>) {
|
2022-04-14 23:51:14 +00:00
|
|
|
if !self.selection_completed {
|
|
|
|
Self::set_theme(self.original_theme.clone(), cx);
|
|
|
|
self.selection_completed = true;
|
2021-08-02 21:55:27 +00:00
|
|
|
}
|
2022-04-14 23:51:14 +00:00
|
|
|
}
|
2021-08-02 21:55:27 +00:00
|
|
|
|
2022-04-14 23:51:14 +00:00
|
|
|
fn selected_index(&self) -> usize {
|
|
|
|
self.selected_index
|
|
|
|
}
|
2021-08-02 21:55:27 +00:00
|
|
|
|
2023-04-20 13:25:48 +00:00
|
|
|
fn set_selected_index(&mut self, ix: usize, cx: &mut ViewContext<ThemeSelector>) {
|
2022-04-14 23:51:14 +00:00
|
|
|
self.selected_index = ix;
|
|
|
|
self.show_selected_theme(cx);
|
2021-08-02 21:55:27 +00:00
|
|
|
}
|
|
|
|
|
2023-04-20 13:25:48 +00:00
|
|
|
fn update_matches(
|
|
|
|
&mut self,
|
|
|
|
query: String,
|
|
|
|
cx: &mut ViewContext<ThemeSelector>,
|
|
|
|
) -> gpui::Task<()> {
|
2022-04-14 23:51:14 +00:00
|
|
|
let background = cx.background().clone();
|
|
|
|
let candidates = self
|
2022-09-08 21:11:48 +00:00
|
|
|
.theme_data
|
2022-04-14 23:51:14 +00:00
|
|
|
.iter()
|
|
|
|
.enumerate()
|
2022-09-08 21:11:48 +00:00
|
|
|
.map(|(id, meta)| StringMatchCandidate {
|
2022-04-14 23:51:14 +00:00
|
|
|
id,
|
2022-09-08 21:11:48 +00:00
|
|
|
char_bag: meta.name.as_str().into(),
|
|
|
|
string: meta.name.clone(),
|
2022-04-14 23:51:14 +00:00
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
2021-08-02 21:55:27 +00:00
|
|
|
|
2023-04-26 08:23:27 +00:00
|
|
|
cx.spawn(|this, mut cx| async move {
|
2022-04-14 23:51:14 +00:00
|
|
|
let matches = if query.is_empty() {
|
|
|
|
candidates
|
|
|
|
.into_iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(index, candidate)| StringMatch {
|
|
|
|
candidate_id: index,
|
|
|
|
string: candidate.string,
|
|
|
|
positions: Vec::new(),
|
|
|
|
score: 0.0,
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
} else {
|
|
|
|
match_strings(
|
|
|
|
&candidates,
|
|
|
|
&query,
|
|
|
|
false,
|
|
|
|
100,
|
|
|
|
&Default::default(),
|
|
|
|
background,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
};
|
2021-08-02 21:55:27 +00:00
|
|
|
|
2023-04-26 09:13:46 +00:00
|
|
|
this.update(&mut cx, |this, cx| {
|
|
|
|
let delegate = this.delegate_mut();
|
|
|
|
delegate.matches = matches;
|
|
|
|
delegate.selected_index = delegate
|
|
|
|
.selected_index
|
|
|
|
.min(delegate.matches.len().saturating_sub(1));
|
|
|
|
delegate.show_selected_theme(cx);
|
|
|
|
})
|
|
|
|
.log_err();
|
2022-04-14 23:51:14 +00:00
|
|
|
})
|
2021-08-02 21:55:27 +00:00
|
|
|
}
|
2022-03-11 22:59:05 +00:00
|
|
|
|
2022-04-28 22:17:56 +00:00
|
|
|
fn render_match(
|
|
|
|
&self,
|
|
|
|
ix: usize,
|
2022-10-15 00:09:15 +00:00
|
|
|
mouse_state: &mut MouseState,
|
2022-04-28 22:17:56 +00:00
|
|
|
selected: bool,
|
|
|
|
cx: &AppContext,
|
2023-04-21 19:04:03 +00:00
|
|
|
) -> AnyElement<Picker<Self>> {
|
2023-05-17 21:44:55 +00:00
|
|
|
let theme = theme::current(cx);
|
2023-06-14 12:02:32 +00:00
|
|
|
let style = theme.picker.item.in_state(selected).style_for(mouse_state);
|
2022-04-14 23:51:14 +00:00
|
|
|
|
2023-05-17 21:44:55 +00:00
|
|
|
let theme_match = &self.matches[ix];
|
2022-04-14 23:51:14 +00:00
|
|
|
Label::new(theme_match.string.clone(), style.label.clone())
|
|
|
|
.with_highlights(theme_match.positions.clone())
|
|
|
|
.contained()
|
|
|
|
.with_style(style.container)
|
2023-04-21 19:04:03 +00:00
|
|
|
.into_any()
|
2022-03-11 22:59:05 +00:00
|
|
|
}
|
2021-08-02 21:55:27 +00:00
|
|
|
}
|