Visual Studio Code, rebuilt in Rust
Rust
//! The workbench as the reference draws it in the modern floating layout
//! (src/vs/workbench/browser/media/floatingPanels.css), empty or with a folder in the Explorer.
//! Sizes are the reference's CSS pixels.
pub mod accessibility;
pub mod accessible_view;
pub mod new_file;
mod action_bar;
mod actions;
mod part_navigation;
mod activity_bar_menu;
mod breakpoint_widget;
mod inline_breakpoints;
mod coverage_decorations;
mod bulk_text_edits;
mod configuration;
mod configuration_migration;
mod content_hover;
mod content_hover_view;
mod code_editors;
mod editor_groups;
pub(crate) mod auxiliary_windows;
mod history_actions;
mod configuration_resolver;
mod context_menu;
pub mod custom_editors;
mod call_stack_menu;
mod variable_menu;
mod debug_commands;
mod debug_console;
mod debug_memory;
mod problems_quick_fix;
mod problems_table;
pub(crate) mod modal_editor_group;
mod problems_view;
mod refactor_preview;
mod view_containers;
mod view_visibility;
mod debug_toolbar;
mod debug_visualizers;
mod debug_dynamic;
pub(crate) mod debug_service;
pub(crate) mod debug_task_runner;
mod debug_view;
mod disassembly_view;
mod testing_continuous;
mod testing_filter;
mod testing_gutter;
mod testing_message_colorizer;
mod testing_peek;
mod testing_coverage_view;
mod testing_results;
mod testing_item_menu;
mod testing_profiles;
pub(crate) mod testing_service;
mod testing_view;
mod define_keybinding;
mod exception_widget;
mod floating_menu;
mod diagnostics;
mod dialog_view;
mod backup_tracker;
mod banner;
mod dialogs;
mod lifecycle;
mod editor_commands;
mod editor_menus;
mod editor_options;
mod side_by_side_editor;
mod editor_type_picker;
mod breadcrumbs;
mod editor_message;
pub mod editor_status;
mod editor_view;
mod explorer_actions;
pub(crate) mod extension_bisect;
pub(crate) mod extension_management;
pub(crate) mod extension_host_restart;
mod user_data_profile;
mod profiles_editor_view;
mod icon_label;
mod icon_select_box;
mod settings_import;
pub use profiles_editor_view::NEW_WINDOW_PROFILE_SETTING;
mod explorer_find;
mod explorer_keyboard;
mod extension_editor;
mod extension_features;
mod extensions_list_view;
mod workspace_recommendations;
pub(crate) mod localization;
pub(crate) mod extensions_view;
mod bulk_undo;
mod file_compare;
mod file_dialogs;
pub mod simple_file_dialog;
mod file_participants;
mod find_options_widget;
mod find_widget;
mod folding_providers;
mod auto_save;
pub(crate) mod host;
mod power;
pub(crate) mod command_line;
#[cfg(unix)]
mod shell_command;
mod relauncher;
mod update;
mod update_title_bar;
pub(crate) mod customize_layout;
mod quick_pick_pin;
pub(crate) mod perf_view;
mod proxy_auth;
mod user_activity;
mod process_explorer;
mod views_welcome;
mod welcome_page;
pub mod walkthrough_part;
mod hover;
pub(crate) mod host_requests;
mod input_box_view;
mod keybindings_editor_view;
mod keyboard;
mod markdown_document;
mod markdown_view;
mod text_selection;
mod empty_view;
mod layout;
mod list_scroll;
mod search_list;
mod tree_guides;
mod local_history_actions;
mod document_highlights;
mod document_symbols;
mod goto_definition_link;
mod goto_symbol;
mod marker_navigation;
mod hierarchy_peek;
mod outline_find;
mod outline_pane;
mod peek_view;
mod references_peek;
pub mod menubar;
mod middle_scroll;
mod drag_scrolling;
mod minimap_view;
mod model_events;
pub mod logs_actions;
pub mod interactive;
mod kernel_picker;
mod notebook_editor;
mod notebook_variables;
mod repl_notebook;
mod notebook_outline;
mod notebook_status_bar;
mod notebooks;
pub(crate) use notebooks::notebook_cells;
mod notifications_view;
mod panel;
mod part_grid;
mod part_view;
mod zen_mode;
mod activity;
mod centered_layout;
pub mod output_actions;
mod output_view;
mod view_filter;
mod preferences;
pub(crate) mod terminal_view;
pub(crate) mod opener;
mod terminal_host;
pub(crate) mod terminal_editor;
mod external_terminal;
mod terminal_contrib;
mod terminal_find;
mod terminal_links;
mod terminal_suggest;
mod editor_quick_access;
mod terminal_quick_access;
mod terminal_history;
mod terminal_decorations;
mod terminal_environment;
mod terminal_notifications;
mod terminal_initial_hint;
mod terminal_quick_fix;
mod terminal_resize_overlay;
mod output_compound;
mod terminal_sticky_scroll;
mod task_configure;
pub mod task_matcher_prompt;
pub mod task_quick_pick;
pub(crate) mod task_service;
pub mod quick_access;
mod quick_open;
mod readonly;
mod search_actions;
mod side_bar;
mod view_locations;
mod view_panes;
mod search_editor_view;
mod search_editor_widget;
mod search_view;
mod scroll_locking;
mod settings_editor_view;
mod status_entries;
mod transition;
mod parameter_hints;
mod action_widget;
mod commenting_ranges;
mod inline_progress;
mod paste_edits;
mod code_actions;
mod code_lens;
mod color_detector;
mod inline_completions;
mod language_detection;
pub mod snippet_actions;
mod emmet;
mod tab_completion;
mod open_editors_view;
mod outline_actions;
mod inlay_hints_accessibility;
mod comment_navigation;
mod comment_node;
mod comment_thread_floating;
mod comment_thread_floating_layout;
mod comment_thread_widget;
pub mod comments;
mod comments_view;
mod color_picker;
mod standalone_color_picker;
mod inlay_hints;
mod debug_inline_values;
mod debug_hover;
mod debug_console_find;
mod rename;
mod links;
mod selection_ranges;
mod semantic_tokens;
mod linked_editing;
mod language_status;
mod format;
mod snippet_completions;
mod snippet_controller;
mod suggest_controller;
mod suggest_widget;
use status_entries::{EDITOR_STATUS_PRIORITY, PROBLEMS_PRIORITY};
pub mod themes;
mod quick_input_view;
mod text_file_editor_tracker;
mod text_files;
mod trusted_publishers;
pub(crate) mod text_model_resolver;
mod control_characters;
mod editor_placeholder;
mod large_file;
mod inspect_tokens;
mod gutter_mouse;
mod word_wrap;
mod smooth_scrolling;
mod sticky_outline;
mod save_conflict;
mod editor_state;
mod editor_group_watermark;
mod empty_editor_hint;
mod text_input;
mod timeline_view;
mod title_bar_actions;
use title_bar_actions::TITLE_BAR_LEADING_GROUP;
mod type_navigation;
pub(crate) mod unattended_restart;
mod unicode_highlight;
mod unusual_line_terminators;
pub(crate) mod scm_actions;
pub mod scm_graph_view;
mod scm_repositories_view;
mod scm_tree;
mod scm_view;
mod tree_view;
mod file_decorations;
mod webview_views;
mod media_preview;
mod editor_insets;
mod image_carousel;
mod release_notes;
pub(crate) mod webview_panels;
mod custom_documents;
mod url_actions;
pub(crate) mod url_handler;
mod workspace_actions;
mod workspace_editing;
mod workspace_trust_editor;
mod workspace_trust_view;
use std::cell::Cell;
use std::collections::{HashMap, HashSet};
use std::ops::Range;
use std::path::PathBuf;
use std::rc::Rc;
use std::sync::{Arc, Mutex};
use config::service::Settings;
use config::workspace::Workspace;
use storage::Storage;
use gpui::{ClickEvent, Context, Div, DragMoveEvent, ExternalPaths, FocusHandle, FontFeatures, FontWeight, MouseButton, MouseDownEvent, Rgba, Stateful, Task, Window, div, prelude::*, px, rgb, svg, transparent_black};
use serde_json::Value;
use crate::commands::Commands;
use crate::contextkey;
use crate::keybindings::{OS, Os};
use crate::menus::MenuId;
use crate::editor::EditorGroup;
use crate::explorer::FileTree;
use explorer_actions::DraggedItems;
use hover::{HoverContent, HoverOptions, HoverPosition};
use search_view::SideBarView;
use crate::file_icons::{FileIconTheme, FolderIcon};
use crate::host_sync::HostSync;
use crate::languages::Languages;
use crate::theme::{ColorThemeEntry, Theme, ThemeKind};
/// commandCenterControl.ts's toolbar: the navigation buttons and the center.
const COMMAND_CENTER: &str = "title-command-center";
/// titlebarPart.ts `actionToolBar`: the editor, layout and global actions.
const TITLE_ACTIONS: &str = "title-actions";
const ACTIVITY_ITEM_GROUP: &str = "activity-item";
const TITLE_BAR_TOOLBARS: [&str; 3] = [COMMAND_CENTER, update_title_bar::TOOLBAR, TITLE_ACTIONS];
pub struct Workbench {
pub theme: Theme,
/// The contributed color themes the theme picker offers.
pub color_themes: Vec<ColorThemeEntry>,
/// The extensions the host runs, whose contributions the workbench reads.
pub extensions: Vec<exthost::scan::ScannedExtension>,
/// The repo root: codicon SVGs are read from upstream's own files under it.
pub app_root: PathBuf,
/// The opened folder; `None` is the empty window.
pub folder: Option<FileTree>,
pub languages: Languages,
/// `None` draws files without icons, as `workbench.iconTheme: null` does.
pub file_icons: Option<FileIconTheme>,
/// `None` is the Default product icon theme: the codicons.
pub product_icons: Option<crate::product_icons::ProductIconTheme>,
pub editors: EditorGroup,
/// The window's settings, shared with the extension host thread that reloads them on file changes.
pub settings: Arc<Mutex<Settings>>,
pub(crate) host: HostSync,
pub commands: Commands,
/// MainThreadCommands `_commandRegistrations`: the host's registration of each id it registered.
pub(crate) extension_commands: HashMap<String, crate::commands::Registration>,
/// Extension tests run: dialogs answer their default without showing (abstractFileDialogService.ts skipDialogs).
pub skip_dialogs: bool,
/// product.json `extensionsGallery.serviceUrl` of the installs; tests point it at a local gallery.
pub(crate) gallery_url: String,
pub host_colors: themes::HostColorScheme,
/// The Dock icon last set for `window.appIcon`; `None` is the bundle's, what the app starts with.
pub app_icon: Option<&'static [u8]>,
/// The update tooltip's logo, decoded once per `window.appIcon` look and appearance.
pub app_logo: Arc<gpui::Image>,
/// `--wait` of command lines that opened files here, until their editors close.
wait_files: Vec<command_line::WaitFiles>,
/// When the window finished restoring (`LifecyclePhase.Restored`).
pub restored_at: Option<std::time::Instant>,
/// The blockers `can_restart_unattended` logged last.
unattended_restart_logged: String,
/// The group and path of each editor `release_closed_wait_files` saw last, to tell a close from a move.
wait_shown: Vec<(u32, PathBuf)>,
/// `--wait` markers of command lines without files: deleted when this window closes.
wait_markers: Vec<PathBuf>,
/// `IEnvironmentService.isExtensionDevelopment`.
pub extension_development: bool,
/// The window's `--extensionDevelopmentPath` and `--extensionTestsPath`, kept for a host restart and a reload.
pub development: Option<exthost::extension_development::ExtensionDevelopment>,
/// The window's `--inspect[-brk]-extensions`, `--debugId` and `--extensionEnvironment`.
pub extension_host_debug: exthost::extension_development::ExtensionHostDebug,
/// Extension hosts started in this window: the exit of one that a restart replaced is not the window's.
pub extension_host_starts: u32,
/// How `open_window` runs this window's hosts; `None` in a workbench a test builds, whose restart starts no host.
pub(crate) host_launch: Option<crate::extension_host_start::HostLaunch>,
/// extensionsProposedApi.ts of this window, for the extensions a host restart scans again.
pub proposed_api: exthost::proposed_api::ProposedApi,
proxy_auth: proxy_auth::ProxyAuth,
/// The command line profile this window was opened with, kept by a reload as the window's configuration is.
pub cli_profile: crate::CliProfile,
/// workbench.ts `setUnexpectedErrorHandler(createUnexpectedErrorHandler(logService))`.
pub unexpected_errors: crate::output::logger::UnexpectedErrorHandler,
pub keybindings: keyboard::Keybindings,
pub keybindings_editor: keybindings_editor_view::KeybindingsEditor,
pub trust_editor: workspace_trust_editor::TrustEditor,
pub settings_editor: settings_editor_view::SettingsEditor,
keybinding_marks: define_keybinding::KeybindingMarks,
unsupported_settings_render: Option<Task<()>>,
diagnostics: diagnostics::Diagnostics,
accessibility: accessibility::AccessibilityState,
metered: power::MeteredConnection,
relauncher: relauncher::Relauncher,
/// The update service's last state (update.ts `UpdateContribution.state`).
update_state: crate::update::State,
update_title_bar: update_title_bar::UpdateTitleBar,
user_activity: user_activity::UserActivity,
welcome: welcome_page::WelcomePage,
playground: walkthrough_part::Playground,
/// Context keys set with the `setContext` command.
context_keys: HashMap<String, Value>,
pub output: crate::output::Output,
output_view: output_view::OutputView,
terminal_view: terminal_view::TerminalView,
tasks: task_service::TaskService,
/// The status bar message and the task that hides it.
status_message: Option<(String, Task<()>)>,
scrollbars: editor_view::Scrollbars,
fold_controls: editor_view::FoldControls,
sticky_scroll: editor_view::sticky_scroll_view::StickyScroll,
empty_editor_hint: empty_editor_hint::EmptyEditorHint,
minimap: minimap_view::Minimap,
code_editors: code_editors::CodeEditors,
cursor_blink: std::cell::RefCell<editor_view::cursor_blink::CursorBlink>,
unicode_highlight: unicode_highlight::UnicodeHighlight,
unusual_line_terminators: unusual_line_terminators::UnusualLineTerminators,
auto_save: auto_save::AutoSave,
text_files: text_files::TextFiles,
settings_watcher: configuration::SettingsWatcher,
find: find_widget::FindWidget,
/// The line and whether on its folding control a press may toggle a fold (folding.ts `mouseDownInfo`).
fold_mouse_down: Option<(usize, bool)>,
/// Window x of the text area, recorded at paint, to turn a click into a column.
text_area_left: Rc<Cell<f32>>,
/// The text area's window bounds at paint, where content widgets that may overflow it lay out.
text_area_bounds: Rc<Cell<gpui::Bounds<f32>>>,
editor_message: editor_message::EditorMessages,
inspect_tokens: inspect_tokens::InspectTokens,
marker_navigation: marker_navigation::MarkerNavigation,
goto_symbol: goto_symbol::GotoSymbol,
definition_link: goto_definition_link::DefinitionLink,
text_selection: text_selection::TextSelection,
document_highlights: document_highlights::DocumentHighlights,
document_symbols: document_symbols::DocumentSymbols,
references_peek: references_peek::ReferencesPeek,
outline_pane: outline_pane::OutlinePane,
open_editors_focus: FocusHandle,
/// The Open Editors list's focused row by position, moved by the keyboard; `None` follows the active editor
/// (openEditorsView.ts `focusActiveEditor` on each refresh).
open_editors_focused_row: Option<usize>,
open_editors_scroll: gpui::ScrollHandle,
hierarchy_peek: hierarchy_peek::HierarchyPeek,
content_hover: content_hover::ContentHover,
suggest: suggest_controller::Suggest,
parameter_hints: parameter_hints::ParameterHints,
code_actions: code_actions::CodeActions,
action_widget: action_widget::ActionWidget,
paste_edits: paste_edits::PasteEdits,
code_lenses: code_lens::CodeLenses,
inlay_hints: inlay_hints::InlayHints,
debug_inline_values: debug_inline_values::DebugInlineValues,
debug_hover: debug_hover::DebugHover,
disassembly_view: disassembly_view::DisassemblyView,
color_detector: color_detector::ColorDetector,
inline_completions: inline_completions::InlineCompletions,
language_detection: language_detection::LanguageDetection,
rename: rename::Rename,
links: links::Links,
semantic_tokens: semantic_tokens::SemanticTokens,
linked_editing: linked_editing::LinkedEditing,
language_status: language_status::LanguageStatus,
formatting: format::Formatting,
snippet_files: snippet_completions::SnippetFiles,
/// The snippet choice the controller last showed the suggest widget for.
snippet_choice_shown: Option<(crate::editor::Position, crate::editor::Position)>,
/// The providers extensions registered through MainThreadLanguageFeatures.
pub(crate) language_features: crate::language_features::LanguageFeatures,
folding_providers: folding_providers::FoldingProviders,
sticky_outline: sticky_outline::StickyOutline,
cancellable_operations: editor_state::CancellableOperations,
readonly_overrides: readonly::SessionOverrides,
/// The editor labels of opened local history entries (`openEntry`), by model path.
local_history_labels: HashMap<PathBuf, String>,
/// localHistoryCommands.ts `itemSelectedForCompare`: the timeline item of Select for Compare.
local_history_selected_for_compare: Option<Value>,
pub(crate) urls: url_handler::Urls,
/// fileService.ts `hasProvider`: the schemes extensions registered file system providers for.
pub(crate) file_system_providers: HashSet<String>,
pub editor_focus: FocusHandle,
/// multiDiffEditorFileList.ts: the multi diff editor's changed files list.
multi_diff_file_list_focus: FocusHandle,
/// multiEditorTabsControl.ts: the active group's active tab, the only tab with `tabIndex = 0`.
editor_tab_focus: FocusHandle,
/// bannerController.ts: the banner element is focusable (`tabIndex = 0`) inside the editor's DOM node.
unicode_banner_focus: FocusHandle,
status_bar_focus: FocusHandle,
/// contentHoverWidget.ts: the hover's container is focusable (`tabIndex = 0`) inside the editor's DOM node.
content_hover_focus: FocusHandle,
debug_hover_focus: FocusHandle,
/// standaloneColorPickerWidget.ts: the standalone color picker's body is focusable (`tabIndex = 0`) inside the editor's DOM node.
standalone_color_picker_focus: FocusHandle,
/// hoverWidget.ts: the workbench hover's container is focusable (`tabIndex = 0`).
workbench_hover_focus: FocusHandle,
/// `editor.tabFocusMode` when Toggle Tab Key Moves Focus last ran, and the mode it set.
tab_focus_toggle: Option<(bool, bool)>,
/// inputMode.ts `InputMode` is `overtype` instead of `insert`: one mode for every editor, starting in insert.
overtype: bool,
editor_focus_listeners: Vec<gpui::Subscription>,
/// `--screenshot` hides the input carets, as ref-screenshot does in the reference, so the blink phase drops out.
pub(crate) input_carets_hidden: bool,
/// The IME's marked text: a UTF-16 range of the cursor's line.
marked_text: Option<Range<usize>>,
/// A left or middle press in the text or the gutter is held, so moves extend the selection.
selecting_text: Option<editor_view::MouseDown>,
text_drag: Option<editor_view::drag_and_drop::TextDrag>,
/// editorOptions.ts `stopRenderingLineAfter` (10000) of the group's code editor; longLinesHelper.ts clears it.
stop_rendering_line_after: Option<usize>,
editor_zoom: crate::editor::zoom::EditorZoom,
middle_scroll: Option<middle_scroll::MiddleScroll>,
drag_scrolling: Option<drag_scrolling::DragScrolling>,
smooth_scrolling: Option<smooth_scrolling::SmoothScrolling>,
pub quick_inputs: quick_input_view::QuickInputs,
pub quick_access: quick_access::QuickAccess,
pub explorer: explorer_actions::ExplorerState,
pub side_bar_view: search_view::SideBarView,
pub search: search_view::SearchView,
list_scrolls: list_scroll::ListScrolls,
pub search_editors: search_editor_view::SearchEditors,
pub search_editor_widget: search_editor_widget::SearchEditorWidget,
pub webview_panels: webview_panels::WebviewPanels,
notebook_views: notebook_editor::NotebookViews,
notebook_variables: notebook_variables::NotebookVariables,
/// breadcrumbsControl.ts `_updateEditorTypeControl` per group: computed when the group's active editor (or the
/// two settings) changes, keyed by that state, never when extensions register editors afterwards.
editor_type_controls: std::cell::RefCell<HashMap<u32, (String, Option<editor_type_picker::EditorTypes>)>>,
/// The previous session's custom editors (contributedCustomEditors.ts memento) until the extensions register theirs.
memento_custom_editors: Option<Vec<custom_editors::Contribution>>,
pub scm: scm_view::ScmView,
timeline: timeline_view::TimelineView,
scm_graph: scm_graph_view::ScmGraph,
tree_views: tree_view::TreeViews,
decorations: std::cell::RefCell<crate::decorations::Decorations>,
quick_diffs: editor_view::quick_diff::QuickDiffs,
pub(crate) github_clients: crate::main_thread_github::GitHubClients,
/// `contributes.commands` icons, for the view toolbars.
pub command_icons: HashMap<String, crate::menus::CommandIcon>,
pub submenu_icons: HashMap<crate::menus::MenuId, crate::menus::CommandIcon>,
webview_views: webview_views::WebviewViews,
pub extensions_viewlet: extensions_view::ExtensionsViewlet,
pub extension_editors: Vec<extension_editor::ExtensionEditor>,
/// `MenuRegistry`.
pub menus: Vec<crate::menus::MenuItem>,
pub(crate) context_menu: Option<context_menu::ContextMenu>,
/// The activity bar's Manage button, the anchor of its menu.
manage_bounds: Rc<Cell<gpui::Bounds<gpui::Pixels>>>,
/// Each view container title's `...`, the anchor of its menu.
container_more_bounds: Rc<std::cell::RefCell<HashMap<&'static str, gpui::Bounds<gpui::Pixels>>>>,
/// The container whose `...` opened the menu, with that menu's `context_menus_shown`.
container_more_menu: Option<(&'static str, usize)>,
context_menus_shown: usize,
pub(crate) notifications: notifications_view::NotificationsUi,
/// Extension status bar entries, in statusbarModel.ts order.
pub(crate) status_entries: Vec<crate::main_thread_status_bar::StatusEntry>,
/// The status bar's entries as last drawn, hidden ones included, for its context menu (statusbarModel.ts `entries`).
drawn_status_entries: std::cell::RefCell<Vec<status_entries::DrawnStatusEntry>>,
/// statusbarpart.css `transition: background-color 0.15s ease-out`, as debugging starts and stops.
status_bar_background: std::cell::Cell<Option<transition::Transition<4>>>,
/// Each window's drawn entries, ids and bounds in the bar's order, for statusbarModel.ts `focusNextEntry`.
status_entry_bounds: Rc<std::cell::RefCell<Vec<(gpui::WindowId, String, gpui::Bounds<gpui::Pixels>)>>>,
/// statusbarModel.ts `getFocusedEntry`: per window, as each window has its own status bar part.
status_entry_focus: HashMap<gpui::WindowId, String>,
action_bars: action_bar::ActionBars,
pub hovers: hover::Hovers,
/// What the application menu shows now.
installed_menubar: Option<menubar::Menubar>,
/// menubarControl.ts `menuUpdater`, pending.
menubar_update: Option<Task<()>>,
/// `BoundModelReferenceCollection`'s timeout: the next host reference to expire, and the task that releases it.
host_model_release: Option<(std::time::Instant, Task<()>)>,
/// StorageScope.PROFILE of the current profile, shared with the extension host's MainThreadStorage.
pub(crate) profile_storage: Arc<Mutex<Storage>>,
/// StorageScope.APPLICATION: the default profile's storage, the same as `profile_storage` in that profile.
application_storage: Arc<Mutex<Storage>>,
/// StorageScope.WORKSPACE of the opened folder, shared with the extension host's MainThreadStorage.
pub(crate) workspace_storage: Arc<Mutex<Storage>>,
pub(crate) user_data_profiles: user_data_profile::UserDataProfiles,
icon_select: icon_select_box::IconSelect,
part_grid: part_grid::PartGrid,
part_views: HashMap<part_view::CachedPart, gpui::Entity<part_view::PartView>>,
zen_mode: zen_mode::ZenMode,
activities: activity::Activities,
centered_layout: centered_layout::CenteredLayout,
side_by_side_splits: side_by_side_editor::SideBySideSplits,
editor_type_picker: editor_type_picker::EditorTypePicker,
breadcrumbs: breadcrumbs::Breadcrumbs,
edit_setting: preferences::edit_setting::EditSetting,
editor_groups: editor_groups::EditorGroups,
auxiliary_windows: auxiliary_windows::AuxiliaryWindows,
history: history_actions::History,
panel: panel::Panel,
explorer_panes: view_panes::ContainerPanes,
scm_panes: view_panes::ContainerPanes,
/// The pane views of the containers generated for moved views, by container id.
generated_panes: HashMap<&'static str, view_panes::ContainerPanes>,
/// The panes of moved views, built with `&mut self` before a container draws them.
moved_view_panes: std::cell::RefCell<Vec<view_panes::ViewPane>>,
pub workspace_trust: crate::workspace_trust::WorkspaceTrust,
/// fileCommands.ts `globalResourceToCompare`, set by Select for Compare (`resourceSelectedForCompare`).
resource_to_compare: Option<crate::diff_editor::Side>,
/// fileActions.ts `CompareWithClipboardAction.SCHEME_COUNTER`.
clipboard_compare_count: u32,
/// The APPLICATION_SHARED storage: trust and the recently opened list.
pub shared_storage: Arc<Mutex<Storage>>,
/// The recently opened list as last read, which the menu bar shows.
recently_opened: crate::recently_opened::RecentlyOpened,
/// Custom dialogs, the shown one first.
dialogs: Vec<dialog_view::ShownDialog>,
lifecycle: lifecycle::Lifecycle,
text_file_editor_tracker: text_file_editor_tracker::TextFileEditorTracker,
banner: banner::Banner,
pub debug: debug_service::DebugService,
debug_view: debug_view::DebugView,
debug_console: debug_console::DebugConsole,
problems: problems_view::ProblemsView,
problems_quick_fixes: problems_quick_fix::ProblemsQuickFixes,
/// scrollLocking.ts `paneInitialScrollTop`: each visible code editor's model id and its scroll position when tracked.
locked_scroll_panes: Vec<(u32, crate::editor::Scroll)>,
/// quickPickPin.ts pins by storage key as this session last stored them, with the `id` storage drops.
session_pins: HashMap<String, Vec<Value>>,
comments: comments::Comments,
problems_focus: FocusHandle,
refactor_preview: refactor_preview::RefactorPreview,
breakpoint_widget: Option<breakpoint_widget::BreakpointWidget>,
pub testing: testing_service::TestingService,
testing_view: testing_view::TestingView,
testing_peek: Option<testing_peek::TestingPeek>,
multi_diff_layout_logs: Vec<editor_view::multi_diff_editor_view::MultiDiffLayoutLog>,
multi_diff_sash: editor_view::multi_diff_editor_view::MultiDiffSash,
}
impl Render for Workbench {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
// Cursors move and editors switch on many paths; every one of them renders.
self.forget_painted_hover_targets();
self.close_editor_message_on_cursor_change(cx);
self.stop_inspect_tokens_on_model_change();
self.follow_inspect_tokens_cursor(cx);
self.follow_marker_navigation_cursor();
self.follow_references_peek_editor();
self.follow_hierarchy_peek_editor(window, cx);
self.sync_stack_frame_columns();
self.sync_inline_breakpoints(cx);
self.sync_inline_coverage(cx);
// The floating widgets sit on the rows of each file's diff, recomputed after an edit.
self.update_multi_diff_editor();
self.append_multi_diff_layout_logs();
self.lay_out_floating_comment_widgets(window);
self.follow_symbol_navigation();
self.follow_definition_link(cx);
self.refresh_selection_highlights(window);
self.refresh_document_highlights(window, cx);
self.refresh_outline_pane(cx);
self.mark_debug_views_created();
self.update_webview_panels(window, cx);
self.update_notebook_views(window, cx);
self.update_diff_editor();
self.update_merge_editor(cx);
self.update_diff_symbols(cx);
self.update_multi_diff_symbols(cx);
self.update_quick_diff(cx);
self.follow_active_editor_repository();
self.update_timeline(cx);
self.update_scm_graph(cx);
self.update_extension_editor_webviews(window, cx);
self.auto_save_models_changed(cx);
self.backup_models_changed(cx);
self.track_dirty_text_files(cx);
self.delete_saved_wait_markers();
self.send_notebook_visible_ranges();
self.render_notebook_cell_lists();
self.follow_content_hover_editor(window, cx);
self.follow_debug_hover_editor(window, cx);
self.follow_standalone_color_picker_focus(window, cx);
self.follow_hover_accessible_view(window, cx);
self.follow_rename_editor_state(window, cx);
self.folding_model_changed(cx);
self.sticky_outline_frame(cx);
self.accessibility_frame(cx);
self.update_metered_connection(cx);
self.check_relaunch_settings(window, cx);
self.sync_update_settings(cx);
self.welcome_page_frame(window, cx);
self.follow_customize_layout(window, cx);
let viewport = window.viewport_size();
let window_border = self.theme.color("window.activeBorder");
// The reference lays the grid out at the full window size and shifts it inside the 1px window border.
let inset = if window_border.is_some() { 1. } else { 0. };
self.sync_editors(cx);
self.auxiliary_windows.main_window = Some(window.window_handle());
self.close_auxiliary_windows_of_closed_parts(cx);
// The window title follows the main part's active editor (windowTitle.ts is scoped to the window's part).
let main_group = self.part_active_group(editor_groups::MAIN_PART).expect("the main part has a group");
let status_bar = self.is_status_bar_visible().then(|| self.part_view(part_view::CachedPart::StatusBar, cx));
let grid = div()
.absolute()
.left(px(inset))
.top(px(inset))
.w(viewport.width)
.h(viewport.height)
.flex()
.flex_col()
.child(self.part_view(part_view::CachedPart::TitleBar, cx))
.children(self.banner_part(window, cx))
.child(self.middle_section(viewport, window, cx))
.children(status_bar);
let modal_editor = self.modal_group_widget(window, cx);
let profile_icon_picker = self.profile_icon_picker(window, cx);
// The overlays show in the active window of the workbench, this one unless an editor window is.
let overlays_here = self.auxiliary_windows.active().is_none();
let quick_input = if overlays_here { self.quick_input_widget(window, cx) } else { None };
let notifications = self.notifications_widget(viewport, window, cx);
let context_menu = if overlays_here { self.context_menu_widget(cx) } else { None };
let action_widget = if overlays_here { self.action_widget(window, cx) } else { None };
let hover = if overlays_here { self.hover_widget(window, cx) } else { None };
let dialog = if overlays_here { self.custom_dialog_widget(window, cx) } else { None };
let accessible_view = self.accessible_view_widget(window, cx);
self.schedule_menubar_update(window, cx);
self.with_group(main_group, |workbench| workbench.sync_window_title(window));
// The labels drawn above asked for the decorations they lack.
self.send_decoration_requests(window.current_view(), cx);
div()
.relative()
.size_full()
.overflow_hidden()
.bg(self.shell_background(window.is_window_active()))
.text_color(self.color("foreground"))
.text_size(px(13.))
.capture_key_up(cx.listener(|workbench, _: &gpui::KeyUpEvent, _, cx| workbench.reset_keybinding_hold_mode(cx)))
.on_key_down(cx.listener(Self::tab_key_down))
.child(grid)
.children(self.debug_toolbar(window, cx))
.children(self.breadcrumbs_picker_widget(window, cx))
.when(self.debug_view.toolbar_drag.is_some(), |root| {
root.on_mouse_move(cx.listener(|workbench, event, window, cx| workbench.drag_debug_toolbar(event, window, cx)))
.on_mouse_up(gpui::MouseButton::Left, cx.listener(|workbench, _, _, _| workbench.end_debug_toolbar_drag()))
})
.children(modal_editor)
.children(profile_icon_picker)
// The quick input lives in the workbench container, inside the window border as the grid.
.children(quick_input.map(|widget| div().absolute().left(px(inset)).top(px(inset)).child(widget)))
.children(notifications)
.children(accessible_view)
.children(context_menu)
.children(dialog)
.children(action_widget)
// Above the editor's deferred content widgets (priority 1), as the debug hover's eye button's "Click to expand".
.children(hover.map(|hover| gpui::deferred(hover).with_priority(2)))
.child(self.cross_window_drag(cx))
.on_modifiers_changed(cx.listener(|workbench, event: &gpui::ModifiersChangedEvent, window, cx| {
workbench.hover_modifiers_changed(event.modifiers.alt, cx);
workbench.terminal_link_modifiers_changed(event.modifiers, cx);
// The debug toolbar's items swap to their alt commands while Alt is held.
if workbench.debug.state() != crate::debug::session::State::Inactive {
cx.notify();
}
if let Some(input) = workbench.quick_inputs.current_mut() {
input.modifiers_changed(event.modifiers);
workbench.flush_quick_input_events(window, cx);
}
}))
.child(self.aria_live_region())
.capture_any_mouse_down(cx.listener(|workbench, _, _, cx| workbench.user_activity(cx)))
// style.css: .macos-tahoe windows round 16px, older macOS 10px.
.when_some(window_border, |root, border| root.child(div().absolute().inset_0().border_1().rounded(px(if is_tahoe_or_newer() { 16. } else { 10. })).border_color(border)))
}
}
impl Workbench {
pub fn new(theme: Theme, app_root: PathBuf, workspace: Option<Workspace>, languages: Languages, settings: Arc<Mutex<Settings>>, host: HostSync, cx: &mut Context<Self>) -> Self {
let accessibility = accessibility::AccessibilityState::new(&app_root, cx);
let mut workbench = Workbench {
theme,
color_themes: Vec::new(),
extensions: Vec::new(),
app_root,
folder: workspace.map(|workspace| FileTree::new(workspace, settings.clone())),
languages,
file_icons: None,
product_icons: None,
editors: EditorGroup::default(),
settings,
host,
commands: Commands::default(),
extension_commands: HashMap::new(),
skip_dialogs: false,
gallery_url: exthost::gallery::SERVICE_URL.into(),
host_colors: themes::host_color_scheme(cx),
app_icon: None,
app_logo: themes::logo_image("default", false),
wait_files: Vec::new(),
restored_at: None,
unattended_restart_logged: String::new(),
wait_shown: Vec::new(),
wait_markers: Vec::new(),
extension_development: false,
development: None,
extension_host_debug: Default::default(),
extension_host_starts: 0,
host_launch: None,
proposed_api: exthost::proposed_api::ProposedApi::unbuilt(),
proxy_auth: proxy_auth::ProxyAuth::default(),
cli_profile: Default::default(),
unexpected_errors: Default::default(),
keybindings: keyboard::Keybindings::default(),
keybindings_editor: keybindings_editor_view::KeybindingsEditor::new(cx),
trust_editor: Default::default(),
settings_editor: settings_editor_view::SettingsEditor::new(cx),
keybinding_marks: Default::default(),
unsupported_settings_render: None,
diagnostics: Default::default(),
accessibility,
metered: Default::default(),
relauncher: Default::default(),
update_state: crate::update::State::Uninitialized,
update_title_bar: Default::default(),
user_activity: Default::default(),
welcome: Default::default(),
playground: Default::default(),
context_keys: HashMap::new(),
output: Default::default(),
output_view: output_view::OutputView::new(cx),
terminal_view: terminal_view::TerminalView::new(cx),
tasks: Default::default(),
status_message: None,
scrollbars: Default::default(),
fold_controls: Default::default(),
sticky_scroll: editor_view::sticky_scroll_view::StickyScroll::new(cx),
empty_editor_hint: empty_editor_hint::EmptyEditorHint::default(),
minimap: Default::default(),
code_editors: Default::default(),
cursor_blink: Default::default(),
unicode_highlight: Default::default(),
unusual_line_terminators: Default::default(),
auto_save: Default::default(),
text_files: Default::default(),
settings_watcher: Default::default(),
find: find_widget::FindWidget::new(cx),
fold_mouse_down: None,
text_area_left: Rc::default(),
text_area_bounds: Rc::default(),
editor_message: Default::default(),
inspect_tokens: Default::default(),
marker_navigation: Default::default(),
goto_symbol: Default::default(),
definition_link: Default::default(),
text_selection: Default::default(),
document_highlights: Default::default(),
document_symbols: Default::default(),
references_peek: references_peek::ReferencesPeek::new(cx),
outline_pane: outline_pane::OutlinePane::new(cx),
open_editors_focus: cx.focus_handle().tab_stop(true),
open_editors_focused_row: None,
open_editors_scroll: Default::default(),
hierarchy_peek: hierarchy_peek::HierarchyPeek::new(cx),
content_hover: Default::default(),
suggest: Default::default(),
parameter_hints: Default::default(),
code_actions: Default::default(),
action_widget: Default::default(),
paste_edits: Default::default(),
code_lenses: Default::default(),
inlay_hints: Default::default(),
debug_inline_values: Default::default(),
debug_hover: debug_hover::DebugHover::new(cx),
disassembly_view: disassembly_view::DisassemblyView::new(cx),
color_detector: Default::default(),
inline_completions: Default::default(),
language_detection: Default::default(),
rename: rename::Rename::new(cx),
links: links::Links::default(),
semantic_tokens: Default::default(),
linked_editing: Default::default(),
language_status: Default::default(),
formatting: Default::default(),
snippet_files: Default::default(),
snippet_choice_shown: None,
language_features: Default::default(),
folding_providers: Default::default(),
sticky_outline: Default::default(),
cancellable_operations: Default::default(),
readonly_overrides: Default::default(),
local_history_labels: HashMap::new(),
local_history_selected_for_compare: None,
urls: url_handler::Urls::default(),
file_system_providers: HashSet::new(),
editor_focus: cx.focus_handle().tab_stop(true),
multi_diff_file_list_focus: cx.focus_handle(),
editor_tab_focus: cx.focus_handle().tab_stop(true),
unicode_banner_focus: cx.focus_handle(),
banner: banner::Banner::new(cx),
status_bar_focus: cx.focus_handle().tab_stop(true),
content_hover_focus: cx.focus_handle(),
debug_hover_focus: cx.focus_handle(),
standalone_color_picker_focus: cx.focus_handle(),
workbench_hover_focus: cx.focus_handle(),
tab_focus_toggle: None,
overtype: false,
editor_focus_listeners: Vec::new(),
input_carets_hidden: false,
marked_text: None,
selecting_text: None,
text_drag: None,
stop_rendering_line_after: Some(10_000),
editor_zoom: Default::default(),
middle_scroll: None,
drag_scrolling: None,
smooth_scrolling: None,
quick_inputs: quick_input_view::QuickInputs::new(cx),
quick_access: quick_access::QuickAccess::default(),
explorer: explorer_actions::ExplorerState::new(cx),
side_bar_view: search_view::SideBarView::Explorer,
search: search_view::SearchView::new(cx),
list_scrolls: Default::default(),
search_editors: HashMap::new(),
search_editor_widget: search_editor_widget::SearchEditorWidget::new(cx),
webview_panels: Default::default(),
notebook_views: notebook_editor::NotebookViews::new(cx),
notebook_variables: Default::default(),
editor_type_controls: Default::default(),
memento_custom_editors: None,
scm: scm_view::ScmView::new(cx),
timeline: timeline_view::TimelineView::new(cx),
scm_graph: Default::default(),
tree_views: Default::default(),
decorations: Default::default(),
quick_diffs: Default::default(),
github_clients: Default::default(),
command_icons: HashMap::new(),
submenu_icons: HashMap::new(),
webview_views: Default::default(),
extensions_viewlet: extensions_view::ExtensionsViewlet::new(cx),
extension_editors: Vec::new(),
menus: crate::menus::workbench_menus(),
context_menu: None,
manage_bounds: Rc::default(),
container_more_bounds: Rc::default(),
container_more_menu: None,
context_menus_shown: 0,
notifications: notifications_view::NotificationsUi::new(cx),
status_entries: Vec::new(),
drawn_status_entries: Default::default(),
status_bar_background: Default::default(),
status_entry_bounds: Default::default(),
status_entry_focus: HashMap::new(),
action_bars: Default::default(),
hovers: hover::Hovers::default(),
installed_menubar: None,
menubar_update: None,
host_model_release: None,
profile_storage: Arc::new(Mutex::new(Storage::in_memory())),
application_storage: Arc::new(Mutex::new(Storage::in_memory())),
workspace_storage: Arc::new(Mutex::new(Storage::in_memory())),
user_data_profiles: Default::default(),
icon_select: Default::default(),
part_grid: part_grid::PartGrid::default(),
part_views: HashMap::new(),
zen_mode: Default::default(),
activities: Default::default(),
centered_layout: Default::default(),
side_by_side_splits: Default::default(),
editor_type_picker: Default::default(),
breadcrumbs: breadcrumbs::Breadcrumbs::new(cx),
edit_setting: Default::default(),
editor_groups: Default::default(),
auxiliary_windows: Default::default(),
history: Default::default(),
panel: panel::Panel::default(),
explorer_panes: view_panes::ContainerPanes::default(),
scm_panes: view_panes::ContainerPanes::default(),
generated_panes: HashMap::new(),
moved_view_panes: Default::default(),
workspace_trust: Default::default(),
resource_to_compare: None,
clipboard_compare_count: 0,
shared_storage: Arc::new(Mutex::new(Storage::in_memory())),
recently_opened: Default::default(),
dialogs: Vec::new(),
lifecycle: Default::default(),
text_file_editor_tracker: Default::default(),
debug: Default::default(),
debug_view: debug_view::DebugView::new(cx),
debug_console: debug_console::DebugConsole::new(cx),
problems: problems_view::ProblemsView::new(cx),
problems_quick_fixes: Default::default(),
locked_scroll_panes: Vec::new(),
session_pins: HashMap::new(),
comments: comments::Comments::new(cx),
problems_focus: cx.focus_handle().tab_stop(true),
refactor_preview: Default::default(),
breakpoint_widget: None,
testing: Default::default(),
testing_view: testing_view::TestingView::new(cx),
testing_peek: None,
multi_diff_layout_logs: Vec::new(),
multi_diff_sash: Default::default(),
};
crate::main_thread_commands::register_commands_documentation(&mut workbench);
workbench.editors.disposed.settings = Some(workbench.settings.clone());
workbench.register_workbench_actions();
workbench.register_notebook_commands();
workbench.update_cell_undo_redo_comparison_key();
workbench.register_interactive_commands();
workbench.register_repl_commands();
workbench.register_diff_editor_commands();
workbench.register_file_compare_commands();
workbench.register_diff_gutter_commands();
workbench.register_unchanged_region_commands();
workbench.register_scroll_locking_commands();
workbench.register_scm_view_mode_commands();
workbench.register_merge_editor_commands();
workbench.register_breadcrumbs_commands();
workbench.register_empty_editor_hint_commands();
workbench.register_multi_diff_editor_commands();
workbench.register_timeline_commands();
workbench.register_scm_commands();
workbench.register_scm_repositories_commands();
workbench.register_scm_actions();
workbench.register_text_file_commands();
workbench.register_save_conflict_commands();
workbench.register_cancel_operation_command();
workbench.register_large_file_commands();
workbench.register_media_preview_commands();
workbench.register_image_carousel_commands();
workbench.register_release_notes_commands();
workbench.register_inspect_tokens_commands();
workbench.register_word_wrap_commands();
workbench.register_editor_commands();
workbench.register_find_commands();
workbench.register_sticky_scroll_commands();
workbench.register_context_commands();
workbench.register_run_commands();
workbench.register_activity_bar_context_menu_commands();
workbench.register_quick_input_commands();
workbench.register_quick_access_commands();
workbench.register_theme_commands();
workbench.register_editor_status_commands();
workbench.register_status_bar_focus_commands();
workbench.register_title_bar_actions();
workbench.register_explorer_actions();
workbench.register_explorer_keyboard_commands();
workbench.register_explorer_find_commands();
workbench.register_type_navigation_commands();
workbench.register_list_scroll_commands();
workbench.register_search_actions();
workbench.register_layout_actions();
workbench.register_customize_layout_commands();
workbench.register_zen_mode_actions();
workbench.register_centered_layout_actions();
workbench.register_editor_group_actions();
workbench.register_editor_quick_access_commands();
workbench.register_editor_menu_commands();
workbench.register_webview_actions();
workbench.register_history_actions();
workbench.register_search_editor_actions();
workbench.register_search_editor_widget_actions();
workbench.register_file_participant_commands();
workbench.register_keybindings_editor_commands();
workbench.register_settings_editor_commands();
workbench.register_readonly_commands();
workbench.register_preferences_commands();
workbench.register_side_by_side_commands();
workbench.register_editor_message_commands();
workbench.register_content_hover_commands();
workbench.register_debug_hover_commands();
workbench.register_hover_commands();
workbench.register_suggest();
workbench.register_settings_suggest_commands();
workbench.register_parameter_hints();
workbench.register_code_actions();
workbench.register_code_lens();
workbench.register_inlay_hints();
workbench.register_color_detector();
workbench.register_standalone_color_picker();
workbench.register_inline_completions();
workbench.register_language_detection();
workbench.register_rename();
workbench.register_links();
workbench.register_linked_editing();
workbench.register_language_status();
workbench.update_markers_file_decorations();
workbench.register_semantic_tokens();
workbench.register_format();
workbench.register_snippet_completions();
workbench.register_snippet_actions();
workbench.register_emmet();
workbench.register_tab_completion();
workbench.register_outline_actions();
workbench.register_folding_providers();
workbench.register_inlay_hints_accessibility();
workbench.register_snippet_commands();
workbench.register_marker_navigation_commands();
workbench.register_quick_diff_commands();
workbench.register_goto_symbol_commands();
workbench.register_definition_link_commands();
workbench.register_document_highlight_commands();
workbench.register_document_symbol_commands();
workbench.register_references_peek_commands();
workbench.register_hierarchy_commands();
workbench.register_define_keybinding_command();
workbench.register_notifications_commands();
workbench.register_output_actions();
workbench.register_logs_actions();
workbench.register_terminal_actions();
workbench.register_terminal_editor_actions();
workbench.register_problems_panel_view();
workbench.register_output_panel_view();
workbench.register_debug_console_panel_view();
workbench.register_test_results_panel_view();
workbench.register_terminal_panel_view();
// mainThreadComments.ts registers the Comments container at order 10, after the others.
workbench.register_comments_panel_view();
workbench.register_refactor_preview();
workbench.register_perf_view();
workbench.register_accessibility_actions();
workbench.start_system_events(cx);
workbench.user_activity(cx);
workbench.register_extension_management_commands();
workbench.register_trusted_publisher_commands();
workbench.register_extension_host_commands();
workbench.register_user_data_profile_commands();
workbench.register_profiles_editor_commands();
workbench.register_settings_import_commands();
workbench.register_workspace_trust_commands();
workbench.register_workspace_trust_editor_commands();
workbench.register_host_commands();
workbench.register_activation_error_commands();
workbench.register_extension_bisect_commands();
workbench.register_workspace_commands();
workbench.register_workspace_editing_commands();
workbench.register_dialog_commands();
#[cfg(unix)]
workbench.register_shell_command_commands();
workbench.register_lifecycle_commands();
workbench.register_banner_commands();
workbench.register_url_commands();
workbench.register_local_history_commands();
workbench.register_extensions_view_commands();
workbench.register_view_focus_commands();
workbench.register_part_navigation_commands();
workbench.register_move_view_actions();
workbench.register_workspace_recommendations_commands();
workbench.register_localization_commands();
workbench.register_new_file_commands();
workbench.register_welcome_page_commands();
workbench.register_playground_commands();
workbench.register_process_explorer_commands();
workbench.register_auxiliary_window_commands();
workbench.register_debug_commands();
workbench.register_debug_console_commands();
workbench.register_breakpoint_widget_commands();
workbench.register_testing_commands();
workbench.register_testing_peek_commands();
// Keybindings resolve before the focused element sees the key, and with nothing focused too.
let entity = cx.entity().downgrade();
cx.intercept_keystrokes(move |event, window, cx| {
// Every window's keys come here: this workbench takes those of its own windows.
if auxiliary_windows::window_workbench(window, cx).is_none_or(|owner| owner.entity_id() != entity.entity_id()) {
return;
}
let _ = entity.update(cx, |workbench, cx| {
workbench.user_activity(cx);
workbench.dispatch_key(&event.keystroke, window, cx)
});
})
.detach();
cx.on_app_quit(|workbench, _| {
workbench.store_explorer_view_state();
workbench.store_scm_repositories_view_state();
workbench.store_scm_view_state();
workbench.store_editor_view_states();
workbench.save_editor_part_state();
workbench.persist_terminal_state();
workbench.store_editor_resolver_cache();
workbench.store_active_side_bar_container();
async {}
})
.detach();
cx.on_release(|workbench, _| {
workbench.store_explorer_view_state();
workbench.store_scm_repositories_view_state();
workbench.store_scm_view_state();
workbench.store_editor_view_states();
workbench.store_editor_resolver_cache();
workbench.store_active_side_bar_container();
})
.detach();
workbench.customize_theme();
workbench.editors.set_previews_disabled(workbench.setting("workbench.editor.enablePreview") == false);
workbench
}
/// Switches to the profile's and the application's storage and reads the command history, the quick input
/// position and the notification filters from them.
pub fn restore_profile_state(&mut self, profile: Arc<Mutex<Storage>>, application: Arc<Mutex<Storage>>) {
self.profile_storage = profile;
self.application_storage = application;
self.load_command_history();
self.load_quick_input_view_state();
self.load_notification_filters();
self.leave_moved_side_bar_container();
}
/// The opened folders, which quick pick items show their resource paths relative to.
pub fn workspace(&self) -> Option<&Workspace> {
self.folder.as_ref().map(|tree| &tree.workspace)
}
/// A single click opens a preview editor and keeps the focus in the Explorer, a double click opens a pinned
/// one and focuses it (`workbench.editor.enablePreview`).
pub fn open_file(&mut self, path: PathBuf, pinned: bool, window: &mut Window, cx: &mut Context<Self>) {
self.open_disk_resource("file", path, pinned, window, cx);
}
/// `open_file` of a resource of a disk provider (`on_disk`): a file or a user data file.
pub(super) fn open_disk_resource(&mut self, scheme: &str, path: PathBuf, pinned: bool, window: &mut Window, cx: &mut Context<Self>) {
let revealed = self.reveal_group(&Value::Null, |editor| editor.document().is(scheme, &path));
if revealed.is_none() && self.enter_modal_group_for_all(window, cx) {
return self.open_file_in_active_group(scheme, path, pinned, window, cx);
}
// editorGroupFinder.ts `handleModalEditorPart`: an editor opened without preserveFocus closes the modal.
if pinned && self.setting("workbench.editor.useModal") != serde_json::json!("all") {
self.close_modal_editor(window, cx);
}
match revealed {
Some(group) => self.activate_group(group),
None => {
self.leave_side_group();
self.leave_locked_group(scheme, &path);
}
}
self.open_file_in_active_group(scheme, path, pinned, window, cx);
}
pub(super) fn open_file_in_active_group(&mut self, scheme: &str, path: PathBuf, pinned: bool, window: &mut Window, cx: &mut Context<Self>) {
// The group of a side by side editor opens the file, not its secondary side's.
self.leave_side_group();
if path.to_string_lossy().ends_with(crate::search_editor::SEARCH_EDITOR_EXT) {
return self.open_search_editor_file(path, pinned, window, cx);
}
if self.custom_editor_for(scheme, &path).is_some() {
let options = serde_json::json!({ "pinned": pinned, "preserveFocus": !pinned });
drop(self.open_with_resolver(&crate::editor::uri_value(scheme, &path), None, options, window, cx));
return;
}
self.open_text_file(scheme, path, pinned, window, cx);
}
/// Opens or activates the file in the text editor, whatever editor its resource resolves to.
pub(super) fn open_text_file(&mut self, scheme: &str, path: PathBuf, pinned: bool, window: &mut Window, cx: &mut Context<Self>) {
match self.editors.find(scheme, &path) {
Some(index) => {
self.editors.activate(index);
if pinned {
self.editors.pin(index);
}
}
// A model without an editor, as a search replace leaves it, keeps its text.
None => match self.editors.models.iter().position(|model| model.document().is_on_disk(scheme, &path)) {
Some(index) => self.editors.show_model(index, !pinned),
None => {
let document = self.read_file_editor_document(scheme, &path, false, false);
self.editors.open(document, !pinned);
}
},
}
self.marked_text = None;
match pinned && self.editors.active().is_some() {
true => window.focus(&self.editor_focus, cx),
false => window.focus(&self.explorer.focus, cx),
}
self.editors_changed(cx);
}
/// Keeps the Explorer selection and the extension host in step with the group.
fn editors_changed(&mut self, cx: &mut Context<Self>) {
if std::mem::take(&mut self.editors.closed_last_editor) {
self.close_group_if_empty(self.editors.id);
}
self.editors.columns = if self.editors.has_several_groups() { self.groups_in_grid_order() } else { Vec::new() };
for closed in std::mem::take(&mut self.editors.closed) {
self.remember_closed_editor(closed);
}
self.release_closed_wait_files();
self.record_navigation(None);
self.editors.panel_editor_shown = self.output_view_visible() && self.output_editor().is_some();
self.open_editors_focused_row = None;
self.reveal_active_file();
self.record_editor_history();
self.capture_notebook_status_editor();
self.init_notebook_variables_view(None, cx);
self.notebook_variables_editor_changed(cx);
self.drop_closed_search_editors();
self.drop_closed_playground();
self.sync_terminal_editors(cx);
self.sync_preferences_editors(cx);
self.load_search_editor_widget();
self.find_editor_changed();
self.schedule_keybinding_marks(cx);
self.schedule_unsupported_settings_markers(cx);
self.update_marker_decorations(None);
self.code_actions_model_changed(cx);
self.code_lens_model_changed(cx);
self.sync_comment_widgets();
self.refresh_commenting_ranges(false, cx);
self.inlay_hints_model_changed(cx);
self.update_debug_inline_values(cx);
self.color_detector_model_changed(cx);
self.inline_completions_model_changed(cx);
self.language_detection_model_changed(cx);
self.links_model_changed(cx);
self.linked_editing_reinitialize(true, cx);
self.semantic_tokens_model_changed(cx);
self.refresh_language_status(cx);
self.sync_webview_panels();
self.auto_lock_group();
self.sync_editor_insets();
self.text_files_editors_changed();
self.leave_compact_mode();
self.save_editor_part_state();
self.close_auxiliary_windows_of_closed_parts(cx);
self.show_editor_if_hidden(cx);
self.send_notebook_changes();
self.send_host_changes();
self.problems_quick_fix_model_opened(cx);
self.track_locked_scroll_panes();
cx.notify();
}
/// No color resolves to transparent, as an unset CSS variable does.
fn color(&self, key: &str) -> Rgba {
self.theme.color(key).unwrap_or(transparent_black().into())
}
/// A foreground key with no color inherits, as CSS `color` does; every part here inherits `foreground`.
fn foreground_or(&self, key: &str) -> Rgba {
self.theme.color(key).unwrap_or(self.color("foreground"))
}
/// The command center's Go Back and Go Forward (`MenuId.CommandCenter` orders 1 and 2): enabled by their
/// `canNavigateBack`/`canNavigateForward` precondition, pointer only then (actionbar.css `.disabled`).
fn navigation_button(&self, icon: &'static str, title: &'static str, command: &'static str, enabled: bool, cx: &mut Context<Self>) -> Stateful<Div> {
self.icon(icon, 22., 16., self.navigation_icon_color(enabled))
.id(command)
.rounded(px(4.))
.when(enabled, |button| {
button.cursor_pointer().hover(|button| button.bg(self.color("toolbar.hoverBackground"))).on_click(cx.listener(move |workbench, _, window, cx| {
workbench.execute_command(command, Vec::new(), window, cx);
}))
})
.on_mouse_down(MouseButton::Right, cx.listener(show_command_center_context_menu))
.child(self.action_hover(title, command, HoverOptions::instant()))
}
/// The codicon is the label's `::before`: modernUI commandCenter.css colors the label in
/// `commandCenter.foreground`, but actionbar.css's `.disabled .action-label:not(.icon)::before` wins on the glyph.
fn navigation_icon_color(&self, enabled: bool) -> Rgba {
self.color(if enabled { "commandCenter.foreground" } else { "disabledForeground" })
}
/// The window's title bar; an editor window's has its compact mode and always on top toggles in place of the
/// layout controls, and its border. A compact one has no command center and shows the editor actions.
/// NativeTitlebarPart.minimumHeight: DEFAULT_CUSTOM_TITLEBAR_HEIGHT with the command center, else macTitlebarSize.
fn title_bar_height(&self, compact: bool) -> f32 {
let command_center_visible = !compact && self.setting("window.commandCenter") != Value::Bool(false);
match (command_center_visible, is_tahoe_or_newer()) {
(true, _) => 35.,
// titlebarPart.ts `minimumHeight`.
(false, _) if OS == Os::Windows => 30.,
(false, true) => 32.,
(false, false) => 28.,
}
}
fn title_bar(&self, viewport_width: f32, window: &Window, editor_window: Option<auxiliary_windows::EditorWindowTitle>, cx: &mut Context<Self>) -> Div {
let active = window.is_window_active();
let auxiliary = editor_window.is_some();
let compact = editor_window.is_some_and(|state| state.compact);
// titlebarPart.ts `updateStyles` and titlebarpart.css `.inactive`: inactive colors, the contents at 0.6 opacity.
let foreground = match active {
true => self.color("titleBar.activeForeground"),
false => self.theme.color("titleBar.inactiveForeground").unwrap_or_else(|| self.color("foreground")),
};
let background = self.theme.opaque_on_workbench(if active { "titleBar.activeBackground" } else { "titleBar.inactiveBackground" }).or_else(|| self.theme.opaque_on_workbench("titleBar.activeBackground"));
let opacity = if active { 1. } else { 0.6 };
let command_center_visible = !compact && self.setting("window.commandCenter") != Value::Bool(false);
let layout_control_enabled = self.setting("workbench.layoutControl.enabled") != Value::Bool(false);
let disabled = self.color("disabledForeground");
let command_center = div()
.id("command-center-center")
.flex()
.items_center()
// .command-center-center is 38vw wide plus its border (titlebarpart.css).
.w(px(viewport_width * 0.38 + 2.))
.relative()
.cursor_pointer()
.h(px(24.))
.pl(px(8.))
.border_1()
.rounded(px(6.))
.bg(self.color(if self.command_center_debugging() { "commandCenter.debuggingBackground" } else { "commandCenter.background" }))
.border_color(self.color(if active { "commandCenter.border" } else { "commandCenter.inactiveBorder" }))
.text_color(if active { self.color("commandCenter.foreground") } else { foreground })
.hover(|center| center.bg(self.color("commandCenter.activeBackground")).border_color(self.color("commandCenter.activeBorder")).text_color(self.color("commandCenter.activeForeground")))
.text_size(px(12.))
// `.command-center-center.multiple`: 12px padding on each side, outside the 38vw; compact mode's toolbars
// share the width (`flex: 1`) around commandCenterControl.ts's `circleSmallFilled` spacer.
.map(|center| match self.command_center_debug_actions(window, cx) {
Some(actions) => center
.w(px(viewport_width * 0.38 + 2. + 24.))
.pl(px(12.))
.pr(px(12.))
.child(actions.flex_1().min_w_0().overflow_hidden())
.child(div().debug_selector(|| "command-center-spacer".into()).px(px(8.)).opacity(0.5).child(self.icon("circle-small-filled", 16., 16., self.color("commandCenter.foreground"))))
.child(div().debug_selector(|| "command-center-label".into()).flex_1().min_w_0().child(div().pl(px(8.)).child(self.command_center_label()))),
None => center.child(div().debug_selector(|| "command-center-label".into()).child(self.command_center_label())),
})
.child(self.hover_target("command-center", HoverContent::Text(self.command_center_tooltip()), HoverOptions::instant()))
.on_mouse_down(MouseButton::Right, cx.listener(show_command_center_context_menu))
// A Cmd+click belongs to the title's menu, which upstream takes in the capture phase.
.on_click(cx.listener(|workbench, event: &ClickEvent, window, cx| {
if !event.modifiers().platform {
workbench.execute_command("workbench.action.quickOpenWithModes", Vec::new(), window, cx);
}
}));
let toolbars = self.title_bar_toolbars(window);
let [command_center_bar, update_bar, actions_bar] = toolbars;
for bar in toolbars {
self.forget_action_bar_items(bar);
}
// commandCenterControl.ts: the center is a nested toolbar, a tab stop of its own after the navigation buttons'.
let title_item = |bar: &'static str, key: &str, item: Stateful<Div>, cx: &gpui::App| self.action_bar_item_with_tab_stop(bar, key, key == "center", item, cx).into_any_element();
let title_bar_toolbar = |bar: &'static str, toolbar: Div, cx: &mut Context<Self>| self.action_bar(bar, false, toolbar, cx);
let title = div().id("window-title").flex().items_center().map(|title| match command_center_visible {
// commandCenterControl.ts: the navigation buttons and the center are `MenuId.CommandCenter`'s toolbar.
true => {
let mut items = Vec::new();
if contextkey::truthy(Some(&self.setting("workbench.navigationControl.enabled"))) {
let back = self.navigation_button("arrow-left", "Go Back", "workbench.action.navigateBack", self.can_navigate(true), cx);
let forward = self.navigation_button("arrow-right", "Go Forward", "workbench.action.navigateForward", self.can_navigate(false), cx).ml(px(4.)).mr(px(10.));
items.push(title_item(command_center_bar, "back", back, cx));
items.push(title_item(command_center_bar, "forward", forward, cx));
}
items.push(title_item(command_center_bar, "center", command_center, cx));
title.child(title_bar_toolbar(command_center_bar, div().flex().items_center().children(items), cx)).child(div().w(px(6.)))
}
false => title.text_size(px(12.)).text_color(foreground).child(self.window_title()),
});
// On macOS a right-click or a Cmd+click on the title shows the title's own menu (titlebarPart.ts).
let title = title
// Windows hit-tests the drag area before any listener runs: the command center blocks it.
.when(OS == Os::Windows && command_center_visible, |title| title.occlude())
.on_mouse_down(MouseButton::Right, cx.listener(|workbench, event: &MouseDownEvent, window, cx| {
cx.stop_propagation();
workbench.show_title_bar_context_menu(MenuId::TitleBarTitleContext, event.position, window, cx);
}))
.on_mouse_down(MouseButton::Left, cx.listener(move |workbench, event: &MouseDownEvent, window, cx| {
// The command center is `-webkit-app-region: no-drag`; the plain window title drags.
if command_center_visible || event.modifiers.platform {
cx.stop_propagation();
}
if event.modifiers.platform {
workbench.show_title_bar_context_menu(MenuId::TitleBarTitleContext, event.position, window, cx);
}
}));
let height = self.title_bar_height(compact);
// titlebarpart.css: the items center in the bar above its 1px border, 1.5px higher in a 32px bar than in a 35px one;
// the plain window title is a 22px line, 0.5px higher than the 24px command center's middle.
let lift = (35. - height) / 2.;
let (top, line) = match command_center_visible {
true => (6.5 - lift, 24.),
false => (6.5 - lift + 0.5, 22.),
};
// titlebarpart.css `.mac .titlebar-container`: line-height 22px.
let center = div().absolute().top(px(top)).w_full().h(px(line)).flex().justify_center().items_center().line_height(px(22.)).opacity(opacity).child(title);
// titlebarPart.ts `createActionToolBarMenus`: the editor actions and a separator before the layout actions, whose
// Customize Layout moves into the overflow then (layoutActions.ts `EditorActionsInTitleBar`).
let editor_actions = !auxiliary && self.title_bar_has_editor_actions();
let mut editor_action_items = Vec::new();
if editor_actions && self.editors.active_index().is_some() {
editor_action_items = self.editor_title_primary_actions(Some(actions_bar), foreground, window, cx);
editor_action_items.push(div().w(px(1.)).h(px(16.)).my(px(3.)).mx(px(4.)).bg(disabled).into_any_element());
}
let side_bar_toggle = if self.side_bar_visible() { "layout-sidebar-left" } else { "layout-sidebar-left-off" };
let layout_actions = [
("layout", "Customize Layout...", "workbench.action.customizeLayout"),
(side_bar_toggle, "Toggle Primary Side Bar", "workbench.action.toggleSidebarVisibility"),
(if self.is_part_visible(layout::Part::Panel) { "layout-panel" } else { "layout-panel-off" }, "Toggle Panel", "workbench.action.togglePanel"),
(if self.is_part_visible(layout::Part::AuxiliaryBar) { "layout-sidebar-right" } else { "layout-sidebar-right-off" }, "Toggle Secondary Side Bar", "workbench.action.toggleAuxiliaryBar"),
];
// titlebarPart.ts `createActionToolBar`: the editor, layout and global actions are one toolbar, one tab stop.
let mut actions = Vec::new();
// titlebarPart.ts: `MenuId.TitleBar`'s `0_leading` group and a separator lead the toolbar, but in a compact window.
if !compact {
let leading = self.title_bar_global_buttons(TITLE_BAR_LEADING_GROUP, foreground, window, cx);
if !leading.is_empty() {
actions.extend(leading.into_iter().map(|(id, button)| title_item(actions_bar, &id, button, cx)));
actions.push(div().w(px(1.)).h(px(16.)).my(px(3.)).mx(px(4.)).bg(disabled).into_any_element());
}
}
// titlebarPart.ts: a compact editor window shows `MenuId.CompactWindowEditorTitle` (a diff's Previous and
// Next Change).
if compact {
for (key, (_, action)) in self.diff_title_actions(cx).into_iter().filter(|(order, _)| *order < 12.).enumerate() {
actions.push(title_item(actions_bar, &key.to_string(), action, cx));
}
}
if let Some(state) = editor_window.filter(|_| layout_control_enabled) {
for (command, action) in self.editor_window_title_actions(state, foreground, cx) {
actions.push(title_item(actions_bar, command, action, cx));
}
}
actions.extend(editor_action_items);
if !auxiliary && layout_control_enabled {
for (name, title, command) in layout_actions.into_iter().filter(|(_, _, command)| !editor_actions || *command != "workbench.action.customizeLayout") {
let action = self.icon(name, 22., 16., foreground)
.id(command)
.debug_selector(move || command.to_owned())
.cursor_pointer()
.on_click(cx.listener(move |workbench, _, window, cx| drop(workbench.execute_command(command, Vec::new(), window, cx))))
.child(self.action_hover(title, command, HoverOptions::instant()));
actions.push(title_item(actions_bar, command, action, cx));
}
}
// titlebarPart.ts: the global actions (`MenuId.TitleBar`, the bell) come after the layout controls.
if !compact {
let global = self.title_bar_global_buttons("navigation", foreground, window, cx);
actions.extend(global.into_iter().map(|(id, button)| title_item(actions_bar, &id, button, cx)));
}
// titlebarPart.ts: with the activity bar on top or bottom, its global Manage action moves to the title bar.
// globalCompositeBar.ts `SimpleGlobalActivityActionViewItem`: a left mouse down opens MenuId.GlobalActivity below it.
if !auxiliary && self.compact_activity_bar_location().is_some() {
let show_manage_menu = |workbench: &mut Self, window: &mut Window, cx: &mut Context<Self>| {
let context = workbench.key_context(window);
let actions = crate::menus::menu_actions(&workbench.menus, MenuId::GlobalActivity, &context);
workbench.show_context_menu_actions(actions, &context, workbench.manage_bounds.get(), Vec::new(), window, cx);
};
let manage = self.icon("settings-gear", 22., 16., foreground)
.id("manage-title")
.cursor_pointer()
.child(output_view::bounds_tracker(self.manage_bounds.clone()))
.child(self.hover_target("manage-title", HoverContent::Text("Manage".into()), HoverOptions::instant()))
.on_mouse_down(MouseButton::Left, cx.listener(move |workbench, _, window, cx| show_manage_menu(workbench, window, cx)))
.on_click(cx.listener(move |workbench, event: &ClickEvent, window, cx| {
if event.is_keyboard() {
show_manage_menu(workbench, window, cx);
}
}));
actions.push(title_item(actions_bar, "manage", manage, cx));
}
// The toolbar's overflow ends it: the editor actions' secondary ones, Customize Layout and the other global groups.
let global_overflow = !compact && !self.title_bar_global_secondary_actions(&self.key_context(window)).is_empty();
if editor_actions && (layout_control_enabled || self.editors.active_index().is_some()) || global_overflow {
let more = self.editor_title_more_button(foreground, true, cx);
actions.push(title_item(actions_bar, "more", more, cx));
}
let right = div()
.absolute()
.top(px(7.5 - lift))
// On Windows the toolbar ends at the window controls.
.right(px(if OS == Os::Windows { 138. + 12. } else { 12. }))
.opacity(opacity)
.flex()
.gap(px(4.))
.when(OS == Os::Windows, |right| right.occlude())
.on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation())
.when(!compact, |right| right.children(self.update_title_bar_entry(update_bar, !auxiliary, cx)))
.child(title_bar_toolbar(actions_bar, div().flex().gap(px(4.)).children(actions), cx));
// titlebarPart.ts `updateStyles`: an auxiliary title bar keeps `titleBar.border` under the modern UI.
let border = self.theme.color("titleBar.border").filter(|_| auxiliary);
div()
.relative()
.h(px(height))
.flex_none()
.when_some(background, |bar, background| bar.bg(background))
.when_some(border, |bar, border| bar.border_b_1().border_color(border))
.child(center)
.child(right)
// titlebarPart.ts on Windows: the menu bar at the left, the window controls at the right, and the rest
// of the bar Windows' caption, which moves, snaps and maximizes the window.
.when(OS == Os::Windows, |bar| {
bar.window_control_area(gpui::WindowControlArea::Drag)
.children((!auxiliary).then(|| self.custom_menubar(foreground, window, cx)).flatten().map(|menubar| div().absolute().left_0().top_0().h_full().opacity(opacity).child(menubar)))
.child(div().absolute().right_0().top_0().h_full().child(self.window_controls(foreground, window)))
})
// titlebarpart.css `.titlebar-drag-region`: the bar drags the window and a double click runs the system's
// title bar action; the no-drag parts stop the press before it gets here.
.on_mouse_down(MouseButton::Left, |event, window, _| match event.click_count {
2 => window.titlebar_double_click(),
_ => window.start_window_move(),
})
.on_mouse_down(MouseButton::Right, cx.listener(|workbench, event: &MouseDownEvent, window, cx| workbench.show_title_bar_context_menu(MenuId::TitleBarContext, event.position, window, cx)))
}
/// The title bar toolbars of `window`: an editor window's are bars of their own, named after its part.
fn title_bar_toolbars(&self, window: &Window) -> [&'static str; 3] {
match self.auxiliary_part_of_window(window) {
Some(part) => TITLE_BAR_TOOLBARS.map(|bar| self.part_action_bar(bar, part)),
None => TITLE_BAR_TOOLBARS,
}
}
/// The modern UI shell around the floating parts (floatingPanels.css), opaque as titlebarPart.ts sets it.
fn shell_background(&self, active: bool) -> Rgba {
let shell = if active { "modernUI.shellBackground" } else { "modernUI.inactiveShellBackground" };
self.theme.opaque_on_workbench(shell).or_else(|| self.theme.opaque_on_workbench("modernUI.shellBackground")).unwrap_or_else(|| self.color("titleBar.activeBackground"))
}
/// windowTitle.ts `getFullWindowTitle` with the macOS `window.title` default
/// `${activeEditorShort}${separator}${rootName}${separator}${folderLocation}${separator}${profileName}`, the last two
/// empty here, or `nameLong` when it is empty, with the title decorations.
pub(crate) fn window_title(&self) -> String {
let root_name = self.workspace().map(Workspace::label).unwrap_or_default();
let active_editor = self.editors.active().map(|editor| self.editor_name(editor)).unwrap_or_default();
let parts: Vec<_> = [active_editor, root_name].into_iter().filter(|part| !part.is_empty()).collect();
let title = match parts.is_empty() {
true => crate::product::name_long().to_string(),
false => parts.join(" \u{2014} "),
};
self.title_decorated(title)
}
/// windowTitle.ts `getTitleDecorations`: "[Extension Development Host]" before the title in an extension
/// development window.
fn title_decorated(&self, title: String) -> String {
match self.extension_development {
true => format!("[Extension Development Host] {title}"),
false => title,
}
}
/// windowTitle.ts `getCommandCenterLabel` with commandCenterControl.ts `showFileName: showTabs === 'none'`: the
/// active editor's short title, dirty marked, else the workspace name, else "Search", after the title decorations.
fn command_center_label(&self) -> String {
let file_name = self.editors.active().filter(|_| self.editor_show_tabs() == "none").map(|editor| {
let dirty = if self.editor_shows_dirty(editor) { "\u{25cf} " } else { "" };
format!("{dirty}{}", self.editor_name(editor))
});
let label = file_name.or_else(|| self.workspace().map(Workspace::label)).filter(|label| !label.is_empty());
self.title_decorated(label.unwrap_or_else(|| "Search".into()))
}
/// commandCenterControl.ts `getTooltip`; its submenu has no keybinding, so "Search {0} \u{2014} {1}".
fn command_center_tooltip(&self) -> String {
let root_name = self.workspace().map(Workspace::label).unwrap_or_default();
format!("Search {root_name} \u{2014} {}", self.window_title())
}
fn side_bar_visible(&self) -> bool {
self.is_part_visible(layout::Part::SideBar)
}
fn activity_bar(&self, window: &Window, cx: &mut Context<Self>) -> Div {
// activitybarPart.ts: the hovers go away from the side bar's edge of the window.
let left = self.arrangement().side_bar == layout::Position::Left;
let hover = HoverOptions::composite_bar(if left { HoverPosition::Right } else { HoverPosition::Left });
let unpinned = self.unpinned_containers(layout::Part::SideBar);
let containers = side_bar::VIEW_CONTAINERS.into_iter().filter(|(_, _, _, command, _)| !self.container_hidden_if_empty(command) && !unpinned.iter().any(|id| id == command) && self.container_location(command) == layout::Part::SideBar);
let mut items: Vec<_> = containers.map(|(icon, _, title, command, view)| {
let active = view.is_some_and(|view| self.side_bar_view == view) && self.side_bar_visible();
let drag_icon = self.codicon_svg(icon);
let icon = if active { self.active_view_icon(icon) } else { self.inactive_view_icon(icon, self.activity_item_foreground("activity", command, window, cx)) };
let item = self.activity_composite_item(self.action_bar_item("activity", command, icon.id(command), cx), "activity", command, title, drag_icon, active, cx)
.debug_selector(move || command.to_owned())
.cursor_pointer()
.child(self.action_hover(title, command, hover))
.relative()
.children(self.container_activity_badge(command))
.when(view.is_some(), |icon| icon.on_click(cx.listener(move |workbench, event: &ClickEvent, window, cx| workbench.activity_item_clicked(active, command, event, window, cx))));
(command, item)
}).collect();
items.extend(self.testing_activity_item(hover, window, cx).map(|item| ("workbench.view.testing", item)));
items.extend(self.contributed_activity_items(hover, window, cx));
items.extend(self.generated_activity_items(hover, window, cx));
let order = self.activity_bar_containers();
items.sort_by_key(|(id, _)| order.iter().position(|(other, _)| other == id));
// floatingPanels.css and activitybarPart.ts: compact density narrows the lane to 4px and the item gap to 4px.
let compact = self.compact_density();
div()
.w(px(if compact { 40. } else { 44. }))
.flex()
.flex_col()
.items_center()
.justify_between()
.py(px(if compact { 1. } else { 3. }))
.border_1()
// activitybarPart.ts `updateStyles` with the modern UI: the inactive background while the window is not focused.
.bg(self.color(if window.is_window_active() { "modernActivityBar.background" } else { "modernActivityBar.inactiveBackground" }))
.map(|bar| match compact {
// A left rail and a right rail after the side bar draw the seam beside them.
true => bar.border_color(gpui::transparent_black()).child(self.compact_card_strokes("modernActivityBar.border", [true, true, true, left || self.side_bar_visible()], true)),
false => bar
.border_color(self.color("modernActivityBar.border"))
.when(self.side_bar_visible() && left, |bar| bar.rounded_l(px(8.)))
.when(self.side_bar_visible() && !left, |bar| bar.rounded_r(px(8.)))
.when(!self.side_bar_visible(), |bar| bar.rounded(px(8.))),
})
.child(self.action_bar(
"activity",
true,
div()
.flex()
.flex_col()
.gap(px(if compact { 4. } else { 8. }))
.children(items.into_iter().map(|(_, item)| item)),
cx,
))
.map(|bar| self.with_bar_end_drop(bar, layout::Part::SideBar, cx))
.on_mouse_down(MouseButton::Right, cx.listener(|workbench, event: &MouseDownEvent, window, cx| workbench.show_activity_bar_context_menu(event.position, window, cx)))
.child(self.action_bar("global-activity", true, div().child(self.action_bar_item("global-activity", "manage",
self.icon(self.manage_activity_icon(), 36., 24., self.activity_item_foreground("global-activity", "manage", window, cx))
.id("manage")
.role(gpui::Role::Button)
.aria_label(self.manage_activity_title())
.cursor_pointer()
.children(self.manage_activity_badge())
.child(output_view::bounds_tracker(self.manage_bounds.clone()))
.child(self.hover_target("manage", HoverContent::Text(self.manage_activity_title()), hover))
// globalCompositeBar.ts: a left mouse down, or the button's press, opens MenuId.GlobalActivity beside it.
.on_mouse_down(MouseButton::Left, cx.listener(|workbench, _, window, cx| workbench.show_manage_menu(window, cx)))
.on_click(cx.listener(|workbench, event: &ClickEvent, window, cx| {
if event.is_keyboard() {
workbench.show_manage_menu(window, cx);
}
}))
.on_a11y_action(gpui::accesskit::Action::Click, {
let workbench = cx.entity().downgrade();
move |_, window, cx| {
let _ = workbench.update(cx, |workbench, cx| workbench.show_manage_menu(window, cx));
}
}),
cx,
)), cx))
}
fn show_manage_menu(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let context = self.key_context(window);
let actions = crate::menus::menu_actions(&self.menus, MenuId::GlobalActivity, &context);
self.show_context_menu_beside(actions, &context, self.manage_bounds.get(), window, cx);
}
/// The Testing container shows while it has a view (`hideIfEmpty`).
fn testing_activity_item(&self, hover: HoverOptions, window: &Window, cx: &mut Context<Self>) -> Option<gpui::Stateful<Div>> {
if !self.testing_container_shown() || self.container_location(view_locations::TESTING_CONTAINER) != layout::Part::SideBar || self.unpinned_containers(layout::Part::SideBar).iter().any(|id| id == "workbench.view.testing") {
return None;
}
let active = self.side_bar_view == SideBarView::Testing && self.side_bar_visible();
let icon = if active { self.active_view_icon("beaker") } else { self.inactive_view_icon("beaker", self.activity_item_foreground("activity", "workbench.view.testing", window, cx)) };
let command = "workbench.view.testing";
Some(self.activity_composite_item(self.action_bar_item("activity", command, icon.id(command), cx), "activity", command, "Testing", self.codicon_svg("beaker"), active, cx).debug_selector(move || command.to_owned()).cursor_pointer().child(self.action_hover("Testing", command, hover)).on_click(cx.listener(move |workbench, event: &ClickEvent, window, cx| workbench.activity_item_clicked(active, command, event, window, cx))))
}
/// The checked view: a 32px rounded indicator behind the icon (activityaction.css); High Contrast
/// hides it and outlines the item in contrastActiveBorder instead (menuActivityBar.css `.hc-black, .hc-light`).
fn active_view_icon(&self, name: &str) -> Div {
let indicator = div().absolute().top(px(2.)).left(px(2.)).size(px(32.)).rounded(px(4.));
let indicator = if self.theme.kind.is_high_contrast() {
indicator.when_some(self.theme.color("contrastActiveBorder"), |indicator, outline| indicator.border_1().border_color(outline))
} else {
indicator.when_some(self.theme.color("modernActivityBarItem.activeBackground"), |indicator, background| indicator.bg(background))
};
div().relative().child(indicator).child(self.icon(name, 36., 24., self.color("modernActivityBarItem.activeForeground")))
}
/// activityaction.css `.action-item:focus .action-label`: an unchecked item draws in activityBar.foreground while
/// focused, else in activityBar.inactiveForeground.
pub(super) fn activity_item_foreground(&self, bar: &'static str, key: &str, window: &Window, cx: &gpui::App) -> Rgba {
let focused = self.action_bar_focus(bar, key, cx).is_focused(window);
self.color(if focused { "activityBar.foreground" } else { "activityBar.inactiveForeground" })
}
/// modernUI activityBar.css `.action-item:not(.checked):hover`: the same 32px indicator in
/// modernActivityBarItem.hoverBackground, and the icon in hoverForeground.
fn inactive_view_icon(&self, name: &str, foreground: Rgba) -> Div {
let hover_background = self.color("modernActivityBarItem.hoverBackground");
let hover_foreground = self.foreground_or("modernActivityBarItem.hoverForeground");
let indicator = div().absolute().top(px(2.)).left(px(2.)).size(px(32.)).rounded(px(4.)).group_hover(ACTIVITY_ITEM_GROUP, |indicator| indicator.bg(hover_background));
let icon = self.styled_icon(name, 36., 24., |icon| icon.text_color(foreground).group_hover(ACTIVITY_ITEM_GROUP, |icon| icon.text_color(hover_foreground)));
div().relative().child(indicator).child(icon)
}
/// style.css `.hc-black .monaco-list:not(.element-focused):focus:before`: in high contrast a focused list
/// without a focused row is outlined in focusBorder, above its sticky rows.
fn high_contrast_list_outline(&self, list_focused: bool, has_focused_row: bool) -> Option<Div> {
let color = self.theme.color("focusBorder").filter(|_| self.theme.kind.is_high_contrast() && list_focused && !has_focused_row)?;
Some(div().absolute().inset_0().border_1().border_color(color))
}
/// The Explorer: title, then the folder's view with its tree, Outline and Timeline as panes.
fn side_bar_view(&self, window: &Window, cx: &mut Context<Self>) -> Div {
let foreground = self.foreground_or("sideBar.foreground");
if let SideBarView::Contributed(id) = self.side_bar_view {
return self.contributed_side_bar(id, window, cx);
}
if let SideBarView::Generated(id) = self.side_bar_view {
return self.view_pane_container(view_panes::PaneContainer::Generated(id), &self.container_title(id), Vec::new(), window, cx).child(div().h(px(5.)).flex_none());
}
if self.side_bar_view == SideBarView::Scm {
return self.scm_side_bar(window, cx);
}
if self.side_bar_view == SideBarView::Extensions {
let title = self.side_bar_title_text(self.extensions_title()).child(self.extensions_title_actions(cx));
return self.side_bar_frame().text_color(foreground).child(title).child(self.extensions_view(window, cx));
}
if self.side_bar_view == SideBarView::Search {
let container = self.view_pane_container(view_panes::PaneContainer::Search, "Search", vec![self.search_view_pane(window, cx)], window, cx);
let merged = self.panes(view_panes::PaneContainer::Search).view.as_ref().is_some_and(|view| view.ids().len() == 1);
return container.when(!merged, |container| container.child(div().h(px(5.)).flex_none()));
}
if self.side_bar_view == SideBarView::Debug {
return self.debug_side_bar(window, cx);
}
if self.side_bar_view == SideBarView::Testing {
let testing = vec![self.testing_view_pane(window, cx), self.test_coverage_view_pane(cx)];
return self.view_pane_container(view_panes::PaneContainer::Contributed(view_locations::TESTING_CONTAINER), "Testing", testing, window, cx).child(div().h(px(5.)).flex_none());
}
self.explorer_view_container(window, cx)
// The pane view is laid out 5px shorter than the side bar content.
.child(div().h(px(5.)).flex_none())
}
/// The folder's view with its tree, or the empty window's view.
pub(in crate::workbench) fn folder_view_pane(&self, window: &Window, cx: &mut Context<Self>) -> view_panes::ViewPane {
// explorerViewlet.ts: the empty view shows while `workspaceFolderCount` is 0, in an empty workspace too.
let Some(tree) = self.folder.as_ref().filter(|tree| !tree.workspace.folders.is_empty()) else {
return view_panes::ViewPane { id: view_panes::EMPTY_VIEW, title: "No Folder Opened".into(), description: None, actions: None, draws_more: false, show_actions: false, body: self.empty_view(window, cx).into_any_element() };
};
let foreground = self.foreground_or("sideBar.foreground");
// explorerviewlet.css: in high contrast an explorer item has a 20px line from the row top, 1px above center.
let label_line = if self.theme.kind.is_high_contrast() { 20. } else { 22. };
let edit_focused = self.explorer.input_focus.is_focused(window);
let tree_focused = self.explorer.focus.is_focused(window);
// listWidget.ts: the focused tree draws the active selection colors and outlines its focused row.
let (selection, selection_foreground) = match tree_focused {
true => (self.color("list.activeSelectionBackground"), self.theme.color("list.activeSelectionForeground")),
false => (self.color("list.inactiveSelectionBackground"), self.theme.color("list.inactiveSelectionForeground")),
};
// `.context-menu-visible .monaco-list.last-focused`: the tree's context menu keeps the focused row outlined.
let outlined = tree_focused || self.context_menu.as_ref().is_some_and(|menu| menu.focus_to_return.as_ref() == Some(&self.explorer.focus));
let focus_outline = self.theme.color("list.focusOutline").or(self.theme.color("focusBorder")).filter(|_| outlined);
let edited = self.explorer.edit.as_ref().map(|edit| edit.renamed.clone());
// listWidget.ts: the drop target folder and the rows below it take list.dropBackground.
let drop_target = self.explorer.drop_target.as_ref().filter(|_| cx.has_active_drag());
let drop_background = self.color("list.dropBackground");
let tree_rows = tree.rows();
// explorerViewer.ts `onDragOver`: over a compact row's folder other than its last, that name alone is the
// drop target, without feedback on the rows.
let segment_drop_target = drop_target.filter(|target| tree_rows.iter().any(|row| row.compressed.iter().rev().skip(1).any(|(path, _)| path == *target)));
// listWidget.ts `drop-target-before` and `drop-target-after`: a dragged root's place is a 1px
// list.dropBetweenBackground line above or below a root, or below the last row for the end.
let last_row = tree_rows.len().saturating_sub(1);
let between_color = self.theme.color("list.dropBetweenBackground");
let root_drop = self.explorer.root_drop.clone().filter(|_| cx.has_active_drag() && self.explorer.drop_target.is_none());
let drop_line = |index: usize, path: &std::path::Path| match &root_drop {
Some(drop) if drop.target.as_deref() == Some(path) => Some(!drop.after),
Some(drop) if drop.target.is_none() && index == last_row => Some(false),
_ => None,
};
// listWidget.ts: a selected row gets a 1px dotted listSelectionOutline (activeContrastBorder) inside it.
let selection_outline = self.theme.color("contrastActiveBorder");
let folder_name = tree.workspace.label();
let has_folder_icons = self.file_icons.as_ref().is_some_and(FileIconTheme::has_folder_icons);
let active: Vec<usize> = tree_rows.iter().enumerate().filter(|(_, row)| tree.focused.as_ref() == Some(&row.path) || tree.selected.contains(&row.path)).map(|(index, _)| index).collect();
let guides = self.tree_guides(list_scroll::ListId::Explorer, tree_rows.iter().map(|row| row.depth).collect(), &active, window);
// abstractTree.ts `AbstractFindController`: its 30px paddingTop keeps the sticky rows away while find is open.
let sticky = if self.explorer.find.is_open() { Vec::new() } else { self.tree_sticky_rows(list_scroll::ListId::Explorer, &tree_rows.iter().map(|row| row.depth).collect::<Vec<_>>()) };
let sticky_rows: Vec<_> = sticky.iter().map(|sticky| (sticky.index, tree_rows[sticky.index].clone())).collect();
if self.explorer.reveal_after_layout {
// Runs after this draw laid the tree out.
cx.defer_in(window, |workbench, _, cx| {
if workbench.explorer.scroll.bounds().size.height > px(0.) && std::mem::take(&mut workbench.explorer.reveal_after_layout) {
workbench.reveal_explorer_focus(true);
cx.notify();
}
});
}
// abstractTree.ts `StickyScrollWidget` toggles only `focused` on its rows, never `selected`.
let mut render_row = |(index, row): (usize, crate::explorer::Row), sticky: bool| {
let row_guides = guides.row(index, 0.);
let twistie = match (row.is_dir || row.has_nests, row.expanded) {
(true, true) => Some("chevron-down"),
(true, false) => Some("chevron-right"),
(false, _) => None,
};
let navigation = tree.navigation(&row);
// explorerViewer.ts `ExplorerAccessibilityProvider.getAriaLabel`: the name, a compact row's names joined.
let aria_label = match row.compressed.is_empty() {
true => row.name.clone(),
false => row.compressed.iter().map(|(_, name)| name.as_str()).collect::<Vec<_>>().join("/"),
};
let (aria_level, aria_expanded) = (row.depth, (row.is_dir || row.has_nests).then_some(row.expanded));
let path = row.path;
let is_dir = row.is_dir;
let is_edited = edited.as_ref().is_some_and(|renamed| match renamed {
Some(renamed) => *renamed == path || row.compressed.iter().any(|(folder, _)| folder == renamed),
None => row.is_new,
});
let is_selected = !sticky && !row.is_new && tree.selected.contains(&path);
let is_focused = !sticky && !row.is_new && tree.focused.as_ref() == Some(&path);
let name = match (is_edited, navigation) {
(true, _) => self.explorer_input_box(label_line, edit_focused, cx).into_any_element(),
(false, Some(navigation)) => self.compressed_label(&row.compressed, &navigation, is_focused, segment_drop_target, cx).into_any_element(),
(false, None) => self.explorer_find_label(row.name, &path, (is_dir || row.has_nests) && !row.expanded),
};
let is_cut = tree.is_cut(&path);
let is_drop_target = !row.is_new && segment_drop_target.is_none() && drop_target.is_some_and(|target| path.starts_with(target));
let drag = self.explorer_drag(&path).filter(|_| !is_edited);
// views.css align-icons-and-twisties: with file icons and no folder icons a file's empty twistie collapses
// to its 8px indent and the icon takes its place, so file and folder names line up.
// explorerViewer.ts renderInputBox: the icon follows the name while it is typed.
let icon_path = match self.explorer.edit.as_ref().filter(|_| is_edited) {
Some(edit) => PathBuf::from(&edit.input.value),
None => path.clone(),
};
let file_icon = self.file_icons.as_ref().and_then(|icons| match is_dir {
true => {
let folder = FolderIcon { path: &path, is_root: tree.is_root(&path), expanded: row.expanded };
icons.folder_icon(folder, self.theme.kind, 13., label_line, foreground)
}
false => icons.file_icon(&icon_path, self.languages.language_of(&icon_path, ""), self.theme.kind, 13., label_line, foreground),
});
let indent = self.tree_indent() * (row.depth - 1) as f32;
// explorerViewer.ts passes `explorer.decorations` as the label's `fileDecorations`.
let decoration = self.file_decoration(&path, is_dir);
let decoration_color = self.decoration_color(decoration.as_ref(), self.setting("explorer.decorations.colors") != Value::Bool(false));
let badge = self.decoration_badge(decoration.as_ref(), foreground).filter(|_| self.setting("explorer.decorations.badges") != Value::Bool(false));
// The decoration color is the label's, not the bars' beside it.
let bars_color = selection_foreground.filter(|_| is_selected).unwrap_or(foreground);
let coverage_bars = self.explorer_coverage_bars(&path).map(|bars| bars.text_color(bars_color));
// testing.css `.explorer-item-with-test-coverage .monaco-icon-label::after`: 12px between the badge and the bars.
let badge = badge.map(|badge| if coverage_bars.is_some() { badge.mr(px(12.)) } else { badge });
let has_nests = row.has_nests;
let (twistie_path, row_expanded) = (path.clone(), row.expanded);
let twistie_element = |chevron| {
self.icon(chevron, 16., 16., foreground).id(("explorer-twistie", index)).on_click(cx.listener(move |workbench, _: &ClickEvent, _, cx| {
// explorerView.ts `expandOnlyOnTwistieClick`: a nest's row opens the file, its twistie toggles.
cx.stop_propagation();
workbench.folder.as_mut().expect("rows exist only with a folder").set_expanded(&twistie_path, !row_expanded);
cx.notify();
}))
};
let hover = self.hover_target(("explorer-hover", index), HoverContent::Text(match decoration.as_ref().and_then(|decoration| decoration.tooltip.as_deref()) {
// labels.ts: the title, then the decoration's tooltip.
Some(tooltip) => format!("{} \u{2022} {tooltip}", crate::quick_input::path_label(&path, None)),
None => crate::quick_input::path_label(&path, None),
}), HoverOptions::mouse());
div()
.id(index)
.role(gpui::Role::TreeItem)
.aria_label(aria_label)
.aria_level(aria_level)
.aria_selected(is_selected)
.when_some(aria_expanded, |row, expanded| row.aria_expanded(expanded))
// listWidget.ts: the list keeps the focus and points aria-activedescendant at the focused row.
.when(is_focused, |row| row.aria_active_descendant())
// list.css: .monaco-list.mouse-support .monaco-list-row is cursor: pointer.
.cursor_pointer()
.relative()
.when(!row.is_new && !is_edited, |row| row.child(hover))
.h(px(22.))
.mx(px(self.pane_row_inset()))
.flex()
.items_center()
.rounded(px(4.))
// list.css `.monaco-list-row:hover:not(.selected):not(.focused)`.
.when(!is_focused && !is_selected && !sticky, |row| row.hover(|row| row.bg(self.color("list.hoverBackground"))))
.map(|row| match (file_icon, twistie.is_some() || has_folder_icons) {
(Some(icon), false) => row.pl(px(8. + indent)).child(icon.self_start()).child(div().self_start().flex_1().ml(px(6.)).line_height(px(label_line)).child(name)),
// monaco-tl-twistie: 3px row inset, 8px base indent plus 8px per level, a 16px icon, 6px before the label.
(icon, _) => {
let label_margin = if icon.is_some() { 6. } else { 3. };
row.pl(px(11. + indent))
.child(match twistie {
Some(chevron) if has_nests => twistie_element(chevron).into_any_element(),
Some(chevron) => self.icon(chevron, 16., 16., foreground).into_any_element(),
None => div().size(px(16.)).flex_none().into_any_element(),
})
.children(icon.map(|icon| icon.self_start().ml(px(3.))))
.child(div().self_start().flex_1().ml(px(label_margin)).line_height(px(label_line)).child(name))
}
})
.children(badge)
.children(coverage_bars)
.when_some(decoration_color, |row, color| row.text_color(color))
// explorerviewlet.css: .explorer-item.cut is at half opacity. Its 0.3 for the other rows while a name is
// edited needs `.explorer-folders-view` twice in the ancestry, which the reference DOM never has.
.children(row_guides)
.when(is_cut, |row| row.opacity(0.5))
.when(is_selected, |row| {
// Dashed: GPUI has no dotted border.
// An overlay, since a border on the row would push its content 1px like a CSS border, not an outline.
let outline = selection_outline.filter(|_| !is_edited).map(|color| div().absolute().inset_0().rounded(px(4.)).border_1().border_dashed().border_color(color));
row.relative().bg(selection).when_some(selection_foreground, |row, color| row.text_color(color)).children(outline)
})
.when(is_drop_target, |row| row.bg(drop_background))
.when_some(drop_line(index, &path).filter(|_| !sticky).zip(between_color), |row, (top, color)| {
let line = div().absolute().left_0().w_full().h(px(1.)).bg(color);
row.relative().child(if top { line.top_0() } else { line.bottom_0() })
})
.when_some(focus_outline.filter(|_| is_focused && !is_edited), |row, color| {
row.relative().child(div().absolute().inset_0().rounded(px(4.)).border_1().border_color(color))
})
.when_some(drag, |row, drag| row.on_drag(drag, |drag, _, _, cx| cx.new(|_| drag.clone())))
.when(!row.is_new, |row| {
let over = path.clone();
let external_over = path.clone();
row.on_drag_move(cx.listener(move |workbench, event: &DragMoveEvent<DraggedItems>, window, cx| {
if event.bounds.contains(&event.event.position) {
let items = event.drag(cx).clone();
let after = event.event.position.y >= event.bounds.center().y;
workbench.explorer_drag_over(Some(&over), after, &items, window, cx);
}
}))
.on_drag_move(cx.listener(move |workbench, event: &DragMoveEvent<ExternalPaths>, window, cx| {
if event.bounds.contains(&event.event.position) {
workbench.explorer_external_drag_over(Some(&external_over), window, cx);
}
}))
.on_drop(cx.listener(|workbench, items: &DraggedItems, window, cx| {
cx.stop_propagation();
workbench.explorer_drop(items, window, cx);
}))
.on_drop(cx.listener(|workbench, paths: &ExternalPaths, window, cx| {
cx.stop_propagation();
workbench.explorer_import(paths, window, cx);
}))
})
.when(!is_edited, |row| {
let target = path.clone();
row.on_mouse_down(MouseButton::Right, cx.listener(move |workbench, event: &MouseDownEvent, window, cx| {
cx.stop_propagation();
// explorerView.ts `onContextMenu`: a compact row right-clicked off its names acts on its last folder.
let tree = workbench.folder.as_mut().expect("rows exist only with a folder");
if let Some(mut navigation) = tree.navigation_at(&target) {
navigation.last();
tree.set_navigation(&navigation);
}
workbench.show_explorer_context_menu(Some(&target), event.position, window, cx);
}))
.on_click(cx.listener(move |workbench, event: &ClickEvent, window, cx| {
let modifiers = event.modifiers();
let mode = workbench.list_click_mode();
let multi_select = if mode.alt_multi_select { modifiers.alt } else { modifiers.platform };
let double_click = event.click_count() == 2;
window.focus(&workbench.explorer.focus.clone(), cx);
let tree = workbench.folder.as_mut().expect("rows exist only with a folder");
tree.click_select(&path, multi_select, modifiers.shift);
cx.notify();
if multi_select || modifiers.shift {
return;
}
match is_dir {
true if mode.expand_on_double_click && !double_click => {}
true => tree.toggle(&path),
false if mode.open_on_double_click && !double_click => {}
false => workbench.open_file(path.clone(), double_click, window, cx),
}
}))
})
};
// listView.ts renders only the rows in the viewport, which the window's height bounds; padding stands for the rest.
let first = ((-f32::from(self.explorer.scroll.offset().y) / 22.) as usize).min(tree_rows.len());
let last = (first + (f32::from(window.viewport_size().height) / 22.) as usize + 2).min(tree_rows.len());
let (above, below, height) = (px(first as f32 * 22.), px((tree_rows.len() - last) as f32 * 22.), px(tree_rows.len() as f32 * 22.));
let rows: Vec<_> = tree_rows.into_iter().enumerate().skip(first).take(last - first).map(|row| render_row(row, false)).collect();
let sticky_elements = sticky_rows.into_iter().map(|row| render_row(row, true).into_any_element()).collect();
let sticky_container = self.tree_sticky_container(list_scroll::ListId::Explorer, &sticky, sticky_elements, cx);
// explorerView.ts `setEditable`: no horizontal scrolling while a name is edited.
let horizontal = self.horizontal_scrolling(list_scroll::ListId::Explorer) && self.explorer.edit.is_none();
let explorer_focused = self.explorer.focus.is_focused(window);
view_panes::ViewPane { id: view_panes::FILE_VIEW, title: folder_name, description: None, actions: None, draws_more: false, show_actions: explorer_focused, body:
div().id("explorer-pane").relative().flex_1().min_h_0().flex().flex_col()
.child(
div()
.id("explorer-tree")
.role(gpui::Role::Tree)
.aria_label("Files Explorer")
.track_focus(&self.explorer.focus)
.overflow_y_scroll()
.when(horizontal, |tree| tree.overflow_x_scroll())
.track_scroll(&self.explorer.scroll)
.flex_1()
.min_h_0()
.flex()
.flex_col()
.on_mouse_down(MouseButton::Right, cx.listener(|workbench, event: &MouseDownEvent, window, cx| workbench.show_explorer_context_menu(None, event.position, window, cx)))
// abstractTree.ts `AbstractFindController.open`: `paddingTop: 30` while the find widget shows.
.when(self.explorer.find.is_open(), |tree| tree.pt(px(30.)))
// `horizontalScrolling`: unwrapped rows as wide as the widest rendered one, at least the view.
// listView.ts: rows are absolutely placed 22px boxes, so the tree's layout never measures them; with
// `horizontalScrolling` they stay in flow, the list as wide as the widest rendered row.
.child(match horizontal {
true => div().flex_none().flex().flex_col().pt(above).pb(below).self_start().min_w_full().whitespace_nowrap().children(rows),
false => div().flex_none().relative().h(height).children(rows.into_iter().zip(first..).map(|(row, index)| div().absolute().top(px(index as f32 * 22.)).left_0().right_0().h(px(22.)).flex().flex_col().child(row))),
})
.child(self.explorer_empty_area(cx)),
).child(self.list_scroll_layer(list_scroll::ListId::Explorer, cx)).children(sticky_container).children(self.high_contrast_list_outline(tree_focused, tree.focused.is_some())).children(self.explorer_find_widget(window, cx)).into_any_element() }
}
/// The Explorer's `ViewPaneContainer`: Open Editors while shown, the folder's or the empty window's view, Outline
/// and Timeline, the ones still in it (`layout_view_panes` leaves out the moved ones).
fn explorer_view_container(&self, window: &Window, cx: &mut Context<Self>) -> Div {
let mut panes = vec![self.folder_view_pane(window, cx), self.outline_view_pane(window, cx), self.timeline_pane(window, cx)];
if self.explorer_panes.view.as_ref().is_some_and(|view| view.ids().contains(&view_panes::OPEN_EDITORS_VIEW)) {
panes.push(self.open_editors_pane(window, cx));
}
self.view_pane_container(view_panes::PaneContainer::Explorer, "Explorer", panes, window, cx)
}
fn side_bar_frame(&self) -> Div {
div().flex_1().min_h_0().flex().flex_col()
}
/// The composite title bar: the view's name and its actions.
fn side_bar_title_text(&self, name: String) -> Div {
self.side_bar_title_shared(self.view_header_text(&name, false), false)
}
/// modernUI padding.css `.title-label`: beside the launch picker (`has-start-debug-action-item`) the label takes
/// `flex: 1` of the title against the actions' `flex: 2`.
fn side_bar_title_shared(&self, name: String, has_start_debug: bool) -> Div {
// part.css: the side bar's composite progress bar runs along the title's bottom 2px.
let container = if self.side_bar_view == SideBarView::Extensions { Some(extensions_view::VIEWLET_ID) } else { self.side_bar_container() };
div()
.relative()
.children(container.and_then(|container| self.scoped_progress_bar(container)).map(|bar| bar.bottom_0()))
.h(px(32.))
.flex_none()
.flex()
.items_center()
.justify_between()
.px(px(4.))
// compositePart.ts `updateStyles` adds the border-bottom inside the border-box title, under a fixed-height
// label and actions (part.css), so nothing in the title moves when a theme defines it.
.when_some(self.theme.color("sideBarTitle.border"), |title, border| title.child(div().absolute().left_0().right_0().bottom_0().h(px(1.)).bg(border)))
// part.css `.title-label h2`: ellipsis where `.title-actions` (8px padding-left) takes the room.
.child(div().debug_selector(|| "side-bar-title-label".into()).self_start().min_w_0().when(has_start_debug, |label| label.flex_grow(1.).flex_basis(px(0.))).truncate().pl(px(8.)).line_height(px(32.)).text_size(px(12.)).font_weight(FontWeight::SEMIBOLD).text_color(self.foreground_or("sideBarTitle.foreground")).child(name))
}
/// modernUI fontRamp.css: view, part and panel tab headers capitalize their words, all caps with
/// `workbench.experimental.modernUIUppercaseViewHeaders`; a workspace name the user chose keeps its case.
fn view_header_text(&self, text: &str, preserve_case: bool) -> String {
header_case(text, self.setting("workbench.experimental.modernUIUppercaseViewHeaders") == true, preserve_case)
}
/// The letterpress of an empty group, `size` wide (256px, 200px in a part of several groups): the VSRustium logo
/// in place of upstream's, in the fill and opacity of upstream's letterpress-*.svg for the theme kind.
fn watermark(&self, size: f32) -> Div {
let color = match self.theme.kind {
ThemeKind::Dark => gpui::black().opacity(0.3),
ThemeKind::Light => gpui::black().opacity(0.1),
ThemeKind::HighContrastDark => rgb(0x3c3c3c).into(),
ThemeKind::HighContrastLight => rgb(0xd9d9d9).into(),
};
div().child(svg().external_path(letterpress_path()).size(px(size)).text_color(color))
}
fn status_bar(&self, window: &Window, cx: &mut Context<Self>) -> Div {
let (foreground, background) = match self.folder {
_ if self.statusbar_in_debug_mode() => (self.color("statusBar.debuggingForeground"), self.color("statusBar.debuggingBackground")),
Some(_) => (self.color("statusBar.foreground"), self.color("statusBar.background")),
None => (self.color("statusBar.noFolderForeground"), self.color("statusBar.noFolderBackground")),
};
let duration = std::time::Duration::from_millis(150);
let (background, moving) = transition::follow(&self.status_bar_background, transition::rgba_values(background), duration, transition::ease_out, !self.is_motion_reduced());
let background = transition::values_rgba(background);
self.drawn_status_entries.borrow_mut().clear();
let window_id = window.window_handle().window_id();
self.status_entry_bounds.borrow_mut().retain(|(drawn_in, ..)| *drawn_in != window_id);
// statusbarpart.css: pointer only on items with a command; status.message has none.
let restricted_mode = self.restricted_mode_status_item(foreground, cx).and_then(|item| self.status_bar_entry("status.workspaceTrust", "Workspace Trust", None, true, item, cx));
let left_bell = self.notifications_status_item(true, foreground, cx).and_then(|item| self.status_bar_entry("status.notifications", "Notifications", None, false, item, cx));
let hidden_entries = self.hidden_status_entries();
let problems_first = restricted_mode.is_none() && left_bell.is_none() && !self.status_entries.iter().any(|entry| entry.align_left && entry.priority > PROBLEMS_PRIORITY && !hidden_entries.contains(&entry.id));
let problems = self.problems_status_item(problems_first, foreground, cx);
let problems = self.status_bar_entry("status.problems", "Problems", None, false, problems, cx);
let mut left = Vec::from_iter(restricted_mode.into_iter().chain(left_bell));
left.extend(self.extension_status_items(true, |priority| priority > PROBLEMS_PRIORITY, foreground, cx));
left.extend(problems);
left.extend(self.extension_status_items(true, |priority| priority <= PROBLEMS_PRIORITY, foreground, cx));
left.extend(self.status_bar_message().and_then(|message| self.status_bar_entry("status.message", "Status Message", None, false, message, cx)));
left.extend(self.window_progress_entry(cx.background_executor().now()).and_then(|entry| self.status_bar_extension_entry(&entry, foreground, cx)));
left.extend(self.terminal_recording_status_entry().and_then(|entry| self.status_bar_extension_entry(&entry, foreground, cx)));
let mut right = self.extension_status_items(false, |priority| priority > EDITOR_STATUS_PRIORITY, foreground, cx);
right.extend(self.editor_status_items(foreground, window, cx));
right.extend(self.extension_status_items(false, |priority| priority <= EDITOR_STATUS_PRIORITY, foreground, cx));
right.extend(self.notifications_status_item(false, foreground, cx).and_then(|item| self.status_bar_entry("status.notifications", "Notifications", None, false, item, cx)));
let height = self.status_bar_height();
div()
.relative()
.h(px(height))
.flex_none()
.track_focus(&self.status_bar_focus)
.on_mouse_down(MouseButton::Right, cx.listener(|workbench, event: &MouseDownEvent, window, cx| workbench.show_status_bar_context_menu(None, event.position, window, cx)))
.flex()
.justify_between()
.items_center()
.px(px(if self.compact_density() { 8. } else { 12. }))
.bg(background)
.text_color(foreground)
.text_size(px(12.))
// statusbarpart.css: font-variant-numeric: tabular-nums.
.font_features(FontFeatures(Arc::new(vec![("tnum".into(), 1)])))
// statusbarpart.css: a 1px `statusBar.focusBorder` outline inside the focused part, its bottom corners rounded on macOS.
.when_some(self.theme.color("statusBar.focusBorder").filter(|_| self.status_bar_focus.is_focused(window) && !self.status_entry_focus.contains_key(&window_id)), |bar, border| {
let radius = px(if is_tahoe_or_newer() { 16. } else { 10. });
bar.child(div().absolute().inset_0().border_1().rounded_bl(radius).rounded_br(radius).border_color(border))
})
.child(self.status_bar_side(left).h(px(height - 4.)))
.child(self.status_bar_side(right).h(px(height - 4.)))
.when(moving, |bar| bar.child(transition::next_frame()))
}
/// The `status.message` entry: multi-chord mode, else the latest status message (notificationsStatus.ts).
fn status_bar_message(&self) -> Option<Div> {
let message = self.chord_status().or_else(|| self.status_message.as_ref().map(|(message, _)| message.clone()))?;
let (margin, padding) = self.status_item_spacing();
Some(div().h_full().ml(px(margin)).px(px(padding)).flex().items_center().child(message))
}
/// A codicon centered in a square box, or the product icon theme's glyph for it. Drawn as SVG: Wu's text system
/// skips fonts without an 'm' glyph, so icon fonts cannot load (gpui_macos text_system.rs load_family).
fn icon(&self, name: &str, box_size: f32, icon_size: f32, color: Rgba) -> Div {
self.turned_icon(name, box_size, icon_size, color, 0.)
}
/// An icon rotated by `turns` of a full turn, clockwise.
fn turned_icon(&self, name: &str, box_size: f32, icon_size: f32, color: Rgba, turns: f32) -> Div {
self.styled_icon(name, box_size, icon_size, |icon| icon.text_color(color).with_transformation(gpui::Transformation::rotate(gpui::percentage(turns))))
}
/// An icon whose SVG `style` colors.
fn styled_icon(&self, name: &str, box_size: f32, icon_size: f32, style: impl FnOnce(gpui::Svg) -> gpui::Svg) -> Div {
let ids = crate::codicons::icon_ids(name);
let icon = match self.product_icons.as_ref().and_then(|theme| theme.svg(&ids, icon_size)) {
Some(glyph) => svg().data(glyph.as_bytes()),
None => {
let codicon = ids.last().expect("icon_ids starts with the id");
svg().external_path(crate::codicons::svg_path(&self.app_root, crate::symbol_icons::svg_name(codicon)).to_string_lossy().into_owned())
}
};
div()
.debug_selector(|| format!("codicon-{}", ids[0]))
.relative()
.size(px(box_size))
.flex_none()
.flex()
.items_center()
.justify_center()
.child(style(icon.size(px(icon_size))))
}
}
fn show_command_center_context_menu(workbench: &mut Workbench, event: &MouseDownEvent, window: &mut Window, cx: &mut Context<Workbench>) {
cx.stop_propagation();
workbench.show_command_center_context_menu(event.position, window, cx);
}
/// platform.ts `isTahoeOrNewer`: Darwin 25 is macOS 26.
#[cfg(target_os = "macos")]
fn is_tahoe_or_newer() -> bool {
static TAHOE: std::sync::LazyLock<bool> = std::sync::LazyLock::new(|| objc2_foundation::NSProcessInfo::processInfo().operatingSystemVersion().majorVersion >= 26);
*TAHOE
}
#[cfg(any(target_os = "linux", target_os = "windows"))]
fn is_tahoe_or_newer() -> bool {
false
}
/// gpui draws svgs from paths: the letterpress (port/AppIcon.icon's cursor in one fill, its eyes cut out, cropped to the logo) is
/// written once to the temporary folder.
fn letterpress_path() -> String {
static PATH: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| {
let path = std::env::temp_dir().join(format!("vsrustium-letterpress-{}.svg", std::process::id()));
std::fs::write(&path, include_str!("workbench/letterpress.svg")).expect("the temporary folder is writable");
path.to_string_lossy().into_owned()
});
PATH.clone()
}
/// CSS `text-transform: capitalize`, which modernUI fontRamp.css puts in place of the part titles' and the call stack
/// session states' `uppercase`.
fn capitalize_words(text: &str) -> String {
text.split(' ').map(|word| word.chars().next().map_or_else(String::new, |first| first.to_uppercase().chain(word.chars().skip(1)).collect())).collect::<Vec<_>>().join(" ")
}
fn header_case(text: &str, uppercase: bool, preserve_case: bool) -> String {
if uppercase {
return text.to_uppercase();
}
if preserve_case { text.to_string() } else { capitalize_words(text) }
}
#[cfg(test)]
mod scroll_frame_time;
#[cfg(test)]
mod tests {
use std::path::Path;
use config::workspace::Workspace;
use exthost::scan::scan_extension;
use gpui::{Modifiers, TestAppContext, point, px};
use serde_json::json;
use super::Workbench;
use crate::host_sync::HostSync;
use crate::languages::Languages;
use crate::theme::load_theme;
/// modernUI fontRamp.css: the Run and Debug title reads "Run And Debug", as CSS `capitalize` draws it.
#[test]
fn capitalize_words_uppercases_each_word_s_first_letter() {
assert_eq!(super::capitalize_words("Run and debug x"), "Run And Debug X");
}
#[test]
fn view_headers_capitalize_keep_the_workspace_name_or_go_all_caps() {
assert_eq!(super::header_case("npm scripts", false, false), "Npm Scripts");
assert_eq!(super::header_case("Explorer: my folder", false, true), "Explorer: my folder");
assert_eq!(super::header_case("Explorer: my folder", true, true), "EXPLORER: MY FOLDER");
}
/// A test's app, its window and workbench are freed when the test ends. Kept, they added about 15 MB per test until
/// the full run passed 24 GB and was killed.
#[gpui::test(iterations = 2)]
fn a_test_s_app_is_freed_when_the_test_ends(cx: &mut TestAppContext) {
thread_local!(static PREVIOUS_APP: std::cell::RefCell<std::rc::Weak<gpui::AppCell>> = Default::default());
assert_eq!(PREVIOUS_APP.with_borrow(|app| app.strong_count()), 0, "the previous iteration's app is still alive");
PREVIOUS_APP.set(std::rc::Rc::downgrade(&cx.app));
let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
let themes = scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap();
let theme = load_theme(&[themes], "Dark Modern").unwrap();
let folder = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../../port/fixtures/workspace");
let host = HostSync::new(std::sync::mpsc::channel().0);
cx.add_window_view(|_, cx| Workbench::new(theme, app_root.clone(), Some(Workspace::folder(folder)), Languages::new(&[]), crate::default_settings(), host, cx));
}
#[gpui::test]
fn clicking_a_folder_row_toggles_it(cx: &mut TestAppContext) {
let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
let themes = scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap();
let theme = load_theme(&[themes], "Dark Modern").unwrap();
let folder = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../../port/fixtures/workspace");
let docs = folder.join("docs");
let languages = Languages::new(&[]);
let host = HostSync::new(std::sync::mpsc::channel().0);
let (workbench, cx) = cx.add_window_view(|_, cx| Workbench::new(theme, app_root.clone(), Some(Workspace::folder(folder)), languages, crate::default_settings(), host, cx));
cx.run_until_parked();
// The first tree row (docs) spans y 96..118 under the side bar's "workspace" header (reference DOM dump).
let docs_row = point(px(120.), px(107.));
let docs_expanded = |cx: &mut gpui::VisualTestContext| {
workbench.read_with(cx, |workbench, _| workbench.folder.as_ref().unwrap().rows().iter().any(|row| row.path == docs && row.expanded))
};
cx.simulate_click(docs_row, Modifiers::none());
assert!(docs_expanded(cx));
cx.simulate_click(docs_row, Modifiers::none());
assert!(!docs_expanded(cx));
}
#[gpui::test]
fn inspect_key_mappings_json_opens_pinned_untitled_editors_with_the_layout(cx: &mut TestAppContext) {
let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
let themes = scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap();
let theme = load_theme(&[themes], "Dark Modern").unwrap();
let host = HostSync::new(std::sync::mpsc::channel().0);
let (workbench, cx) = cx.add_window_view(|_, cx| Workbench::new(theme, app_root.clone(), None, Languages::new(&[]), crate::default_settings(), host, cx));
for _ in 0..2 {
workbench.update_in(cx, |workbench, window, cx| drop(workbench.execute_command("workbench.action.inspectKeyMappingsJSON", Vec::new(), window, cx)));
}
cx.run_until_parked();
let editors = workbench.read_with(cx, |workbench, _| {
workbench.editors.editors.iter().map(|editor| (editor.document().uri_string(), editor.preview, editor.document().lines[..2].join("\n"))).collect::<Vec<_>>()
});
// Windows has no layout data yet: port/ledger.md M24 row app/src/keyboard_layout.rs:7.
let layout = if cfg!(windows) { "null," } else { "{" };
let expected = |name: &str| (format!("untitled:{name}"), false, format!("{{\n\t\"layout\": {layout}"));
assert_eq!(editors, [expected("Untitled-1"), expected("Untitled-2")]);
}
#[gpui::test]
fn keys_and_typed_text_edit_the_focused_editor_and_reach_the_host(cx: &mut TestAppContext) {
let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
let themes = scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap();
let theme = load_theme(&[themes], "Dark Modern").unwrap();
let dir = std::env::temp_dir().join(format!("code-gpui-keys-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let file = dir.join("a.txt");
std::fs::write(&file, "ab\ncd").unwrap();
let (sender, receiver) = std::sync::mpsc::channel();
let (workbench, cx) = cx.add_window_view(|window, cx| {
let mut workbench = Workbench::new(theme, app_root.clone(), None, Languages::new(&[]), crate::default_settings(), HostSync::new(sender), cx);
workbench.load_keybindings(crate::keybindings::default_keybindings(&[]), dir.join("keybindings.json"));
workbench.open_file(file.clone(), true, window, cx);
workbench
});
cx.run_until_parked();
receiver.try_iter().count();
let text = |cx: &mut gpui::VisualTestContext| workbench.read_with(cx, |workbench, _| workbench.editors.active().unwrap().document().lines.join("\n"));
cx.simulate_keystrokes("down end");
cx.simulate_input("!");
assert_eq!(text(cx), "ab\ncd!");
cx.simulate_keystrokes(&crate::keybindings::primary_keys("shift-home secondary-c"));
assert_eq!(cx.read_from_clipboard().and_then(|item| item.text()).as_deref(), Some("cd!"));
cx.simulate_keystrokes("backspace");
assert_eq!(text(cx), "ab\n");
cx.simulate_keystrokes(&crate::keybindings::primary_keys("secondary-z secondary-z enter tab"));
assert_eq!(text(cx), "ab\ncd\n ");
cx.simulate_keystrokes(&crate::keybindings::primary_keys("secondary-s"));
assert_eq!(std::fs::read_to_string(&file).unwrap(), "ab\ncd\n ");
assert!(workbench.read_with(cx, |workbench, _| !workbench.editors.active().unwrap().is_dirty()));
let methods: Vec<_> = receiver.try_iter().map(|message| message.method).collect();
assert_eq!(methods.iter().filter(|method| **method == "$acceptModelChanged").count(), 6);
assert_eq!(methods.last(), Some(&"$acceptModelSaved"));
std::fs::remove_dir_all(dir).unwrap();
}
#[gpui::test]
fn saving_runs_the_text_save_participants_of_the_settings(cx: &mut TestAppContext) {
let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
let themes = scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap();
let theme = load_theme(&[themes], "Dark Modern").unwrap();
let dir = std::env::temp_dir().join(format!("code-gpui-save-participants-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let file = dir.join("a.txt");
std::fs::write(&file, "ab \ncd\n\n\n").unwrap();
std::fs::create_dir_all(dir.join("User")).unwrap();
let settings = std::sync::Arc::new(std::sync::Mutex::new(config::service::Settings::load(config::registry::Registry::with_extensions(&[]), dir.join("User"), None)));
let (sender, receiver) = std::sync::mpsc::channel();
let (workbench, cx) = cx.add_window_view(|window, cx| {
let mut workbench = Workbench::new(theme, app_root.clone(), None, Languages::new(&[]), settings, HostSync::new(sender), cx);
workbench.load_keybindings(crate::keybindings::default_keybindings(&[]), dir.join("keybindings.json"));
workbench.open_file(file.clone(), true, window, cx);
workbench
});
cx.run_until_parked();
workbench.update(cx, |workbench, _| {
workbench.update_setting("files.trimTrailingWhitespace", json!(true));
workbench.update_setting("files.trimFinalNewlines", json!(true));
});
receiver.try_iter().count();
cx.simulate_keystrokes(&crate::keybindings::primary_keys("secondary-s"));
assert_eq!(std::fs::read_to_string(&file).unwrap(), "ab\ncd\n");
assert!(workbench.read_with(cx, |workbench, _| !workbench.editors.active().unwrap().is_dirty()));
let methods: Vec<_> = receiver.try_iter().map(|message| message.method).collect();
assert_eq!(methods.iter().filter(|method| **method == "$acceptModelChanged").count(), 2);
cx.simulate_keystrokes(&crate::keybindings::primary_keys("secondary-z"));
assert_eq!(workbench.read_with(cx, |workbench, _| workbench.editors.active().unwrap().document().lines.join("\n")), "ab \ncd\n\n\n");
std::fs::remove_dir_all(dir).unwrap();
}
#[gpui::test]
fn in_tab_focus_mode_tab_moves_focus_to_the_status_bar_instead_of_indenting(cx: &mut TestAppContext) {
let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
let themes = scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap();
let theme = load_theme(&[themes], "Dark Modern").unwrap();
let dir = std::env::temp_dir().join(format!("code-gpui-tab-focus-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let file = dir.join("a.txt");
std::fs::write(&file, "ab").unwrap();
let (workbench, cx) = cx.add_window_view(|window, cx| {
let mut workbench = Workbench::new(theme, app_root.clone(), None, Languages::new(&[]), crate::default_settings(), HostSync::new(std::sync::mpsc::channel().0), cx);
workbench.load_keybindings(crate::keybindings::default_keybindings(&[]), dir.join("keybindings.json"));
workbench.open_file(file.clone(), true, window, cx);
workbench.watch_editor_focus(window, cx);
workbench
});
cx.update(|window, _| window.activate_window());
cx.run_until_parked();
let state = |cx: &mut gpui::VisualTestContext| {
workbench.update_in(cx, |workbench, window, _| {
let moves_focus = workbench.key_context(window).get("editorTabMovesFocus").cloned();
let editor = workbench.editors.active().unwrap();
(editor.document().lines.join("\n"), editor.highlights.len(), workbench.status_bar_focus.is_focused(window), moves_focus)
})
};
// wordHighlighter.ts stops the highlights once no code editor has the focus.
// toggleTabFocusMode.ts: Ctrl+M outside macOS.
let toggle_tab_focus = if matches!(crate::keybindings::OS, crate::keybindings::Os::Macintosh) { "ctrl-shift-m tab" } else { "ctrl-m tab" };
cx.simulate_keystrokes(toggle_tab_focus);
assert_eq!(state(cx), ("ab".to_string(), 0, true, Some(json!(true))));
cx.simulate_keystrokes("shift-tab");
assert_eq!(state(cx), ("ab".to_string(), 1, false, Some(json!(true))));
cx.simulate_keystrokes(toggle_tab_focus);
assert_eq!(state(cx), (" ab".to_string(), 0, false, Some(json!(false))));
std::fs::remove_dir_all(dir).unwrap();
}
#[gpui::test]
fn user_keybindings_remove_defaults_and_bind_chords(cx: &mut TestAppContext) {
let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
let themes = scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap();
let theme = load_theme(&[themes], "Dark Modern").unwrap();
let dir = std::env::temp_dir().join(format!("code-gpui-keybindings-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let file = dir.join("a.txt");
std::fs::write(&file, "abc").unwrap();
let user_file = dir.join("keybindings.json");
let primary = if matches!(crate::keybindings::OS, crate::keybindings::Os::Macintosh) { "cmd" } else { "ctrl" };
let rules = format!(
r#"// keybindings.json is JSONC
[{{ "key": "end", "command": "-cursorEnd" }}, {{ "key": "{primary}+k {primary}+e", "command": "cursorEnd", "when": "editorTextFocus && mine" }}]"#
);
std::fs::write(&user_file, rules).unwrap();
let (workbench, cx) = cx.add_window_view(|window, cx| {
let mut workbench = Workbench::new(theme, app_root.clone(), None, Languages::new(&[]), crate::default_settings(), HostSync::new(std::sync::mpsc::channel().0), cx);
workbench.load_keybindings(crate::keybindings::default_keybindings(&[]), user_file.clone());
workbench.open_file(file.clone(), true, window, cx);
workbench
});
cx.run_until_parked();
let column = |cx: &mut gpui::VisualTestContext| workbench.read_with(cx, |workbench, _| workbench.editors.active().unwrap().selection().head.column);
let status = |cx: &mut gpui::VisualTestContext| workbench.read_with(cx, |workbench, _| workbench.chord_status());
cx.simulate_keystrokes("end");
assert_eq!(column(cx), 0, "the user removed the default End binding");
cx.simulate_keystrokes(&crate::keybindings::primary_keys("secondary-k secondary-e"));
assert_eq!(column(cx), 0, "the chord's when clause needs the `mine` context key");
workbench.update_in(cx, |workbench, window, cx| workbench.execute_command("_setContext", vec!["mine".into(), true.into()], window, cx));
cx.simulate_keystrokes(&crate::keybindings::primary_keys("secondary-k"));
let ctrl_k = if matches!(crate::keybindings::OS, crate::keybindings::Os::Macintosh) { "⌘K" } else { "Ctrl+K" };
assert_eq!(status(cx), Some(format!("({ctrl_k}) was pressed. Waiting for second key of chord...")));
cx.simulate_keystrokes(&crate::keybindings::primary_keys("secondary-e"));
assert_eq!(column(cx), 3);
assert_eq!(status(cx), None, "leaving chord mode drops its message");
cx.simulate_keystrokes(&crate::keybindings::primary_keys("secondary-k x"));
let message = workbench.read_with(cx, |workbench, _| workbench.status_message.as_ref().map(|(message, _)| message.clone()));
assert_eq!(message, Some(format!("The key combination ({ctrl_k}, X) is not a command.")));
cx.simulate_keystrokes(&crate::keybindings::primary_keys("secondary-k secondary-w"));
assert!(workbench.read_with(cx, |workbench, _| workbench.editors.editors.is_empty()), "workbench.action.closeAllEditors");
std::fs::remove_dir_all(dir).unwrap();
}
#[gpui::test]
fn folding_and_cursor_keys_run_through_the_keybindings(cx: &mut TestAppContext) {
let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
let themes = scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap();
let theme = load_theme(&[themes], "Dark Modern").unwrap();
let dir = std::env::temp_dir().join(format!("code-gpui-folding-keys-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let file = dir.join("a.txt");
std::fs::write(&file, "a\n b\n c\n d\ne\n f").unwrap();
let (sender, receiver) = std::sync::mpsc::channel();
let (workbench, cx) = cx.add_window_view(|window, cx| {
let mut workbench = Workbench::new(theme, app_root.clone(), None, Languages::new(&[]), crate::default_settings(), HostSync::new(sender), cx);
workbench.load_keybindings(crate::keybindings::default_keybindings(&[]), dir.join("keybindings.json"));
workbench.open_file(file.clone(), true, window, cx);
workbench
});
cx.run_until_parked();
let hidden = |cx: &mut gpui::VisualTestContext| workbench.read_with(cx, |workbench, _| workbench.editors.active().unwrap().folding.hidden().to_vec());
let cursors = |cx: &mut gpui::VisualTestContext| workbench.read_with(cx, |workbench, _| workbench.editors.active().unwrap().selections.len());
cx.simulate_keystrokes(&crate::keybindings::primary_keys("secondary-k secondary-0"));
assert_eq!(hidden(cx), [1..4, 5..6], "editor.foldAll");
cx.simulate_keystrokes(&crate::keybindings::primary_keys("secondary-k secondary-j"));
assert!(hidden(cx).is_empty(), "editor.unfoldAll");
// folding.ts: editor.fold and editor.unfold are Ctrl+Shift+[ and ] outside macOS.
let (fold, unfold) = if matches!(crate::keybindings::OS, crate::keybindings::Os::Macintosh) { ("alt-cmd-[", "alt-cmd-]") } else { ("ctrl-shift-[", "ctrl-shift-]") };
cx.simulate_keystrokes(fold);
assert_eq!(hidden(cx), [1..4], "editor.fold");
cx.simulate_keystrokes(unfold);
assert!(hidden(cx).is_empty(), "editor.unfold");
cx.simulate_keystrokes(&crate::keybindings::primary_keys("down secondary-k secondary-["));
assert_eq!(hidden(cx), [2..3], "editor.foldRecursively");
cx.simulate_keystrokes(&crate::keybindings::primary_keys("secondary-k secondary-]"));
assert!(hidden(cx).is_empty(), "editor.unfoldRecursively");
cx.simulate_keystrokes(&crate::keybindings::primary_keys("alt-secondary-down"));
assert_eq!(cursors(cx), 2, "editor.action.insertCursorBelow");
cx.simulate_keystrokes("escape");
assert_eq!(cursors(cx), 1, "removeSecondaryCursors wins over cancelSelection with several cursors");
// An edit that drops the folded lines changes the visible lines: the host hears the new visibleRanges.
cx.simulate_keystrokes("up");
cx.simulate_keystrokes(fold);
receiver.try_iter().count();
cx.simulate_keystrokes(&crate::keybindings::primary_keys("secondary-a"));
cx.simulate_input("x");
let visible_ranges: Vec<_> = receiver.try_iter().filter(|message| message.method == "$acceptEditorPropertiesChanged" && !message.args[1]["visibleRanges"].is_null()).collect();
assert_eq!(visible_ranges.len(), 1, "{visible_ranges:?}");
std::fs::remove_dir_all(dir).unwrap();
}
#[gpui::test]
fn folding_the_cursor_line_moves_the_cursor_to_the_fold_start(cx: &mut TestAppContext) {
let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
let themes = scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap();
let theme = load_theme(&[themes], "Dark Modern").unwrap();
let file = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../../port/fixtures/folding/long.py");
// A host that is up, as in the app: no extension declares the `onCommand` events of the keys.
let (sender, host_receiver) = std::sync::mpsc::channel();
let mut host = HostSync::new(sender);
host.installed_extensions_registered = true;
let (workbench, cx) = cx.add_window_view(|window, cx| {
let mut workbench = Workbench::new(theme, app_root.clone(), None, Languages::new(&[]), crate::default_settings(), host, cx);
workbench.load_keybindings(crate::keybindings::default_keybindings(&[]), std::env::temp_dir().join("code-gpui-fold-cursor-keybindings.json"));
workbench.open_file(file.clone(), true, window, cx);
workbench
});
cx.run_until_parked();
// The keys of the pixel-check "folded" window, dispatched like `--screenshot-keys` in main.rs.
cx.update(|window, cx| {
// cursorBottom and editor.fold are Ctrl+End and Ctrl+Shift+[ outside macOS.
let keys = if matches!(crate::keybindings::OS, crate::keybindings::Os::Macintosh) { "cmd-down up alt-cmd-[ up up up up up up up up up up up up alt-cmd-[" } else { "ctrl-end up ctrl-shift-[ up up up up up up up up up up up up ctrl-shift-[" };
for key in keys.split(' ') {
window.dispatch_keystroke(gpui::Keystroke::parse(key).unwrap(), cx);
}
});
// The host answers each event on its own turn, in the order it was asked.
for message in host_receiver.try_iter().filter(|message| message.method == "$activateByEvent") {
message.reply.unwrap().send(Ok(serde_json::Value::Null)).unwrap();
cx.run_until_parked();
}
let (hidden, head) = workbench.read_with(cx, |workbench, _| {
let editor = workbench.editors.active().unwrap();
(editor.folding.hidden().to_vec(), editor.selection().head)
});
assert_eq!((hidden, head.line, head.column), (vec![75..89, 99..101], 74, 25), "`def merge` and `if __name__` folded, cursor on line 75");
}
#[gpui::test]
fn selection_anchor_keys_select_from_the_anchor(cx: &mut TestAppContext) {
use crate::editor::{Position, Selection};
let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
let themes = scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap();
let theme = load_theme(&[themes], "Dark Modern").unwrap();
let dir = std::env::temp_dir().join(format!("code-gpui-anchor-keys-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let file = dir.join("a.txt");
std::fs::write(&file, "hello\nworld").unwrap();
let (sender, _receiver) = std::sync::mpsc::channel();
let (workbench, cx) = cx.add_window_view(|window, cx| {
let mut workbench = Workbench::new(theme, app_root.clone(), None, Languages::new(&[]), crate::default_settings(), HostSync::new(sender), cx);
workbench.load_keybindings(crate::keybindings::default_keybindings(&[]), dir.join("keybindings.json"));
workbench.open_file(file.clone(), true, window, cx);
workbench
});
cx.run_until_parked();
let state = |cx: &mut gpui::VisualTestContext| {
workbench.read_with(cx, |workbench, _| {
let editor = workbench.editors.active().unwrap();
(editor.selection_anchor, editor.selections.clone())
})
};
cx.simulate_keystrokes(&crate::keybindings::primary_keys("right secondary-k secondary-b"));
assert_eq!(state(cx).0, Some(Position::new(0, 1)), "editor.action.setSelectionAnchor");
assert_eq!(workbench.read_with(cx, |workbench, _| workbench.accessibility.alert.clone().unwrap().0), "Anchor set at 1:2");
cx.simulate_input("ab");
assert_eq!(state(cx).0, Some(Position::new(0, 3)), "typing at the anchor pushes it");
cx.simulate_keystrokes("escape");
assert_eq!(state(cx).0, None, "editor.action.cancelSelectionAnchor");
cx.simulate_keystrokes(&crate::keybindings::primary_keys("secondary-k secondary-b down secondary-k secondary-k"));
assert_eq!(state(cx), (None, vec![Selection { anchor: Position::new(0, 3), head: Position::new(1, 3) }]), "editor.action.selectFromAnchorToCursor");
std::fs::remove_dir_all(dir).unwrap();
}
#[gpui::test]
fn dragging_selected_text_moves_it_and_a_key_cancels_the_drag(cx: &mut TestAppContext) {
use crate::editor::{Position, Selection};
let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
let themes = scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap();
let theme = load_theme(&[themes], "Dark Modern").unwrap();
let dir = std::env::temp_dir().join(format!("code-gpui-text-drag-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let file = dir.join("a.txt");
std::fs::write(&file, "Port notes for\nthe editor").unwrap();
let (sender, _receiver) = std::sync::mpsc::channel();
let (workbench, cx) = cx.add_window_view(|window, cx| {
let mut workbench = Workbench::new(theme, app_root.clone(), None, Languages::new(&[]), crate::default_settings(), HostSync::new(sender), cx);
workbench.open_file(file.clone(), true, window, cx);
workbench
});
cx.run_until_parked();
let notes = Selection { anchor: Position::new(0, 5), head: Position::new(0, 10) };
let alt = gpui::Modifiers { alt: true, ..Default::default() };
workbench.update(cx, |workbench, cx| {
workbench.editors.active_mut().unwrap().set_selections(vec![notes]);
assert!(!workbench.start_text_drag(0, 7., alt, 1), "Alt adds a cursor instead");
assert!(!workbench.start_text_drag(0, 7., Default::default(), 2), "a double click selects a word");
assert!(!workbench.start_text_drag(0, 2., Default::default(), 1), "outside the selection");
assert!(workbench.start_text_drag(0, 7., Default::default(), 1));
workbench.drag_text(0, 8., false, cx);
assert_eq!(workbench.text_drag.as_ref().unwrap().target, None, "no target inside the selection");
workbench.drag_text(1, 7., false, cx);
assert_eq!(workbench.text_drag.as_ref().unwrap().target, Some(Position::new(1, 7)));
assert_eq!(workbench.text_cursor_style(), gpui::CursorStyle::Arrow);
workbench.drop_text(1, 7., Default::default(), cx);
let editor = workbench.editors.active().unwrap();
assert_eq!(editor.document().lines, ["Port for", "the edinotestor"]);
assert_eq!(editor.selections, [Selection { anchor: Position::new(1, 7), head: Position::new(1, 12) }]);
assert!(workbench.start_text_drag(1, 9., Default::default(), 1));
workbench.drag_text(1, 9., true, cx);
workbench.drag_text(0, 0., true, cx);
assert_eq!(workbench.text_cursor_style(), gpui::CursorStyle::DragCopy);
});
cx.simulate_keystrokes("escape");
workbench.read_with(cx, |workbench, _| assert!(workbench.text_drag.is_none(), "a key press cancels the drag"));
std::fs::remove_dir_all(dir).unwrap();
}
#[gpui::test]
fn comment_keys_toggle_the_comments_of_the_file_language(cx: &mut TestAppContext) {
let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
let themes = scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap();
let theme = load_theme(&[themes], "Dark Modern").unwrap();
let typescript = scan_extension(&app_root.join("extensions/typescript-basics"), true).unwrap();
let dir = std::env::temp_dir().join(format!("code-gpui-comment-keys-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let file = dir.join("a.ts");
std::fs::write(&file, "let a = 1;\n let b = 2;").unwrap();
let (sender, _receiver) = std::sync::mpsc::channel();
let (workbench, cx) = cx.add_window_view(|window, cx| {
let mut workbench = Workbench::new(theme, app_root.clone(), None, Languages::new(&[typescript]), crate::default_settings(), HostSync::new(sender), cx);
workbench.load_keybindings(crate::keybindings::default_keybindings(&[]), dir.join("keybindings.json"));
workbench.open_file(file.clone(), true, window, cx);
workbench
});
cx.run_until_parked();
let lines = |cx: &mut gpui::VisualTestContext| workbench.read_with(cx, |workbench, _| workbench.editors.active().unwrap().document().lines.clone());
cx.simulate_keystrokes(&crate::keybindings::primary_keys("secondary-/"));
assert_eq!(lines(cx), ["// let a = 1;", " let b = 2;"], "editor.action.commentLine");
cx.simulate_keystrokes(&crate::keybindings::primary_keys("secondary-/"));
assert_eq!(lines(cx), ["let a = 1;", " let b = 2;"], "editor.action.commentLine removes");
cx.simulate_keystrokes(&crate::keybindings::primary_keys("secondary-a secondary-k secondary-c"));
assert_eq!(lines(cx), ["// let a = 1;", "// let b = 2;"], "editor.action.addCommentLine");
cx.simulate_keystrokes(&crate::keybindings::primary_keys("secondary-k secondary-u"));
assert_eq!(lines(cx), ["let a = 1;", " let b = 2;"], "editor.action.removeCommentLine");
cx.simulate_keystrokes("shift-alt-a");
assert_eq!(lines(cx), ["/* let a = 1;", " let b = 2; */"], "editor.action.blockComment");
cx.simulate_keystrokes(&crate::keybindings::primary_keys("secondary-z"));
assert_eq!(lines(cx), ["let a = 1;", " let b = 2;"], "undo");
let second = dir.join("b.ts");
std::fs::write(&second, "export function main() {\n\treturn 42;\n}\n").unwrap();
cx.update(|window, cx| workbench.update(cx, |workbench, cx| workbench.open_file(second.clone(), true, window, cx)));
cx.run_until_parked();
cx.simulate_keystrokes(&crate::keybindings::primary_keys("down end secondary-/ shift-alt-a secondary-a secondary-k secondary-c"));
assert_eq!(lines(cx), ["// export function main() {", "// \t// return 42;/* */", "// }", ""], "the reference's steps");
std::fs::remove_dir_all(dir).unwrap();
}
#[gpui::test]
fn line_operation_keys_edit_the_lines(cx: &mut TestAppContext) {
let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
let themes = scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap();
let theme = load_theme(&[themes], "Dark Modern").unwrap();
let dir = std::env::temp_dir().join(format!("code-gpui-line-keys-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let file = dir.join("a.txt");
std::fs::write(&file, "one\ntwo\nthree").unwrap();
let (sender, _receiver) = std::sync::mpsc::channel();
let (workbench, cx) = cx.add_window_view(|window, cx| {
let mut workbench = Workbench::new(theme, app_root.clone(), None, Languages::new(&[]), crate::default_settings(), HostSync::new(sender), cx);
workbench.load_keybindings(crate::keybindings::default_keybindings(&[]), dir.join("keybindings.json"));
workbench.open_file(file.clone(), true, window, cx);
workbench
});
cx.run_until_parked();
let lines = |cx: &mut gpui::VisualTestContext| workbench.read_with(cx, |workbench, _| workbench.editors.active().unwrap().document().lines.clone());
cx.simulate_keystrokes("alt-down");
assert_eq!(lines(cx), ["two", "one", "three"], "editor.action.moveLinesDownAction");
cx.simulate_keystrokes("shift-alt-down");
assert_eq!(lines(cx), ["two", "one", "one", "three"], "editor.action.copyLinesDownAction");
// linesOperations.ts binds joinLines on macOS only.
match matches!(crate::keybindings::OS, crate::keybindings::Os::Macintosh) {
true => cx.simulate_keystrokes("ctrl-j"),
false => workbench.update_in(cx, |workbench, window, cx| drop(workbench.execute_command("editor.action.joinLines", Vec::new(), window, cx))),
}
assert_eq!(lines(cx), ["two", "one", "one three"], "editor.action.joinLines");
cx.simulate_keystrokes(&crate::keybindings::primary_keys("shift-secondary-k"));
assert_eq!(lines(cx), ["two", "one"], "editor.action.deleteLines");
cx.simulate_keystrokes(&crate::keybindings::primary_keys("secondary-]"));
assert_eq!(lines(cx), ["two", " one"], "editor.action.indentLines");
std::fs::remove_dir_all(dir).unwrap();
}
#[gpui::test]
fn the_find_widget_searches_what_its_inputs_hold(cx: &mut TestAppContext) {
let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
let themes = scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap();
let theme = load_theme(&[themes], "Dark Modern").unwrap();
let dir = std::env::temp_dir().join(format!("code-gpui-find-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let file = dir.join("a.txt");
std::fs::write(&file, "the cat\nand the dog\nthe end").unwrap();
let (workbench, cx) = cx.add_window_view(|window, cx| {
let mut workbench = Workbench::new(theme, app_root.clone(), None, Languages::new(&[]), crate::default_settings(), HostSync::new(std::sync::mpsc::channel().0), cx);
workbench.load_keybindings(crate::keybindings::default_keybindings(&[]), dir.join("keybindings.json"));
workbench.open_file(file.clone(), true, window, cx);
workbench
});
cx.run_until_parked();
let run = |command: &str, cx: &mut gpui::VisualTestContext| workbench.update_in(cx, |workbench, window, cx| workbench.execute_command(command, vec![], window, cx));
let found = |cx: &mut gpui::VisualTestContext| {
workbench.read_with(cx, |workbench, _| {
let state = &workbench.find.controller.state;
let selection = workbench.editors.active().unwrap().selection();
(state.matches_position, state.matches_count, (selection.anchor.line, selection.anchor.column))
})
};
run("actions.find", cx);
cx.run_until_parked();
cx.simulate_input("the");
assert_eq!(found(cx), (1, 3, (0, 0)));
let zone = |cx: &mut gpui::VisualTestContext| workbench.read_with(cx, |workbench, _| (workbench.editors.viewport.top_whitespace, workbench.editors.active().unwrap().scroll.top));
assert_eq!(zone(cx), (33., 0.), "the match on line 1 stays revealed below the zone");
// findInputFocussed binds Enter to Find Next.
cx.simulate_keystrokes("enter");
assert_eq!(found(cx), (2, 3, (1, 4)));
// findDecorations.ts: the ruler and minimap marks, the current match last.
let marks = workbench.read_with(cx, |workbench, _| {
let marks = workbench.find_marks(workbench.editors.active().unwrap(), 400.);
marks.iter().map(|mark| (mark.range.start.line, mark.in_minimap, mark.current)).collect::<Vec<_>>()
});
assert_eq!(marks, [(0, true, false), (2, true, false), (1, true, true)]);
run("editor.action.goToMatchFindAction", cx);
cx.simulate_input("-1");
assert_eq!(found(cx), (3, 3, (2, 0)));
assert!(workbench.read_with(cx, |workbench, _| workbench.find.go_to_match_highlight.is_some()));
cx.simulate_input("9");
let message = workbench.read_with(cx, |workbench, _| workbench.quick_inputs.current().unwrap().validation_message.clone());
assert_eq!(message.as_deref(), Some("Please type a number between 1 and 3"));
cx.simulate_keystrokes("backspace backspace backspace 2 enter");
assert_eq!(found(cx), (2, 3, (1, 4)));
assert!(workbench.read_with(cx, |workbench, _| workbench.quick_inputs.current().is_none() && workbench.find.go_to_match_highlight.is_none()));
cx.simulate_keystrokes("backspace backspace");
assert_eq!(workbench.read_with(cx, |workbench, _| workbench.find.controller.state.search_string.clone()), "t");
// The find input has the focus, so the replace input takes it.
run("editor.action.startFindReplaceAction", cx);
cx.simulate_input("a");
cx.simulate_keystrokes("shift-tab");
cx.simulate_input("the");
cx.simulate_keystrokes(&crate::keybindings::primary_keys("alt-secondary-enter"));
assert_eq!(zone(cx).0, 62.);
let text = workbench.read_with(cx, |workbench, _| workbench.editors.active().unwrap().document().lines.join("\n"));
assert_eq!(text, "a cat\nand a dog\na end");
cx.simulate_keystrokes("escape");
assert!(workbench.read_with(cx, |workbench, _| !workbench.find.controller.state.is_revealed));
assert_eq!(zone(cx), (0., 0.));
let args = serde_json::json!({ "searchString": "a", "isRegex": true });
workbench.update_in(cx, |workbench, window, cx| workbench.execute_command("editor.actions.findWithArgs", vec![args], window, cx));
let state = workbench.read_with(cx, |workbench, _| {
let state = &workbench.find.controller.state;
(state.search_string.clone(), state.is_regex, state.is_replace_revealed, state.matches_count)
});
assert_eq!(state, ("a".to_string(), true, false, 5));
// saveQueryState and the history delayer write the workspace storage.
cx.executor().advance_clock(std::time::Duration::from_millis(500));
cx.run_until_parked();
let stored = |key: &str, cx: &mut gpui::VisualTestContext| workbench.read_with(cx, |workbench, _| workbench.workspace_storage.lock().unwrap().get(key).map(str::to_owned));
assert_eq!(stored("editor.isRegex", cx).as_deref(), Some("true"));
assert_eq!(stored("workbench.find.history", cx).as_deref(), Some(r#"["a"]"#));
cx.simulate_input("b");
cx.simulate_keystrokes("up");
assert_eq!(workbench.read_with(cx, |workbench, _| workbench.find.controller.state.search_string.clone()), "a");
assert_eq!(workbench.read_with(cx, |workbench, _| workbench.find.find_history.elements().len()), 2);
// loadQueryState and the history's load on the next start.
let restored = workbench.update(cx, |workbench, _| {
workbench.find.find_history.clear();
workbench.restore_workspace_state(workbench.workspace_storage.clone());
(workbench.find.controller.state.is_regex, workbench.find.find_history.elements().len())
});
assert_eq!(restored, (true, 2));
use crate::editor::{Position, Selection};
// wordHighlighter.ts: a match found from the find input keeps the editor's highlighted word.
workbench.update(cx, |workbench, _| workbench.editors.active_mut().unwrap().set_selections(vec![Selection::caret(Position::new(0, 3))]));
cx.simulate_input("n");
cx.simulate_keystrokes("enter");
let highlights = workbench.read_with(cx, |workbench, _| {
let editor = workbench.editors.active().unwrap();
(workbench.find.controller.state.search_string.clone(), editor.selection().head, editor.highlights.clone())
});
assert_eq!(highlights, ("an".to_string(), Position::new(1, 2), vec![(0, 2..5)]));
// onDidFocusEditorText: closing the widget focuses the editor, whose selection's word is highlighted.
cx.simulate_keystrokes("escape");
assert_eq!(workbench.read_with(cx, |workbench, _| workbench.editors.active().unwrap().highlights.clone()), vec![(1, 0..3)]);
// SimpleButton: a focused button triggers on Enter.
run("actions.find", cx);
cx.simulate_input("a");
let position = |cx: &mut gpui::VisualTestContext| workbench.read_with(cx, |workbench, _| workbench.find.controller.state.matches_position);
let before = position(cx);
workbench.update_in(cx, |workbench, window, cx| window.focus(&workbench.find.button_focus["find-next"].clone(), cx));
cx.simulate_keystrokes("enter");
assert_eq!(position(cx), before + 1);
// A click on the widget's background, between the sash and the replace toggle, blurs and stops the highlights.
workbench.update(cx, |workbench, cx| {
// The slide in runs on the wall clock.
workbench.find.toggled = None;
workbench.editors.active_mut().unwrap().highlights = vec![(0, 0..1)];
cx.notify();
});
cx.run_until_parked();
let widget = cx.debug_bounds("find-widget").unwrap();
cx.simulate_click(widget.origin + point(px(3.5), px(16.)), Modifiers::none());
let blurred = workbench.update_in(cx, |workbench, window, cx| (window.focused(cx).is_none(), workbench.editors.active().unwrap().highlights.is_empty()));
assert_eq!(blurred, (true, true));
std::fs::remove_dir_all(dir).unwrap();
}
}