theme_selector.rs

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