Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
ui: Keep project action shortcuts inside their buttons (#65531)
junkmailkeep03efd49220ba
diff --git a/crates/ui/src/components/button/button.rs b/crates/ui/src/components/button/button.rsindex d461b8ed266..ac324cdc310 100644--- a/crates/ui/src/components/button/button.rs+++ b/crates/ui/src/components/button/button.rs@@ -87,6 +87,7 @@ pub struct Button {end_icon: Option<Icon>,key_binding: Option<KeyBinding>,key_binding_position: KeybindingPosition,+ wrap_key_binding: bool,alpha: Option<f32>,truncate: bool,loading: bool,@@ -111,6 +112,7 @@ impl Button {end_icon: None,key_binding: None,key_binding_position: KeybindingPosition::default(),+ wrap_key_binding: false,alpha: None,truncate: false,loading: false,@@ -210,6 +212,13 @@ impl Button {self}+ /// Lets the keybinding wrap onto its own line when the button is too narrow+ /// to fit it next to the label, growing the button to fit both lines.+ pub fn wrap_key_binding(mut self, wrap: bool) -> Self {+ self.wrap_key_binding = wrap;+ self+ }+/// Sets the position of the keybinding relative to the button label.////// This method allows you to specify where the keybinding should be displayed@@ -431,14 +440,18 @@ impl RenderOnce for Button {self.base.aria_label = Some(label.clone());}+ let keyshortcuts = self+ .key_binding+ .as_ref()+ .and_then(|key_binding| key_binding.keyboard_shortcut_text(window, cx));+ let wrap_key_binding =+ self.wrap_key_binding && keyshortcuts.is_some() && KeyBinding::default_visibility(cx);+// Announce the displayed keybinding to assistive technology via// `aria-keyshortcuts` so screen-reader users hear the same shortcut// sighted users see next to the button.if self.base.aria_keyshortcuts.is_none()- && let Some(keyshortcuts) = self- .key_binding- .as_ref()- .and_then(|key_binding| key_binding.keyboard_shortcut_text(window, cx))+ && let Some(keyshortcuts) = keyshortcuts{self.base.aria_keyshortcuts = Some(keyshortcuts);}@@ -460,57 +473,84 @@ impl RenderOnce for Button {self.label_color.unwrap_or_default()};let loading_icon_id = (self.base.id().clone(), "loading");+ let wrapped_key_binding = if wrap_key_binding {+ self.key_binding.take()+ } else {+ None+ };- self.base.child(- h_flex()- .when(self.truncate, |this| this.min_w_0().overflow_hidden())- .gap(DynamicSpacing::Base04.rems(cx))- .when_else(- self.loading,- |this| {- this.child(- Icon::new(IconName::LoadCircle)- .size(IconSize::Small)- .color(Color::Muted)- .with_keyed_rotate_animation(loading_icon_id, 2),- )- },- |this| {- this.when_some(self.start_icon, |this, icon| {- this.child(if is_disabled {- icon.color(Color::Disabled)- } else {- icon- })+ let content = h_flex()+ .when(self.truncate, |this| this.min_w_0().overflow_hidden())+ .gap(DynamicSpacing::Base04.rems(cx))+ .when_else(+ self.loading,+ |this| {+ this.child(+ Icon::new(IconName::LoadCircle)+ .size(IconSize::Small)+ .color(Color::Muted)+ .with_keyed_rotate_animation(loading_icon_id, 2),+ )+ },+ |this| {+ this.when_some(self.start_icon, |this, icon| {+ this.child(if is_disabled {+ icon.color(Color::Disabled)+ } else {+ icon})- },- )- .child(- h_flex()- .when(self.truncate, |this| this.min_w_0().overflow_hidden())- .when(- self.key_binding_position == KeybindingPosition::Start,- |this| this.flex_row_reverse(),- )- .gap(DynamicSpacing::Base06.rems(cx))- .justify_between()- .child(- Label::new(label)- .color(label_color)- .size(self.label_size.unwrap_or_default())- .when_some(self.alpha, |this, alpha| this.alpha(alpha))- .when(self.truncate, |this| this.truncate()),- )- .children(self.key_binding),- )- .when_some(self.end_icon, |this, icon| {- this.child(if is_disabled {- icon.color(Color::Disabled)- } else {- icon})- }),- )+ },+ )+ .child(+ h_flex()+ .when(self.truncate, |this| this.min_w_0().overflow_hidden())+ .when(+ self.key_binding_position == KeybindingPosition::Start,+ |this| this.flex_row_reverse(),+ )+ .gap(DynamicSpacing::Base06.rems(cx))+ .justify_between()+ .child(+ Label::new(label)+ .color(label_color)+ .size(self.label_size.unwrap_or_default())+ .when_some(self.alpha, |this, alpha| this.alpha(alpha))+ .when(self.truncate, |this| this.truncate()),+ )+ .children(self.key_binding),+ )+ .when_some(self.end_icon, |this, icon| {+ this.child(if is_disabled {+ icon.color(Color::Disabled)+ } else {+ icon+ })+ });++ if let Some(key_binding) = wrapped_key_binding {+ let row_height = self.base.size.rems();+ let content = content+ .flex_none()+ .min_h(row_height)+ .when(self.truncate, |this| this.max_w_full());+ let key_binding = h_flex().flex_none().min_h(row_height).child(key_binding);+ // Wrapping follows child order even in a reversed row, so order the+ // children instead to keep a `Start` keybinding on the first line.+ self.base.auto_height().child(+ h_flex()+ .w_full()+ .flex_wrap()+ .justify_center()+ .gap_x(DynamicSpacing::Base06.rems(cx))+ .map(|this| match self.key_binding_position {+ KeybindingPosition::Start => this.child(key_binding).child(content),+ KeybindingPosition::End => this.child(content).child(key_binding),+ }),+ )+ } else {+ self.base.child(content)+ }}}diff --git a/crates/ui/src/components/button/button_like.rs b/crates/ui/src/components/button/button_like.rsindex c7c96d17b8f..a54f9b49a97 100644--- a/crates/ui/src/components/button/button_like.rs+++ b/crates/ui/src/components/button/button_like.rs@@ -1,8 +1,8 @@use documented::Documented;use gpui::{- AnyElement, AnyView, ClickEvent, CursorStyle, DefiniteLength, FocusHandle, Hsla, MouseButton,- MouseClickEvent, MouseDownEvent, MouseUpEvent, Rems, Role, StyleRefinement, Toggled, relative,- transparent_black,+ AnyElement, AnyView, ClickEvent, CursorStyle, DefiniteLength, FocusHandle, Hsla, Length,+ MouseButton, MouseClickEvent, MouseDownEvent, MouseUpEvent, Rems, Role, StyleRefinement,+ Toggled, relative, transparent_black,};use smallvec::SmallVec;@@ -489,10 +489,10 @@ pub struct ButtonLike {pub(super) hover_background: Option<Hsla>,pub(super) active_background: Option<Hsla>,pub(super) width: Option<DefiniteLength>,- pub(super) height: Option<DefiniteLength>,+ pub(super) height: Option<Length>,pub(super) layer: Option<ElevationIndex>,tab_index: Option<isize>,- size: ButtonSize,+ pub(super) size: ButtonSize,rounding: Option<ButtonLikeRounding>,pub(super) aria_label: Option<SharedString>,aria_description: Option<SharedString>,@@ -567,7 +567,12 @@ impl ButtonLike {}pub fn height(mut self, height: DefiniteLength) -> Self {- self.height = Some(height);+ self.height = Some(height.into());+ self+ }++ pub fn auto_height(mut self) -> Self {+ self.height = Some(Length::Auto);self}diff --git a/crates/ui/src/components/project_empty_state.rs b/crates/ui/src/components/project_empty_state.rsindex f5ef58da3d3..49ffc6d2341 100644--- a/crates/ui/src/components/project_empty_state.rs+++ b/crates/ui/src/components/project_empty_state.rs@@ -71,6 +71,7 @@ impl RenderOnce for ProjectEmptyState {Button::new("open_project", "Open Project").full_width().key_binding(self.open_project_key_binding)+ .wrap_key_binding(true).when_some(self.on_open_project, |button, handler| {button.on_click(handler)}),diff --git a/crates/workspace/src/welcome.rs b/crates/workspace/src/welcome.rsindex 837418e8a68..ea31eb484c1 100644--- a/crates/workspace/src/welcome.rs+++ b/crates/workspace/src/welcome.rs@@ -105,16 +105,19 @@ impl RenderOnce for SectionButton {.child(h_flex().w_full()+ .min_w_0()+ .gap_2().justify_between().child(h_flex()+ .min_w_0().gap_2().child(Icon::new(self.icon).color(Color::Muted).size(IconSize::Small),)- .child(Label::new(self.label)),+ .child(Label::new(self.label).truncate()),).child(KeyBinding::for_action_in(action_ref, &self.focus_handle, cx)