TableDelegate support render_empty (#142)

This commit is contained in:
Floyd Wang 2024-08-14 11:01:57 +08:00 committed by GitHub
parent e1f07103d6
commit ccad5cd98e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 85 additions and 63 deletions

View file

@ -10,7 +10,7 @@ use crate::{
h_flex, h_flex,
input::ClearButton, input::ClearButton,
list::{self, List, ListDelegate, ListItem}, list::{self, List, ListDelegate, ListItem},
theme::ActiveTheme, theme::{ActiveTheme, Colorize},
v_flex, Icon, IconName, Sizable, Size, StyleSized, StyledExt, v_flex, Icon, IconName, Sizable, Size, StyleSized, StyledExt,
}; };
@ -197,7 +197,7 @@ where
h_flex() h_flex()
.justify_center() .justify_center()
.py_6() .py_6()
.text_color(cx.theme().muted) .text_color(cx.theme().muted_foreground.opacity(0.6))
.child(Icon::new(IconName::Inbox).size(px(28.))) .child(Icon::new(IconName::Inbox).size(px(28.)))
.into_any_element() .into_any_element()
} }

View file

@ -3,7 +3,7 @@ use std::{cell::Cell, rc::Rc};
use crate::{ use crate::{
h_flex, h_flex,
scroll::{ScrollableAxis, ScrollableMask, Scrollbar, ScrollbarState}, scroll::{ScrollableAxis, ScrollableMask, Scrollbar, ScrollbarState},
theme::ActiveTheme, theme::{ActiveTheme, Colorize},
v_flex, Icon, IconName, v_flex, Icon, IconName,
}; };
use gpui::{ use gpui::{
@ -180,6 +180,17 @@ pub trait TableDelegate: Sized + 'static {
/// Move the column at the given `col_ix` to insert before the column at the given `to_ix`. /// Move the column at the given `col_ix` to insert before the column at the given `to_ix`.
fn move_col(&mut self, col_ix: usize, to_ix: usize) {} fn move_col(&mut self, col_ix: usize, to_ix: usize) {}
/// Return a Element to show when table is empty.
fn render_empty(&self, cx: &mut ViewContext<Table<Self>>) -> impl IntoElement {
h_flex()
.size_full()
.justify_center()
.py_6()
.text_color(cx.theme().muted_foreground.opacity(0.6))
.child(Icon::new(IconName::Inbox).size_16())
.into_any_element()
}
} }
impl<D> Table<D> impl<D> Table<D>
@ -704,72 +715,83 @@ where
.size_full(), .size_full(),
), ),
) )
.child( .map(|this| {
h_flex().id("table-body").flex_grow().size_full().child( if rows_count == 0 {
uniform_list(view, "table-uniform-list", rows_count, { this.child(div().size_full().child(self.delegate.render_empty(cx)))
let horizontal_scroll_handle = horizontal_scroll_handle.clone(); } else {
move |table, visible_range, cx| { this.child(
visible_range h_flex().id("table-body").flex_grow().size_full().child(
.map(|row_ix| { uniform_list(view, "table-uniform-list", rows_count, {
table let horizontal_scroll_handle = horizontal_scroll_handle.clone();
.delegate move |table, visible_range, cx| {
.render_tr(row_ix, cx) visible_range
.id(("table-row", row_ix)) .map(|row_ix| {
.w_full()
.when(row_ix > 0, |this| {
this.border_t_1().border_color(cx.theme().border)
})
.when(table.stripe && row_ix % 2 != 0, |this| {
this.bg(cx.theme().table_even)
})
.hover(|this| {
if table.selected_row == Some(row_ix) {
this
} else {
this.bg(cx.theme().table_hover)
}
})
.children((0..cols_count).map(|col_ix| {
table table
.col_wrap(col_ix, cx) // Make the row scroll sync with the horizontal_scroll_handle to support horizontal scrolling. .delegate
.left(horizontal_scroll_handle.offset().x) .render_tr(row_ix, cx)
.child( .id(("table-row", row_ix))
.w_full()
.when(row_ix > 0, |this| {
this.border_t_1()
.border_color(cx.theme().border)
})
.when(table.stripe && row_ix % 2 != 0, |this| {
this.bg(cx.theme().table_even)
})
.hover(|this| {
if table.selected_row == Some(row_ix) {
this
} else {
this.bg(cx.theme().table_hover)
}
})
.children((0..cols_count).map(|col_ix| {
table table
.render_cell(col_ix, cx) .col_wrap(col_ix, cx) // Make the row scroll sync with the horizontal_scroll_handle to support horizontal scrolling.
.flex_shrink_0() .left(horizontal_scroll_handle.offset().x)
.child( .child(
table table
.delegate .render_cell(col_ix, cx)
.render_td(row_ix, col_ix, cx), .flex_shrink_0()
), .child(
table.delegate.render_td(
row_ix, col_ix, cx,
),
),
)
}))
.child(last_empty_col(cx))
// Row selected style
.when_some(
table.selected_row,
|this, selected_row| {
this.when(
row_ix == selected_row
&& table.selection_state
== SelectionState::Row,
|this| this.bg(cx.theme().table_active),
)
},
)
.on_mouse_down(
MouseButton::Left,
cx.listener(move |this, _, cx| {
this.on_row_click(row_ix, cx);
}),
) )
}))
.child(last_empty_col(cx))
// Row selected style
.when_some(table.selected_row, |this, selected_row| {
this.when(
row_ix == selected_row
&& table.selection_state == SelectionState::Row,
|this| this.bg(cx.theme().table_active),
)
}) })
.on_mouse_down( .collect::<Vec<_>>()
MouseButton::Left, }
cx.listener(move |this, _, cx| { })
this.on_row_click(row_ix, cx); .flex_grow()
}), .size_full()
) .with_sizing_behavior(gpui::ListSizingBehavior::Auto)
}) .track_scroll(vertical_scroll_handle)
.collect::<Vec<_>>() .into_any_element(),
} ),
}) )
.flex_grow() }
.size_full() });
.with_sizing_behavior(gpui::ListSizingBehavior::Auto)
.track_scroll(vertical_scroll_handle)
.into_any_element(),
),
);
let view = cx.view().clone(); let view = cx.view().clone();
div() div()