Skip to content

Commit

Permalink
Add scroll bar visibility option to Table widget (emilk#3981)
Browse files Browse the repository at this point in the history
There was no way to customize this as ScrollArea gets created inside
Table widget, this exposes the functionality, so scroll bars on tables
can be customized.

<!--
Please read the "Making a PR" section of
[`CONTRIBUTING.md`](https://github.com/emilk/egui/blob/master/CONTRIBUTING.md)
before opening a Pull Request!

* Keep your PR:s small and focused.
* If applicable, add a screenshot or gif.
* If it is a non-trivial addition, consider adding a demo for it to
`egui_demo_lib`, or a new example.
* Do NOT open PR:s from your `master` branch, as that makes it hard for
maintainers to add commits to your PR.
* Remember to run `cargo fmt` and `cargo cranky`.
* Open the PR as a draft until you have self-reviewed it and run
`./scripts/check.sh`.
* When you have addressed a PR comment, mark it as resolved.

Please be patient! I will review your PR, but my time is limited!
-->
  • Loading branch information
richardhozak authored and hacknus committed Oct 30, 2024
1 parent 40254eb commit acb4af5
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions crates/egui_extras/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
//! | fixed size | all available space/minimum | 30% of available width | fixed size |
//! Takes all available height, so if you want something below the table, put it in a strip.
use egui::{Align, NumExt as _, Rangef, Rect, Response, ScrollArea, Ui, Vec2, Vec2b};
use egui::{
scroll_area::ScrollBarVisibility, Align, NumExt as _, Rangef, Rect, Response, ScrollArea, Ui,
Vec2, Vec2b,
};

use crate::{
layout::{CellDirection, CellSize, StripLayoutFlags},
Expand Down Expand Up @@ -171,6 +174,7 @@ struct TableScrollOptions {
min_scrolled_height: f32,
max_scroll_height: f32,
auto_shrink: Vec2b,
scroll_bar_visibility: ScrollBarVisibility,
}

impl Default for TableScrollOptions {
Expand All @@ -184,6 +188,7 @@ impl Default for TableScrollOptions {
min_scrolled_height: 200.0,
max_scroll_height: 800.0,
auto_shrink: Vec2b::TRUE,
scroll_bar_visibility: ScrollBarVisibility::VisibleWhenNeeded,
}
}
}
Expand Down Expand Up @@ -362,6 +367,15 @@ impl<'a> TableBuilder<'a> {
self
}

/// Set the visibility of both horizontal and vertical scroll bars.
///
/// With `ScrollBarVisibility::VisibleWhenNeeded` (default), the scroll bar will be visible only when needed.
#[inline]
pub fn scroll_bar_visibility(mut self, scroll_bar_visibility: ScrollBarVisibility) -> Self {
self.scroll_options.scroll_bar_visibility = scroll_bar_visibility;
self
}

/// What layout should we use for the individual cells?
#[inline]
pub fn cell_layout(mut self, cell_layout: egui::Layout) -> Self {
Expand Down Expand Up @@ -606,6 +620,7 @@ impl<'a> Table<'a> {
min_scrolled_height,
max_scroll_height,
auto_shrink,
scroll_bar_visibility,
} = scroll_options;

let cursor_position = ui.cursor().min;
Expand All @@ -616,7 +631,8 @@ impl<'a> Table<'a> {
.stick_to_bottom(stick_to_bottom)
.min_scrolled_height(min_scrolled_height)
.max_height(max_scroll_height)
.auto_shrink(auto_shrink);
.auto_shrink(auto_shrink)
.scroll_bar_visibility(scroll_bar_visibility);

if let Some(scroll_offset_y) = scroll_offset_y {
scroll_area = scroll_area.vertical_scroll_offset(scroll_offset_y);
Expand Down

0 comments on commit acb4af5

Please sign in to comment.