Preview
Loading GPUI
Props
Install
cargo add evilbuttonsUsage
use evilbuttons::CommandButton;
use gpui::{div, prelude::*, Context, IntoElement, Render, Window};
struct ButtonDemo;
impl Render for ButtonDemo {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
div().child(
CommandButton::new("save")
.shortcut("mod+s")
.on_command(|_, _| save())
.on_click(|_, _, _| save()),
)
}
}Variants
Three keys
Each part of the shortcut gets its own cap.
Loading GPUI
CommandButton::new("palette")
.shortcut("mod+shift+p")
.label("Command palette")Named keys
enter, esc, space, and the arrow keys render as symbols or short names.
Loading GPUI
CommandButton::new("send")
.shortcut("mod+enter")
.label("Send")No keycaps
Hide the caps with show_shortcut(false).
Loading GPUI
CommandButton::new("new-file")
.shortcut("alt+n")
.show_shortcut(false)
.label("New file")Props
CommandButton is a GPUI RenderOnce builder; its state is keyed by id.
| Method | Type | Default | Description |
|---|---|---|---|
new(id) | impl Into<ElementId> | - | Unique id for the button's state. |
shortcut(s) | impl Into<SharedString> | "mod+s" | Parts separated by +. Accepts mod, ctrl, alt, shift, named keys like enter or esc, and any single key. |
on_command(f) | Fn(&mut Window, &mut App) | - | Fired when the keyboard shortcut is pressed. |
on_click(f) | Fn(&ClickEvent, &mut Window, &mut App) | - | Fired when the button is clicked. The keycaps press on click too. |
prevent_default(bool) | bool | true | Stops the keystroke from reaching your app's own key bindings, and in the browser the page's default handling, like the save dialog on ⌘S. |
show_shortcut(bool) | bool | true | Show the shortcut as keycaps inside the button. |
label(s) | impl Into<SharedString> | "Save" | The button label. |
apple(bool) | bool | macOS builds | Show ⌘ ⌥ ⇧ symbols and sink the mod cap for ⌘. |
disabled(bool) | bool | false | Disables the button and its shortcut. |
Good to know
modmeans ⌘ on Mac and Ctrl everywhere else; either one triggers it.- The shortcut works anywhere in the window (it uses GPUI's keystroke interceptors), not just when the button has focus.
- Holding the shortcut fires
on_commandonce, not on repeat. GPUI doesn't report key repeats to interceptors, so a matching keystroke within 600ms of the last one, with the same modifiers still held, counts as a repeat. - Each modifier cap sinks as you hold it. GPUI has no window-wide modifier event, so the button samples the held modifiers every 30ms.
- In the browser the build can't detect a Mac, so the caps read "Ctrl" unless you pass
apple(true).