Vampir repository guide #
Vampir is a widget toolkit for GPUI. It is a plain library crate with no application inside it: gpui and unicode-segmentation are the only dependencies, and it builds anywhere GPUI does. gpui is the crates.io gpui-ce release (the community edition of GPUI, Apache-2.0, notice in THIRD_PARTY.md) renamed in Cargo.toml; do not change it back to a git dependency, that is what makes the crate publishable.
This is a short working guide for contributors and coding tools. The detailed documentation is in docs/; use the relevant page when you need more context.
docs/hosting.md |
Wiring the toolkit into a view: the ControlHost impl, pumping drags, asking for frames, ids, themes. |
docs/widgets.md |
Every widget in the crate and what each is for. |
docs/keyboard.md |
Keyboard and focus: what the controls handle themselves, what the host binds, where the ring goes. |
docs/design.md |
Why it looks the way it does, and the rules that keep it coherent. |
docs/writing-a-control.md |
The shape a control has to keep, the traps, the checklist. |
docs/macos-apps.md |
The menu bar, the shortcuts and the bundle — what macOS does not give you for free. |
CONTRIBUTING.md |
How a change gets in, and the bar it has to clear. |
The basic model #
A control is a function of the data it is handed. It takes a value, a Palette, and a Context<V>, and returns an element. It owns nothing.
What cannot be a pure function is small, always the same shape, and lives in one place: ControlState. That is which pop-up is open and how far into its fade, when each animated control last changed, and whatever is being dragged. Nothing else.
This keeps controls generic over the host view rather than tying them to one application. The host owns application data and callbacks.
Nothing changes in a single frame. Every visible change of state animates, with one of the three durations in design.md › Motion, and it is noticed from the value during render (ControlState::blend, tween) rather than started by the click, so a shortcut or a menu item moves a control exactly as the pointer does. A control that animates takes a WidgetContext — the palette, the host and the GPUI context in one parameter — so it can read ControlState; one that does not takes a Palette and a cx.
Core conventions #
Keep these conventions in mind when changing the library. Each is explained in more detail in the linked documentation.
Shape — every control is a free function generic over V: ControlHost, taking Palette by value, inside a WidgetContext when it also needs the host's state. No structs with builders, no trait objects, no RootView anywhere. State that has to survive a frame belongs to the host, not to the control. → writing-a-control.md
Light comes from above. Raised means you press it; recessed means you put something in it. Never hard-code a shadow or a gradient — add a recipe to lighting instead. → design.md
Both colour schemes, always. A light-only or dark-only path is a bug; the is_dark flag exists so one code path covers both. Only the roles in Palette; adding a role means covering every hue, saturation and both schemes in palette generation. → design.md
Choose the app's colours as part of its design. The developer or coding agent chooses Theme::hue and Theme::saturation for the app's purpose and existing brand, and sets them in the host's constructor. Use saturation 0.0 for grey, values below 1.0 for a muted tool, 1.0 for the standard palette, and up to 3.0 for a vivid app. Make and briefly explain that design decision; do not routinely ask the user to pick a hue or saturation. Do not add hue or saturation sliders to every app: expose them only when theme editing or personalisation is part of that app's requirements. The gallery's Colour page demonstrates the optional pickers; it is not a template for app chrome. Keep following the desktop's light/dark scheme unless the app's requirements say otherwise, and check text contrast at the chosen saturation in both schemes. → hosting.md
Shared metrics. CONTROL_HEIGHT (30) and CONTROL_RADIUS (6), so a row of mixed controls lines up without tuning. CHIP_HEIGHT (26) is the one deliberate exception. → design.md
Select on release, not on press, because pressing is also how a drag starts. Handle the release that never comes. Anything that blocks the mouse must forward moves and releases. Disable rather than remove. → design.md
Ring the active element, and if it holds options, the current option inside it. A group is one tab stop; the ring goes on the chosen dot, the active tab, the slider's thumb — never around the container. Clicking an option puts the keyboard on it, so Tab carries on from there. Tab, Shift-Tab, Escape and text editing are the toolkit's to bind (bind_keys, root); a host binds only its own shortcuts. Anything that takes the keyboard hands it back when it closes, or to the root when there is nowhere to hand it back to. → keyboard.md
The gallery shows widgets; the crate does the work. Anything the gallery would have to implement to demonstrate a widget — focus return, key routing, a fade, a theme, a layout helper — belongs in the crate, so the next host gets it too. A host-side pattern in the gallery that runs to more than a few lines is a missing API, not an example. → hosting.md
Ids are &'static str, and are both the element id and the widget's identity in ControlState. Two widgets of the same kind in one view must not share one. → hosting.md
Comments say why, not what. Test the logic, not the pixels. Keep drawing thin and put the thinking somewhere a test can reach. → writing-a-control.md
Visual patterns. A coloured bar down the side of a selected row, underlined text tabs, uppercase micro-captions, monospace labels, status dots, nested rounded cards, a hairline between every row, flat surfaces. See the design guide for the reasoning behind these choices. → design.md
Working in the repository #
cargo run --example gallery # every widget, one window (add --features command-bar-window for the Native view page's bar)
cargo fmt
cargo clippy --all-targets -- -D warnings
cargo test
CI runs exactly those three checks and then builds the gallery. The first build is slow, because GPUI is large.
examples/gallery.rs is the closest thing to a manual test suite. A change that is visible should be visible there, in both schemes. It is also the smallest complete host, so it is where host-side patterns get demonstrated rather than only described.
Avoid adding dependencies. The crate currently has two, which keeps it easy to integrate with projects that already use GPUI.
When you add a control, the checklist at the end of writing-a-control.md lists everywhere it has to be registered — lib.rs, the docs table, the README list, the gallery and a test.
Anything added to the toolkit must be added to the gallery and shown working in a screenshot, or the pull request is rejected. Not a mock-up and not a widget sitting inert: the behaviour, on screen, from the real binary. The next section is how to produce that without a hand on the mouse.
Prose in the docs is one paragraph per line. Do not hard-wrap it at a column; editors soft-wrap, and hard breaks only make diffs noisy.
Verifying on screen #
tools/gallery.sh drives the gallery from a script. It is macOS-only — AppleScript for the keyboard, CoreGraphics for the pointer, screencapture for the pixels — and it is how every screenshot in docs/ was made.
tools/gallery.sh launch # builds dist/Vampir gallery.app and opens it
tools/gallery.sh key cmd-3 # keystrokes in GPUI's own syntax: cmd-shift-s, escape, tab, enter
tools/gallery.sh type "sync" # text
tools/gallery.sh click 120 300 # window-relative points; also right, double
tools/gallery.sh drag 100 300 400 300
tools/gallery.sh scroll 500 400 0 -40 # dx dy, a trackpad-style scroll at a point
tools/gallery.sh shot out.png # the window at 1x; or a region: shot out.png X Y W H
tools/gallery.sh ink out.png Y0 Y1 X0 X1 # first and last inked column, for measuring alignment
tools/gallery.sh screenshots # regenerate docs/*.png from a fresh launch
tools/gallery.sh quit
A verification pass is: launch, drive to the state, shot, then look at the image — read it back and check the thing you meant to check is there. Compare a before and an after when the change is subtle; md5 -q on two shots of the same region tells you whether anything moved at all, and ink tells you where.
Things that will otherwise cost you an hour:
- Keystrokes go to the frontmost window. Every
key,typeand pointer command brings the gallery to the front first and fails loudly if it cannot. Do not send keystrokes any other way. - A keystroke that matches a binding never reaches a key listener. GPUI dispatches it as the action and stops. If you are adding key handling to a control, listen for the action, or make sure nothing binds the key first.
- Focus on an element that has gone dispatches nothing — not even the window's own shortcuts. If ⌘K "stopped working", something removed the focused element and did not re-home the keyboard.
- A double-click is
clickState, not timing.tools/gallery.sh doublesets it; twoclicks in a row are two single clicks. - The ring only shows for keyboard input. After a click, press Tab once to reveal focus before judging where it is. Capture it with
shot --no-park out.png: the defaultshotmoves the pointer, which GPUI treats as mouse input and hides the ring. - The spinner animates, so two shots of the Controls page are never byte-identical. Crop it out when comparing.
- Coordinates are window-relative.
boundsprints where the window is; the docs screenshots were taken at the default 980×752. - Animations are faster than the shutter. A
shottakes over a second from the command to the capture, so it never catches a 140ms slide. To see one half way,VAMPIR_SLOW_MOTION=8 tools/gallery.sh launch, then drive the pointer withtarget/tools/inputand capture withscreencapture -x -o -Ragainstboundsdirectly, about a third of a second after the click. The footer shows a "slow motion" badge while it is on; never take a docs screenshot that way.
Check both schemes for anything visual. The gallery opens in whatever scheme the desktop is in, so set the scheme rather than assume it: the System | Light | Dark segment sits at the right of the header, and for a window W wide the three are click $((W - 280)) 67, click $((W - 208)) 67 and click $((W - 136)) 67.
Scope #
The library is independent of any particular application. App-specific features such as a menu bar, key bindings and windows belong in the host, which is why macos-apps.md is written as guidance for hosts and demonstrated in the gallery rather than built into the crate. The Edit menu is the one exception (edit_menu), because its items are the toolkit's own text actions.
Wiring text inputs and forms #
Create each Entity<TextInput> once in the host constructor and retain it on the host; TextInput::new takes placeholder text, so load an initial value with set_text. Render it with text_field, text_area or search_field and InputStyle::from_palette, rather than rendering the entity directly or rebuilding it in render. Call vampir::bind_keys(cx) at startup and wrap the host root with vampir::root; this supplies editing actions, Tab, Shift-Tab and Escape. Do not bind Tab again in the host. A tab bar is one tab stop with arrow navigation; before removing a page's focused fields, call controls.focus_root(window, cx) so shortcuts still reach the new page.
Wire on_change to live edits and on_submit to the same host operation used by the form's submit button. Capture a weak host entity in callbacks and call host.update; read the retained input entities inside that operation. Vampir delivers input callbacks after releasing the input update, with a snapshot of the triggering text. Avoid retaining a strong host reference from an input the host owns. set_text loads content without firing on_change. Keep validation failures and pending operations visible, preserve entered values on failure, and disable submission while pending. Dialog bodies can contain fields and dropdowns: Tab and Shift-Tab must visit the body as well as enabled footer buttons and stay inside the modal; dropdown lists must appear above its panel. Verify typing, selection, paste, repeated Enter submission, forward/reverse Tab and dropdown selection in both schemes in the real gallery.