Skip to content

gpui: Add keybinding macro for library-default key bindings - #60742

Closed
mikayla-maki wants to merge 3 commits into
mainfrom
fable/gpui-default-keybindings
Closed

gpui: Add keybinding macro for library-default key bindings#60742
mikayla-maki wants to merge 3 commits into
mainfrom
fable/gpui-default-keybindings

Conversation

@mikayla-maki

@mikayla-maki mikayla-maki commented Jul 10, 2026

Copy link
Copy Markdown
Member

GPUI-native components need working keyboard behavior without requiring every application to assemble a keymap. This adds the registration half of that story:

keybinding!("enter", Confirm);                 // action gpui_components::Confirm, global
keybinding!("escape", Cancel, "TextInput");   // scoped to a key context
  • keybinding! declares the action (like actions!) and registers a default binding for it in one step. Actions are named under the registering crate's namespace via the existing namespace argument on the Action derive, which now accepts namespace = crate to use the compiling crate's name (reads CARGO_CRATE_NAME at expansion).
  • Registration is link-time (inventory, the same mechanism actions already use), so third-party component crates participate identically with zero init code, and defaults are genuinely pre-user.
  • Loading happens at the beginning of Application::run (and run_embedded; test/headless/bench contexts load at construction), before any user bindings, so user keymaps shadow defaults via declaration-order precedence. This stays action-based rather than raw key dispatch, so user remapping, the command palette, and keymap introspection all keep working.
  • Opting out: Application::without_default_key_bindings() is a pre-run builder flag that skips loading entirely (Zed sets it, since it owns its whole keymap via JSON); individual defaults can be masked by binding NoAction over them. KeyBinding::is_default() exposes provenance for keymap UIs.
  • Invalid registrations (bad keystrokes/context) panic in debug builds and are logged and skipped in release.

Part of the groundwork for a GPUI-native component library (with #60739 and #60740).

Release Notes:

  • N/A

Components can now declare an action and register a default key binding
for it in one step:

    keybinding!("enter", Confirm);
    keybinding!("escape", Cancel, "TextInput");

The action is registered under the crate's namespace (via the new
crate_namespace flag on the Action derive), and the binding is
registered at link time with inventory and added to every App's keymap
at creation, before any user bindings, so user keymaps shadow defaults
through declaration-order precedence.

Applications that own their entire keymap can opt out with
Application::without_default_key_bindings; Zed does. Individual
defaults can be masked by binding NoAction over them.
@cla-bot cla-bot Bot added the cla-signed The user has signed the Contributor License Agreement label Jul 10, 2026
@zed-community-bot zed-community-bot Bot added the staff Pull requests authored by a current member of Zed staff label Jul 10, 2026
@mikayla-maki mikayla-maki self-assigned this Jul 10, 2026
zed-zippy Bot and others added 2 commits July 10, 2026 08:06
- The Action derive's existing namespace argument now accepts the
  literal token crate to use the compiling crate's name, replacing the
  separate crate_namespace flag.
- Default key bindings load at the beginning of Application::run (and
  run_embedded), gated on a flag that without_default_key_bindings
  clears pre-run, so opting out truly skips loading. Test, headless,
  bench, and visual-test contexts load them at construction.
@mikayla-maki
mikayla-maki marked this pull request as draft July 11, 2026 23:01
@SomeoneToIgnore

Copy link
Copy Markdown
Contributor

To keep the PR queue cleaner, will close this PR for now, as this is a draft still with no changes for over 3 weeks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla-signed The user has signed the Contributor License Agreement staff Pull requests authored by a current member of Zed staff

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants