Visual Studio Code, rebuilt in Rust
Rust
//! The Source Control view (scmViewPane.ts with scmInput.ts and activity.ts): the `MainThreadSCM` requests, the
//! repository's input box and action button, the resource groups with their resources and inline actions, the
//! `scm/title` actions, the activity bar badge and the status bar entries of the active repository.
use std::collections::{HashMap, HashSet};
use gpui::{Bounds, ClickEvent, Context, Div, FocusHandle, MouseButton, MouseDownEvent, Rgba, Window, div, img, prelude::*, px};
use serde_json::{Value, json};
use super::Workbench;
use super::hover::{HoverContent, HoverOptions};
use super::input_box_view::{InputId, InputLook, InputView};
use super::scm_tree::TreeRow;
use super::view_panes::{PaneContainer, SCM_CHANGES_VIEW, ViewPane};
use crate::commands::Outcome;
use crate::editor::TextDocument;
use crate::input_box::InputBox;
use crate::main_thread_status_bar::{StatusEntry, set_or_update_entry};
use crate::menus::{CommandIcon, MenuId};
use crate::scm::{Group, Provider, Resource, Scm, ScmChange};
use crate::workbench::host_requests::uri_parts;
/// scm.css: list rows are 22px.
pub(super) const ROW_HEIGHT: f32 = 22.;
/// The tree's indent per level (`workbench.tree.indent` 8) plus the twistie (16).
pub(super) const INDENT: f32 = 8.;
pub(super) const TWISTIE: f32 = 16.;
/// A row of the Changes view with its tree depth (abstractTree.ts, from 1) and whether it is the selected one.
struct ScmRow {
depth: usize,
selected: bool,
element: gpui::AnyElement,
}
/// scmViewService.ts `ISCMViewServiceState` but its sort key: the repositories by `getProviderStorageKey` and the
/// shown ones by their index there.
struct PreviousState {
all: Vec<String>,
visible: Vec<i64>,
}
const VISIBLE_REPOSITORIES_KEY: &str = "scm:view:visibleRepositories";
/// `getProviderStorageKey`.
fn scm_provider_storage_key(provider: &Provider) -> String {
match provider.root_uri.is_null() {
true => format!("{}:{}", provider.id, provider.label),
false => format!("{}:{}:{}", provider.id, provider.label, crate::debug::uri_string(&provider.root_uri)),
}
}
/// The view's state besides the model.
pub struct ScmView {
pub model: Scm,
pub input: InputBox,
pub input_view: InputView,
pub input_focus: FocusHandle,
pub list_focus: FocusHandle,
/// The input box models of the source controls (`inputBoxDocumentUri`), by provider handle.
input_models: HashMap<u64, (String, std::path::PathBuf)>,
/// Collapsed groups, by provider and group handle.
collapsed: HashSet<(u64, u64)>,
/// The selected row: provider, group, resource handle (`None` for the group row).
pub selected: Option<(u64, u64, Option<u64>)>,
/// The handle the input box shows, so a provider switch reloads it.
shown_input: Option<u64>,
/// Collapsed tree-mode folders: provider, group and the folder relative to the root.
pub(super) collapsed_folders: HashSet<(u64, u64, std::path::PathBuf)>,
/// scmViewService.ts: the focused repository, whose input the input box edits.
pub(super) focused: Option<u64>,
/// `activeRepository`: the last changed of the active editor's repository and the focused one.
active: Option<u64>,
/// The active editor the active repository last followed.
active_editor: Option<(String, std::path::PathBuf)>,
/// Collapsed repository rows, by provider handle.
pub(super) collapsed_repositories: HashSet<u64>,
/// scmViewService.ts `visibleRepositories`: the selected repositories by `selectionIndex`, hidden ones included.
pub(super) visible: Vec<u64>,
/// `toggleSortKey`: the Repositories view's sort key once picked, else `scm.repositories.sortOrder`'s.
pub(super) sort_key: Option<&'static str>,
/// `didFinishLoadingRepositories`, and the debounced `eventuallyFinishLoading` that sets it.
pub(super) finished_loading: bool,
finish_loading: Option<gpui::Task<()>>,
previous_state: Option<PreviousState>,
did_select_repository: bool,
pub(super) repositories: super::scm_repositories_view::RepositoriesView,
pub(super) input_histories: super::scm_actions::InputHistories,
}
impl ScmView {
pub fn new(cx: &mut gpui::App) -> Self {
ScmView {
model: Scm::default(),
input: InputBox::default(),
input_view: InputView::default(),
input_focus: cx.focus_handle().tab_stop(true),
list_focus: cx.focus_handle().tab_stop(true),
input_models: HashMap::new(),
collapsed: HashSet::new(),
selected: None,
shown_input: None,
collapsed_folders: HashSet::new(),
focused: None,
active: None,
active_editor: None,
collapsed_repositories: HashSet::new(),
visible: Vec::new(),
sort_key: None,
finished_loading: false,
finish_loading: None,
previous_state: None,
did_select_repository: false,
repositories: super::scm_repositories_view::RepositoriesView::new(cx),
input_histories: Default::default(),
}
}
}
impl Workbench {
pub(in crate::workbench) fn register_scm_commands(&mut self) {
self.commands.register("workbench.view.scm", |workbench, _, window, cx| {
workbench.show_view_container(super::view_locations::SCM_CONTAINER, cx);
workbench.focus_scm_input(window, cx);
cx.notify();
Outcome::done()
});
self.commands.register("scm.acceptInput", |workbench, _, window, cx| workbench.accept_scm_input(window, cx));
self.commands.register("scm.clearInput", |workbench, _, window, cx| {
workbench.edit_scm_input(|input| input.set_value(String::new()), window, cx);
Outcome::done()
});
self.commands.register("scm.clearValidation", |workbench, _, _, cx| {
if let Some(provider) = workbench.focused_scm_provider_mut() {
provider.input.validation = None;
}
cx.notify();
Outcome::done()
});
}
/// One `MainThreadSCM` request.
pub(crate) fn handle_scm_request(&mut self, method: &str, args: &[Value], window: &mut Window, cx: &mut Context<Self>) -> Outcome {
self.record_scm_input_value(method, args);
if method == "$onDidChangeHistoryProviderHistoryItemRefs" {
if let (Some(handle), Some(changes)) = (args.first().and_then(Value::as_u64), args.get(1)) {
self.scm_history_item_refs_changed(handle, changes["silent"] == true, cx);
}
return Outcome::done();
}
if method == "$onDidChangeArtifacts" {
if let Some(handle) = args.first().and_then(Value::as_u64) {
self.refresh_scm_artifacts(handle, cx);
}
return Outcome::done();
}
let change = self.scm.model.apply(method, args);
if method == "$onDidChangeHistoryProviderCurrentHistoryItemRefs"
&& let Some(handle) = args.first().and_then(Value::as_u64)
{
self.refresh_scm_artifacts(handle, cx);
}
// `artifactProvider` read by the view's repository autorun.
if method == "$updateSourceControl"
&& let Some(handle) = args.first().and_then(Value::as_u64)
{
self.load_expanded_scm_artifacts(handle, cx);
}
if !method.starts_with("$setInput") && !method.starts_with("$showValidation") {
self.quick_diffs.generation += 1;
}
match change {
ScmChange::None => return Outcome::done(),
ScmChange::Registered(handle) => self.scm_registered(handle, args.get(7).cloned().unwrap_or(Value::Null), cx),
ScmChange::Changed => {}
}
if method == "$spliceResourceStates" {
self.scm_group_resources_changed(cx);
}
let handle = args.first().and_then(Value::as_u64);
if method == "$setInputBoxValue"
&& let Some(handle) = handle
{
let value = self.scm.model.provider(handle).map(|provider| provider.input.value.clone()).unwrap_or_default();
self.sync_scm_input_model(handle, &value, cx);
if self.focused_scm_provider().map(|provider| provider.handle) == Some(handle) {
self.scm.input.set_value(value);
}
}
if method == "$unregisterSourceControl"
&& let Some(handle) = handle
&& let Some((scheme, path)) = self.scm.input_models.remove(&handle)
&& let Some(index) = self.editors.models.iter().position(|model| model.document().is(&scheme, &path))
{
self.editors.models.remove(index);
self.editors_changed(cx);
}
// scmViewService.ts `onDidRemoveRepository`: with none left shown the first repository shows, and the first
// shown repository takes the removed one's focus.
if method == "$unregisterSourceControl" {
self.eventually_finish_scm_loading(cx);
self.scm.visible.retain(|visible| Some(*visible) != handle);
if self.visible_scm_providers().is_empty() {
self.scm.visible.extend(self.scm_repositories().first().map(|provider| provider.handle));
}
self.sync_scm_tree_selection();
self.sync_scm_repository_menus();
}
if method == "$unregisterSourceControl" && self.scm.focused == handle {
self.scm.focused = None;
self.focus_scm_repository(self.visible_scm_providers().first().map(|provider| provider.handle));
}
self.load_scm_input();
self.refresh_scm_status_entries();
let _ = window;
cx.notify();
Outcome::done()
}
/// `$registerSourceControl`: the input box's text model (`createModelReference` of `inputBoxDocumentUri`, which
/// the host then sees as a document) and the selection the host hears of for the first repository.
fn scm_registered(&mut self, handle: u64, input_uri: Value, cx: &mut Context<Self>) {
self.eventually_finish_scm_loading(cx);
if let Ok((scheme, path)) = uri_parts(&input_uri) {
let document = TextDocument::from_text(scheme, &path, "", "utf8".into(), Some("scminput"), &self.languages, &self.settings.lock().unwrap());
self.editors.open_model(document);
self.scm.input_models.insert(handle, (scheme.to_string(), path));
self.editors_changed(cx);
}
let focus = self.show_added_scm_repository(handle);
self.sync_scm_tree_selection();
self.sync_scm_repository_menus();
let visible = self.scm.model.provider(handle).is_some_and(|provider| !provider.hidden);
if focus && visible && self.focused_scm_provider().is_none_or(|focused| focused.hidden || self.scm.focused.is_none()) {
self.focus_scm_repository(Some(handle));
}
self.restore_scm_input(handle, cx);
self.load_expanded_scm_artifacts(handle, cx);
}
/// scmViewService.ts `onDidAddRepository`: until repositories finish loading, `previousState` picks the shown ones:
/// a repository it lacks shows them all, the first one it showed replaces those shown so far, and once one it
/// showed came, one it hid stays hidden. Else it shows in `multiple` selection mode or as the first shown one. False
/// when the focus stays as it is.
fn show_added_scm_repository(&mut self, handle: u64) -> bool {
let single = self.scm_single_selection();
if let Some(previous) = self.scm.previous_state.as_ref().filter(|_| !self.scm.finished_loading) {
let key = self.scm.model.provider(handle).map(scm_provider_storage_key);
match previous.all.iter().position(|stored| Some(stored) == key.as_ref()) {
None => {
if !single || self.scm.visible.is_empty() {
self.scm.visible = self.scm_repositories().iter().map(|provider| provider.handle).collect();
}
self.scm.did_select_repository = false;
return false;
}
Some(index) if !previous.visible.contains(&(index as i64)) => {
if self.scm.did_select_repository {
return false;
}
}
Some(_) => {
if !self.scm.did_select_repository {
self.scm.visible.clear();
self.scm.focused = None;
self.scm.did_select_repository = true;
}
}
}
}
if !single || self.scm.visible.is_empty() {
self.scm.visible.push(handle);
}
true
}
/// `onWillSaveState`, once repositories finished loading: every repository and the shown ones by their index.
pub(super) fn store_scm_view_state(&mut self) {
if !self.scm.finished_loading {
return;
}
let all: Vec<String> = self.scm_repositories().into_iter().map(scm_provider_storage_key).collect();
let visible = self.visible_scm_providers().into_iter().map(|provider| {
let key = scm_provider_storage_key(provider);
all.iter().position(|stored| *stored == key).map_or(-1, |index| index as i64)
});
let state = PreviousState { visible: visible.collect(), all };
let stored = json!({ "all": state.all, "visible": state.visible, "sortKey": self.scm_repository_sort_key() });
self.workspace_storage.lock().unwrap().set(VISIBLE_REPOSITORIES_KEY, stored.to_string());
self.scm.previous_state = Some(state);
}
/// The service's constructor: `previousState` with its sort key, in `single` selection mode one shown repository.
pub(super) fn restore_scm_view_state(&mut self) {
let stored = self.workspace_storage.lock().unwrap().get(VISIBLE_REPOSITORIES_KEY).and_then(|raw| serde_json::from_str::<Value>(raw).ok());
let Some(stored) = stored else { return };
let strings = |key: &str| stored[key].as_array().into_iter().flatten().filter_map(|value| value.as_str().map(String::from)).collect::<Vec<_>>();
let mut visible: Vec<i64> = stored["visible"].as_array().into_iter().flatten().filter_map(Value::as_i64).collect();
if self.scm_single_selection() {
visible.truncate(1);
}
self.scm.sort_key = match stored["sortKey"].as_str() {
Some("name") => Some("name"),
Some("path") => Some("path"),
Some("discoveryTime") => Some("discoveryTime"),
_ => None,
};
self.scm.previous_state = Some(PreviousState { all: strings("all"), visible });
}
/// `extensionService.onWillStop`: the shown repositories are kept for the extension host's next repositories.
pub(super) fn scm_extension_host_stopping(&mut self) {
self.store_scm_view_state();
self.scm.finished_loading = false;
self.scm.visible.clear();
}
/// scmViewService.ts `eventuallyFinishLoading` (`@debounce(5000)`): `didFinishLoadingRepositories` turns true 5 s
/// after the last repository came or went, and the Repositories view then expands a lone repository.
fn eventually_finish_scm_loading(&mut self, cx: &mut Context<Self>) {
if self.scm.finished_loading {
return;
}
self.scm.finish_loading = Some(cx.spawn(async move |workbench, cx| {
cx.background_executor().timer(std::time::Duration::from_secs(5)).await;
let _ = workbench.update(cx, |workbench, cx| {
workbench.scm.finished_loading = true;
if !workbench.scm.repositories.has_view_state {
workbench.expand_lone_scm_repository(cx);
}
cx.notify();
});
}));
}
/// scmViewService.ts `repositories`: every repository but hidden ones, in the sort key's order.
pub(in crate::workbench) fn scm_repositories(&self) -> Vec<&Provider> {
let mut providers: Vec<&Provider> = self.scm.model.providers.iter().filter(|provider| !provider.hidden).collect();
match self.scm_repository_sort_key() {
"name" => providers.sort_by_key(|provider| self.scm_repository_name(provider)),
"path" => providers.sort_by(|a, b| a.root_uri["path"].as_str().cmp(&b.root_uri["path"].as_str())),
_ => {}
}
providers
}
/// `visibleRepositories`: the selected repositories; sorted by discovery time they keep their selection order.
pub(in crate::workbench) fn visible_scm_providers(&self) -> Vec<&Provider> {
let mut providers: Vec<&Provider> = self.scm_repositories().into_iter().filter(|provider| self.scm.visible.contains(&provider.handle)).collect();
if self.scm_repository_sort_key() == "discoveryTime" {
providers.sort_by_key(|provider| self.scm.visible.iter().position(|handle| *handle == provider.handle));
}
providers
}
/// `ISCMRepositorySortKey`: the picked one, else `getViewSortOrder`.
pub(super) fn scm_repository_sort_key(&self) -> &'static str {
self.scm.sort_key.unwrap_or_else(|| match self.setting("scm.repositories.sortOrder").as_str() {
Some("name") => "name",
Some("path") => "path",
_ => "discoveryTime",
})
}
pub(super) fn scm_single_selection(&self) -> bool {
self.setting("scm.repositories.selectionMode") == json!("single")
}
/// The `visibleRepositories` setter: a focused repository no longer shown hands the focus to the first shown one.
pub(super) fn set_visible_scm_repositories(&mut self, handles: Vec<u64>) {
// scmRepositoriesViewPane.ts `updateTreeSelection`: a change from outside the tree moves its focus to the
// shown repository.
if self.scm.visible != handles {
self.scm.repositories.focused = None;
}
self.scm.visible = handles;
self.sync_scm_tree_selection();
if self.scm.focused.is_some_and(|focused| !self.scm.visible.contains(&focused)) {
self.scm.focused = None;
let first = self.scm_repositories().into_iter().find(|provider| self.scm.visible.contains(&provider.handle));
self.focus_scm_repository(first.map(|provider| provider.handle));
}
}
/// The `scm.repositories.selectionMode` change: `single` keeps the first shown repository, `multiple` shows all.
pub(super) fn scm_selection_mode_changed(&mut self) {
let repositories: Vec<u64> = self.scm_repositories().iter().map(|provider| provider.handle).collect();
let visible: Vec<u64> = self.visible_scm_providers().iter().map(|provider| provider.handle).collect();
if self.scm_single_selection() && visible.len() > 1 {
self.set_visible_scm_repositories(vec![visible[0]]);
} else if !self.scm_single_selection() && repositories.len() > 1 {
self.set_visible_scm_repositories(repositories);
}
}
/// scmViewService.ts `focus`: the focused repository's input is the one the input box edits, the host hears of it
/// (mainThreadSCM.ts `onDidFocusRepository`) and it becomes the active repository.
pub(super) fn focus_scm_repository(&mut self, handle: Option<u64>) {
if self.scm.focused == handle || handle.is_some_and(|handle| !self.scm.visible.contains(&handle)) {
return;
}
self.scm.focused = handle;
self.scm.active = handle;
self.host.send("ExtHostSCM", "$setSelectedSourceControl", vec![json!(handle)]);
self.load_scm_input();
self.refresh_scm_status_entries();
}
/// mainThreadSCM.ts `name`: the workspace folder's name when the root is one, else the root's base name, else
/// the label.
pub(super) fn scm_repository_name(&self, provider: &Provider) -> String {
let Some(root) = provider.root_uri["path"].as_str().map(std::path::Path::new) else { return provider.label.clone() };
if let Some(folder) = self.workspace().and_then(|workspace| workspace.folder_of(root)).filter(|folder| folder.path == root) {
return folder.name.clone();
}
root.file_name().map_or_else(|| provider.label.clone(), |name| name.to_string_lossy().into_owned())
}
/// scmRepositoryRenderer.ts: the provider label for repositories at one root, else labels.ts `shorten` of the
/// parent folders of the same-named ones.
pub(super) fn scm_repository_description(&self, provider: &Provider) -> Option<String> {
let root = provider.root_uri["path"].as_str()?;
let repositories = self.scm_repositories();
if repositories.iter().filter(|other| other.root_uri["path"].as_str() == Some(root)).count() > 1 {
return Some(provider.label.clone());
}
let own_name = self.scm_repository_name(provider);
let same_name: Vec<_> = repositories.iter().filter(|other| other.root_uri["path"].is_string() && self.scm_repository_name(other) == own_name).collect();
if same_name.len() < 2 {
return None;
}
let parents: Vec<_> = same_name.iter().map(|other| std::path::Path::new(other.root_uri["path"].as_str().unwrap()).parent().map(|parent| crate::quick_input::path_label(parent, self.workspace())).unwrap_or_default()).collect();
let position = same_name.iter().position(|other| other.handle == provider.handle)?;
Some(crate::quick_input::shorten(&parents).swap_remove(position))
}
pub(super) fn focused_scm_provider(&self) -> Option<&Provider> {
self.scm.focused.and_then(|handle| self.scm.model.provider(handle))
}
fn focused_scm_provider_mut(&mut self) -> Option<&mut Provider> {
self.scm.focused.and_then(|handle| self.scm.model.provider_mut(handle))
}
/// scmViewService.ts `activeRepository`: the repository of the active editor or the focused one, whichever
/// changed last.
pub(in crate::workbench) fn active_scm_provider(&self) -> Option<&Provider> {
self.scm.active.and_then(|handle| self.scm.model.provider(handle)).or_else(|| self.focused_scm_provider())
}
/// `_activeEditorRepositoryObs`: a newly active editor inside a repository (`scmService.getRepository`, the
/// deepest root holding it) makes that repository active.
pub(in crate::workbench) fn follow_active_editor_repository(&mut self) {
let Some(editor) = self.editors.active() else { return };
let key = (editor.document().scheme.clone(), editor.document().path.clone());
if self.scm.active_editor.as_ref() == Some(&key) {
return;
}
let repository = self.scm_repository_of(&key.0, &key.1).map(|provider| provider.handle);
self.scm.active_editor = Some(key);
if repository.is_some() && repository != self.scm.active {
self.scm.active = repository;
self.refresh_scm_status_entries();
}
}
/// `scmService.getRepository`: the deepest repository root holding the resource.
fn scm_repository_of(&self, scheme: &str, path: &std::path::Path) -> Option<&Provider> {
self.scm
.model
.providers
.iter()
.filter(|provider| provider.root_uri["scheme"] == json!(scheme) && provider.root_uri["path"].as_str().is_some_and(|root| path.starts_with(root)))
.max_by_key(|provider| provider.root_uri["path"].as_str().map_or(0, str::len))
}
/// activity.ts `SCMActiveResourceContextKeyController`: the active editor's repository and whether one of its
/// groups lists the editor's resource.
fn scm_active_resource_context(&self, context: &mut HashMap<String, Value>) {
let Some(document) = self.editors.pane_editor().map(|editor| editor.document()) else { return };
let Some(provider) = self.scm_repository_of(&document.scheme, &document.path) else { return };
let is_resource = |resource: &Resource| resource.uri["scheme"] == json!(document.scheme) && resource.uri["path"].as_str().is_some_and(|path| document.path == std::path::Path::new(path));
let has_changes = provider.groups.iter().any(|group| group.resources.iter().any(is_resource));
context.insert("scmActiveResourceHasChanges".into(), json!(has_changes));
// mainThreadSCM.ts `MainThreadSCMProvider.id`, not the extension's `id`.
context.insert("scmActiveResourceRepository".into(), json!(format!("scm{}", provider.handle)));
}
/// activity.ts `_updateStatusBar`: the active repository's `statusBarCommands` as `status.scm.<index>` entries.
fn refresh_scm_status_entries(&mut self) {
self.status_entries.retain(|entry| !entry.entry_id.starts_with("status.scm."));
let Some(provider) = self.active_scm_provider() else { return };
let root = provider.root_uri["path"].as_str().map(|path| path.rsplit('/').next().unwrap_or_default().to_string());
let label = match root {
Some(root) => format!("{root} ({})", provider.label),
None => provider.label.clone(),
};
let entries: Vec<StatusEntry> = provider
.status_bar_commands
.iter()
.enumerate()
.map(|(index, command)| {
let tooltip = match command["tooltip"].as_str() {
Some(tooltip) => format!("{label} - {tooltip}"),
None => label.clone(),
};
StatusEntry {
entry_id: format!("status.scm.{index}"),
extension_id: None,
id: format!("status.scm.{index}"),
name: "Source Control".into(),
text: command["title"].as_str().unwrap_or_default().to_string(),
tooltip: Some(tooltip),
command: command["id"].as_str().map(|id| json!({ "id": id, "arguments": command["arguments"] })),
color: None,
background_color: None,
kind: None,
align_left: true,
// The first entry at 10000, each next one right after the one before it (compact).
priority: 10000. - index as f64 * 1e-6,
secondary: 0,
show_progress: false,
}
})
.collect();
for entry in entries {
set_or_update_entry(&mut self.status_entries, entry);
}
}
/// activity.ts: the badge of the Source Control view, `scm.countBadge` `all` (default), `focused` or `off`.
pub(in crate::workbench) fn scm_badge(&self) -> u64 {
match self.setting("scm.countBadge").as_str().unwrap_or("all") {
"off" => 0,
"focused" => self.active_scm_provider().map_or(0, Provider::count),
_ => self.visible_scm_providers().into_iter().map(Provider::count).sum(),
}
}
pub(super) fn focus_scm_input(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let focus = self.scm.input_focus.clone();
window.focus(&focus, cx);
self.blink_caret(InputId::ScmInput, window, cx);
}
/// scmInput.ts: an edit of the input box sets the repository's input, which the host hears.
pub(in crate::workbench) fn edit_scm_input(&mut self, edit: impl FnOnce(&mut InputBox), window: &mut Window, cx: &mut Context<Self>) {
edit(&mut self.scm.input);
let value = self.scm.input.value.clone();
let _ = window;
let Some(provider) = self.focused_scm_provider_mut() else { return };
if provider.input.value == value {
return;
}
provider.input.value = value.clone();
provider.input.validation = None;
let handle = provider.handle;
self.host.send("ExtHostSCM", "$onInputBoxValueChange", vec![json!(handle), json!(value)]);
self.sync_scm_input_model(handle, &value, cx);
cx.notify();
}
/// The input boxes' text models, which live as long as their source control.
pub(super) fn scm_input_models(&self) -> Vec<(String, std::path::PathBuf)> {
self.scm.input_models.values().cloned().collect()
}
pub(super) fn sync_scm_input_model(&mut self, handle: u64, value: &str, cx: &mut Context<Self>) {
let Some((scheme, path)) = self.scm.input_models.get(&handle).cloned() else { return };
self.replace_model_text(&scheme, &path, value.to_string(), true, cx);
}
/// `scm.acceptInput`: the provider's `acceptInputCommand` with its arguments.
fn accept_scm_input(&mut self, window: &mut Window, cx: &mut Context<Self>) -> Outcome {
let Some(command) = self.focused_scm_provider().and_then(|provider| provider.accept_input_command.clone()) else { return Outcome::done() };
self.run_dto_command(&command, window, cx)
}
/// Runs an `ICommandDto` (`{ id, arguments }`).
fn run_dto_command(&mut self, command: &Value, window: &mut Window, cx: &mut Context<Self>) -> Outcome {
let Some(id) = command["id"].as_str().map(str::to_string) else { return Outcome::done() };
let args = command["arguments"].as_array().cloned().unwrap_or_default();
self.execute_command(&id, args, window, cx)
}
/// The context keys of a resource or group row (`scmProvider`, `scmProviderRootUri`, `scmProviderHasRootUri`,
/// `scmResourceGroup`, `scmResourceState`), on top of the workbench's.
pub(super) fn scm_context(&self, provider: &Provider, group: Option<&Group>, resource: Option<&Resource>, window: &Window) -> HashMap<String, Value> {
let mut context = self.key_context(window);
context.insert("scmProvider".into(), json!(provider.id));
context.insert("scmProviderRootUri".into(), json!(provider.root_uri["path"]));
context.insert("scmProviderHasRootUri".into(), json!(!provider.root_uri.is_null()));
if let Some(group) = group {
context.insert("scmResourceGroup".into(), json!(group.id));
context.insert("scmResourceGroupState".into(), json!(group.context_value.clone().unwrap_or_default()));
}
if let Some(resource) = resource {
context.insert("scmResourceState".into(), json!(resource.context_value.clone().unwrap_or_default()));
}
context
}
/// The key context while the view has focus: the repository and the input's state.
pub(in crate::workbench) fn scm_key_context(&self, window: &Window, context: &mut HashMap<String, Value>) {
self.scm_active_resource_context(context);
let Some(provider) = self.active_scm_provider() else { return };
context.insert("scm.providerCount".into(), json!(self.scm.model.providers.len()));
if self.scm.input_focus.is_focused(window) {
context.insert("scmRepository".into(), json!(true));
context.insert("scmInputHasValidationMessage".into(), json!(provider.input.validation.is_some()));
// scmInput.ts: the caret at line 1 column 1, or at the end of the last line.
context.insert("scmInputIsInFirstPosition".into(), json!(self.scm.input.selection.1 == 0));
context.insert("scmInputIsInLastPosition".into(), json!(self.scm.input.selection.1 == self.scm.input.len()));
}
if self.side_bar_view == super::search_view::SideBarView::Scm {
context.insert("activeViewlet".into(), json!("workbench.view.scm"));
}
self.scm_repositories_list_context(window, context);
}
/// The `ISCMResource` argument a resource menu action gets, as `MainThreadSCMResource.toJSON`.
pub(super) fn resource_arg(provider: &Provider, group: &Group, resource: &Resource) -> Value {
json!({ "$mid": 3, "sourceControlHandle": provider.handle, "groupHandle": group.handle, "handle": resource.handle })
}
fn group_arg(provider: &Provider, group: &Group) -> Value {
json!({ "$mid": 4, "sourceControlHandle": provider.handle, "groupHandle": group.handle })
}
pub(super) fn provider_arg(provider: &Provider) -> Value {
json!({ "$mid": 5, "handle": provider.handle })
}
/// The Source Control container: the Changes pane, the Graph pane while the active repository has a history
/// provider, then the contributed views. Several repositories, or `scm.alwaysShowRepositories`, show a row per
/// repository with its children below it (scmViewPane.ts `SCMTreeDataSource.getChildren`).
pub(in crate::workbench) fn scm_side_bar(&self, window: &Window, cx: &mut Context<Self>) -> Div {
let laid_out = self.scm_panes.view.as_ref().is_some_and(|view| view.ids().contains(&super::view_panes::SCM_REPOSITORIES_VIEW));
let mut panes: Vec<ViewPane> = laid_out.then(|| self.scm_repositories_pane(window, cx)).into_iter().collect();
panes.push(self.scm_changes_pane(window, cx));
panes.extend(self.active_scm_provider().filter(|provider| provider.has_history_provider).map(|provider| self.scm_graph_pane(provider, window, cx)));
self.view_pane_container(PaneContainer::Scm, "Source Control", panes, window, cx).child(div().h(px(5.)).flex_none())
}
pub(in crate::workbench) fn scm_changes_pane(&self, window: &Window, cx: &mut Context<Self>) -> ViewPane {
let foreground = self.foreground_or("sideBarSectionHeader.foreground");
let providers = self.visible_scm_providers();
let show_repositories = providers.len() > 1 || self.setting("scm.alwaysShowRepositories") == json!(true);
// `updateScmProviderContextKeys` and `getActionsContext`: the title sees a repository only when it is the only row.
let (mut context, args) = match (show_repositories, providers.first()) {
(false, Some(provider)) => (self.scm_context(provider, None, None, window), vec![Self::provider_arg(provider)]),
_ => (self.key_context(window), Vec::new()),
};
self.scm_view_context(&mut context);
// paneview.css: a header action is its 20px label and a 4px margin, 24px apart as these 22px buttons 2px apart.
let inline = self.inline_actions(MenuId::ScmTitle, Some(SCM_CHANGES_VIEW), &context, args.clone(), foreground, cx).gap(px(2.));
let more = self.scm_more_actions(context, args, foreground, cx);
let actions = div().flex().items_center().gap(px(2.)).child(inline).child(self.action_bar_item(SCM_CHANGES_VIEW, "more", more, cx));
let show_actions = self.scm.input_focus.is_focused(window) || self.scm.list_focus.is_focused(window);
// scmViewPane.ts: no repository shows the view's welcome content.
let body = match providers.is_empty() {
true => div().flex().flex_col().children(self.view_welcome(super::views_welcome::SCM_VIEW, window, cx)).into_any_element(),
false => {
let mut rows = Vec::new();
for provider in &providers {
if show_repositories {
rows.push(ScmRow { depth: 1, selected: false, element: self.scm_repository_row(provider, providers.len(), window, cx).into_any_element() });
if self.scm.collapsed_repositories.contains(&provider.handle) {
continue;
}
}
self.push_scm_repository_children(&mut rows, provider, providers.len(), usize::from(show_repositories), window, cx);
}
let depths = rows.iter().map(|row| row.depth).collect();
let active: Vec<usize> = rows.iter().enumerate().filter(|(_, row)| row.selected).map(|(index, _)| index).collect();
let group = gpui::SharedString::from("scm-list");
let guides = self.tree_guides_in_group(group.clone(), depths, &active);
let inset = self.pane_row_inset();
// modernUI padding.css: the rows sit `pane_row_inset` in from the pane's edges, the guides from the row.
let rows = rows.into_iter().enumerate().map(|(index, row)| div().relative().mx(px(inset)).child(row.element).children(guides.row(index, 0.)));
self.scrollable("scm-list", None, div().id("scm-list").debug_selector(|| "scm-list".into()).group(group).track_focus(&self.scm.list_focus).children(rows), cx).flex_1().into_any_element()
}
};
ViewPane { id: SCM_CHANGES_VIEW, title: "Changes".into(), description: None, actions: Some(actions.into_any_element()), draws_more: true, show_actions, body }
}
/// A repository's input, action button (`scm.showActionButton`) and groups; the groups show when some has
/// changes, or for the only repository without an action button.
fn push_scm_repository_children(&self, rows: &mut Vec<ScmRow>, provider: &Provider, repository_count: usize, level: usize, window: &Window, cx: &mut Context<Self>) {
if provider.input.visible {
rows.push(ScmRow { depth: 1 + level, selected: false, element: self.scm_input_row(provider, level, window, cx).into_any_element() });
}
let button = provider.action_button.as_ref().filter(|button| button["command"].is_object() && self.setting("scm.showActionButton") != json!(false));
if let Some(button) = button {
rows.push(ScmRow { depth: 1 + level, selected: false, element: self.scm_action_button(provider.handle, button, level, cx).into_any_element() });
}
let has_some_changes = provider.groups.iter().any(|group| !group.resources.is_empty());
if !has_some_changes && !(repository_count == 1 && button.is_none()) {
return;
}
for group in provider.groups.iter().filter(|group| !(group.hide_when_empty && group.resources.is_empty())) {
let selected = self.scm.selected == Some((provider.handle, group.handle, None));
rows.push(ScmRow { depth: 1 + level, selected, element: self.scm_group_row(provider, group, level, window, cx).into_any_element() });
if self.scm.collapsed.contains(&(provider.handle, group.handle)) {
continue;
}
let root = std::path::PathBuf::from(provider.root_uri["path"].as_str().unwrap_or_default());
let collapsed = |folder: &std::path::Path| self.scm.collapsed_folders.contains(&(provider.handle, group.handle, folder.to_path_buf()));
let tree = self.scm_tree_mode();
for row in self.scm_rows(&root, &group.resources, &collapsed) {
rows.push(match row {
TreeRow::Folder { path, label, depth } => ScmRow { depth: 2 + level + depth, selected: false, element: self.scm_folder_row(provider, group, path, label, depth + level, window, cx).into_any_element() },
TreeRow::Resource { index, depth } => {
let resource = &group.resources[index];
let selected = self.scm.selected == Some((provider.handle, group.handle, Some(resource.handle)));
let element = self.scm_resource_row(provider, group, resource, tree.then_some(depth), level, window, cx).into_any_element();
ScmRow { depth: 2 + level + if tree { depth } else { 0 }, selected, element }
}
});
}
}
}
/// A repository row of the Changes view: the twistie, then `scm_repository_parts` with `MenuId.SCMTitle`.
fn scm_repository_row(&self, provider: &Provider, repository_count: usize, window: &Window, cx: &mut Context<Self>) -> gpui::Stateful<Div> {
let foreground = self.foreground_or("sideBar.foreground");
let handle = provider.handle;
let collapsed = self.scm.collapsed_repositories.contains(&handle);
let selected = self.scm.focused == Some(handle) && repository_count > 1;
div()
.id(gpui::SharedString::from(format!("scm-repository-{handle}")))
.debug_selector(move || format!("scm-repository-{handle}"))
.h(px(ROW_HEIGHT))
.pr(px(8.))
.flex()
.items_center()
.rounded(px(4.))
.cursor_pointer()
.when(selected, |row| row.bg(self.color("list.inactiveSelectionBackground")))
.hover(|row| row.bg(self.color("list.hoverBackground")))
.on_click(cx.listener(move |workbench, _: &ClickEvent, _, cx| {
workbench.focus_scm_repository(Some(handle));
if !workbench.scm.collapsed_repositories.remove(&handle) {
workbench.scm.collapsed_repositories.insert(handle);
}
cx.notify();
}))
.on_mouse_down(MouseButton::Right, cx.listener(move |workbench, event: &MouseDownEvent, window, cx| workbench.show_scm_source_control_menu(handle, Vec::new(), event.position, window, cx)))
.child(self.icon(if collapsed { "chevron-right" } else { "chevron-down" }, TWISTIE, 16., foreground))
.children(self.scm_repository_parts(provider, MenuId::ScmTitle, gpui::FontWeight::BOLD, foreground, window, cx))
}
/// scmRepositoryRenderer.ts `RepositoryRenderer`: the repository icon (`repo-selected` while shown among several),
/// the name (`parent / name` in explorer mode) with a description telling same-named repositories apart,
/// then the status bar commands and `menu`'s actions, and the count per `scm.providerCountBadge`.
pub(super) fn scm_repository_parts(&self, provider: &Provider, menu: MenuId, name_weight: gpui::FontWeight, foreground: Rgba, window: &Window, cx: &mut Context<Self>) -> Vec<gpui::AnyElement> {
let handle = provider.handle;
let repositories = self.scm_repositories();
let icon = match provider.icon.as_deref() {
Some(icon) if icon != "repo" => icon,
_ if self.scm.visible.contains(&handle) && repositories.len() > 1 => "repo-selected",
_ => "repo",
};
let parent = provider.parent.filter(|_| self.scm_explorer_enabled()).and_then(|parent| repositories.iter().find(|other| other.handle == parent));
let name = match parent {
Some(parent) => format!("{} / {}", self.scm_repository_name(parent), self.scm_repository_name(provider)),
None => self.scm_repository_name(provider),
};
let root = provider.root_uri["path"].as_str();
let description = self.scm_repository_description(provider);
let title = match root {
Some(root) => format!("{}: {}", provider.label, crate::quick_input::path_label(std::path::Path::new(root), None)),
None => provider.label.clone(),
};
let mut toolbar = div().flex().items_center().overflow_hidden();
for (index, command) in provider.status_bar_commands.iter().enumerate() {
let label = command["title"].as_str().unwrap_or_default().to_string();
let tooltip = command["tooltip"].as_str().map_or_else(|| label.clone(), str::to_string);
let command = command.clone();
let id = gpui::SharedString::from(format!("scm-repository-status-{menu:?}-{handle}-{index}"));
// util.ts `StatusBarActionViewItem`: each text run is a span, a flex item, so its outer whitespace
// collapses; scm.css gives it 2px margin-left.
let span = |text: &str| Some(text.trim()).filter(|text| !text.is_empty()).map(|text| div().ml(px(2.)).child(text.to_string()).into_any_element());
let chars: Vec<char> = label.chars().collect();
let mut parts = Vec::new();
let mut text = String::new();
let mut at = 0;
while at < chars.len() {
if let Some((name, length)) = super::quick_input_view::icon_at(&chars[at..]) {
parts.extend(span(&std::mem::take(&mut text)));
parts.push(self.icon(&name, 16., 16., foreground).into_any_element());
at += length;
continue;
}
text.push(chars[at]);
at += 1;
}
parts.extend(span(&text));
toolbar = toolbar.child(
div()
.id(id.clone())
.ml(px(4.))
// modernUI: the action label's padding is 2px.
.px(px(2.))
.h(px(20.))
.flex()
.items_center()
.rounded(px(4.))
// actionbar.css `.action-label`: 11px.
.text_size(px(11.))
.whitespace_nowrap()
.cursor_pointer()
.hover(|action| action.bg(self.color("toolbar.hoverBackground")))
.on_click(cx.listener(move |workbench, _: &ClickEvent, window, cx| {
cx.stop_propagation();
drop(workbench.run_dto_command(&command, window, cx));
}))
.children(parts)
.child(self.hover_target(id.clone(), HoverContent::Text(tooltip), HoverOptions::mouse())),
);
}
let context = self.scm_context(provider, None, None, window);
toolbar = toolbar.child(self.inline_actions(menu, None, &context, vec![Self::provider_arg(provider)], foreground, cx));
// `connectPrimaryMenu`: the actions outside the primary group are the toolbar's "...".
let primary = if menu == MenuId::ScmTitle { "navigation" } else { "inline" };
let secondary = crate::menus::menu_group_actions(&self.menus, menu, &context, |group| group != primary);
if !secondary.is_empty() {
let more = self.icon("toolbar-more", 22., 16., foreground);
let args = vec![Self::provider_arg(provider)];
let more = more.id(gpui::SharedString::from(format!("scm-repository-more-{menu:?}-{handle}"))).debug_selector(move || format!("scm-repository-more-{menu:?}-{handle}"));
toolbar = toolbar.child(
more.rounded(px(4.))
.cursor_pointer()
.hover(|icon| icon.bg(self.color("toolbar.hoverBackground")))
.on_mouse_down(MouseButton::Left, cx.listener(move |workbench, event: &MouseDownEvent, window, cx| {
window.prevent_default();
cx.stop_propagation();
workbench.show_context_menu_actions(secondary.clone(), &context, Bounds::new(event.position, Default::default()), args.clone(), window, cx);
}))
.child(self.hover_target(gpui::SharedString::from(format!("scm-repository-more-hover-{menu:?}-{handle}")), HoverContent::Text("More Actions...".into()), HoverOptions::action_item())),
);
}
let count = provider.count();
let show_count = match self.setting("scm.providerCountBadge").as_str() {
Some("visible") => true,
Some("auto") => count > 0,
_ => false,
};
let badge = show_count.then(|| {
div()
.ml(px(4.))
.min_w(px(18.))
.h(px(18.))
.px(px(5.))
.rounded(px(11.))
.flex()
.items_center()
.justify_center()
.text_size(px(11.))
.bg(self.color("badge.background"))
.text_color(self.color("badge.foreground"))
.child(count.to_string())
});
let mut parts = vec![
self.icon(icon, 18., 16., foreground).pr(px(2.)).into_any_element(),
div().flex_none().min_w_0().overflow_hidden().whitespace_nowrap().font_weight(name_weight).child(name).into_any_element(),
];
parts.extend(description.map(|description| div().ml(px(6.)).flex_none().whitespace_nowrap().text_size(px(12.)).opacity(0.7).child(description).into_any_element()));
parts.push(div().flex_1().min_w(px(24.)).flex().justify_end().child(toolbar).into_any_element());
parts.extend(badge.map(IntoElement::into_any_element));
parts.push(self.hover_target(gpui::SharedString::from(format!("scm-repository-hover-{menu:?}-{handle}")), HoverContent::Text(title), HoverOptions::mouse()).into_any_element());
parts
}
/// scmViewService.ts `explorerEnabledConfig`: `scm.repositories.explorer` in `single` selection mode.
pub(super) fn scm_explorer_enabled(&self) -> bool {
self.setting("scm.repositories.explorer") == json!(true) && self.scm_single_selection()
}
/// The input box shows the active repository's input once, then keeps its own state while edited.
pub(in crate::workbench) fn load_scm_input(&mut self) {
let shown = self.focused_scm_provider().map(|provider| (provider.handle, provider.input.value.clone()));
if shown.as_ref().map(|shown| shown.0) != self.scm.shown_input {
self.scm.shown_input = shown.as_ref().map(|shown| shown.0);
self.scm.input.set_value(shown.map(|shown| shown.1).unwrap_or_default());
}
}
/// scmViewPane.ts `InputRenderer`: the input editor with its placeholder (`{0}` the commit key), 2px border radius
/// and `input.*` colors, and the validation message under it.
/// One input box edits the focused repository's input; another repository's input shows its value until a click
/// focuses that repository.
fn scm_input_row(&self, provider: &Provider, level: usize, window: &Window, cx: &mut Context<Self>) -> gpui::Stateful<Div> {
let editing = self.scm.focused == Some(provider.handle);
let focused = editing && self.scm.input_focus.is_focused(window);
// scmViewPane.ts `getPlaceholderValue`: `{0}` is the Cmd+Enter label, the provider's placeholder else "Message ({0} to commit)".
let placeholder = provider.input.placeholder.replace("{0}", "\u{2318}Enter");
let border = match (focused, &provider.input.validation) {
(_, Some((_, 0))) => self.theme.color("inputValidation.errorBorder"),
(_, Some((_, 1))) => self.theme.color("inputValidation.warningBorder"),
(_, Some(_)) => self.theme.color("inputValidation.infoBorder"),
(true, None) => self.theme.color("focusBorder"),
(false, None) => self.theme.color("input.border"),
};
let look = InputLook { password: false, placeholder: Some(placeholder), focused };
let input = match editing {
true => self.input_text(InputId::ScmInput, look, cx).into_any_element(),
false if provider.input.value.is_empty() => div().whitespace_nowrap().overflow_hidden().text_color(self.color("input.placeholderForeground")).child(look.placeholder.unwrap_or_default()).into_any_element(),
false => div().whitespace_nowrap().overflow_hidden().child(provider.input.value.lines().next().unwrap_or_default().to_string()).into_any_element(),
};
// modernUI roundedCorners.css: 4px when the SCM view sits in the panel.
let container = self.movable_view(SCM_CHANGES_VIEW).map_or(PaneContainer::Scm.container_id(), |view| self.view_container(&view));
let in_panel = self.container_location(container) == super::layout::Part::Panel;
let field = div()
.h(px(26.))
.px(px(6.))
.flex()
.items_center()
.rounded(px(if in_panel { 4. } else { 2. }))
.bg(self.color("input.background"))
.text_color(self.color("input.foreground"))
.border_1()
.border_color(border.unwrap_or(gpui::transparent_black().into()))
.cursor_text()
.child(input);
let validation = provider.input.validation.clone().filter(|_| focused).map(|(message, kind)| {
let key = ["error", "warning", "info"][kind.min(2) as usize];
div()
.px(px(6.))
.py(px(4.))
.text_size(px(12.))
.bg(self.color(&format!("inputValidation.{key}Background")))
.text_color(self.foreground_or(&format!("inputValidation.{key}Foreground")))
.child(message)
});
let handle = provider.handle;
div()
.id(gpui::SharedString::from(format!("scm-input-{handle}")))
.pl(px(INDENT * (1 + level) as f32 + TWISTIE - 4.))
.pr(px(8.))
.pt(px(4.))
.pb(px(4.))
.when(!editing, |row| {
row.on_click(cx.listener(move |workbench, _: &ClickEvent, window, cx| {
workbench.focus_scm_repository(Some(handle));
workbench.focus_scm_input(window, cx);
cx.notify();
}))
})
.child(field)
.children(validation)
}
/// scmViewPane.ts `ActionButtonRenderer`: the button with its title (`$(icon)` labels) and, with secondary
/// commands, the dropdown part after a separator.
fn scm_action_button(&self, handle: u64, button: &Value, level: usize, cx: &mut Context<Self>) -> Div {
let enabled = button["enabled"] != false;
let foreground = self.color("button.foreground");
let background = self.color("button.background");
let command = button["command"].clone();
let title = command["title"].as_str().unwrap_or_default().to_string();
let label = self.button_label(&title, foreground);
let main = div()
.id(gpui::SharedString::from(format!("scm-action-button-{handle}")))
.flex_1()
.h_full()
.flex()
.items_center()
.justify_center()
.when(enabled, |main| main.cursor_pointer().hover(|main| main.bg(self.color("button.hoverBackground"))))
.on_click(cx.listener(move |workbench, _: &ClickEvent, window, cx| {
if enabled {
drop(workbench.run_dto_command(&command, window, cx));
}
}))
.child(label);
let secondary = button["secondaryCommands"].as_array().is_some_and(|commands| !commands.is_empty());
let dropdown = secondary.then(|| {
div()
.flex()
.h_full()
.items_center()
.child(div().w(px(1.)).h(px(16.)).bg(self.theme.color("button.separator").unwrap_or(foreground)))
.child(self.icon("chevron-down", 26., 16., foreground).cursor_pointer())
});
// scmViewPane.ts `ListDelegate.getHeight`: the row is `DEFAULT_HEIGHT` 28 plus 8, the button centered in it.
div()
.h(px(36.))
.flex_none()
.flex()
.items_center()
.pl(px(INDENT * (1 + level) as f32 + TWISTIE - 4.))
.pr(px(8.))
.child(div().flex_1().h(px(26.)).flex().rounded(px(2.)).bg(background).text_color(foreground).when(!enabled, |button| button.opacity(0.4)).child(main).children(dropdown))
}
fn scm_group_row(&self, provider: &Provider, group: &Group, level: usize, window: &Window, cx: &mut Context<Self>) -> gpui::Stateful<Div> {
let foreground = self.foreground_or("sideBar.foreground");
let collapsed = self.scm.collapsed.contains(&(provider.handle, group.handle));
let context = self.scm_context(provider, Some(group), None, window);
// modernUI padding.css: 2px between a row's inline actions.
let actions = self.inline_actions(MenuId::ScmResourceGroupContext, None, &context, vec![Self::group_arg(provider, group)], foreground, cx).gap(px(2.));
let key = (provider.handle, group.handle);
let selected = self.scm.selected == Some((provider.handle, group.handle, None));
// countBadge.css under fontRamp.css: 19px high and wide at least, 10px text, 6px after the actions.
let badge_selector = format!("scm-group-count-{}-{}", provider.handle, group.handle);
let badge = div()
.debug_selector(move || badge_selector)
.flex_none()
.ml(px(6.))
.min_w(px(19.))
.h(px(19.))
.px(px(6.))
.rounded_full()
.flex()
.items_center()
.justify_center()
.text_size(px(10.))
.line_height(px(11.))
.bg(self.color("badge.background"))
.text_color(self.color("badge.foreground"))
.child(group.resources.len().to_string());
let id = gpui::SharedString::from(format!("scm-group-{}-{}", provider.handle, group.handle));
let menu_context = context.clone();
let menu_args = vec![Self::group_arg(provider, group)];
div()
.id(id.clone())
.debug_selector(|| id.to_string())
.group(id.clone())
.h(px(ROW_HEIGHT))
// abstractTree.ts `indentSize`: the twistie 8px further in than the input row's and the actions, and the
// label after the twistie's 6px padding (tree.css), measured against the reference with ui-pair.
.pl(px(INDENT * (1 + level) as f32 + 3.))
// scm.css `.resource-group`: 4px right padding.
.pr(px(4.))
.flex()
.items_center()
.rounded(px(4.))
.cursor_pointer()
.when(selected, |row| row.bg(self.color("list.inactiveSelectionBackground")))
.hover(|row| row.bg(self.color("list.hoverBackground")))
.on_click(cx.listener(move |workbench, _: &ClickEvent, _, cx| {
workbench.focus_scm_repository(Some(key.0));
workbench.scm.selected = Some((key.0, key.1, None));
if !workbench.scm.collapsed.remove(&key) {
workbench.scm.collapsed.insert(key);
}
cx.notify();
}))
.on_mouse_down(MouseButton::Right, cx.listener(move |workbench, event: &MouseDownEvent, window, cx| {
let actions = crate::menus::menu_group_actions(&workbench.menus, MenuId::ScmResourceGroupContext, &menu_context, |group| group != "inline");
workbench.show_context_menu_actions(actions, &menu_context, Bounds::new(event.position, Default::default()), menu_args.clone(), window, cx);
}))
.child(self.icon(if collapsed { "chevron-right" } else { "chevron-down" }, TWISTIE, 16., foreground))
.child(div().flex_1().min_w_0().ml(px(3.)).overflow_hidden().whitespace_nowrap().child(group.label.clone()))
.child(div().invisible().group_hover(id, |actions| actions.visible()).child(actions))
.child(badge)
}
/// scmViewPane.ts `ResourceRenderer`: file icon, name, the folder as description, inline actions on hover and the
/// decoration letter from the resource's tooltip and color (git's `letter` via its decoration provider).
fn scm_resource_row(&self, provider: &Provider, group: &Group, resource: &Resource, tree_depth: Option<usize>, level: usize, window: &Window, cx: &mut Context<Self>) -> gpui::Stateful<Div> {
let foreground = self.foreground_or("sideBar.foreground");
let path = std::path::PathBuf::from(resource.uri["path"].as_str().unwrap_or_default());
let name = path.file_name().unwrap_or_default().to_string_lossy().into_owned();
let root = provider.root_uri["path"].as_str().unwrap_or_default();
// scmViewPane.ts `ResourceRenderer`: `hidePath` in tree mode.
let folder = match tree_depth {
Some(_) => String::new(),
None => path.parent().and_then(|parent| parent.strip_prefix(root).ok()).map(|folder| folder.to_string_lossy().into_owned()).unwrap_or_default(),
};
let context = self.scm_context(provider, Some(group), Some(resource), window);
let args = vec![Self::resource_arg(provider, group, resource)];
let actions = self.inline_actions(MenuId::ScmResourceStateContext, None, &context, args.clone(), foreground, cx).gap(px(2.));
let decoration = self.file_decoration(&path, false);
// labels.ts titles the label with its path, then the decoration's tooltip.
let title = match decoration.as_ref().and_then(|decoration| decoration.tooltip.as_deref()) {
Some(tooltip) => format!("{} \u{2022} {tooltip}", crate::quick_input::path_label(&path, None)),
None => crate::quick_input::path_label(&path, None),
};
let letter = decoration.and_then(|decoration| {
let color = decoration.color.as_deref().and_then(|color| self.theme.color(color)).unwrap_or(foreground);
Some((decoration.letter?, color))
});
let language = self.languages.language_of(&path, "").to_string();
let icon = self.file_icons.as_ref().and_then(|icons| icons.file_icon(&path, &language, self.theme.kind, 13., ROW_HEIGHT, foreground)).unwrap_or_else(|| div().w(px(16.)));
let key = (provider.handle, group.handle, Some(resource.handle));
let selected = self.scm.selected == Some(key);
let id = gpui::SharedString::from(format!("scm-resource-{}-{}-{}", provider.handle, group.handle, resource.handle));
let handles = (provider.handle, group.handle, resource.handle);
let editor_command = resource.command.clone().filter(|command| matches!(command["id"].as_str(), Some("_workbench.open" | "_workbench.diff")));
div()
.id(id.clone())
.debug_selector(|| id.to_string())
.group(id.clone())
.h(px(ROW_HEIGHT))
.relative()
// The icon at the indent, measured against the reference with ui-pair.
.pl(px(INDENT * (2 + level + tree_depth.unwrap_or(0)) as f32))
.pr(px(8.))
.flex()
.items_center()
.rounded(px(4.))
.cursor_pointer()
.when(resource.faded, |row| row.opacity(0.7))
.when(selected, |row| row.bg(self.color("list.inactiveSelectionBackground")))
.hover(|row| row.bg(self.color("list.hoverBackground")))
.on_click(cx.listener(move |workbench, event: &ClickEvent, window, cx| {
workbench.focus_scm_repository(Some(key.0));
workbench.scm.selected = Some(key);
// scmViewPane.ts `open`: `_workbench.open`/`_workbench.diff` arrive converted and run here; the ext host keeps only other commands.
if let Some(command) = editor_command.as_ref() {
drop(workbench.run_dto_command(command, window, cx));
} else {
// `ResourceTree.open`: a single click previews with the focus kept, a double click pins and focuses.
let preserve_focus = event.click_count() < 2;
drop(workbench.host.call("ExtHostSCM", "$executeResourceCommand", vec![json!(handles.0), json!(handles.1), json!(handles.2), json!(preserve_focus)]));
}
cx.notify();
}))
.on_mouse_down(MouseButton::Right, cx.listener(move |workbench, event: &MouseDownEvent, window, cx| {
let actions = crate::menus::menu_group_actions(&workbench.menus, MenuId::ScmResourceStateContext, &context, |group| group != "inline");
workbench.show_context_menu_actions(actions, &context, Bounds::new(event.position, Default::default()), args.clone(), window, cx);
}))
.child(self.hover_target(gpui::SharedString::from(format!("{id}-hover")), HoverContent::Text(title), HoverOptions::mouse()))
.child(icon.mr(px(6.)))
// scmViewPane.ts `fileDecorations: { colors: false }`: only the letter takes the decoration's color.
.child(div().flex_none().whitespace_nowrap().when(resource.strike_through, |name| name.line_through()).child(name))
.child(div().flex_1().min_w_0().ml(px(6.)).overflow_hidden().whitespace_nowrap().text_size(px(12.)).opacity(0.7).child(folder))
.child(div().invisible().group_hover(id, |actions| actions.visible()).child(actions))
// scm.css `.monaco-icon-label::after`: a centered 16px box 5px after the label, at fontRamp.css's 11px.
.children(letter.map(|(letter, color)| {
div().flex_none().w(px(16.)).ml(px(5.)).flex().justify_center().text_size(px(11.)).font_weight(gpui::FontWeight::SEMIBOLD).opacity(0.75).text_color(color).child(letter)
}))
}
/// The `inline` group of a menu as icon buttons (`createAndFillInActionBarActions` with `primaryGroup` inline or
/// navigation): a command's icon, else its title.
/// In a title, `bar` names the view whose action bar (actionbar.ts) the buttons are items of.
pub(in crate::workbench) fn inline_actions(&self, menu: MenuId, bar: Option<&'static str>, context: &HashMap<String, Value>, args: Vec<Value>, foreground: Rgba, cx: &mut Context<Self>) -> Div {
let primary = if matches!(menu, MenuId::ScmTitle | MenuId::ScmSourceControlTitle | MenuId::ViewTitle) { "navigation" } else { "inline" };
let mut items: Vec<&crate::menus::MenuItem> = self.menus.iter().filter(|item| item.menu == menu && item.group == primary && item.when.as_ref().is_none_or(|when| when.evaluate(context))).collect();
items.sort_by(|a, b| a.order.total_cmp(&b.order).then_with(|| a.title.cmp(&b.title)));
let mut buttons = div().flex().items_center();
for item in items {
let (id, button) = match &item.entry {
crate::menus::Entry::Command { id, .. } => (id.clone(), self.command_button(id, item.title.clone(), args.clone(), foreground, cx)),
crate::menus::Entry::Submenu { submenu, .. } => {
let button = self.submenu_button(*submenu, self.submenu_icons.get(submenu), item.title.clone(), context.clone(), args.clone(), foreground, cx);
(format!("{submenu:?}"), button)
}
};
buttons = buttons.child(match bar {
Some(bar) => self.action_bar_item(bar, &id, button, cx),
None => button,
});
}
buttons
}
/// A command's or submenu's icon in a toolbar: its codicon or image, else its title.
pub(in crate::workbench) fn command_icon(&self, icon: Option<&CommandIcon>, title: &str, foreground: Rgba) -> Div {
match icon {
Some(CommandIcon::Codicon(name)) => self.icon(name, 22., 16., foreground),
Some(CommandIcon::Image(light, dark)) => {
let path = if self.theme.kind == crate::theme::ThemeKind::Light { light } else { dark };
div().size(px(22.)).flex().items_center().justify_center().child(img(path.clone()).size(px(16.)))
}
None => div().px(px(4.)).child(title.to_string()),
}
}
/// A submenu as a toolbar button (`SubmenuEntryActionViewItem`): its icon opens the submenu below it, whose
/// commands get `args`.
pub(in crate::workbench) fn submenu_button(&self, submenu: MenuId, icon: Option<&CommandIcon>, title: String, context: HashMap<String, Value>, args: Vec<Value>, foreground: Rgba, cx: &mut Context<Self>) -> gpui::Stateful<Div> {
let bounds = std::rc::Rc::new(std::cell::Cell::new(Bounds::default()));
let measured = bounds.clone();
let key = format!("{submenu:?}-{}", args.first().map(Value::to_string).unwrap_or_default());
self.command_icon(icon, &title, foreground)
.id(gpui::SharedString::from(format!("submenu-button-{key}")))
.debug_selector(move || format!("submenu-button-{submenu:?}"))
.rounded(px(4.))
.cursor_pointer()
.hover(|icon| icon.bg(self.color("toolbar.hoverBackground")))
.on_mouse_down(MouseButton::Left, stop_action_mouse_down)
.on_click(cx.listener(move |workbench, _: &ClickEvent, window, cx| {
cx.stop_propagation();
let actions = crate::menus::menu_actions(&workbench.menus, submenu, &context);
workbench.show_context_menu_actions(actions, &context, bounds.get(), args.clone(), window, cx);
}))
.child(gpui::canvas(move |element_bounds, _, _| measured.set(element_bounds), |_, _, _, _| {}).absolute().size_full())
.child(self.hover_target(gpui::SharedString::from(format!("submenu-button-hover-{key}")), HoverContent::Text(title), HoverOptions::action_item()))
}
/// A menu command as a toolbar button (`MenuEntryActionViewItem`): its icon, else its title, with its hover.
pub(in crate::workbench) fn command_button(&self, id: &str, title: String, args: Vec<Value>, foreground: Rgba, cx: &mut Context<Self>) -> gpui::Stateful<Div> {
self.command_button_with_icon(id, self.command_icons.get(id), title, args, foreground, cx)
}
pub(in crate::workbench) fn command_button_with_icon(&self, id: &str, icon: Option<&crate::menus::CommandIcon>, title: String, args: Vec<Value>, foreground: Rgba, cx: &mut Context<Self>) -> gpui::Stateful<Div> {
let icon = self.command_icon(icon, &title, foreground);
let command = id.to_string();
icon.id(gpui::SharedString::from(format!("command-button-{id}-{}", args.first().map(Value::to_string).unwrap_or_default())))
.rounded(px(4.))
.cursor_pointer()
.hover(|icon| icon.bg(self.color("toolbar.hoverBackground")))
.on_mouse_down(MouseButton::Left, stop_action_mouse_down)
.on_click(cx.listener(move |workbench, _: &ClickEvent, window, cx| {
cx.stop_propagation();
drop(workbench.execute_command(&command, args.clone(), window, cx));
}))
.child(self.hover_target(gpui::SharedString::from(format!("command-button-hover-{id}")), HoverContent::Text(title), HoverOptions::action_item()))
}
/// A split button submenu (menuEntryActionViewItem.ts `DropdownWithDefaultActionViewItem`, `togglePrimaryAction`):
/// the command last run from its dropdown, stored per workspace as `<submenu>_lastActionId`, else the first, and
/// a chevron that opens the submenu. None when the submenu has no commands.
pub(in crate::workbench) fn split_button(&self, submenu: MenuId, submenu_title: String, context: &HashMap<String, Value>, args: Vec<Value>, foreground: Rgba, cx: &mut Context<Self>) -> Option<gpui::Stateful<Div>> {
let actions = crate::menus::menu_actions(&self.menus, submenu, context);
let storage_key = format!("{submenu:?}_lastActionId");
let last = self.workspace_storage.lock().unwrap().get(&storage_key).map(str::to_string);
let commands = || actions.iter().filter_map(|action| match action {
crate::menus::MenuAction::Command { id, title, .. } => Some((id, title)),
_ => None,
});
let (id, title) = commands().find(|(id, _)| last.as_ref() == Some(*id)).or_else(|| commands().next())?;
let primary_selector = format!("split-button-primary-{id}");
let primary = self.command_button(id, title.clone(), args.clone(), foreground, cx).debug_selector(move || primary_selector);
let bounds = std::rc::Rc::new(std::cell::Cell::new(Bounds::default()));
let measured = bounds.clone();
let context = context.clone();
// shortcut: every group's split button of this submenu shows open, upgrade when two groups show one.
let open = self.context_menu.as_ref().is_some_and(|menu| menu.primary_action_storage_key.as_ref() == Some(&storage_key));
let hover = self.color("toolbar.hoverBackground");
// menuEntryActionViewItem.css: a 16px chevron label pulled 3px under the primary action (`margin-left: -3px`);
// absolute in a 13px right padding, as a negative margin zeroes the button's width in taffy.
let dropdown = self
.icon("chevron-down", 16., 12., foreground)
.h(px(22.))
.absolute()
.right_0()
.rounded(px(5.))
.when(open, |icon| icon.bg(self.color("toolbar.activeBackground")))
.id(gpui::SharedString::from(format!("split-button-dropdown-{submenu:?}")))
.debug_selector(move || format!("split-button-dropdown-{submenu:?}"))
.cursor_pointer()
.hover(move |icon| icon.bg(hover))
.on_mouse_down(MouseButton::Left, stop_action_mouse_down)
.on_click(cx.listener(move |workbench, _: &ClickEvent, window, cx| {
cx.stop_propagation();
let actions = crate::menus::menu_actions(&workbench.menus, submenu, &context);
workbench.show_context_menu_actions(actions, &context, bounds.get(), args.clone(), window, cx);
if let Some(menu) = workbench.context_menu.as_mut() {
menu.primary_action_storage_key = Some(storage_key.clone());
}
}))
.child(gpui::canvas(move |element_bounds, _, _| measured.set(element_bounds), |_, _, _, _| {}).absolute().size_full())
.child(self.hover_target(gpui::SharedString::from(format!("split-button-hover-{submenu:?}")), HoverContent::Text(submenu_title), HoverOptions::action_item()));
Some(div().id(gpui::SharedString::from(format!("split-button-{submenu:?}"))).relative().flex().flex_none().pr(px(13.)).rounded(px(5.)).hover(move |button| button.bg(hover)).child(primary).child(dropdown))
}
}
/// actionViewItems.ts `EventHelper.stop(e, true)` on mouse down: an action takes no focus and the header under it
/// neither focuses nor starts a drag.
pub(super) fn stop_action_mouse_down(_: &MouseDownEvent, window: &mut Window, cx: &mut gpui::App) {
window.prevent_default();
cx.stop_propagation();
}
#[cfg(test)]
mod tests;