From 9b8240c6bc93b52168429284ed348fba4542304b Mon Sep 17 00:00:00 2001 From: Floyd Wang Date: Tue, 15 Oct 2024 21:35:17 +0800 Subject: [PATCH] table: Improve `render_tr` to bind the `context_menu` (#347) image --- crates/story/src/table_story.rs | 14 ++++++++++++++ crates/ui/src/table.rs | 10 ++++------ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/crates/story/src/table_story.rs b/crates/story/src/table_story.rs index 8ecabec8..ebd5555d 100644 --- a/crates/story/src/table_story.rs +++ b/crates/story/src/table_story.rs @@ -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>, + ) -> gpui::Stateful { + 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, diff --git a/crates/ui/src/table.rs b/crates/ui/src/table.rs index 0fa7fb30..e81235c4 100644 --- a/crates/ui/src/table.rs +++ b/crates/ui/src/table.rs @@ -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>) -> Div { - h_flex() + fn render_tr(&self, row_ix: usize, cx: &mut ViewContext>) -> Stateful
{ + 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()