Skip to content

Visual Studio Code, rebuilt in Rust

Rust
8.5 KB
Raw Blame

Workbench

The workbench is everything in a window around the editors: the layout, the parts, the views, keys, commands and menus. This file goes one level under ARCHITECTURE.md, section "Layers inside app". Upstream is src/vs/workbench/browser and src/vs/workbench/services.

One entity, many files

app/src/workbench.rs::Workbench is the one GPUI entity of a window. It has a field for every model of the window: the file tree, the editor groups, the terminals, the settings, the storage, the extension host queue.

The files in app/src/workbench do not define types of their own that own state. Each one is an impl Workbench block for one upstream feature, named after it: app/src/workbench/panel.rs for panelPart.ts, app/src/workbench/view_locations.rs for viewDescriptorService.ts. A file holds the drawing of the feature, its input handling, its commands and its storage keys. Small state structs of a feature are fields of Workbench, defined in the feature's file.

This shape replaces upstream's services and dependency injection. A feature that needs another feature calls its method on self. Jump-to-definition finds every call.

Invariant: no file in app/src/workbench keeps a copy of state that another file owns. Read it from the Workbench field each time.

Do not add a new entity for a feature because it "feels like a component". A new entity is right only for a separate window (app/src/workbench/auxiliary_windows.rs::AuxiliaryWindow) or for caching a part (app/src/workbench/part_view.rs::PartView).

Layout

The window is a title bar, a grid of parts, and a status bar.

  • app/src/workbench/layout.rs::Part names the grid parts: activity bar, side bar, editor, panel, auxiliary bar. app/src/workbench/layout.rs::Grid is upstream's grid of layout.ts for the floating (modern) layout. It computes each part's rectangle from the window size, the part sizes and the positions of the side bar and the panel.
  • app/src/workbench/part_grid.rs::PartGrid holds part sizes and visibility, stores them under upstream's LayoutStateModel keys, and has the layout commands and the sashes between parts.
  • app/src/workbench/part_view.rs::CachedPart lists what draws as its own cached view: each grid part, the title bar and the status bar. Workbench::render places these views. A cached view draws again only after a notify.
  • Sizes are upstream's CSS pixels, taken from the reference's CSS and checked with port/tools/pixel-check and port/tools/ref-dom. Do not tune a size by eye.

Invariant: a notify of the Workbench draws every part. A change that stays inside one part must notify that part's view: app/src/workbench/part_view.rs::notify_editor_view is the pattern. app/src/workbench/part_view/tests.rs counts renders per part and fails when a scroll in one part draws another.

Trap: a size measured during a draw cannot call Window::refresh in that draw. Use app/src/workbench/part_view.rs::redraw_after_frame.

Parts

Part Code
Title bar and command center app/src/workbench.rs, app/src/workbench/title_bar_actions.rs, app/src/workbench/update_title_bar.rs
Native menu bar (macOS) app/src/workbench/menubar.rs
Activity bar app/src/workbench/activity.rs, app/src/workbench/activity_bar_menu.rs
Side bar app/src/workbench/side_bar.rs
Panel and secondary side bar app/src/workbench/panel.rs
Editor part app/src/workbench/editor_groups.rs, app/src/editor_grid.rs, app/src/workbench/editor_view.rs (editor.md)
Status bar app/src/workbench/status_entries.rs, app/src/workbench/editor_status.rs, app/src/main_thread_status_bar.rs

Views and view containers

A view container is an icon in the activity bar or a tab in the panel. A view is a pane inside a container.

  • app/src/workbench/view_containers.rs: the containers, built in ones and the ones extensions contribute.
  • app/src/workbench/view_locations.rs: which container each view is in, after the user moved views (views.customizations).
  • app/src/workbench/view_visibility.rs: which views are hidden.
  • app/src/workbench/view_panes.rs: the collapsible panes of a container with their sizes.
  • app/src/workbench/tree_view.rs and app/src/workbench/webview_views.rs: the two kinds of views an extension contributes.

