Skip to content

Visual Studio Code, rebuilt in Rust

Rust
26.7 KB
Raw Blame
//! The color and file icon theme pickers (workbench/contrib/themes/browser/themes.contribution.ts
//! `SelectColorThemeCommandId`, `SelectFileIconThemeCommandId` and `InstalledThemesPicker`) and `setColorTheme` and
//! `setFileIconTheme` of workbenchThemeService.ts.

use std::time::Duration;

use gpui::{Context, Task, Window};
use config::service::Settings;
use serde_json::json;

use super::Workbench;
use super::quick_access::show_failure;
use crate::command_palette::locale_compare;
use crate::commands::Outcome;
use crate::main_thread_quick_open::Owner;
use crate::quick_input::{Button, Entry, Event, Item, Kind, QuickInput};
use crate::theme::{ColorThemeEntry, ThemeKind};

const COLOR_THEME_SETTING: &str = "workbench.colorTheme";
const ICON_THEME_SETTING: &str = "workbench.iconTheme";
const PRODUCT_ICON_THEME_SETTING: &str = "workbench.productIconTheme";
/// `ThemeSettingDefaults.COLOR_THEME_DARK` and `COLOR_THEME_LIGHT`: pinned first in their group.
const DEFAULT_DARK: &str = "Dark 2026";
const DEFAULT_LIGHT: &str = "Light 2026";
/// `selectTheme`: a theme is previewed once the focus rested on it this long.
const PREVIEW_DELAY: Duration = Duration::from_millis(200);

/// `IHostColorSchemeService`: the system's dark appearance and its Increase Contrast accessibility setting.
#[derive(Clone, Copy, PartialEq, Debug, Default)]
pub struct HostColorScheme {
    pub dark: bool,
    pub high_contrast: bool,
}

/// Electron's `nativeTheme.shouldUseDarkColors` and `shouldUseHighContrastColors` on macOS.
#[cfg(target_os = "macos")]
pub fn host_color_scheme(_cx: &gpui::App) -> HostColorScheme {
    let defaults = objc2_foundation::NSUserDefaults::standardUserDefaults();
    let style = defaults.stringForKey(&objc2_foundation::NSString::from_str("AppleInterfaceStyle"));
    let high_contrast = objc2_app_kit::NSWorkspace::sharedWorkspace().accessibilityDisplayShouldIncreaseContrast();
    HostColorScheme { dark: style.is_some_and(|style| style.to_string() == "Dark"), high_contrast }
}

/// Electron on Linux: dark from the xdg-desktop-portal `color-scheme`, which GPUI reads and watches;
/// `shouldUseHighContrastColors` is macOS and Windows only, so never high contrast.
#[cfg(target_os = "linux")]
pub fn host_color_scheme(cx: &gpui::App) -> HostColorScheme {
    let dark = matches!(cx.window_appearance(), gpui::WindowAppearance::Dark | gpui::WindowAppearance::VibrantDark);
    HostColorScheme { dark, high_contrast: false }
}

/// Electron on Windows: dark from the system's app theme, which GPUI reads and watches.
// shortcut: never high contrast yet; port/ledger.md M24 row app/src/workbench/accessibility.rs:531 (SPI_GETHIGHCONTRAST).
#[cfg(target_os = "windows")]
pub fn host_color_scheme(cx: &gpui::App) -> HostColorScheme {
    let dark = matches!(cx.window_appearance(), gpui::WindowAppearance::Dark | gpui::WindowAppearance::VibrantDark);
    HostColorScheme { dark, high_contrast: false }
}

pub fn logo_image(look: &str, dark: bool) -> std::sync::Arc<gpui::Image> {
    std::sync::Arc::new(gpui::Image::from_bytes(gpui::ImageFormat::Png, crate::app_icon::logo(look, dark).to_vec()))
}

/// themeConfiguration.ts `getColorThemeSettingId`: the setting that names the color theme for this host scheme.
pub fn color_theme_setting_id(settings: &Settings, host: HostColorScheme) -> &'static str {
    let enabled = |key| settings.get_value(Some(key), &Default::default()) == Some(json!(true));
    if enabled("window.autoDetectHighContrast") && host.high_contrast {
        return if host.dark { "workbench.preferredHighContrastColorTheme" } else { "workbench.preferredHighContrastLightColorTheme" };
    }
    if enabled("window.autoDetectColorScheme") {
        return if host.dark { "workbench.preferredDarkColorTheme" } else { "workbench.preferredLightColorTheme" };
    }
    COLOR_THEME_SETTING
}

