From 51a23aa526fad60b5708800072fe4fe7e1ecc649 Mon Sep 17 00:00:00 2001 From: Ren Amamiya Date: Mon, 16 Nov 2026 19:23:24 +0700 Subject: [PATCH] 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. --- crates/core/src/dropdown_menu.rs | 27 ++++++++++++++++++++------- crates/core/tests/sizing.rs | 15 ++++++++++++++- examples/gallery/src/main.rs | 3 ++- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/crates/core/src/dropdown_menu.rs b/crates/core/src/dropdown_menu.rs index 0c66c46..d2b1aa6 100644 --- a/crates/core/src/dropdown_menu.rs +++ b/crates/core/src/dropdown_menu.rs @@ -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(mut self, icon: I) -> Self where I: Into, @@ -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) diff --git a/crates/core/tests/sizing.rs b/crates/core/tests/sizing.rs index 95612ec..5abb86b 100644 --- a/crates/core/tests/sizing.rs +++ b/crates/core/tests/sizing.rs @@ -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))); } diff --git a/examples/gallery/src/main.rs b/examples/gallery/src/main.rs index e567a01..5eeb6f9 100644 --- a/examples/gallery/src/main.rs +++ b/examples/gallery/src/main.rs @@ -3104,7 +3104,8 @@ impl Render for DropdownMenuDemo { }); let more = DropdownMenu::new("dropdown-demo-more") - .label("More") + .icon(IconRole::More) + .accessibility_label("More actions") .variant(ButtonVariant::Ghost) .open(self.open_menu == Some(DemoDropdown::More)) .on_open_change({