Each built-in view has a model in app/src and a view file: app/src/explorer.rs with app/src/workbench/explorer_actions.rs, app/src/search_model.rs with app/src/workbench/search_view.rs, app/src/scm.rs with app/src/workbench/scm_view.rs.

Editor groups

app/src/editor.rs::EditorGroup is the whole editor part. Its direct fields are the active group. The other groups wait in other_groups. app/src/editor_grid.rs::EditorGrid is the split layout.

Code that draws or changes a group that is not the active one runs inside app/src/workbench/editor_groups.rs::with_group. It swaps that group in as the active one, runs the closure and swaps it back. app/src/workbench/code_editors.rs::with_code_editor does the same for the second code editor of one tab (a diff side, a notebook cell).

Trap: inside with_group the "active" group is not the group the user sees as active. Code that reads focus or sends the active editor to the extension host must not run there.

Keys, commands, context keys, menus

One path serves the keyboard, the mouse, menus and extensions:

  1. app/src/workbench.rs intercepts every keystroke of the window before the focused element gets it.
  2. app/src/workbench/keyboard.rs::dispatch_key asks app/src/keybindings.rs::Resolver for the command. The resolver holds the default table of the OS, extension keybindings and the user's keybindings.json, in upstream's order. Chords of two keys and their status messages are handled there.
  3. Each keybinding has a when clause. app/src/contextkey.rs::Expr evaluates it against the context keys that app/src/workbench/keyboard.rs::key_context builds from the workbench state and the focus.
  4. app/src/commands.rs::execute_command runs the command. A feature registers its commands in Workbench::new through app/src/commands.rs::register, under upstream's command ids. A command of an extension is a handler in the same registry that calls the host, and execute_command first waits for the onCommand activation of the id, as upstream does.
  5. Menus are data. app/src/menus.rs holds upstream's menu ids with their items, groups, order and when clauses. A context menu (app/src/workbench/context_menu.rs), the menu bar and a view title read the same registry and call execute_command.

Invariant: key_context is cheap. It runs for every key and several times per frame for menus and toolbars. It must not read the disk, compile a regex or walk a whole list. A value that is costly to compute is cached where the state changes.

Do not handle a key in an element's on_key_down for a command that upstream binds through a keybinding. The user cannot rebind it, and the when clause is lost.

Overlays

Quick input (app/src/quick_input.rs, app/src/workbench/quick_input_view.rs), the command palette (app/src/command_palette.rs), hovers (app/src/workbench/hover.rs), notifications (app/src/notifications.rs, app/src/workbench/notifications_view.rs) and dialogs (app/src/dialog.rs) draw over the grid from Workbench::render. Each follows its upstream widget for position, size and keyboard handling.

Lists

A list draws only the rows in view, as upstream's listView.ts. app/src/workbench/list_scroll.rs holds the shared scroll behavior: wheel sensitivity, smooth scrolling, scrollbars, the list.scroll* commands. A new list must not lay out rows outside the view: 800 Explorer rows cost 190 ms per frame before the Explorer drew only the visible ones (port/notes.md, "Benchmarks").

Add a workbench feature

  1. Find the unit in the ledger (rg -n '^\| workbench/contrib/<name> ' port/ledger/units) and read the upstream files it names.
  2. Put the logic that needs no window in a model in app/src, with upstream's tests.
  3. Add a file in app/src/workbench with the impl Workbench block: drawing, input, commands under upstream's ids, storage under upstream's keys.
  4. If the extension host must know, send through app/src/host_sync.rs::HostSync (extension-host.md).
  5. Add #[gpui::test] tests that drive the feature with keys and clicks (testing.md).
  6. Compare with VS Code: port/tools/pixel-check for a still window, port/tools/ui-pair for a state reached by input.
  7. Update the unit's row in the ledger in the same commit.