button: Support specified anchor to dropdown button (#1367)

This commit is contained in:
Floyd Wang 2025-10-14 11:19:22 +08:00 committed by GitHub
parent 1abb6c2222
commit 47978bc50e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 17 additions and 4 deletions

View file

@ -1,6 +1,7 @@
use gpui::{ use gpui::{
Action, App, AppContext as _, ClickEvent, Context, Entity, Focusable, InteractiveElement, Action, App, AppContext as _, ClickEvent, Context, Corner, Entity, Focusable,
IntoElement, ParentElement as _, Render, Styled as _, Window, prelude::FluentBuilder, px, InteractiveElement, IntoElement, ParentElement as _, Render, Styled as _, Window,
prelude::FluentBuilder, px,
}; };
use gpui_component::{ use gpui_component::{
@ -691,7 +692,7 @@ impl Render for ButtonStory {
DropdownButton::new("dropdown-button2") DropdownButton::new("dropdown-button2")
.button(Button::new("btn").label("Click Me")) .button(Button::new("btn").label("Click Me"))
.selected(selected) .selected(selected)
.popup_menu(move |this, _, _| { .popup_menu_with_anchor(Corner::BottomRight, move |this, _, _| {
this.menu_with_check( this.menu_with_check(
"Disabled", "Disabled",
disabled, disabled,

View file

@ -25,6 +25,7 @@ pub struct DropdownButton {
variant: Option<ButtonVariant>, variant: Option<ButtonVariant>,
size: Option<Size>, size: Option<Size>,
rounded: ButtonRounded, rounded: ButtonRounded,
anchor: Corner,
} }
impl DropdownButton { impl DropdownButton {
@ -40,6 +41,7 @@ impl DropdownButton {
variant: None, variant: None,
size: None, size: None,
rounded: ButtonRounded::default(), rounded: ButtonRounded::default(),
anchor: Corner::TopRight,
} }
} }
@ -56,6 +58,16 @@ impl DropdownButton {
self self
} }
pub fn popup_menu_with_anchor(
mut self,
anchor: impl Into<Corner>,
popup_menu: impl Fn(PopupMenu, &mut Window, &mut Context<PopupMenu>) -> PopupMenu + 'static,
) -> Self {
self.popup_menu = Some(Box::new(popup_menu));
self.anchor = anchor.into();
self
}
pub fn rounded(mut self, rounded: impl Into<ButtonRounded>) -> Self { pub fn rounded(mut self, rounded: impl Into<ButtonRounded>) -> Self {
self.rounded = rounded.into(); self.rounded = rounded.into();
self self
@ -158,7 +170,7 @@ impl RenderOnce for DropdownButton {
.when_some(self.outline, |this, _| this.outline()) .when_some(self.outline, |this, _| this.outline())
.when_some(self.size, |this, size| this.with_size(size)) .when_some(self.size, |this, size| this.with_size(size))
.when_some(self.variant, |this, variant| this.with_variant(variant)) .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) popup_menu(this, window, cx)
}), }),
) )