-
Notifications
You must be signed in to change notification settings - Fork 594
kbd: Add KbdGroup component. #2313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
elcoosp
wants to merge
1
commit into
longbridge:main
Choose a base branch
from
elcoosp:feat/kbd-group
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| use gpui::{ | ||
| App, AppContext, Context, Entity, FocusHandle, Focusable, IntoElement, ParentElement, Render, | ||
| Styled, Window, | ||
| }; | ||
| use gpui_component::{ | ||
| kbd::Kbd, | ||
| kbd_group::KbdGroup, | ||
| v_flex, | ||
| }; | ||
|
|
||
| use crate::section; | ||
|
|
||
| pub struct KbdGroupStory { | ||
| focus_handle: FocusHandle, | ||
| } | ||
|
|
||
| impl super::Story for KbdGroupStory { | ||
| fn title() -> &'static str { | ||
| "KbdGroup" | ||
| } | ||
|
|
||
| fn description() -> &'static str { | ||
| "Display grouped keyboard shortcuts." | ||
| } | ||
|
|
||
| fn new_view(window: &mut Window, cx: &mut App) -> Entity<impl Render> { | ||
| Self::view(window, cx) | ||
| } | ||
| } | ||
|
|
||
| impl KbdGroupStory { | ||
| pub fn view(window: &mut Window, cx: &mut App) -> Entity<Self> { | ||
| cx.new(|cx| Self::new(window, cx)) | ||
| } | ||
|
|
||
| fn new(_: &mut Window, cx: &mut Context<Self>) -> Self { | ||
| Self { | ||
| focus_handle: cx.focus_handle(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Focusable for KbdGroupStory { | ||
| fn focus_handle(&self, _: &App) -> FocusHandle { | ||
| self.focus_handle.clone() | ||
| } | ||
| } | ||
|
|
||
| impl Render for KbdGroupStory { | ||
| fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement { | ||
| v_flex() | ||
| .gap_4() | ||
| .child(section("Basic").child( | ||
| KbdGroup::new() | ||
| .child(Kbd::new(gpui::Keystroke::parse("cmd-c").unwrap())) | ||
| .child(Kbd::new(gpui::Keystroke::parse("ctrl-v").unwrap())), | ||
| )) | ||
| .child(section("Three keys").child( | ||
| KbdGroup::new() | ||
| .child(Kbd::new(gpui::Keystroke::parse("cmd-shift-p").unwrap())) | ||
| .child(Kbd::new(gpui::Keystroke::parse("cmd-ctrl-t").unwrap())) | ||
| .child(Kbd::new(gpui::Keystroke::parse("escape").unwrap())), | ||
| )) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| use gpui::{App, IntoElement, ParentElement, RenderOnce, StyleRefinement, Styled, Window, div}; | ||
|
|
||
| use crate::{ActiveTheme, StyledExt, h_flex, kbd::Kbd}; | ||
|
|
||
| /// A group of `Kbd` elements, rendered with a "+" separator between them. | ||
| #[derive(IntoElement)] | ||
| pub struct KbdGroup { | ||
| style: StyleRefinement, | ||
| children: Vec<Kbd>, | ||
| } | ||
|
|
||
| impl KbdGroup { | ||
| /// Create a new empty `KbdGroup`. | ||
| pub fn new() -> Self { | ||
| Self { | ||
| style: StyleRefinement::default(), | ||
| children: Vec::new(), | ||
| } | ||
| } | ||
|
|
||
| /// Add a `Kbd` child to the group. | ||
| pub fn child(mut self, kbd: impl Into<Kbd>) -> Self { | ||
| self.children.push(kbd.into()); | ||
| self | ||
| } | ||
| } | ||
|
|
||
| impl ParentElement for KbdGroup { | ||
| fn extend(&mut self, elements: impl IntoIterator<Item = gpui::AnyElement>) { | ||
| // Not used; children are added via `.child()`. | ||
| let _ = elements; | ||
| } | ||
| } | ||
|
|
||
| impl Styled for KbdGroup { | ||
| fn style(&mut self) -> &mut StyleRefinement { | ||
| &mut self.style | ||
| } | ||
| } | ||
|
|
||
| impl RenderOnce for KbdGroup { | ||
| fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement { | ||
| let mut group = h_flex().gap_1().items_center().refine_style(&self.style); | ||
|
|
||
| let len = self.children.len(); | ||
| for (i, kbd) in self.children.into_iter().enumerate() { | ||
| group = group.child(kbd); | ||
| if i + 1 < len { | ||
| group = group.child(div().text_color(cx.theme().muted_foreground).child("+")); | ||
| } | ||
| } | ||
| group | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use gpui::TestAppContext; | ||
|
|
||
| #[gpui::test] | ||
| fn test_kbd_group_single(cx: &mut TestAppContext) { | ||
| cx.update(|_| { | ||
| let group = KbdGroup::new().child(Kbd::new(gpui::Keystroke::parse("cmd-c").unwrap())); | ||
| let _ = group.into_any_element(); | ||
| }); | ||
| } | ||
|
|
||
| #[gpui::test] | ||
| fn test_kbd_group_multiple(cx: &mut TestAppContext) { | ||
| cx.update(|_| { | ||
| let group = KbdGroup::new() | ||
| .child(Kbd::new(gpui::Keystroke::parse("cmd-c").unwrap())) | ||
| .child(Kbd::new(gpui::Keystroke::parse("ctrl-v").unwrap())); | ||
| let _ = group.into_any_element(); | ||
| }); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| --- | ||
| title: KbdGroup | ||
| description: Groups multiple keyboard shortcuts with a \"+\" separator. | ||
| --- | ||
|
|
||
| # KbdGroup | ||
|
|
||
| A component for grouping multiple `Kbd` elements, automatically inserting a \"+\" separator between them. This mimics the visual style of key‑binding instructions like **Ctrl+C** or **Cmd+Shift+P**. | ||
|
|
||
| ## Import | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Merge into kbd.md |
||
|
|
||
| ```sh | ||
| use gpui_component::kbd::Kbd; | ||
| use gpui_component::kbd_group::KbdGroup; | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| ### Basic Group | ||
|
|
||
| ```sh | ||
| KbdGroup::new() | ||
| .child(Kbd::new(Keystroke::parse("cmd-c").unwrap())) | ||
| .child(Kbd::new(Keystroke::parse("ctrl-v").unwrap())) | ||
| ``` | ||
|
|
||
| ### Multiple Keys | ||
|
|
||
| ```sh | ||
| KbdGroup::new() | ||
| .child(Kbd::new(Keystroke::parse("cmd-shift-p").unwrap())) | ||
| .child(Kbd::new(Keystroke::parse("cmd-ctrl-t").unwrap())) | ||
| .child(Kbd::new(Keystroke::parse("escape").unwrap())) | ||
| ``` | ||
|
|
||
| ### Styling the Group Container | ||
|
|
||
| ```sh | ||
| KbdGroup::new() | ||
| .child(Kbd::new(Keystroke::parse("cmd-s").unwrap())) | ||
| .child(Kbd::new(Keystroke::parse("cmd-enter").unwrap())) | ||
| .gap_2() // increase spacing | ||
| .text_sm() // adjust font size | ||
| ``` | ||
|
|
||
| ## Platform Differences | ||
|
|
||
| Because each `Kbd` element already adapts to the platform, `KbdGroup` will automatically display the correct symbols or text labels and separator format. | ||
|
|
||
| ## Examples | ||
|
|
||
| ### Inline Shortcut Hint | ||
|
|
||
| ```sh | ||
| h_flex() | ||
| .gap_2() | ||
| .items_center() | ||
| .child("Save project (") | ||
| .child(KbdGroup::new() | ||
| .child(Kbd::new(Keystroke::parse("cmd-shift-s").unwrap())) | ||
| ) | ||
| .child(")") | ||
| ``` | ||
|
|
||
| ### Menu Item with Shortcut | ||
|
|
||
| ```sh | ||
| h_flex() | ||
| .justify_between() | ||
| .items_center() | ||
| .child("Find in Files") | ||
| .child(KbdGroup::new() | ||
| .child(Kbd::new(Keystroke::parse("cmd-shift-f").unwrap())) | ||
| ) | ||
| ``` | ||
|
|
||
| ### Tooltip with Keyboard Combination | ||
|
|
||
| ```sh | ||
| Button::new("undo") | ||
| .label("Undo") | ||
| .tooltip( | ||
| KbdGroup::new() | ||
| .child(Kbd::new(Keystroke::parse("cmd-z").unwrap())) | ||
| .child(Kbd::new(Keystroke::parse("ctrl-z").unwrap())) | ||
| ) | ||
| ``` | ||
|
|
||
| ## API Reference | ||
|
|
||
| - **`KbdGroup::new()`** – Creates an empty group. | ||
| - **`.child(kbd)`** – Adds a `Kbd` element to the group; a \"+\" separator is automatically inserted between consecutive children. | ||
| - The component implements `Styled`, so container‑level styling is fully customisable. | ||
|
|
||
| ## Styling | ||
|
|
||
| The `KbdGroup` inherits its base appearance from the individual `Kbd` components. The container itself is a horizontal flex layout with a small gap. You can apply additional styling via the `Styled` trait methods. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
|
|
||
| wr: | ||
| watchexec -w ./wr.sh --clear -r "sh ./wr.sh" | ||
| test: | ||
| cargo nextest run | ||
|
|
||
| # ----------------------------------------------------------------------- | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please not add this, I don't like it. Makefile can handle that, please submit another PR. |
||
| # GPUI Component story gallery & documentation | ||
| # ----------------------------------------------------------------------- | ||
| # Launch the interactive story gallery (native desktop app) | ||
| story: | ||
| cargo run -p gpui-component-story | ||
|
|
||
| # Start the VitePress documentation website locally | ||
| docs: | ||
| cd docs && bun install && bun run dev | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Merge this file into kbd.rs