Scroll bar visibility options (#2729)
* add scroll bar visibility options * ScrollBarVisibility derive Eq --------- Co-authored-by: IVANMK-7 <68190772+IVANMK-7@users.noreply.github.com>
This commit is contained in:
parent
f85a25307d
commit
5910144112
|
|
@ -74,6 +74,14 @@ pub struct ScrollAreaOutput<R> {
|
||||||
pub inner_rect: Rect,
|
pub inner_rect: Rect,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Indicate whether the horizontal and vertical scroll bars must be always visible, hidden or visible when needed.
|
||||||
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||||
|
pub enum ScrollBarVisibility {
|
||||||
|
AlwaysVisible,
|
||||||
|
VisibleWhenNeeded,
|
||||||
|
AlwaysHidden,
|
||||||
|
}
|
||||||
|
|
||||||
/// Add vertical and/or horizontal scrolling to a contained [`Ui`].
|
/// Add vertical and/or horizontal scrolling to a contained [`Ui`].
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
|
|
@ -93,7 +101,7 @@ pub struct ScrollArea {
|
||||||
auto_shrink: [bool; 2],
|
auto_shrink: [bool; 2],
|
||||||
max_size: Vec2,
|
max_size: Vec2,
|
||||||
min_scrolled_size: Vec2,
|
min_scrolled_size: Vec2,
|
||||||
always_show_scroll: bool,
|
scroll_bar_visibility: ScrollBarVisibility,
|
||||||
id_source: Option<Id>,
|
id_source: Option<Id>,
|
||||||
offset_x: Option<f32>,
|
offset_x: Option<f32>,
|
||||||
offset_y: Option<f32>,
|
offset_y: Option<f32>,
|
||||||
|
|
@ -131,14 +139,14 @@ impl ScrollArea {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a scroll area where you decide which axis has scrolling enabled.
|
/// Create a scroll area where you decide which axis has scrolling enabled.
|
||||||
/// For instance, `ScrollAre::new([true, false])` enable horizontal scrolling.
|
/// For instance, `ScrollArea::new([true, false])` enables horizontal scrolling.
|
||||||
pub fn new(has_bar: [bool; 2]) -> Self {
|
pub fn new(has_bar: [bool; 2]) -> Self {
|
||||||
Self {
|
Self {
|
||||||
has_bar,
|
has_bar,
|
||||||
auto_shrink: [true; 2],
|
auto_shrink: [true; 2],
|
||||||
max_size: Vec2::INFINITY,
|
max_size: Vec2::INFINITY,
|
||||||
min_scrolled_size: Vec2::splat(64.0),
|
min_scrolled_size: Vec2::splat(64.0),
|
||||||
always_show_scroll: false,
|
scroll_bar_visibility: ScrollBarVisibility::AlwaysHidden,
|
||||||
id_source: None,
|
id_source: None,
|
||||||
offset_x: None,
|
offset_x: None,
|
||||||
offset_y: None,
|
offset_y: None,
|
||||||
|
|
@ -190,10 +198,11 @@ impl ScrollArea {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// If `false` (default), the scroll bar will be hidden when not needed/
|
/// Set the visibility of both horizontal and vertical scroll bars.
|
||||||
/// If `true`, the scroll bar will always be displayed even if not needed.
|
///
|
||||||
pub fn always_show_scroll(mut self, always_show_scroll: bool) -> Self {
|
/// With `ScrollBarVisibility::VisibleWhenNeeded` (default), the scroll bar will be visible only when needed.
|
||||||
self.always_show_scroll = always_show_scroll;
|
pub fn scroll_bar_visibility(mut self, scroll_bar_visibility: ScrollBarVisibility) -> Self {
|
||||||
|
self.scroll_bar_visibility = scroll_bar_visibility;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -328,7 +337,7 @@ struct Prepared {
|
||||||
/// How much horizontal and vertical space are used up by the
|
/// How much horizontal and vertical space are used up by the
|
||||||
/// width of the vertical bar, and the height of the horizontal bar?
|
/// width of the vertical bar, and the height of the horizontal bar?
|
||||||
current_bar_use: Vec2,
|
current_bar_use: Vec2,
|
||||||
always_show_scroll: bool,
|
scroll_bar_visibility: ScrollBarVisibility,
|
||||||
/// Where on the screen the content is (excludes scroll bars).
|
/// Where on the screen the content is (excludes scroll bars).
|
||||||
inner_rect: Rect,
|
inner_rect: Rect,
|
||||||
content_ui: Ui,
|
content_ui: Ui,
|
||||||
|
|
@ -346,7 +355,7 @@ impl ScrollArea {
|
||||||
auto_shrink,
|
auto_shrink,
|
||||||
max_size,
|
max_size,
|
||||||
min_scrolled_size,
|
min_scrolled_size,
|
||||||
always_show_scroll,
|
scroll_bar_visibility,
|
||||||
id_source,
|
id_source,
|
||||||
offset_x,
|
offset_x,
|
||||||
offset_y,
|
offset_y,
|
||||||
|
|
@ -373,7 +382,7 @@ impl ScrollArea {
|
||||||
|
|
||||||
let current_hscroll_bar_height = if !has_bar[0] {
|
let current_hscroll_bar_height = if !has_bar[0] {
|
||||||
0.0
|
0.0
|
||||||
} else if always_show_scroll {
|
} else if scroll_bar_visibility == ScrollBarVisibility::AlwaysVisible {
|
||||||
max_scroll_bar_width
|
max_scroll_bar_width
|
||||||
} else {
|
} else {
|
||||||
max_scroll_bar_width * ui.ctx().animate_bool(id.with("h"), state.show_scroll[0])
|
max_scroll_bar_width * ui.ctx().animate_bool(id.with("h"), state.show_scroll[0])
|
||||||
|
|
@ -381,7 +390,7 @@ impl ScrollArea {
|
||||||
|
|
||||||
let current_vscroll_bar_width = if !has_bar[1] {
|
let current_vscroll_bar_width = if !has_bar[1] {
|
||||||
0.0
|
0.0
|
||||||
} else if always_show_scroll {
|
} else if scroll_bar_visibility == ScrollBarVisibility::AlwaysVisible {
|
||||||
max_scroll_bar_width
|
max_scroll_bar_width
|
||||||
} else {
|
} else {
|
||||||
max_scroll_bar_width * ui.ctx().animate_bool(id.with("v"), state.show_scroll[1])
|
max_scroll_bar_width * ui.ctx().animate_bool(id.with("v"), state.show_scroll[1])
|
||||||
|
|
@ -501,7 +510,7 @@ impl ScrollArea {
|
||||||
has_bar,
|
has_bar,
|
||||||
auto_shrink,
|
auto_shrink,
|
||||||
current_bar_use,
|
current_bar_use,
|
||||||
always_show_scroll,
|
scroll_bar_visibility,
|
||||||
inner_rect,
|
inner_rect,
|
||||||
content_ui,
|
content_ui,
|
||||||
viewport,
|
viewport,
|
||||||
|
|
@ -612,7 +621,7 @@ impl Prepared {
|
||||||
has_bar,
|
has_bar,
|
||||||
auto_shrink,
|
auto_shrink,
|
||||||
mut current_bar_use,
|
mut current_bar_use,
|
||||||
always_show_scroll,
|
scroll_bar_visibility,
|
||||||
content_ui,
|
content_ui,
|
||||||
viewport: _,
|
viewport: _,
|
||||||
scrolling_enabled,
|
scrolling_enabled,
|
||||||
|
|
@ -706,10 +715,13 @@ impl Prepared {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let show_scroll_this_frame = [
|
let show_scroll_this_frame = match scroll_bar_visibility {
|
||||||
content_is_too_large[0] || always_show_scroll,
|
ScrollBarVisibility::AlwaysVisible => [true, true],
|
||||||
content_is_too_large[1] || always_show_scroll,
|
ScrollBarVisibility::VisibleWhenNeeded => {
|
||||||
];
|
[content_is_too_large[0], content_is_too_large[1]]
|
||||||
|
}
|
||||||
|
ScrollBarVisibility::AlwaysHidden => [false, false],
|
||||||
|
};
|
||||||
|
|
||||||
let max_scroll_bar_width = max_scroll_bar_width_with_margin(ui);
|
let max_scroll_bar_width = max_scroll_bar_width_with_margin(ui);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue