From c91f71ae2104f884df7b341000c038d749062c37 Mon Sep 17 00:00:00 2001 From: Floyd Wang Date: Thu, 1 Aug 2024 10:17:21 +0800 Subject: [PATCH] Table support `render_tr` and `stripe` methods (#94) --- crates/ui/src/table.rs | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/crates/ui/src/table.rs b/crates/ui/src/table.rs index cb80fb3d..cc133f67 100644 --- a/crates/ui/src/table.rs +++ b/crates/ui/src/table.rs @@ -7,9 +7,9 @@ use crate::{ v_flex, Icon, IconName, }; use gpui::{ - actions, canvas, div, prelude::FluentBuilder as _, px, uniform_list, AppContext, Bounds, Div, - DragMoveEvent, EntityId, EventEmitter, FocusHandle, FocusableView, InteractiveElement as _, - IntoElement, KeyBinding, MouseButton, ParentElement as _, Pixels, Point, Render, ScrollHandle, + actions, canvas, div, prelude::FluentBuilder, px, uniform_list, AppContext, Bounds, Div, + DragMoveEvent, EntityId, EventEmitter, FocusHandle, FocusableView, InteractiveElement, + IntoElement, KeyBinding, MouseButton, ParentElement, Pixels, Point, Render, ScrollHandle, SharedString, StatefulInteractiveElement as _, Styled, UniformListScrollHandle, ViewContext, VisualContext as _, WindowContext, }; @@ -110,6 +110,9 @@ pub struct Table { /// The column index that is being resized. resizing_col: Option, + + /// Set stripe style of the table. + stripe: bool, } #[allow(unused)] @@ -148,6 +151,11 @@ pub trait TableDelegate: Sized + 'static { div().size_full().child(self.col_name(col_ix)) } + /// Render the row at the given row and column. + fn render_tr(&self, row_ix: usize, cx: &mut WindowContext) -> Div { + h_flex() + } + /// Render cell at the given row and column. fn render_td(&self, row_ix: usize, col_ix: usize, cx: &mut WindowContext) -> impl IntoElement; @@ -186,6 +194,7 @@ where selected_col: None, resizing_col: None, bounds: Bounds::default(), + stripe: true, }; this.prepare_col_groups(cx); @@ -200,6 +209,11 @@ where &mut self.delegate } + pub fn stripe(mut self, stripe: bool) -> Self { + self.stripe = stripe; + self + } + fn prepare_col_groups(&mut self, cx: &mut ViewContext) { self.col_groups = (0..self.delegate.cols_count()) .map(|col_ix| ColGroup { @@ -701,13 +715,15 @@ where move |table, visible_range, cx| { visible_range .map(|row_ix| { - tr(cx) + table + .delegate + .render_tr(row_ix, cx) .id(("table-row", row_ix)) .w_full() .when(row_ix > 0, |this| { this.border_t_1().border_color(cx.theme().border) }) - .when(row_ix % 2 != 0, |this| { + .when(table.stripe && row_ix % 2 != 0, |this| { this.bg(cx.theme().table_even) }) .hover(|this| {