1use client::telemetry::Telemetry;
2use feature_flags::FeatureFlagAppExt;
3use fs::Fs;
4use fuzzy::{match_strings, StringMatch, StringMatchCandidate};
5use gpui::{
6 actions, impl_actions, AppContext, DismissEvent, EventEmitter, FocusableView, Render,
7 UpdateGlobal, View, ViewContext, VisualContext, WeakView,
8};
9use picker::{Picker, PickerDelegate};
10use serde::Deserialize;
11use settings::{update_settings_file, SettingsStore};
12use std::sync::Arc;
13use theme::{Appearance, Theme, ThemeMeta, ThemeRegistry, ThemeSettings};
14use ui::{prelude::*, v_flex, ListItem, ListItemSpacing};
15use util::ResultExt;
16use workspace::{ui::HighlightedLabel, ModalView, Workspace};
17
18#[derive(PartialEq, Clone, Default, Debug, Deserialize)]
19pub struct Toggle {
20 /// A list of theme names to filter the theme selector down to.
21 pub themes_filter: Option<Vec<String>>,
22}
23
24impl_actions!(theme_selector, [Toggle]);
25actions!(theme_selector, [Reload]);
26
27pub fn init(cx: &mut AppContext) {
28 cx.observe_new_views(
29 |workspace: &mut Workspace, _cx: &mut ViewContext<Workspace>| {
30 workspace.register_action(toggle);
31 },
32 )
33 .detach();
34}
35
36pub fn toggle(workspace: &mut Workspace, toggle: &Toggle, cx: &mut ViewContext<Workspace>) {
37 let fs = workspace.app_state().fs.clone();
38 let telemetry = workspace.client().telemetry().clone();
39 workspace.toggle_modal(cx, |cx| {
40 let delegate = ThemeSelectorDelegate::new(
41 cx.view().downgrade(),
42 fs,
43 telemetry,
44 toggle.themes_filter.as_ref(),
45 cx,
46 );
47 ThemeSelector::new(delegate, cx)
48 });
49}
50
51impl ModalView for ThemeSelector {}
52
53pub struct ThemeSelector {
54 picker: View<Picker<ThemeSelectorDelegate>>,
55}
56
57impl EventEmitter<DismissEvent> for ThemeSelector {}
58
59impl FocusableView for ThemeSelector {
60 fn focus_handle(&self, cx: &AppContext) -> gpui::FocusHandle {
61 self.picker.focus_handle(cx)
62 }
63}
64
65impl Render for ThemeSelector {
66 fn render(&mut self, _cx: &mut ViewContext<Self>) -> impl IntoElement {
67 v_flex().w(rems(34.)).child(self.picker.clone())
68 }
69}
70
71impl ThemeSelector {
72 pub fn new(delegate: ThemeSelectorDelegate, cx: &mut ViewContext<Self>) -> Self {
73 let picker = cx.new_view(|cx| Picker::uniform_list(delegate, cx));
74 Self { picker }
75 }
76}
77
78pub struct ThemeSelectorDelegate {
79 fs: Arc<dyn Fs>,
80 themes: Vec<ThemeMeta>,
81 matches: Vec<StringMatch>,
82 original_theme: Arc<Theme>,
83 selection_completed: bool,
84 selected_index: usize,
85 telemetry: Arc<Telemetry>,
86 view: WeakView<ThemeSelector>,
87}
88
89impl ThemeSelectorDelegate {
90 fn new(
91 weak_view: WeakView<ThemeSelector>,
92 fs: Arc<dyn Fs>,
93 telemetry: Arc<Telemetry>,
94 themes_filter: Option<&Vec<String>>,
95 cx: &mut ViewContext<ThemeSelector>,
96 ) -> Self {
97 let original_theme = cx.theme().clone();
98
99 let staff_mode = cx.is_staff();
100 let registry = ThemeRegistry::global(cx);
101 let mut themes = registry
102 .list(staff_mode)
103 .into_iter()
104 .filter(|meta| {
105 if let Some(theme_filter) = themes_filter {
106 theme_filter.contains(&meta.name.to_string())
107 } else {
108 true
109 }
110 })
111 .collect::<Vec<_>>();
112
113 themes.sort_unstable_by(|a, b| {
114 a.appearance
115 .is_light()
116 .cmp(&b.appearance.is_light())
117 .then(a.name.cmp(&b.name))
118 });
119 let matches = themes
120 .iter()
121 .map(|meta| StringMatch {
122 candidate_id: 0,
123 score: 0.0,
124 positions: Default::default(),
125 string: meta.name.to_string(),
126 })
127 .collect();
128 let mut this = Self {
129 fs,
130 themes,
131 matches,
132 original_theme: original_theme.clone(),
133 selected_index: 0,
134 selection_completed: false,
135 telemetry,
136 view: weak_view,
137 };
138
139 this.select_if_matching(&original_theme.name);
140 this
141 }
142
143 fn show_selected_theme(&mut self, cx: &mut ViewContext<Picker<ThemeSelectorDelegate>>) {
144 if let Some(mat) = self.matches.get(self.selected_index) {
145 let registry = ThemeRegistry::global(cx);
146 match registry.get(&mat.string) {
147 Ok(theme) => {
148 Self::set_theme(theme, cx);
149 }
150 Err(error) => {
151 log::error!("error loading theme {}: {}", mat.string, error)
152 }
153 }
154 }
155 }
156
157 fn select_if_matching(&mut self, theme_name: &str) {
158 self.selected_index = self
159 .matches
160 .iter()
161 .position(|mat| mat.string == theme_name)
162 .unwrap_or(self.selected_index);
163 }
164
165 fn set_theme(theme: Arc<Theme>, cx: &mut AppContext) {
166 SettingsStore::update_global(cx, |store, cx| {
167 let mut theme_settings = store.get::<ThemeSettings>(None).clone();
168 theme_settings.active_theme = theme;
169 theme_settings.apply_theme_overrides();
170 store.override_global(theme_settings);
171 cx.refresh();
172 });
173 }
174}
175
176impl PickerDelegate for ThemeSelectorDelegate {
177 type ListItem = ui::ListItem;
178
179 fn placeholder_text(&self, _cx: &mut WindowContext) -> Arc<str> {
180 "Select Theme...".into()
181 }
182
183 fn match_count(&self) -> usize {
184 self.matches.len()
185 }
186
187 fn confirm(&mut self, _: bool, cx: &mut ViewContext<Picker<ThemeSelectorDelegate>>) {
188 self.selection_completed = true;
189
190 let theme_name = cx.theme().name.clone();
191
192 self.telemetry
193 .report_setting_event("theme", theme_name.to_string());
194
195 let appearance = Appearance::from(cx.appearance());
196
197 update_settings_file::<ThemeSettings>(self.fs.clone(), cx, move |settings, _| {
198 settings.set_theme(theme_name.to_string(), appearance);
199 });
200
201 self.view
202 .update(cx, |_, cx| {
203 cx.emit(DismissEvent);
204 })
205 .ok();
206 }
207
208 fn dismissed(&mut self, cx: &mut ViewContext<Picker<ThemeSelectorDelegate>>) {
209 if !self.selection_completed {
210 Self::set_theme(self.original_theme.clone(), cx);
211 self.selection_completed = true;
212 }
213
214 self.view
215 .update(cx, |_, cx| cx.emit(DismissEvent))
216 .log_err();
217 }
218
219 fn selected_index(&self) -> usize {
220 self.selected_index
221 }
222
223 fn set_selected_index(
224 &mut self,
225 ix: usize,
226 cx: &mut ViewContext<Picker<ThemeSelectorDelegate>>,
227 ) {
228 self.selected_index = ix;
229 self.show_selected_theme(cx);
230 }
231
232 fn update_matches(
233 &mut self,
234 query: String,
235 cx: &mut ViewContext<Picker<ThemeSelectorDelegate>>,
236 ) -> gpui::Task<()> {
237 let background = cx.background_executor().clone();
238 let candidates = self
239 .themes
240 .iter()
241 .enumerate()
242 .map(|(id, meta)| StringMatchCandidate {
243 id,
244 char_bag: meta.name.as_ref().into(),
245 string: meta.name.to_string(),
246 })
247 .collect::<Vec<_>>();
248
249 cx.spawn(|this, mut cx| async move {
250 let matches = if query.is_empty() {
251 candidates
252 .into_iter()
253 .enumerate()
254 .map(|(index, candidate)| StringMatch {
255 candidate_id: index,
256 string: candidate.string,
257 positions: Vec::new(),
258 score: 0.0,
259 })
260 .collect()
261 } else {
262 match_strings(
263 &candidates,
264 &query,
265 false,
266 100,
267 &Default::default(),
268 background,
269 )
270 .await
271 };
272
273 this.update(&mut cx, |this, cx| {
274 this.delegate.matches = matches;
275 this.delegate.selected_index = this
276 .delegate
277 .selected_index
278 .min(this.delegate.matches.len().saturating_sub(1));
279 this.delegate.show_selected_theme(cx);
280 })
281 .log_err();
282 })
283 }
284
285 fn render_match(
286 &self,
287 ix: usize,
288 selected: bool,
289 _cx: &mut ViewContext<Picker<Self>>,
290 ) -> Option<Self::ListItem> {
291 let theme_match = &self.matches[ix];
292
293 Some(
294 ListItem::new(ix)
295 .inset(true)
296 .spacing(ListItemSpacing::Sparse)
297 .selected(selected)
298 .child(HighlightedLabel::new(
299 theme_match.string.clone(),
300 theme_match.positions.clone(),
301 )),
302 )
303 }
304}