/// `ThemeSettingTarget`: `Preview` shows the theme without writing `workbench.colorTheme`.
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum SettingTarget {
    Preview,
    Auto,
}

/// Which theme service the picker sets.
#[derive(Clone, Copy, PartialEq, Debug)]
pub(super) enum ThemeType {
    Color,
    FileIcon,
    ProductIcon,
}

/// What the open picker remembers: the theme to restore, the settings id of each item by handle (`""` for the
/// None file icon theme), and the preview waiting for its delay.
pub struct ColorThemePicker {
    theme_type: ThemeType,
    current: String,
    picks: Vec<String>,
    completed: bool,
    preview: Option<Task<()>>,
}

impl Workbench {
    pub(super) fn register_theme_commands(&mut self) {
        self.commands.register("workbench.action.selectTheme", |workbench, _, window, cx| {
            workbench.show_color_theme_picker(window, cx);
            Outcome::done()
        });
        self.commands.register("workbench.action.selectIconTheme", |workbench, _, window, cx| {
            workbench.show_file_icon_theme_picker(window, cx);
            Outcome::done()
        });
        self.commands.register("workbench.action.selectProductIconTheme", |workbench, _, window, cx| {
            workbench.show_product_icon_theme_picker(window, cx);
            Outcome::done()
        });
    }

    /// The `selectProductIconTheme` action: Default first, then the installed product icon themes.
    fn show_product_icon_theme_picker(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        let (entries, picks) = product_icon_theme_entries(&crate::product_icons::product_icon_themes(&self.extensions));
        let current = self.product_icons.as_ref().map_or(crate::product_icons::DEFAULT_PRODUCT_ICON_THEME, |theme| theme.settings_id.as_str()).to_string();
        let mut input = QuickInput::new(0, Kind::QuickPick);
        input.placeholder = Some("Select Product Icon Theme (Up/Down Keys to Preview)".into());
        input.match_on_description = true;
        input.set_items(entries);
        if let Some(handle) = picks.iter().position(|id| *id == current) {
            input.set_active_items(&[handle as i64]);
        }
        let picker = ColorThemePicker { theme_type: ThemeType::ProductIcon, current, picks, completed: false, preview: None };
        let id = self.quick_inputs.add(input, Owner::ColorThemes(picker));
        self.show_quick_input(id, window, cx);
        self.flush_quick_input_events(window, cx);
    }

    /// `isThemeFromExtension`: the settings id and label of each theme of the type the extension contributes.
    pub(super) fn extension_themes(&self, theme_type: ThemeType, extension_id: &str) -> Vec<(String, String)> {
        let extensions: Vec<_> = self.extensions.iter().filter(|extension| extension.id.eq_ignore_ascii_case(extension_id)).cloned().collect();
        match theme_type {
            ThemeType::Color => self.color_themes.iter().filter(|theme| theme.extension_id.eq_ignore_ascii_case(extension_id)).map(|theme| (theme.settings_id.clone(), theme.label.clone())).collect(),
            ThemeType::FileIcon => crate::file_icons::file_icon_themes(&extensions).into_iter().map(|theme| (theme.id, theme.label)).collect(),
            ThemeType::ProductIcon => crate::product_icons::product_icon_themes(&extensions).into_iter().map(|theme| (theme.settings_id, theme.label)).collect(),
        }
    }

    /// extensionsActions.ts `SetColorThemeAction`, `SetFileIconThemeAction` and `SetProductIconThemeAction`: a pick of the
    /// extension's themes (`getQuickPickEntries`), previewed as the focus moves, the current theme back on cancel.
    pub(super) fn show_extension_theme_picker(&mut self, theme_type: ThemeType, extension_id: &str, window: &mut Window, cx: &mut Context<Self>) {
        let themes = self.extension_themes(theme_type, extension_id);
        if themes.is_empty() {
            return;
        }
        let (current, placeholder) = match theme_type {
            ThemeType::Color => (self.theme.settings_id.clone(), "Select Color Theme"),
            ThemeType::FileIcon => (self.file_icons.as_ref().map_or_else(String::new, |theme| theme.id.clone()), "Select File Icon Theme"),
            ThemeType::ProductIcon => (self.product_icons.as_ref().map_or(crate::product_icons::DEFAULT_PRODUCT_ICON_THEME, |theme| theme.settings_id.as_str()).to_string(), "Select Product Icon Theme"),
        };
        let mut input = QuickInput::new(0, Kind::QuickPick);
        input.placeholder = Some(placeholder.into());
        input.set_items(themes.iter().enumerate().map(|(handle, (_, label))| Entry::Item(Item { handle: handle as i64, label: label.clone(), pickable: true, ..Item::default() })).collect());
        let picks = themes.into_iter().map(|(id, _)| id).collect();
        let picker = ColorThemePicker { theme_type, current, picks, completed: false, preview: None };
        let id = self.quick_inputs.add(input, Owner::ColorThemes(picker));
        self.show_quick_input(id, window, cx);
        self.flush_quick_input_events(window, cx);
    }

