Skip to content

Visual Studio Code, rebuilt in Rust

Rust
4.4 KB
Raw Blame
use std::collections::hash_map::RandomState;
use std::hash::BuildHasher;
use std::sync::OnceLock;

use gpui::{ClickEvent, Context, Div, Window, div, prelude::*, px};

use super::Workbench;

type Entry = (&'static str, &'static str, &'static str);

/// editorGroupWatermark.ts: (label, command id, codicon).
const SHOW_COMMANDS: Entry = ("Show All Commands", "workbench.action.showCommands", "list-selection");
const OPEN_CHAT: Entry = ("Open Chat", "workbench.action.chat.open", "comment-discussion-sparkle");
const EMPTY_WINDOW_ENTRIES: [Entry; 5] = [
    OPEN_CHAT,
    SHOW_COMMANDS,
    ("Open Recent", "workbench.action.openRecent", "history"),
    ("Open File or Folder", "workbench.action.files.openFileFolder", "folder-opened"),
    ("New Untitled Text File", "workbench.action.files.newUntitledFile", "new-file"),
];
const WORKSPACE_ENTRIES: [Entry; 2] = [OPEN_CHAT, SHOW_COMMANDS];
const OTHER_ENTRIES: [Entry; 3] = [
    ("Go to File", "workbench.action.quickOpen", "go-to-file"),
    ("Find in Files", "workbench.action.findInFiles", "search"),
    ("Open Settings", "workbench.action.openGlobalSettings", "settings-gear"),
];
const MINIMUM_ENTRIES: usize = 3;

impl Workbench {
    /// The `.shortcuts` under the main part's lone empty group's letterpress, None when `workbench.tips.enabled` is off.
    pub(super) fn watermark_shortcuts(&self, group: u32, window: &Window, cx: &mut Context<Self>) -> Option<Div> {
        if self.setting("workbench.tips.enabled") == serde_json::json!(false) {
            return None;
        }
        let context = self.key_context(window);
        let foreground = self.color("foreground");
        let description = self.color("descriptionForeground");
        let hover = self.color("toolbar.hoverBackground");
        let mut shortcuts = div().w_full().flex().flex_col();
        for (label, command, icon) in self.watermark_entries(group) {
            let Some(keybinding) = self.keybindings.resolver.lookup_primary_keybinding(command, &context) else { continue };
            let mut caps = div().flex().items_center().mx(px(-2.)).text_color(foreground);
            for chord in &keybinding.keybinding {
                caps = caps.children(chord.key_caps().iter().map(|cap| self.key_cap(cap, false)));
            }
            shortcuts = shortcuts.child(
                div()
                    .id(command)
                    .flex()
                    .items_center()
                    .gap(px(8.))
                    .h(px(28.))
                    .px(px(8.))
                    .rounded(px(4.))
                    .cursor_pointer()
                    .hover(|entry| entry.bg(hover))
                    .child(self.icon(icon, 16., 16., description))
                    .child(div().flex_1().min_w_0().overflow_hidden().text_ellipsis().whitespace_nowrap().text_color(foreground).child(label))
                    .child(caps)
                    .on_click(cx.listener(move |workbench, _: &ClickEvent, window, cx| drop(workbench.execute_command(command, vec![], window, cx)))),
            );
        }
        Some(shortcuts)
    }

    /// The entries before the keybinding check: a workspace fills up to three with a shuffle of the others, fixed
    /// per group like the reference's per-group render.
    pub(super) fn watermark_entries(&self, group: u32) -> Vec<Entry> {
        // Open Chat's `when` is not evaluated: chat is removed by decision (port/notes.md), so its missing command drops it.
        let registered = |entry: &&Entry| self.commands.get(entry.1).is_some();
        let has_folder = self.folder.as_ref().is_some_and(|tree| !tree.workspace.folders.is_empty());
        let candidates = if has_folder { &WORKSPACE_ENTRIES[..] } else { &EMPTY_WINDOW_ENTRIES[..] };
        let mut entries: Vec<Entry> = candidates.iter().filter(registered).copied().collect();
        if has_folder && entries.len() < MINIMUM_ENTRIES {
            let mut others: Vec<Entry> = OTHER_ENTRIES.iter().filter(registered).copied().collect();
            others.sort_by_key(|entry| shuffle_key(group, entry.1));
            others.truncate(MINIMUM_ENTRIES - entries.len());
            entries.extend(others);
        }
        entries
    }
}

/// A per-process random order, the same for one group on every frame.
fn shuffle_key(group: u32, command: &str) -> u64 {
    static SEED: OnceLock<RandomState> = OnceLock::new();
    SEED.get_or_init(RandomState::new).hash_one((group, command))
}