Add icon-only dropdown menu triggers
- Infer a square icon-only trigger when no label is set. - Omit the chevron and horizontal padding in that mode. - Demonstrate it with the gallery's More menu. - Assert the icon-only trigger renders at 32x32.
This commit is contained in:
@@ -80,7 +80,7 @@ impl DropdownMenu {
|
||||
self
|
||||
}
|
||||
|
||||
/// Shows an icon beside the label.
|
||||
/// Shows an icon. Omitting the label renders an icon-only trigger.
|
||||
pub fn icon<I>(mut self, icon: I) -> Self
|
||||
where
|
||||
I: Into<Icon>,
|
||||
@@ -223,6 +223,7 @@ impl RenderOnce for DropdownMenu {
|
||||
};
|
||||
|
||||
let split = self.on_click.is_some();
|
||||
let icon_only = self.icon.is_some() && self.label.is_none() && !split;
|
||||
let inert = self.disabled || self.loading;
|
||||
|
||||
let label = self
|
||||
@@ -262,6 +263,7 @@ impl RenderOnce for DropdownMenu {
|
||||
})
|
||||
.justify_center()
|
||||
.h_full()
|
||||
.when(icon_only, |zone| zone.w_full())
|
||||
.gap_2()
|
||||
.line_height(relative(1.0))
|
||||
.text_size(metrics.text_size)
|
||||
@@ -269,16 +271,22 @@ impl RenderOnce for DropdownMenu {
|
||||
.role(Role::Button)
|
||||
.when_some(a11y_label, |zone, label| zone.aria_label(label))
|
||||
.aria_expanded(self.open)
|
||||
.px(metrics.padding_x)
|
||||
.px(if icon_only {
|
||||
gpui::Pixels::ZERO
|
||||
} else {
|
||||
metrics.padding_x
|
||||
})
|
||||
.when(split, |zone| zone.rounded_l(radius))
|
||||
.when(!split, |zone| {
|
||||
zone.rounded(radius)
|
||||
.when(self.open, |zone| zone.bg(pressed))
|
||||
.child(
|
||||
icon(IconRole::ChevronDown)
|
||||
.size(metrics.dense_icon_size)
|
||||
.color(foreground),
|
||||
)
|
||||
})
|
||||
.when(!icon_only && !split, |zone| {
|
||||
zone.child(
|
||||
icon(IconRole::ChevronDown)
|
||||
.size(metrics.dense_icon_size)
|
||||
.color(foreground),
|
||||
)
|
||||
})
|
||||
.child(
|
||||
h_flex()
|
||||
@@ -371,7 +379,12 @@ impl RenderOnce for DropdownMenu {
|
||||
);
|
||||
|
||||
let trigger = h_flex()
|
||||
.debug_selector({
|
||||
let id = self.id.clone();
|
||||
move || format!("{id}-trigger")
|
||||
})
|
||||
.h(metrics.control_height)
|
||||
.when(icon_only, |trigger| trigger.w(metrics.control_height))
|
||||
.rounded(radius)
|
||||
.border_1()
|
||||
.border_color(border)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#![cfg(feature = "button")]
|
||||
#![cfg(all(feature = "button", feature = "dropdown_menu"))]
|
||||
|
||||
use gpui::{
|
||||
Context, IntoElement, ParentElement as _, Render, Styled as _, TestAppContext,
|
||||
@@ -6,6 +6,9 @@ use gpui::{
|
||||
};
|
||||
use gpui_native_core::button::Button;
|
||||
use gpui_native_core::common::Sizable as _;
|
||||
use gpui_native_core::dropdown_menu::DropdownMenu;
|
||||
use gpui_native_core::menu::MenuItem;
|
||||
use gpui_native_icons::IconRole;
|
||||
use gpui_native_theme::Theme;
|
||||
|
||||
struct Harness;
|
||||
@@ -28,6 +31,12 @@ impl Render for Harness {
|
||||
.xsmall()
|
||||
.debug_selector(|| "xsmall-height-probe".to_string()),
|
||||
)
|
||||
.child(
|
||||
DropdownMenu::new("icon-only-dropdown")
|
||||
.icon(IconRole::More)
|
||||
.accessibility_label("More actions")
|
||||
.items(|_, _| vec![MenuItem::new("One")]),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,7 +51,11 @@ fn buttons_render_at_their_token_heights(cx: &mut TestAppContext) {
|
||||
let xsmall = cx
|
||||
.debug_bounds("xsmall-height-probe")
|
||||
.expect("xsmall button laid out");
|
||||
let icon_only = cx
|
||||
.debug_bounds("icon-only-dropdown-trigger")
|
||||
.expect("icon-only dropdown laid out");
|
||||
|
||||
assert_eq!(medium.size.height, px(32.0));
|
||||
assert_eq!(xsmall.size.height, px(20.0));
|
||||
assert_eq!(icon_only.size, size(px(32.0), px(32.0)));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user