Skip to content

Commit

Permalink
Table support render_tr and stripe methods
Browse files Browse the repository at this point in the history
  • Loading branch information
madcodelife committed Aug 1, 2024
1 parent 35c3b13 commit 678b46a
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions crates/ui/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -110,6 +110,9 @@ pub struct Table<D: TableDelegate> {

/// The column index that is being resized.
resizing_col: Option<usize>,

/// Set stripe style of the table.
stripe: bool,
}

#[allow(unused)]
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -186,6 +194,7 @@ where
selected_col: None,
resizing_col: None,
bounds: Bounds::default(),
stripe: true,
};

this.prepare_col_groups(cx);
Expand All @@ -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>) {
self.col_groups = (0..self.delegate.cols_count())
.map(|col_ix| ColGroup {
Expand Down Expand Up @@ -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| {
Expand Down

0 comments on commit 678b46a

Please sign in to comment.