    /// `WorkbenchThemeService.setProductIconTheme`: `Default` is the codicons; a theme that fails to load leaves the
    /// current one.
    fn set_product_icon_theme(&mut self, settings_id: &str, target: SettingTarget, cx: &mut Context<Self>) {
        let current = self.product_icons.as_ref().map_or(crate::product_icons::DEFAULT_PRODUCT_ICON_THEME, |theme| theme.settings_id.as_str());
        if current != settings_id {
            self.product_icons = match settings_id {
                crate::product_icons::DEFAULT_PRODUCT_ICON_THEME => None,
                id => match crate::product_icons::ProductIconTheme::load(&self.extensions, id) {
                    Ok(theme) => Some(theme),
                    Err(error) => {
                        eprintln!("loading product icon theme {id}: {error:#}");
                        return;
                    }
                },
            };
            cx.notify();
        }
        if target == SettingTarget::Preview {
            return;
        }
        let mut settings = self.settings.lock().unwrap();
        match settings.update_value_derived(PRODUCT_ICON_THEME_SETTING, Some(&json!(settings_id))) {
            Ok(change) if !change.is_empty() => self.host.send_configuration_change(&settings, &change),
            Ok(_) => {}
            Err(error) => eprintln!("writing {PRODUCT_ICON_THEME_SETTING}: {error}"),
        }
    }

    /// The `selectIconTheme` action: None first, then the installed file icon themes, the current one focused.
    fn show_file_icon_theme_picker(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        let (entries, picks) = file_icon_theme_entries(&crate::file_icons::file_icon_themes(&self.extensions));
        let current = self.file_icons.as_ref().map_or_else(String::new, |theme| theme.id.clone());
        let mut input = QuickInput::new(0, Kind::QuickPick);
        input.placeholder = Some("Select File Icon Theme (Up/Down Keys to Preview)".into());
        input.match_on_description = true;
        input.set_items(entries);
        if let Some(handle) = picks.iter().position(|id| *id == current) {
            input.set_active_items(&[handle as i64]);
        }
        let picker = ColorThemePicker { theme_type: ThemeType::FileIcon, current, picks, completed: false, preview: None };
        let id = self.quick_inputs.add(input, Owner::ColorThemes(picker));
        self.show_quick_input(id, window, cx);
        self.flush_quick_input_events(window, cx);
    }

    fn set_theme(&mut self, theme_type: ThemeType, settings_id: &str, target: SettingTarget, cx: &mut Context<Self>) {
        match theme_type {
            ThemeType::Color => self.set_color_theme(settings_id, target, cx),
            ThemeType::FileIcon => self.set_file_icon_theme(settings_id, target, cx),
            ThemeType::ProductIcon => self.set_product_icon_theme(settings_id, target, cx),
        }
    }

    /// `WorkbenchThemeService.setFileIconTheme`: `""` is the None theme, written as `null`; a theme that fails to load
    /// leaves the current one.
    fn set_file_icon_theme(&mut self, id: &str, target: SettingTarget, cx: &mut Context<Self>) {
        let current = self.file_icons.as_ref().map_or("", |theme| theme.id.as_str());
        if current != id {
            self.file_icons = match id {
                "" => None,
                id => match crate::file_icons::FileIconTheme::load(&self.extensions, id) {
                    Ok(theme) => Some(theme),
                    Err(error) => {
                        eprintln!("loading file icon theme {id}: {error:#}");
                        return;
                    }
                },
            };
            cx.notify();
        }
        if target == SettingTarget::Preview {
            return;
        }
        let value = if id.is_empty() { json!(null) } else { json!(id) };
        let mut settings = self.settings.lock().unwrap();
        match settings.update_value_derived(ICON_THEME_SETTING, Some(&value)) {
            Ok(change) if !change.is_empty() => self.host.send_configuration_change(&settings, &change),
            Ok(_) => {}
            Err(error) => eprintln!("writing {ICON_THEME_SETTING}: {error}"),
        }
    }

