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:
2026-11-16 19:23:24 +07:00
parent 21faea00c5
commit 51a23aa526
3 changed files with 36 additions and 9 deletions
+16 -3
View File
@@ -80,7 +80,7 @@ impl DropdownMenu {
self 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 pub fn icon<I>(mut self, icon: I) -> Self
where where
I: Into<Icon>, I: Into<Icon>,
@@ -223,6 +223,7 @@ impl RenderOnce for DropdownMenu {
}; };
let split = self.on_click.is_some(); 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 inert = self.disabled || self.loading;
let label = self let label = self
@@ -262,6 +263,7 @@ impl RenderOnce for DropdownMenu {
}) })
.justify_center() .justify_center()
.h_full() .h_full()
.when(icon_only, |zone| zone.w_full())
.gap_2() .gap_2()
.line_height(relative(1.0)) .line_height(relative(1.0))
.text_size(metrics.text_size) .text_size(metrics.text_size)
@@ -269,12 +271,18 @@ impl RenderOnce for DropdownMenu {
.role(Role::Button) .role(Role::Button)
.when_some(a11y_label, |zone, label| zone.aria_label(label)) .when_some(a11y_label, |zone, label| zone.aria_label(label))
.aria_expanded(self.open) .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_l(radius))
.when(!split, |zone| { .when(!split, |zone| {
zone.rounded(radius) zone.rounded(radius)
.when(self.open, |zone| zone.bg(pressed)) .when(self.open, |zone| zone.bg(pressed))
.child( })
.when(!icon_only && !split, |zone| {
zone.child(
icon(IconRole::ChevronDown) icon(IconRole::ChevronDown)
.size(metrics.dense_icon_size) .size(metrics.dense_icon_size)
.color(foreground), .color(foreground),
@@ -371,7 +379,12 @@ impl RenderOnce for DropdownMenu {
); );
let trigger = h_flex() let trigger = h_flex()
.debug_selector({
let id = self.id.clone();
move || format!("{id}-trigger")
})
.h(metrics.control_height) .h(metrics.control_height)
.when(icon_only, |trigger| trigger.w(metrics.control_height))
.rounded(radius) .rounded(radius)
.border_1() .border_1()
.border_color(border) .border_color(border)
+14 -1
View File
@@ -1,4 +1,4 @@
#![cfg(feature = "button")] #![cfg(all(feature = "button", feature = "dropdown_menu"))]
use gpui::{ use gpui::{
Context, IntoElement, ParentElement as _, Render, Styled as _, TestAppContext, Context, IntoElement, ParentElement as _, Render, Styled as _, TestAppContext,
@@ -6,6 +6,9 @@ use gpui::{
}; };
use gpui_native_core::button::Button; use gpui_native_core::button::Button;
use gpui_native_core::common::Sizable as _; 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; use gpui_native_theme::Theme;
struct Harness; struct Harness;
@@ -28,6 +31,12 @@ impl Render for Harness {
.xsmall() .xsmall()
.debug_selector(|| "xsmall-height-probe".to_string()), .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 let xsmall = cx
.debug_bounds("xsmall-height-probe") .debug_bounds("xsmall-height-probe")
.expect("xsmall button laid out"); .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!(medium.size.height, px(32.0));
assert_eq!(xsmall.size.height, px(20.0)); assert_eq!(xsmall.size.height, px(20.0));
assert_eq!(icon_only.size, size(px(32.0), px(32.0)));
} }
+2 -1
View File
@@ -3104,7 +3104,8 @@ impl Render for DropdownMenuDemo {
}); });
let more = DropdownMenu::new("dropdown-demo-more") let more = DropdownMenu::new("dropdown-demo-more")
.label("More") .icon(IconRole::More)
.accessibility_label("More actions")
.variant(ButtonVariant::Ghost) .variant(ButtonVariant::Ghost)
.open(self.open_menu == Some(DemoDropdown::More)) .open(self.open_menu == Some(DemoDropdown::More))
.on_open_change({ .on_open_change({