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.
This commit is contained in:
Jason Lee 2025-01-13 14:59:03 +08:00 committed by GitHub
parent eb10186463
commit 1218ceb7dc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 27 additions and 18 deletions

View file

@ -157,6 +157,7 @@ impl Size {
_ => px(32.), _ => px(32.),
} }
} }
/// Returns the padding for a table cell. /// Returns the padding for a table cell.
pub fn table_cell_padding(&self) -> Edges<Pixels> { pub fn table_cell_padding(&self) -> Edges<Pixels> {
match self { match self {

View file

@ -151,10 +151,11 @@ pub struct Table<D: TableDelegate> {
fixed_cols: FixedCols, fixed_cols: FixedCols,
pub vertical_scroll_handle: UniformListScrollHandle, pub vertical_scroll_handle: UniformListScrollHandle,
pub scrollbar_state: Rc<Cell<ScrollbarState>>, pub vertical_scrollbar_state: Rc<Cell<ScrollbarState>>,
pub horizontal_scroll_handle: ScrollHandle, pub horizontal_scroll_handle: ScrollHandle,
pub horizontal_scrollbar_state: Rc<Cell<ScrollbarState>>, pub horizontal_scrollbar_state: Rc<Cell<ScrollbarState>>,
scrollbar_visible: Edges<bool>,
selected_row: Option<usize>, selected_row: Option<usize>,
selection_state: SelectionState, selection_state: SelectionState,
right_clicked_row: Option<usize>, right_clicked_row: Option<usize>,
@ -343,7 +344,7 @@ where
fixed_cols: FixedCols::default(), fixed_cols: FixedCols::default(),
horizontal_scroll_handle: ScrollHandle::new(), horizontal_scroll_handle: ScrollHandle::new(),
vertical_scroll_handle: UniformListScrollHandle::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())), horizontal_scrollbar_state: Rc::new(Cell::new(ScrollbarState::new())),
selection_state: SelectionState::Row, selection_state: SelectionState::Row,
selected_row: None, selected_row: None,
@ -356,6 +357,7 @@ where
stripe: false, stripe: false,
border: true, border: true,
size: Size::default(), size: Size::default(),
scrollbar_visible: Edges::all(true),
visible_range: VisibleRangeState::default(), visible_range: VisibleRangeState::default(),
}; };
@ -394,6 +396,21 @@ where
cx.notify(); 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. /// When we update columns or rows, we need to refresh the table.
pub fn refresh(&mut self, cx: &mut ViewContext<Self>) { pub fn refresh(&mut self, cx: &mut ViewContext<Self>) {
self.prepare_col_groups(cx); self.prepare_col_groups(cx);
@ -431,16 +448,6 @@ where
// cx.notify(); // 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. /// Returns the selected row index.
pub fn selected_row(&self) -> Option<usize> { pub fn selected_row(&self) -> Option<usize> {
self.selected_row self.selected_row
@ -765,8 +772,8 @@ where
} }
} }
fn render_scrollbar(&self, cx: &mut ViewContext<Self>) -> Option<impl IntoElement> { fn render_vertical_scrollbar(&self, cx: &mut ViewContext<Self>) -> Option<impl IntoElement> {
let state = self.scrollbar_state.clone(); let state = self.vertical_scrollbar_state.clone();
Some( Some(
div() div()
@ -1240,7 +1247,6 @@ where
self.focus_handle.clone() self.focus_handle.clone()
} }
} }
impl<D> EventEmitter<TableEvent> for Table<D> where D: TableDelegate {} impl<D> EventEmitter<TableEvent> for Table<D> where D: TableDelegate {}
impl<D> Render for Table<D> impl<D> Render for Table<D>
@ -1378,9 +1384,11 @@ where
.absolute() .absolute()
.top_0() .top_0()
.size_full() .size_full()
.child(self.render_horizontal_scrollbar(cx)) .when(self.scrollbar_visible.right && rows_count > 0, |this| {
.when(rows_count > 0, |this| { this.children(self.render_vertical_scrollbar(cx))
this.children(self.render_scrollbar(cx)) })
.when(self.scrollbar_visible.bottom, |this| {
this.child(self.render_horizontal_scrollbar(cx))
}), }),
) )
// Click out to cancel right clicked row // Click out to cancel right clicked row