table: Improve render_tr to bind the context_menu (#347)

<img width="559" alt="image"
src="https://github.com/user-attachments/assets/2aecfa35-5ee1-45cb-a3f3-2409bd09efd7">
This commit is contained in:
Floyd Wang 2024-10-15 21:35:17 +08:00 committed by GitHub
parent 6a5fff889e
commit 9b8240c6bc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 18 additions and 6 deletions

View file

@ -10,6 +10,7 @@ use serde::Deserialize;
use ui::{
button::{Button, ButtonStyled},
checkbox::Checkbox,
context_menu::ContextMenuExt,
h_flex,
indicator::Indicator,
input::{InputEvent, TextInput},
@ -340,6 +341,19 @@ impl TableDelegate for StockTableDelegate {
}
}
fn render_tr(
&self,
row_ix: usize,
_: &mut ViewContext<Table<Self>>,
) -> gpui::Stateful<gpui::Div> {
h_flex().id(("table-row", row_ix)).context_menu(|this, _| {
this.menu("Size Large", Box::new(ChangeSize(Size::Large)))
.menu("Size Medium", Box::new(ChangeSize(Size::Medium)))
.menu("Size Small", Box::new(ChangeSize(Size::Small)))
.menu("Size XSmall", Box::new(ChangeSize(Size::XSmall)))
})
}
fn render_td(
&self,
row_ix: usize,

View file

@ -10,8 +10,8 @@ use gpui::{
actions, canvas, div, prelude::FluentBuilder, px, uniform_list, AppContext, Bounds, Div,
DragMoveEvent, Edges, Entity, EntityId, EventEmitter, FocusHandle, FocusableView,
InteractiveElement, IntoElement, KeyBinding, MouseButton, ParentElement, Pixels, Point, Render,
ScrollHandle, SharedString, StatefulInteractiveElement as _, Styled, UniformListScrollHandle,
ViewContext, VisualContext as _, WindowContext,
ScrollHandle, SharedString, Stateful, StatefulInteractiveElement as _, Styled,
UniformListScrollHandle, ViewContext, VisualContext as _, WindowContext,
};
actions!(
@ -186,8 +186,8 @@ pub trait TableDelegate: Sized + 'static {
}
/// Render the row at the given row and column.
fn render_tr(&self, row_ix: usize, cx: &mut ViewContext<Table<Self>>) -> Div {
h_flex()
fn render_tr(&self, row_ix: usize, cx: &mut ViewContext<Table<Self>>) -> Stateful<Div> {
h_flex().id(("table-row", row_ix))
}
/// Render cell at the given row and column.
@ -942,7 +942,6 @@ where
if row_ix < rows_count {
self.delegate
.render_tr(row_ix, cx)
.id(("table-row", row_ix))
.w_full()
.h(self.size.table_row_height())
.when(row_ix > 0, |this| {
@ -1008,7 +1007,6 @@ where
// Render fake rows to fill the rest table space
self.delegate
.render_tr(row_ix, cx)
.id(("table-row-fake", row_ix))
.w_full()
.h_full()
.border_t_1()