    /// `InstalledThemesPicker.openQuickPick` with the picks of the `selectTheme` action.
    fn show_color_theme_picker(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        let (entries, picks) = color_theme_entries(&self.color_themes);
        let current = self.theme.settings_id.clone();
        let mut input = QuickInput::new(0, Kind::QuickPick);
        // getTitle with `getPreferredColorScheme()` undefined: the port does not read window.autoDetectColorScheme.
        input.placeholder = Some("Select Color Theme (detect system color mode disabled)".into());
        input.match_on_description = true;
        input.buttons = vec![Button {
            handle: 0,
            codicon: Some("color-mode".into()),
            tooltip: Some("Detect system color mode disabled. Click to configure.".into()),
            location: Some(2),
            ..Button::default()
        }];
        input.set_items(entries);
        if let Some(handle) = picks.iter().position(|id| *id == current) {
            input.set_active_items(&[handle as i64]);
        }
        let picker = ColorThemePicker { theme_type: ThemeType::Color, current, picks, completed: false, preview: None };
        let id = self.quick_inputs.add(input, Owner::ColorThemes(picker));
        self.show_quick_input(id, window, cx);
        self.flush_quick_input_events(window, cx);
    }

    /// The picker's listeners: moving previews, accepting keeps the theme, hiding otherwise restores it.
    pub fn color_theme_picker_event(&mut self, id: i64, event: Event, window: &mut Window, cx: &mut Context<Self>) {
        let Some(Owner::ColorThemes(picker)) = self.quick_inputs.owner(id) else { return };
        let theme_type = picker.theme_type;
        match event {
            Event::ChangeActive(handles) => {
                let theme = handles.first().map_or_else(|| picker.current.clone(), |handle| picker.picks[*handle as usize].clone());
                let preview = cx.spawn_in(window, async move |workbench, cx| {
                    cx.background_executor().timer(PREVIEW_DELAY).await;
                    let _ = workbench.update(cx, |workbench, cx| workbench.set_theme(theme_type, &theme, SettingTarget::Preview, cx));
                });
                if let Some(Owner::ColorThemes(picker)) = self.quick_inputs.owner_mut(id) {
                    picker.preview = Some(preview);
                }
            }
            Event::Accept(_) => {
                let Some(input) = self.quick_inputs.get(id) else { return };
                let Some(&handle) = input.selected_handles().first() else { return };
                let theme = picker.picks[handle as usize].clone();
                if let Some(Owner::ColorThemes(picker)) = self.quick_inputs.owner_mut(id) {
                    picker.completed = true;
                    picker.preview = None;
                }
                self.set_theme(theme_type, &theme, SettingTarget::Auto, cx);
                self.hide_quick_input_by_id(id, window, cx);
            }
            Event::TriggerButton(..) => {
                self.hide_quick_input_by_id(id, window, cx);
                // `preferencesService.openSettings({ query })` runs as the command the Settings editor registers.
                let outcome = self.execute_command("workbench.action.openSettings", vec![json!("window.autoDetectColorScheme")], window, cx);
                show_failure("Preferences: Open Settings (UI)".into(), outcome, window, cx);
            }
            Event::TriggerItemButton(handle, ..) => {
                let theme = &picker.picks[handle as usize];
                let extension = match theme_type {
                    ThemeType::Color => self.color_themes.iter().find(|entry| entry.settings_id == *theme).map(|entry| entry.extension_id.clone()),
                    ThemeType::FileIcon | ThemeType::ProductIcon => {
                        let point = if theme_type == ThemeType::FileIcon { "iconThemes" } else { "productIconThemes" };
                        self.extensions.iter().find(|extension| extension.description["contributes"][point].as_array().is_some_and(|themes| themes.iter().any(|entry| entry["id"] == *theme))).map(|extension| extension.id.clone())
                    }
                };
                let Some(extension) = extension else { return };
                // `extensionsWorkbenchService.openSearch` runs as the command the Extensions view registers.
                let outcome = self.execute_command("workbench.extensions.search", vec![json!(format!("@id:{extension}"))], window, cx);
                show_failure("Search Extensions".into(), outcome, window, cx);
            }
            Event::Hide => {
                let restore = (!picker.completed).then(|| picker.current.clone());
                self.quick_inputs.remove(id);
                if let Some(theme) = restore {
                    self.set_theme(theme_type, &theme, SettingTarget::Auto, cx);
                }
            }
            Event::ChangeValue(_) | Event::ChangeSelection(_) | Event::Custom => {}
        }
    }

