list: Use click events instead of mouse events when confirming (#1427)

Fix the issue where inner list buttons do not respond to clicks.
This commit is contained in:
Floyd Wang 2025-10-24 16:51:37 +08:00 committed by GitHub
parent b740123d4b
commit 21eff06e11
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 16 additions and 32 deletions

View file

@ -202,7 +202,6 @@ pub struct Button {
)>, )>,
on_click: Option<Rc<dyn Fn(&ClickEvent, &mut Window, &mut App)>>, on_click: Option<Rc<dyn Fn(&ClickEvent, &mut Window, &mut App)>>,
on_hover: Option<Rc<dyn Fn(&bool, &mut Window, &mut App)>>, on_hover: Option<Rc<dyn Fn(&bool, &mut Window, &mut App)>>,
pub(crate) stop_propagation: bool,
loading: bool, loading: bool,
loading_icon: Option<Icon>, loading_icon: Option<Icon>,
@ -238,7 +237,6 @@ impl Button {
tooltip: None, tooltip: None,
on_click: None, on_click: None,
on_hover: None, on_hover: None,
stop_propagation: true,
loading: false, loading: false,
compact: false, compact: false,
outline: false, outline: false,
@ -334,11 +332,6 @@ impl Button {
self self
} }
pub fn stop_propagation(mut self, val: bool) -> Self {
self.stop_propagation = val;
self
}
pub fn loading_icon(mut self, icon: impl Into<Icon>) -> Self { pub fn loading_icon(mut self, icon: impl Into<Icon>) -> Self {
self.loading_icon = Some(icon.into()); self.loading_icon = Some(icon.into());
self self
@ -536,13 +529,7 @@ impl RenderOnce for Button {
window.prevent_default(); window.prevent_default();
}) })
.when_some(self.on_click.filter(|_| clickable), |this, on_click| { .when_some(self.on_click.filter(|_| clickable), |this, on_click| {
let stop_propagation = self.stop_propagation; this.on_click(move |event, window, cx| {
this.on_click(move |_, _, cx| {
if stop_propagation {
cx.stop_propagation();
}
})
.on_click(move |event, window, cx| {
(on_click)(event, window, cx); (on_click)(event, window, cx);
}) })
}) })

View file

@ -200,7 +200,6 @@ impl RenderOnce for ButtonGroup {
bottom: true, bottom: true,
}) })
} }
.stop_propagation(false)
.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))
.when(self.compact, |this| this.compact()) .when(self.compact, |this| this.compact())

View file

@ -18,8 +18,8 @@ use gpui::{
IntoElement, KeyBinding, Length, MouseButton, ParentElement, Render, Styled, Task, Window, IntoElement, KeyBinding, Length, MouseButton, ParentElement, Render, Styled, Task, Window,
}; };
use gpui::{ use gpui::{
px, size, App, AvailableSpace, Context, Edges, EventEmitter, ListSizingBehavior, px, size, App, AvailableSpace, ClickEvent, Context, Edges, EventEmitter, ListSizingBehavior,
MouseDownEvent, Pixels, ScrollStrategy, Subscription, Pixels, ScrollStrategy, SharedString, StatefulInteractiveElement, Subscription,
}; };
use rust_i18n::t; use rust_i18n::t;
use smol::Timer; use smol::Timer;
@ -422,9 +422,10 @@ where
.mouse_right_clicked_index .mouse_right_clicked_index
.map(|s| s.eq_row(ix)) .map(|s| s.eq_row(ix))
.unwrap_or(false); .unwrap_or(false);
let id = SharedString::from(format!("list-item-{}", ix));
div() div()
.id("list-item") .id(id)
.w_full() .w_full()
.relative() .relative()
.children(self.delegate.render_item(ix, window, cx).map(|item| { .children(self.delegate.render_item(ix, window, cx).map(|item| {
@ -432,20 +433,17 @@ where
.secondary_selected(mouse_right_clicked) .secondary_selected(mouse_right_clicked)
})) }))
.when(self.selectable, |this| { .when(self.selectable, |this| {
this.on_mouse_down( this.on_click(cx.listener(move |this, e: &ClickEvent, window, cx| {
MouseButton::Left, this.mouse_right_clicked_index = None;
cx.listener(move |this, ev: &MouseDownEvent, window, cx| { this.selected_index = Some(ix);
this.mouse_right_clicked_index = None; this.on_action_confirm(
this.selected_index = Some(ix); &Confirm {
this.on_action_confirm( secondary: e.modifiers().secondary(),
&Confirm { },
secondary: ev.modifiers.secondary(), window,
}, cx,
window, );
cx, }))
);
}),
)
.on_mouse_down( .on_mouse_down(
MouseButton::Right, MouseButton::Right,
cx.listener(move |this, _, _, cx| { cx.listener(move |this, _, _, cx| {