button: Bind element ID after button is created (#1325)

- Fix the selected-state error in the popup menu.
This commit is contained in:
Floyd Wang 2025-10-03 14:52:53 +08:00 committed by GitHub
parent 1358b2906e
commit 9754cab4d7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -7,8 +7,8 @@ use crate::{
use gpui::{ use gpui::{
div, prelude::FluentBuilder as _, px, relative, Action, AnyElement, App, ClickEvent, Corners, div, prelude::FluentBuilder as _, px, relative, Action, AnyElement, App, ClickEvent, Corners,
Div, Edges, ElementId, Hsla, InteractiveElement, Interactivity, IntoElement, ParentElement, Div, Edges, ElementId, Hsla, InteractiveElement, Interactivity, IntoElement, ParentElement,
Pixels, RenderOnce, SharedString, StatefulInteractiveElement as _, StyleRefinement, Styled, Pixels, RenderOnce, SharedString, Stateful, StatefulInteractiveElement as _, StyleRefinement,
Window, Styled, Window,
}; };
#[derive(Default, Clone, Copy)] #[derive(Default, Clone, Copy)]
@ -182,7 +182,7 @@ impl ButtonVariant {
#[derive(IntoElement)] #[derive(IntoElement)]
pub struct Button { pub struct Button {
id: ElementId, id: ElementId,
base: Div, base: Stateful<Div>,
style: StyleRefinement, style: StyleRefinement,
icon: Option<Icon>, icon: Option<Icon>,
label: Option<SharedString>, label: Option<SharedString>,
@ -217,9 +217,13 @@ impl From<Button> for AnyElement {
impl Button { impl Button {
pub fn new(id: impl Into<ElementId>) -> Self { pub fn new(id: impl Into<ElementId>) -> Self {
let id = id.into();
Self { Self {
id: id.into(), id: id.clone(),
base: div().flex_shrink_0(), // ID must be set after div is created;
// `popup_menu` uses this id to create the popup menu.
base: div().flex_shrink_0().id(id),
style: StyleRefinement::default(), style: StyleRefinement::default(),
icon: None, icon: None,
label: None, label: None,
@ -415,7 +419,6 @@ impl RenderOnce for Button {
let is_focused = focus_handle.is_focused(window); let is_focused = focus_handle.is_focused(window);
self.base self.base
.id(self.id.clone())
.when(!self.disabled, |this| { .when(!self.disabled, |this| {
this.track_focus( this.track_focus(
&focus_handle &focus_handle