    /// `ThemeConfiguration`'s `workbench.colorCustomizations` and `editor.tokenColorCustomizations` on the current theme.
    pub(crate) fn customize_theme(&mut self) {
        let settings = self.settings.lock().unwrap();
        let value = |key| settings.get_value(Some(key), &Default::default()).unwrap_or_default();
        let (colors, token_colors) = (value("workbench.colorCustomizations"), value("editor.tokenColorCustomizations"));
        let semantic_colors = value("editor.semanticTokenColorCustomizations");
        drop(settings);
        self.theme.set_customizations(&colors, &token_colors);
        self.theme.set_semantic_customizations(&token_colors, &semantic_colors);
        for editor in self.editors.all_editors() {
            editor.model.borrow_mut().document.tokens.theme_changed();
        }
        self.restyle_webviews();
    }

    /// `restoreColorTheme`: applies the theme the settings name for the host scheme, without writing it back.
    pub(crate) fn restore_color_theme(&mut self, cx: &mut Context<Self>) {
        let settings = self.settings.lock().unwrap();
        let configured = settings.get_value(Some(color_theme_setting_id(&settings, self.host_colors)), &Default::default());
        drop(settings);
        if let Some(id) = configured.as_ref().and_then(|id| id.as_str()) {
            self.set_color_theme(id, SettingTarget::Preview, cx);
        }
    }

    /// `hostColorService.onDidChangeColorScheme` with `isPreferredColorSchemeChange`.
    pub fn host_color_scheme_changed(&mut self, cx: &mut Context<Self>) {
        let (previous, current) = (self.host_colors, host_color_scheme(cx));
        self.host_colors = current;
        let settings = self.settings.lock().unwrap();
        let enabled = |key| settings.get_value(Some(key), &Default::default()) == Some(json!(true));
        let dark_changed = previous.dark != current.dark;
        let restore = (enabled("window.autoDetectColorScheme") && dark_changed)
            || (enabled("window.autoDetectHighContrast") && (previous.high_contrast != current.high_contrast || (current.high_contrast && dark_changed)));
        drop(settings);
        if restore {
            self.restore_color_theme(cx);
        }
        self.sync_app_icon();
    }

    /// The Dock icon for `window.appIcon` and the system appearance, set when it differs from the last one.
    pub fn sync_app_icon(&mut self) {
        let look = self.setting("window.appIcon");
        let bundled = crate::product::is_bundled(&self.app_root);
        let look = look.as_str().unwrap_or("default");
        let image = crate::app_icon::image(look, self.host_colors.dark, bundled);
        if image != self.app_icon {
            self.app_icon = image;
            crate::app_icon::set(image);
        }
        if self.app_logo.bytes() != crate::app_icon::logo(look, self.host_colors.dark) {
            self.app_logo = logo_image(look, self.host_colors.dark);
        }
    }

    /// `WorkbenchThemeService.setColorTheme`: applies the theme, tells the extension host its kind, and writes
    /// `workbench.colorTheme` unless previewing (`ThemeConfiguration.writeConfiguration`). A theme that fails to
    /// load leaves the current one, as the picker's error path does.
    pub fn set_color_theme(&mut self, settings_id: &str, target: SettingTarget, cx: &mut Context<Self>) {
        if self.theme.settings_id != settings_id {
            let Some(entry) = self.color_themes.iter().find(|entry| entry.settings_id == settings_id) else { return };
            let theme = match entry.load() {
                Ok(theme) => theme,
                Err(error) => {
                    eprintln!("loading color theme {settings_id}: {error:#}");
                    return;
                }
            };
            self.theme = theme;
            self.customize_theme();
            self.semantic_tokens_settings_changed(true, cx);
            self.host.send_color_theme(self.theme.kind);
            self.host.send_syntax_highlighting_theme_changed();
            cx.notify();
        }
        if target == SettingTarget::Preview {
            return;
        }
        let mut settings = self.settings.lock().unwrap();
        let key = color_theme_setting_id(&settings, self.host_colors);
        match settings.update_value_derived(key, Some(&json!(settings_id))) {
            Ok(change) if !change.is_empty() => self.host.send_configuration_change(&settings, &change),
            Ok(_) => {}
            Err(error) => eprintln!("writing {key}: {error}"),
        }
    }
}

