From 3d3b93da8ba91412e11354bb5e015b11d9b36a27 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sat, 29 Aug 2020 15:14:44 +0200 Subject: [PATCH] Force user to explicitly select a max height for a ScrollArea --- egui/src/containers/scroll_area.rs | 21 ++++++++++----------- egui/src/containers/window.rs | 6 +----- egui/src/demos/app.rs | 2 +- 3 files changed, 12 insertions(+), 17 deletions(-) diff --git a/egui/src/containers/scroll_area.rs b/egui/src/containers/scroll_area.rs index d8b4b4c3..be783cbb 100644 --- a/egui/src/containers/scroll_area.rs +++ b/egui/src/containers/scroll_area.rs @@ -33,22 +33,21 @@ pub struct ScrollArea { always_show_scroll: bool, } -impl Default for ScrollArea { - fn default() -> Self { +impl ScrollArea { + /// 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 { - max_height: 200.0, + max_height, always_show_scroll: false, } } -} -impl ScrollArea { - 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 `false` (default), the scroll bar will be hidden when 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 { self.always_show_scroll = always_show_scroll; diff --git a/egui/src/containers/window.rs b/egui/src/containers/window.rs index 966d0a56..172a4935 100644 --- a/egui/src/containers/window.rs +++ b/egui/src/containers/window.rs @@ -142,11 +142,7 @@ impl<'open> Window<'open> { pub fn scroll(mut self, scroll: bool) -> Self { if scroll { if self.scroll.is_none() { - self.scroll = Some( - ScrollArea::default() - .always_show_scroll(false) - .max_height(f32::INFINITY), // As large as we can be - ); + self.scroll = Some(ScrollArea::auto_sized()); } debug_assert!( self.scroll.is_some(), diff --git a/egui/src/demos/app.rs b/egui/src/demos/app.rs index 28f468f2..dd927971 100644 --- a/egui/src/demos/app.rs +++ b/egui/src/demos/app.rs @@ -360,7 +360,7 @@ impl DemoWindow { CollapsingHeader::new("Scroll area") .default_open(false) .show(ui, |ui| { - ScrollArea::default().show(ui, |ui| { + ScrollArea::from_max_height(200.0).show(ui, |ui| { ui.label(LOREM_IPSUM_LONG); }); });