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,7 +715,11 @@ where
.size_full(), .size_full(),
), ),
) )
.child( .map(|this| {
if rows_count == 0 {
this.child(div().size_full().child(self.delegate.render_empty(cx)))
} else {
this.child(
h_flex().id("table-body").flex_grow().size_full().child( h_flex().id("table-body").flex_grow().size_full().child(
uniform_list(view, "table-uniform-list", rows_count, { uniform_list(view, "table-uniform-list", rows_count, {
let horizontal_scroll_handle = horizontal_scroll_handle.clone(); let horizontal_scroll_handle = horizontal_scroll_handle.clone();
@ -717,7 +732,8 @@ where
.id(("table-row", row_ix)) .id(("table-row", row_ix))
.w_full() .w_full()
.when(row_ix > 0, |this| { .when(row_ix > 0, |this| {
this.border_t_1().border_color(cx.theme().border) this.border_t_1()
.border_color(cx.theme().border)
}) })
.when(table.stripe && row_ix % 2 != 0, |this| { .when(table.stripe && row_ix % 2 != 0, |this| {
this.bg(cx.theme().table_even) this.bg(cx.theme().table_even)
@ -738,21 +754,25 @@ where
.render_cell(col_ix, cx) .render_cell(col_ix, cx)
.flex_shrink_0() .flex_shrink_0()
.child( .child(
table table.delegate.render_td(
.delegate row_ix, col_ix, cx,
.render_td(row_ix, col_ix, cx), ),
), ),
) )
})) }))
.child(last_empty_col(cx)) .child(last_empty_col(cx))
// Row selected style // Row selected style
.when_some(table.selected_row, |this, selected_row| { .when_some(
table.selected_row,
|this, selected_row| {
this.when( this.when(
row_ix == selected_row row_ix == selected_row
&& table.selection_state == SelectionState::Row, && table.selection_state
== SelectionState::Row,
|this| this.bg(cx.theme().table_active), |this| this.bg(cx.theme().table_active),
) )
}) },
)
.on_mouse_down( .on_mouse_down(
MouseButton::Left, MouseButton::Left,
cx.listener(move |this, _, cx| { cx.listener(move |this, _, cx| {
@ -769,7 +789,9 @@ where
.track_scroll(vertical_scroll_handle) .track_scroll(vertical_scroll_handle)
.into_any_element(), .into_any_element(),
), ),
); )
}
});
let view = cx.view().clone(); let view = cx.view().clone();
div() div()