From 1218ceb7dcb873b9328fe2096ff83e99db8a325a Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Mon, 13 Jan 2025 14:59:03 +0800 Subject: [PATCH] table: Add `scrollbar_visible` to Table for control the scrollbar visibility. (#544) Ref #513 #542 Add this to ability to render some custom view on top of the table, but below of the scrollbar. ## Break changes - table: Renamed `scrollbar_state` to `vertical_scrollbar_state`. - table: Removed `scroll_handle` and `horizontal_scroll_handle` method, there have a pub `vertical_scroll_handle` and `horizontal_scroll_handle` prop. --- crates/ui/src/styled.rs | 1 + crates/ui/src/table.rs | 44 ++++++++++++++++++++++++----------------- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/crates/ui/src/styled.rs b/crates/ui/src/styled.rs index f2e863e5..4ef9ffe0 100644 --- a/crates/ui/src/styled.rs +++ b/crates/ui/src/styled.rs @@ -157,6 +157,7 @@ impl Size { _ => px(32.), } } + /// Returns the padding for a table cell. pub fn table_cell_padding(&self) -> Edges { match self { diff --git a/crates/ui/src/table.rs b/crates/ui/src/table.rs index 96208466..fb293ede 100644 --- a/crates/ui/src/table.rs +++ b/crates/ui/src/table.rs @@ -151,10 +151,11 @@ pub struct Table { fixed_cols: FixedCols, pub vertical_scroll_handle: UniformListScrollHandle, - pub scrollbar_state: Rc>, + pub vertical_scrollbar_state: Rc>, pub horizontal_scroll_handle: ScrollHandle, pub horizontal_scrollbar_state: Rc>, + scrollbar_visible: Edges, selected_row: Option, selection_state: SelectionState, right_clicked_row: Option, @@ -343,7 +344,7 @@ where fixed_cols: FixedCols::default(), horizontal_scroll_handle: ScrollHandle::new(), vertical_scroll_handle: UniformListScrollHandle::new(), - scrollbar_state: Rc::new(Cell::new(ScrollbarState::new())), + vertical_scrollbar_state: Rc::new(Cell::new(ScrollbarState::new())), horizontal_scrollbar_state: Rc::new(Cell::new(ScrollbarState::new())), selection_state: SelectionState::Row, selected_row: None, @@ -356,6 +357,7 @@ where stripe: false, border: true, size: Size::default(), + scrollbar_visible: Edges::all(true), visible_range: VisibleRangeState::default(), }; @@ -394,6 +396,21 @@ where cx.notify(); } + /// Get the size of the table. + pub fn size(&self) -> Size { + self.size + } + + /// Set scrollbar visibility. + pub fn scrollbar_visible(mut self, vertical: bool, horizontal: bool) -> Self { + self.scrollbar_visible = Edges { + right: vertical, + bottom: horizontal, + ..Default::default() + }; + self + } + /// When we update columns or rows, we need to refresh the table. pub fn refresh(&mut self, cx: &mut ViewContext) { self.prepare_col_groups(cx); @@ -431,16 +448,6 @@ where // cx.notify(); // } - /// Get scroll handle - pub fn scroll_handle(&self) -> &UniformListScrollHandle { - &self.vertical_scroll_handle - } - - /// Get horizontal scroll handle - pub fn horizontal_scroll_handle(&self) -> &ScrollHandle { - &self.horizontal_scroll_handle - } - /// Returns the selected row index. pub fn selected_row(&self) -> Option { self.selected_row @@ -765,8 +772,8 @@ where } } - fn render_scrollbar(&self, cx: &mut ViewContext) -> Option { - let state = self.scrollbar_state.clone(); + fn render_vertical_scrollbar(&self, cx: &mut ViewContext) -> Option { + let state = self.vertical_scrollbar_state.clone(); Some( div() @@ -1240,7 +1247,6 @@ where self.focus_handle.clone() } } - impl EventEmitter for Table where D: TableDelegate {} impl Render for Table @@ -1378,9 +1384,11 @@ where .absolute() .top_0() .size_full() - .child(self.render_horizontal_scrollbar(cx)) - .when(rows_count > 0, |this| { - this.children(self.render_scrollbar(cx)) + .when(self.scrollbar_visible.right && rows_count > 0, |this| { + this.children(self.render_vertical_scrollbar(cx)) + }) + .when(self.scrollbar_visible.bottom, |this| { + this.child(self.render_horizontal_scrollbar(cx)) }), ) // Click out to cancel right clicked row