Skip to content

[Feat] Add ScrollArea properties to Table and SplitScroll #28

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions demo/src/split_scroll_demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ impl SplitScrollDemo {

SplitScroll {
scroll_enabled: Vec2b::new(true, true),
animated: true,
drag_to_scroll: true,
scroll_bar_visibility: egui::scroll_area::ScrollBarVisibility::VisibleWhenNeeded,
fixed_size: vec2(123.0, 37.0),
scroll_outer_size: vec2(600.0, 400.0),
scroll_content_size: vec2(10_000.0, 10_000.0),
Expand Down
30 changes: 29 additions & 1 deletion demo/src/table_demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ pub struct TableDemo {
num_sticky_cols: usize,
default_column: egui_table::Column,
auto_size_mode: egui_table::AutoSizeMode,
animated: bool,
scroll_bar_visibility: egui::scroll_area::ScrollBarVisibility,
top_row_height: f32,
row_height: f32,
is_row_expanded: BTreeMap<u64, bool>,
Expand All @@ -25,6 +27,8 @@ impl Default for TableDemo {
.range(10.0..=500.0)
.resizable(true),
auto_size_mode: egui_table::AutoSizeMode::default(),
animated: true,
scroll_bar_visibility: egui::scroll_area::ScrollBarVisibility::VisibleWhenNeeded,
top_row_height: 24.0,
row_height: 18.0,
is_row_expanded: Default::default(),
Expand Down Expand Up @@ -252,6 +256,26 @@ impl TableDemo {
);
});
ui.end_row();

ui.label("Scroll bar visibility");
ui.horizontal(|ui| {
ui.radio_value(
&mut self.scroll_bar_visibility,
egui::scroll_area::ScrollBarVisibility::AlwaysHidden,
"AlwaysHidden",
);
ui.radio_value(
&mut self.scroll_bar_visibility,
egui::scroll_area::ScrollBarVisibility::AlwaysVisible,
"AlwaysVisible",
);
ui.radio_value(
&mut self.scroll_bar_visibility,
egui::scroll_area::ScrollBarVisibility::VisibleWhenNeeded,
"VisibleWhenNeeded",
);
});
ui.end_row();
});

let id_salt = Id::new("table_demo");
Expand Down Expand Up @@ -317,6 +341,8 @@ impl TableDemo {
}
});

ui.horizontal(|ui| ui.checkbox(&mut self.animated, "Animate scrolls"));

ui.separator();

let mut table = egui_table::Table::new()
Expand All @@ -331,7 +357,9 @@ impl TableDemo {
},
egui_table::HeaderRow::new(self.top_row_height),
])
.auto_size_mode(self.auto_size_mode);
.auto_size_mode(self.auto_size_mode)
.animated(self.animated)
.scroll_bar_visibility(self.scroll_bar_visibility);

