Force user to explicitly select a max height for a ScrollArea

This commit is contained in:
Emil Ernerfeldt 2020-08-29 15:14:44 +02:00
parent 5df9bfd514
commit 3d3b93da8b
3 changed files with 12 additions and 17 deletions

View file

@ -33,22 +33,21 @@ pub struct ScrollArea {
always_show_scroll: bool, always_show_scroll: bool,
} }
impl Default for ScrollArea { impl ScrollArea {
fn default() -> Self { /// Will make the area be as high as it is allowed to be (i.e. fill the ui it is in)
pub fn auto_sized() -> Self {
Self::from_max_height(f32::INFINITY)
}
/// Use `f32::INFINITY` if you want the scroll area to expand to fit the surrounding Ui
pub fn from_max_height(max_height: f32) -> Self {
Self { Self {
max_height: 200.0, max_height,
always_show_scroll: false, always_show_scroll: false,
} }
} }
}
impl ScrollArea { /// If `false` (default), the scroll bar will be hidden when not needed/
pub fn max_height(mut self, max_height: f32) -> Self {
self.max_height = max_height;
self
}
/// If `false` (defualt), the scroll bar will be hidden when not needed/
/// If `true`, the scroll bar will always be displayed even if not needed. /// 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 { pub fn always_show_scroll(mut self, always_show_scroll: bool) -> Self {
self.always_show_scroll = always_show_scroll; self.always_show_scroll = always_show_scroll;

View file

@ -142,11 +142,7 @@ impl<'open> Window<'open> {
pub fn scroll(mut self, scroll: bool) -> Self { pub fn scroll(mut self, scroll: bool) -> Self {
if scroll { if scroll {
if self.scroll.is_none() { if self.scroll.is_none() {
self.scroll = Some( self.scroll = Some(ScrollArea::auto_sized());
ScrollArea::default()
.always_show_scroll(false)
.max_height(f32::INFINITY), // As large as we can be
);
} }
debug_assert!( debug_assert!(
self.scroll.is_some(), self.scroll.is_some(),

View file

@ -360,7 +360,7 @@ impl DemoWindow {
CollapsingHeader::new("Scroll area") CollapsingHeader::new("Scroll area")
.default_open(false) .default_open(false)
.show(ui, |ui| { .show(ui, |ui| {
ScrollArea::default().show(ui, |ui| { ScrollArea::from_max_height(200.0).show(ui, |ui| {
ui.label(LOREM_IPSUM_LONG); ui.label(LOREM_IPSUM_LONG);
}); });
}); });