/// The `selectTheme` picks without a preferred color scheme: light, dark, then high contrast themes, each
/// group headed by a separator (`toEntries`), with the settings id of each item by handle.
fn color_theme_entries(themes: &[ColorThemeEntry]) -> (Vec<Entry>, Vec<String>) {
    let groups: [(&str, fn(ThemeKind) -> bool); 3] = [
        ("light themes", |kind| kind == ThemeKind::Light),
        ("dark themes", |kind| kind == ThemeKind::Dark),
        ("high contrast themes", ThemeKind::is_high_contrast),
    ];
    let (mut entries, mut picks) = (Vec::new(), Vec::new());
    for (label, in_group) in groups {
        let mut group: Vec<&ColorThemeEntry> = themes.iter().filter(|theme| in_group(theme.kind)).collect();
        group.sort_by(|a, b| is_pinned(b).cmp(&is_pinned(a)).then_with(|| locale_compare(&a.label, &b.label)));
        if !group.is_empty() {
            entries.push(Entry::Separator(Some(label.into())));
        }
        for theme in group {
            entries.push(Entry::Item(Item {
                handle: picks.len() as i64,
                label: theme.label.clone(),
                description: theme_description(theme),
                pickable: true,
                // `manageExtensionIcon` is registered with `Codicon.gear` as its glyph.
                buttons: vec![Button { handle: 0, codicon: Some("gear".into()), tooltip: Some("Manage Extension".into()), ..Button::default() }],
                ..Item::default()
            }));
            picks.push(theme.settings_id.clone());
        }
    }
    (entries, picks)
}

/// The `selectIconTheme` picks: the "file icon themes" separator, None, then the themes by label (`toEntries`).
fn file_icon_theme_entries(themes: &[crate::file_icons::FileIconThemeEntry]) -> (Vec<Entry>, Vec<String>) {
    let none = Item { handle: 0, label: "None".into(), description: Some("Disable File Icons".into()), pickable: true, ..Item::default() };
    let mut entries = vec![Entry::Separator(Some("file icon themes".into())), Entry::Item(none)];
    let mut picks = vec![String::new()];
    let mut sorted: Vec<_> = themes.iter().collect();
    sorted.sort_by(|a, b| locale_compare(&a.label, &b.label));
    for theme in sorted {
        entries.push(Entry::Item(Item {
            handle: picks.len() as i64,
            label: theme.label.clone(),
            description: theme.description.clone().or_else(|| (theme.label != theme.id).then(|| theme.id.clone())),
            pickable: true,
            buttons: vec![Button { handle: 0, codicon: Some("gear".into()), tooltip: Some("Manage Extension".into()), ..Button::default() }],
            ..Item::default()
        }));
        picks.push(theme.id.clone());
    }
    (entries, picks)
}

/// The `selectProductIconTheme` picks: the "product icon themes" separator, Default, then the themes by label.
fn product_icon_theme_entries(themes: &[crate::product_icons::ProductIconThemeEntry]) -> (Vec<Entry>, Vec<String>) {
    let default = Item { handle: 0, label: "Default".into(), pickable: true, ..Item::default() };
    let mut entries = vec![Entry::Separator(Some("product icon themes".into())), Entry::Item(default)];
    let mut picks = vec![crate::product_icons::DEFAULT_PRODUCT_ICON_THEME.to_string()];
    let mut sorted: Vec<_> = themes.iter().collect();
    sorted.sort_by(|a, b| locale_compare(&a.label, &b.label));
    for theme in sorted {
        entries.push(Entry::Item(Item {
            handle: picks.len() as i64,
            label: theme.label.clone(),
            description: theme.description.clone().or_else(|| (theme.label != theme.settings_id).then(|| theme.settings_id.clone())),
            pickable: true,
            buttons: vec![Button { handle: 0, codicon: Some("gear".into()), tooltip: Some("Manage Extension".into()), ..Button::default() }],
            ..Item::default()
        }));
        picks.push(theme.settings_id.clone());
    }
    (entries, picks)
}

fn is_pinned(theme: &ColorThemeEntry) -> bool {
    theme.settings_id == DEFAULT_DARK || theme.settings_id == DEFAULT_LIGHT
}

/// `toEntry`: the default themes' names, else the theme's description, else its settings id when the label differs.
fn theme_description(theme: &ColorThemeEntry) -> Option<String> {
    match theme.settings_id.as_str() {
        DEFAULT_LIGHT => Some("Default Light".into()),
        DEFAULT_DARK => Some("Default Dark".into()),
        _ => theme.description.clone().or_else(|| (theme.label != theme.settings_id).then(|| theme.settings_id.clone())),
    }
}

#[cfg(test)]
mod tests;