if let Some(scroll_to_column) = scroll_to_column {
table = table.scroll_to_column(scroll_to_column, None);
Expand Down
19 changes: 18 additions & 1 deletion egui_table/src/split_scroll.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use egui::{pos2, vec2, Rect, Ui, UiBuilder, Vec2, Vec2b};
use egui::{pos2, scroll_area::ScrollBarVisibility, vec2, Rect, Ui, UiBuilder, Vec2, Vec2b};
/// A scroll area with some portion of its left and/or top side "stuck".
///
/// This produces four quadrants:
Expand Down Expand Up @@ -27,6 +27,17 @@ use egui::{pos2, vec2, Rect, Ui, UiBuilder, Vec2, Vec2b};
pub struct SplitScroll {
pub scroll_enabled: Vec2b,

/// Should the scroll area animate `scroll_to_*` functions?
pub animated: bool,

/// Can the user drag the scroll area to scroll?
///
/// This is useful for touch screens.
pub drag_to_scroll: bool,

/// Set the visibility of both horizontal and vertical scroll bars.
pub scroll_bar_visibility: ScrollBarVisibility,

/// Width of the fixed left side, and height of the fixed top.
pub fixed_size: Vec2,

Expand Down Expand Up @@ -61,6 +72,9 @@ impl SplitScroll {
pub fn show(self, ui: &mut Ui, delegate: &mut dyn SplitScrollDelegate) {
let Self {
scroll_enabled,
animated,
drag_to_scroll,
scroll_bar_visibility,
fixed_size,
scroll_outer_size,
scroll_content_size,
Expand All @@ -85,6 +99,9 @@ impl SplitScroll {
let mut scroll_ui = ui.new_child(UiBuilder::new().max_rect(rect));

egui::ScrollArea::new(scroll_enabled)
.animated(animated)
.drag_to_scroll(drag_to_scroll)
.scroll_bar_visibility(scroll_bar_visibility)
.auto_shrink(false)
.scroll_bar_rect(bottom_right_rect)
.show_viewport(&mut scroll_ui, |ui, scroll_offset| {
Expand Down
43 changes: 42 additions & 1 deletion egui_table/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use std::{
};

use egui::{
vec2, Align, Context, Id, IdMap, NumExt as _, Rangef, Rect, Ui, UiBuilder, Vec2, Vec2b,
scroll_area::ScrollBarVisibility, vec2, Align, Context, Id, IdMap, NumExt as _, Rangef, Rect,
Ui, UiBuilder, Vec2, Vec2b,
};
use vec1::Vec1;

Expand Down Expand Up @@ -115,6 +116,17 @@ pub struct Table {
/// How to do auto-sizing of columns, if at all.
auto_size_mode: AutoSizeMode,

/// Should the scroll area animate `scroll_to_*` functions?
animated: bool,

/// Can the user drag the scroll area to scroll?
///
/// This is useful for touch screens.
drag_to_scroll: bool,

/// Set the visibility of both horizontal and vertical scroll bars.
scroll_bar_visibility: ScrollBarVisibility,

scroll_to_columns: Option<(RangeInclusive<usize>, Option<Align>)>,
scroll_to_rows: Option<(RangeInclusive<u64>, Option<Align>)>,
}
Expand All @@ -128,6 +140,9 @@ impl Default for Table {
headers: vec![HeaderRow::new(16.0)],
num_rows: 0,
auto_size_mode: AutoSizeMode::default(),
animated: true,
drag_to_scroll: true,
scroll_bar_visibility: ScrollBarVisibility::default(),
scroll_to_columns: None,
scroll_to_rows: None,
}
Expand Down Expand Up @@ -268,6 +283,29 @@ impl Table {
self
}

/// Should the table animate `scroll_to_*` functions?
#[inline]
pub fn animated(mut self, animated: bool) -> Self {
self.animated = animated;
self
}

/// Can the user drag the table to scroll?
///
/// This is useful for touch screens.
#[inline]
pub fn drag_to_scroll(mut self, drag_to_scroll: bool) -> Self {
self.drag_to_scroll = drag_to_scroll;
self
}

/// Set the visibility of both horizontal and vertical scroll bars.
#[inline]
pub fn scroll_bar_visibility(mut self, scroll_bar_visibility: ScrollBarVisibility) -> Self {
self.scroll_bar_visibility = scroll_bar_visibility;
self
}

/// Read the globally unique id, based on the current [`Self::id_salt`]
/// and the parent id.
#[inline]
Expand Down Expand Up @@ -436,6 +474,9 @@ impl Table {

SplitScroll {
scroll_enabled: Vec2b::new(true, true),
animated: self.animated,
drag_to_scroll: self.drag_to_scroll,
scroll_bar_visibility: self.scroll_bar_visibility,
fixed_size: sticky_size,
scroll_outer_size: (ui.available_size() - sticky_size).at_least(Vec2::ZERO),
scroll_content_size: Vec2::new(
Expand Down