From 47978bc50e71c143897174dbaf7e92c86d561c1c Mon Sep 17 00:00:00 2001 From: Floyd Wang Date: Tue, 14 Oct 2025 11:19:22 +0800 Subject: [PATCH] button: Support specified anchor to dropdown button (#1367) --- crates/story/src/button_story.rs | 7 ++++--- crates/ui/src/button/dropdown_button.rs | 14 +++++++++++++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/crates/story/src/button_story.rs b/crates/story/src/button_story.rs index 7b480b76..cb6d7d6c 100644 --- a/crates/story/src/button_story.rs +++ b/crates/story/src/button_story.rs @@ -1,6 +1,7 @@ use gpui::{ - Action, App, AppContext as _, ClickEvent, Context, Entity, Focusable, InteractiveElement, - IntoElement, ParentElement as _, Render, Styled as _, Window, prelude::FluentBuilder, px, + Action, App, AppContext as _, ClickEvent, Context, Corner, Entity, Focusable, + InteractiveElement, IntoElement, ParentElement as _, Render, Styled as _, Window, + prelude::FluentBuilder, px, }; use gpui_component::{ @@ -691,7 +692,7 @@ impl Render for ButtonStory { DropdownButton::new("dropdown-button2") .button(Button::new("btn").label("Click Me")) .selected(selected) - .popup_menu(move |this, _, _| { + .popup_menu_with_anchor(Corner::BottomRight, move |this, _, _| { this.menu_with_check( "Disabled", disabled, diff --git a/crates/ui/src/button/dropdown_button.rs b/crates/ui/src/button/dropdown_button.rs index 1dd855e9..85406d72 100644 --- a/crates/ui/src/button/dropdown_button.rs +++ b/crates/ui/src/button/dropdown_button.rs @@ -25,6 +25,7 @@ pub struct DropdownButton { variant: Option, size: Option, rounded: ButtonRounded, + anchor: Corner, } impl DropdownButton { @@ -40,6 +41,7 @@ impl DropdownButton { variant: None, size: None, rounded: ButtonRounded::default(), + anchor: Corner::TopRight, } } @@ -56,6 +58,16 @@ impl DropdownButton { self } + pub fn popup_menu_with_anchor( + mut self, + anchor: impl Into, + popup_menu: impl Fn(PopupMenu, &mut Window, &mut Context) -> PopupMenu + 'static, + ) -> Self { + self.popup_menu = Some(Box::new(popup_menu)); + self.anchor = anchor.into(); + self + } + pub fn rounded(mut self, rounded: impl Into) -> Self { self.rounded = rounded.into(); self @@ -158,7 +170,7 @@ impl RenderOnce for DropdownButton { .when_some(self.outline, |this, _| this.outline()) .when_some(self.size, |this, size| this.with_size(size)) .when_some(self.variant, |this, variant| this.with_variant(variant)) - .popup_menu_with_anchor(Corner::TopRight, move |this, window, cx| { + .popup_menu_with_anchor(self.anchor, move |this, window, cx| { popup_menu(this, window, cx) }), )