table: Use click event instead of mouse down event (#1622)

Fix the issue where clicking a button inside a table row can't stop the
click event from propagating to the table, causing the `td` element to
be selected unexpectedly.
This commit is contained in:
Floyd Wang 2025-11-17 16:30:42 +08:00 committed by GitHub
parent db22454b3a
commit ed92f63a9b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -8,10 +8,10 @@ use crate::{
v_flex, ActiveTheme, Icon, IconName, StyleSized as _, StyledExt, VirtualListScrollHandle, v_flex, ActiveTheme, Icon, IconName, StyleSized as _, StyledExt, VirtualListScrollHandle,
}; };
use gpui::{ use gpui::{
canvas, div, prelude::FluentBuilder, px, uniform_list, AppContext, Axis, Bounds, Context, Div, canvas, div, prelude::FluentBuilder, px, uniform_list, AppContext, Axis, Bounds, ClickEvent,
DragMoveEvent, EventEmitter, FocusHandle, Focusable, InteractiveElement, IntoElement, Context, Div, DragMoveEvent, EventEmitter, FocusHandle, Focusable, InteractiveElement,
ListSizingBehavior, MouseButton, MouseDownEvent, ParentElement, Pixels, Point, Render, IntoElement, ListSizingBehavior, MouseButton, MouseDownEvent, ParentElement, Pixels, Point,
ScrollStrategy, SharedString, StatefulInteractiveElement as _, Styled, Task, Render, ScrollStrategy, SharedString, StatefulInteractiveElement as _, Styled, Task,
UniformListScrollHandle, Window, UniformListScrollHandle, Window,
}; };
@ -303,21 +303,27 @@ where
.count() .count()
} }
fn on_row_click( fn on_row_right_click(
&mut self, &mut self,
ev: &MouseDownEvent, _: &MouseDownEvent,
row_ix: usize,
_: &mut Window,
_: &mut Context<Self>,
) {
self.right_clicked_row = Some(row_ix);
}
fn on_row_left_click(
&mut self,
e: &ClickEvent,
row_ix: usize, row_ix: usize,
_: &mut Window, _: &mut Window,
cx: &mut Context<Self>, cx: &mut Context<Self>,
) { ) {
if ev.button == MouseButton::Right { self.set_selected_row(row_ix, cx);
self.right_clicked_row = Some(row_ix);
} else {
self.set_selected_row(row_ix, cx);
if ev.click_count == 2 { if e.click_count() == 2 {
cx.emit(TableEvent::DoubleClickedRow(row_ix)); cx.emit(TableEvent::DoubleClickedRow(row_ix));
}
} }
} }
@ -803,12 +809,9 @@ where
.child( .child(
self.render_cell(col_ix, window, cx) self.render_cell(col_ix, window, cx)
.id(("col-header", col_ix)) .id(("col-header", col_ix))
.on_mouse_down( .on_click(cx.listener(move |this, _, window, cx| {
MouseButton::Left, this.on_col_head_click(col_ix, window, cx);
cx.listener(move |this, _, window, cx| { }))
this.on_col_head_click(col_ix, window, cx);
}),
)
.child( .child(
h_flex() h_flex()
.size_full() .size_full()
@ -1108,18 +1111,15 @@ where
.border_color(cx.theme().selection), .border_color(cx.theme().selection),
) )
}) })
.on_mouse_down(
MouseButton::Left,
cx.listener(move |this, ev, window, cx| {
this.on_row_click(ev, row_ix, window, cx);
}),
)
.on_mouse_down( .on_mouse_down(
MouseButton::Right, MouseButton::Right,
cx.listener(move |this, ev, window, cx| { cx.listener(move |this, e, window, cx| {
this.on_row_click(ev, row_ix, window, cx); this.on_row_right_click(e, row_ix, window, cx);
}), }),
) )
.on_click(cx.listener(move |this, e, window, cx| {
this.on_row_left_click(e, row_ix, window, cx);
}))
} else { } else {
// Render fake rows to fill the rest table space // Render fake rows to fill the rest table space
self.delegate self.delegate