Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scroll bar visibility option to Table widget #3981

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading