diff --git a/src/view/mod.rs b/src/view/mod.rs index 2430b87..5614c0d 100644 --- a/src/view/mod.rs +++ b/src/view/mod.rs @@ -15,7 +15,7 @@ //! `View::layout()` on its own children. //! //! In order to determine how much space should be given each child, parents -//! can use `View::get_min_size()` on them. +//! can use `View::required_size()` on them. //! //! //! ### Contracts @@ -31,9 +31,9 @@ //! //! In this case, it is safe to omit the call to `View::layout()`. //! -//! * The value returned by `get_min_size` should be an actually viable size, +//! * The value returned by `required_size` should be an actually viable size, //! no matter what the request is. This means calling `View::layout()` with -//! a size returned by `get_min_size` is **never** an error. +//! a size returned by `required_size` is **never** an error. #[macro_use] mod view_wrapper; @@ -85,7 +85,7 @@ pub trait View { /// It's also fine to ignore it and return a fixed value. /// /// Default implementation always return `(1,1)`. - fn get_min_size(&mut self, constraint: Vec2) -> Vec2 { + fn required_size(&mut self, constraint: Vec2) -> Vec2 { let _ = constraint; Vec2::new(1, 1) } @@ -98,7 +98,7 @@ pub trait View { /// * Views can ignore it and always return true (default implementation). /// They will always be assumed to have changed. /// * View Groups can ignore it and always re-layout their children. - /// * If they call `get_min_size` or `layout` with stable parameters, + /// * If they call `required_size` or `layout` with stable parameters, /// the children may cache the result themselves and speed up the /// process anyway. fn needs_relayout(&self) -> bool { diff --git a/src/view/view_wrapper.rs b/src/view/view_wrapper.rs index b36f4c1..b981bbe 100644 --- a/src/view/view_wrapper.rs +++ b/src/view/view_wrapper.rs @@ -30,9 +30,9 @@ pub trait ViewWrapper { self.get_view().draw(printer); } - /// Wraps the `get_min_size` method. - fn wrap_get_min_size(&mut self, req: Vec2) -> Vec2 { - self.get_view_mut().get_min_size(req) + /// Wraps the `required_size` method. + fn wrap_required_size(&mut self, req: Vec2) -> Vec2 { + self.get_view_mut().required_size(req) } /// Wraps the `on_event` method. @@ -66,8 +66,8 @@ impl View for T { self.wrap_draw(printer); } - fn get_min_size(&mut self, req: Vec2) -> Vec2 { - self.wrap_get_min_size(req) + fn required_size(&mut self, req: Vec2) -> Vec2 { + self.wrap_required_size(req) } fn on_event(&mut self, ch: Event) -> EventResult { diff --git a/src/views/box_view.rs b/src/views/box_view.rs index 3b5c8d9..b2c7d06 100644 --- a/src/views/box_view.rs +++ b/src/views/box_view.rs @@ -152,10 +152,10 @@ impl BoxView { impl ViewWrapper for BoxView { wrap_impl!(self.view: T); - fn wrap_get_min_size(&mut self, req: Vec2) -> Vec2 { + fn wrap_required_size(&mut self, req: Vec2) -> Vec2 { let req = self.size.zip_map(req, SizeConstraint::available); - let child_size = self.view.get_min_size(req); + let child_size = self.view.required_size(req); let result = self.size .zip_map(child_size.zip(req), SizeConstraint::result); @@ -191,57 +191,57 @@ mod tests { fn min_size() { let mut min_w = DummyView.full_screen().min_width(5); - assert_eq!(Vec2::new(5, 1), min_w.get_min_size(Vec2::new(1, 1))); - assert_eq!(Vec2::new(5, 10), min_w.get_min_size(Vec2::new(1, 10))); - assert_eq!(Vec2::new(10, 1), min_w.get_min_size(Vec2::new(10, 1))); - assert_eq!(Vec2::new(10, 10), min_w.get_min_size(Vec2::new(10, 10))); + assert_eq!(Vec2::new(5, 1), min_w.required_size(Vec2::new(1, 1))); + assert_eq!(Vec2::new(5, 10), min_w.required_size(Vec2::new(1, 10))); + assert_eq!(Vec2::new(10, 1), min_w.required_size(Vec2::new(10, 1))); + assert_eq!(Vec2::new(10, 10), min_w.required_size(Vec2::new(10, 10))); let mut min_h = DummyView.full_screen().min_height(5); - assert_eq!(Vec2::new(1, 5), min_h.get_min_size(Vec2::new(1, 1))); - assert_eq!(Vec2::new(1, 10), min_h.get_min_size(Vec2::new(1, 10))); - assert_eq!(Vec2::new(10, 5), min_h.get_min_size(Vec2::new(10, 1))); - assert_eq!(Vec2::new(10, 10), min_h.get_min_size(Vec2::new(10, 10))); + assert_eq!(Vec2::new(1, 5), min_h.required_size(Vec2::new(1, 1))); + assert_eq!(Vec2::new(1, 10), min_h.required_size(Vec2::new(1, 10))); + assert_eq!(Vec2::new(10, 5), min_h.required_size(Vec2::new(10, 1))); + assert_eq!(Vec2::new(10, 10), min_h.required_size(Vec2::new(10, 10))); let mut min_s = DummyView.full_screen().min_size((5, 5)); - assert_eq!(Vec2::new(5, 5), min_s.get_min_size(Vec2::new(1, 1))); - assert_eq!(Vec2::new(5, 10), min_s.get_min_size(Vec2::new(1, 10))); - assert_eq!(Vec2::new(10, 5), min_s.get_min_size(Vec2::new(10, 1))); - assert_eq!(Vec2::new(10, 10), min_s.get_min_size(Vec2::new(10, 10))); + assert_eq!(Vec2::new(5, 5), min_s.required_size(Vec2::new(1, 1))); + assert_eq!(Vec2::new(5, 10), min_s.required_size(Vec2::new(1, 10))); + assert_eq!(Vec2::new(10, 5), min_s.required_size(Vec2::new(10, 1))); + assert_eq!(Vec2::new(10, 10), min_s.required_size(Vec2::new(10, 10))); } #[test] fn max_size() { let mut max_w = DummyView.full_screen().max_width(5); - assert_eq!(Vec2::new(1, 1), max_w.get_min_size(Vec2::new(1, 1))); - assert_eq!(Vec2::new(1, 10), max_w.get_min_size(Vec2::new(1, 10))); - assert_eq!(Vec2::new(5, 1), max_w.get_min_size(Vec2::new(10, 1))); - assert_eq!(Vec2::new(5, 10), max_w.get_min_size(Vec2::new(10, 10))); + assert_eq!(Vec2::new(1, 1), max_w.required_size(Vec2::new(1, 1))); + assert_eq!(Vec2::new(1, 10), max_w.required_size(Vec2::new(1, 10))); + assert_eq!(Vec2::new(5, 1), max_w.required_size(Vec2::new(10, 1))); + assert_eq!(Vec2::new(5, 10), max_w.required_size(Vec2::new(10, 10))); let mut max_h = DummyView.full_screen().max_height(5); - assert_eq!(Vec2::new(1, 1), max_h.get_min_size(Vec2::new(1, 1))); - assert_eq!(Vec2::new(1, 5), max_h.get_min_size(Vec2::new(1, 10))); - assert_eq!(Vec2::new(10, 1), max_h.get_min_size(Vec2::new(10, 1))); - assert_eq!(Vec2::new(10, 5), max_h.get_min_size(Vec2::new(10, 10))); + assert_eq!(Vec2::new(1, 1), max_h.required_size(Vec2::new(1, 1))); + assert_eq!(Vec2::new(1, 5), max_h.required_size(Vec2::new(1, 10))); + assert_eq!(Vec2::new(10, 1), max_h.required_size(Vec2::new(10, 1))); + assert_eq!(Vec2::new(10, 5), max_h.required_size(Vec2::new(10, 10))); let mut max_s = DummyView.full_screen().max_size((5, 5)); - assert_eq!(Vec2::new(1, 1), max_s.get_min_size(Vec2::new(1, 1))); - assert_eq!(Vec2::new(1, 5), max_s.get_min_size(Vec2::new(1, 10))); - assert_eq!(Vec2::new(5, 1), max_s.get_min_size(Vec2::new(10, 1))); - assert_eq!(Vec2::new(5, 5), max_s.get_min_size(Vec2::new(10, 10))); + assert_eq!(Vec2::new(1, 1), max_s.required_size(Vec2::new(1, 1))); + assert_eq!(Vec2::new(1, 5), max_s.required_size(Vec2::new(1, 10))); + assert_eq!(Vec2::new(5, 1), max_s.required_size(Vec2::new(10, 1))); + assert_eq!(Vec2::new(5, 5), max_s.required_size(Vec2::new(10, 10))); } #[test] fn full_screen() { let mut full = DummyView.full_screen(); - assert_eq!(Vec2::new(1, 1), full.get_min_size(Vec2::new(1, 1))); - assert_eq!(Vec2::new(1, 10), full.get_min_size(Vec2::new(1, 10))); - assert_eq!(Vec2::new(10, 1), full.get_min_size(Vec2::new(10, 1))); - assert_eq!(Vec2::new(10, 10), full.get_min_size(Vec2::new(10, 10))); + assert_eq!(Vec2::new(1, 1), full.required_size(Vec2::new(1, 1))); + assert_eq!(Vec2::new(1, 10), full.required_size(Vec2::new(1, 10))); + assert_eq!(Vec2::new(10, 1), full.required_size(Vec2::new(10, 1))); + assert_eq!(Vec2::new(10, 10), full.required_size(Vec2::new(10, 10))); } } diff --git a/src/views/button.rs b/src/views/button.rs index 2ee90ae..4cd4ef7 100644 --- a/src/views/button.rs +++ b/src/views/button.rs @@ -90,7 +90,7 @@ impl View for Button { }); } - fn get_min_size(&mut self, _: Vec2) -> Vec2 { + fn required_size(&mut self, _: Vec2) -> Vec2 { // Meh. Fixed size we are. Vec2::new(2 + self.label.width(), 1) } diff --git a/src/views/canvas.rs b/src/views/canvas.rs index aaf1c6f..b24e2f8 100644 --- a/src/views/canvas.rs +++ b/src/views/canvas.rs @@ -13,7 +13,7 @@ pub struct Canvas { draw: Box, on_event: Box EventResult>, - get_min_size: Box Vec2>, + required_size: Box Vec2>, layout: Box, take_focus: Box bool>, } @@ -35,7 +35,7 @@ impl Canvas { state: state, draw: Box::new(|_, _| ()), on_event: Box::new(|_, _| EventResult::Ignored), - get_min_size: Box::new(|_, _| Vec2::new(1, 1)), + required_size: Box::new(|_, _| Vec2::new(1, 1)), layout: Box::new(|_, _| ()), take_focus: Box::new(|_, _| false), } @@ -73,20 +73,20 @@ impl Canvas { self.with(|s| s.set_on_event(f)) } - /// Sets the closure for `get_min_size(Vec2)` - pub fn set_get_min_size(&mut self, f: F) + /// Sets the closure for `required_size(Vec2)` + pub fn set_required_size(&mut self, f: F) where F: 'static + FnMut(Vec2, &mut T) -> Vec2 { - self.get_min_size = Box::new(f); + self.required_size = Box::new(f); } - /// Sets the closure for `get_min_size(Vec2)` + /// Sets the closure for `required_size(Vec2)` /// /// Chainable variant. - pub fn with_get_min_size(self, f: F) -> Self + pub fn with_required_size(self, f: F) -> Self where F: 'static + FnMut(Vec2, &mut T) -> Vec2 { - self.with(|s| s.set_get_min_size(f)) + self.with(|s| s.set_required_size(f)) } /// Sets the closure for `layout(Vec2)` @@ -131,8 +131,8 @@ impl View for Canvas { (self.on_event)(event, &mut self.state) } - fn get_min_size(&mut self, constraint: Vec2) -> Vec2 { - (self.get_min_size)(constraint, &mut self.state) + fn required_size(&mut self, constraint: Vec2) -> Vec2 { + (self.required_size)(constraint, &mut self.state) } fn layout(&mut self, size: Vec2) { diff --git a/src/views/checkbox.rs b/src/views/checkbox.rs index fdfa899..2100946 100644 --- a/src/views/checkbox.rs +++ b/src/views/checkbox.rs @@ -105,7 +105,7 @@ impl Checkbox { } impl View for Checkbox { - fn get_min_size(&mut self, _: Vec2) -> Vec2 { + fn required_size(&mut self, _: Vec2) -> Vec2 { Vec2::new(3, 1) } diff --git a/src/views/dialog.rs b/src/views/dialog.rs index b686725..8f3a04c 100644 --- a/src/views/dialog.rs +++ b/src/views/dialog.rs @@ -247,7 +247,7 @@ impl View for Dialog { } - fn get_min_size(&mut self, req: Vec2) -> Vec2 { + fn required_size(&mut self, req: Vec2) -> Vec2 { // Padding and borders are not available for kids. let nomans_land = self.padding.combined() + self.borders.combined(); @@ -257,7 +257,7 @@ impl View for Dialog { buttons_size.x += self.buttons.len() - 1; } for button in &mut self.buttons { - let s = button.view.get_min_size(req); + let s = button.view.required_size(req); buttons_size.x += s.x; buttons_size.y = max(buttons_size.y, s.y + 1); } @@ -270,7 +270,7 @@ impl View for Dialog { } let content_req = req - taken; - let content_size = self.content.get_min_size(content_req); + let content_size = self.content.required_size(content_req); // On the Y axis, we add buttons and content. // On the X axis, we take the max. @@ -300,7 +300,7 @@ impl View for Dialog { // Buttons are kings, we give them everything they want. let mut buttons_height = 0; for button in self.buttons.iter_mut().rev() { - let size = button.get_min_size(size); + let size = button.required_size(size); buttons_height = max(buttons_height, size.y + 1); button.layout(size); } diff --git a/src/views/linear_layout.rs b/src/views/linear_layout.rs index 254408f..9c6a750 100644 --- a/src/views/linear_layout.rs +++ b/src/views/linear_layout.rs @@ -26,8 +26,8 @@ struct Child { } impl Child { - fn get_min_size(&mut self, req: Vec2) -> Vec2 { - self.size = self.view.get_min_size(req); + fn required_size(&mut self, req: Vec2) -> Vec2 { + self.size = self.view.required_size(req); self.size } @@ -191,7 +191,7 @@ impl View for LinearLayout { fn layout(&mut self, size: Vec2) { // If we can get away without breaking a sweat, you can bet we will. if self.get_cache(size).is_none() { - self.get_min_size(size); + self.required_size(size); } let o = self.orientation; @@ -202,7 +202,7 @@ impl View for LinearLayout { } } - fn get_min_size(&mut self, req: Vec2) -> Vec2 { + fn required_size(&mut self, req: Vec2) -> Vec2 { // Did anything change since last time? if let Some(size) = self.get_cache(req) { return size; @@ -211,7 +211,7 @@ impl View for LinearLayout { // First, make a naive scenario: everything will work fine. let sizes: Vec = self.children .iter_mut() - .map(|c| c.get_min_size(req)) + .map(|c| c.required_size(req)) .collect(); // println_stderr!("Ideal sizes: {:?}", sizes); let ideal = self.orientation.stack(sizes.iter()); @@ -233,7 +233,7 @@ impl View for LinearLayout { // See how they like it that way let min_sizes: Vec = self.children .iter_mut() - .map(|c| c.get_min_size(budget_req)) + .map(|c| c.required_size(budget_req)) .collect(); let desperate = self.orientation.stack(min_sizes.iter()); // println_stderr!("Min sizes: {:?}", min_sizes); @@ -298,7 +298,7 @@ impl View for LinearLayout { let final_sizes: Vec = self.children .iter_mut() .enumerate() - .map(|(i, c)| c.get_min_size(final_lengths[i])) + .map(|(i, c)| c.required_size(final_lengths[i])) .collect(); // println_stderr!("Final sizes2: {:?}", final_sizes); diff --git a/src/views/list_view.rs b/src/views/list_view.rs index f7af360..77a84df 100644 --- a/src/views/list_view.rs +++ b/src/views/list_view.rs @@ -196,7 +196,7 @@ impl View for ListView { }); } - fn get_min_size(&mut self, req: Vec2) -> Vec2 { + fn required_size(&mut self, req: Vec2) -> Vec2 { let label_size = self.children .iter() .map(Child::label) @@ -206,7 +206,7 @@ impl View for ListView { let view_size = self.children .iter_mut() .filter_map(Child::view) - .map(|v| v.get_min_size(req).x) + .map(|v| v.required_size(req).x) .max() .unwrap_or(0); diff --git a/src/views/menu_popup.rs b/src/views/menu_popup.rs index 2db71c3..6beb6ca 100644 --- a/src/views/menu_popup.rs +++ b/src/views/menu_popup.rs @@ -192,7 +192,7 @@ impl View for MenuPopup { }); } - fn get_min_size(&mut self, req: Vec2) -> Vec2 { + fn required_size(&mut self, req: Vec2) -> Vec2 { // We can't really shrink our items here, so it's not flexible. let w = 4 + self.menu diff --git a/src/views/menubar.rs b/src/views/menubar.rs index 39ff6d2..6621602 100644 --- a/src/views/menubar.rs +++ b/src/views/menubar.rs @@ -242,7 +242,7 @@ impl View for Menubar { true } - fn get_min_size(&mut self, _: Vec2) -> Vec2 { + fn required_size(&mut self, _: Vec2) -> Vec2 { // TODO: scroll the options if the screen is too small? // We add 2 to the length of every label for marin. diff --git a/src/views/panel.rs b/src/views/panel.rs index 0324f7f..8b1b1f1 100644 --- a/src/views/panel.rs +++ b/src/views/panel.rs @@ -18,9 +18,9 @@ impl Panel { impl ViewWrapper for Panel { wrap_impl!(self.view: V); - fn wrap_get_min_size(&mut self, req: Vec2) -> Vec2 { + fn wrap_required_size(&mut self, req: Vec2) -> Vec2 { // TODO: make borders conditional? - self.view.get_min_size(req - (2, 2)) + (2, 2) + self.view.required_size(req - (2, 2)) + (2, 2) } fn wrap_draw(&self, printer: &Printer) { diff --git a/src/views/radio.rs b/src/views/radio.rs index 2385ae4..63d8cf0 100644 --- a/src/views/radio.rs +++ b/src/views/radio.rs @@ -139,7 +139,7 @@ impl RadioButton { } impl View for RadioButton { - fn get_min_size(&mut self, _: Vec2) -> Vec2 { + fn required_size(&mut self, _: Vec2) -> Vec2 { if self.label.is_empty() { Vec2::new(3, 1) } else { diff --git a/src/views/select_view.rs b/src/views/select_view.rs index 3dec7d5..d7181ee 100644 --- a/src/views/select_view.rs +++ b/src/views/select_view.rs @@ -377,7 +377,7 @@ impl View for SelectView { } } - fn get_min_size(&mut self, req: Vec2) -> Vec2 { + fn required_size(&mut self, req: Vec2) -> Vec2 { // Items here are not compressible. // So no matter what the horizontal requirements are, // we'll still return our longest item. diff --git a/src/views/shadow_view.rs b/src/views/shadow_view.rs index 80a8111..85d3d3b 100644 --- a/src/views/shadow_view.rs +++ b/src/views/shadow_view.rs @@ -47,10 +47,10 @@ impl ShadowView { impl ViewWrapper for ShadowView { wrap_impl!(self.view: T); - fn wrap_get_min_size(&mut self, req: Vec2) -> Vec2 { + fn wrap_required_size(&mut self, req: Vec2) -> Vec2 { // Make sure req >= offset let offset = self.padding().or_min(req); - self.view.get_min_size(req - offset) + offset + self.view.required_size(req - offset) + offset } fn wrap_layout(&mut self, size: Vec2) { diff --git a/src/views/slider_view.rs b/src/views/slider_view.rs index d099933..235f7b6 100644 --- a/src/views/slider_view.rs +++ b/src/views/slider_view.rs @@ -125,7 +125,7 @@ impl View for SliderView { }); } - fn get_min_size(&mut self, _: Vec2) -> Vec2 { + fn required_size(&mut self, _: Vec2) -> Vec2 { self.orientation.make_vec(self.max_value, 1) } diff --git a/src/views/stack_view.rs b/src/views/stack_view.rs index 61e371a..e48c01a 100644 --- a/src/views/stack_view.rs +++ b/src/views/stack_view.rs @@ -149,7 +149,7 @@ impl View for StackView { for layer in &mut self.layers { // Give each guy what he asks for, within the budget constraints. - let size = Vec2::min(size, layer.view.get_min_size(size)); + let size = Vec2::min(size, layer.view.required_size(size)); layer.size = size; layer.view.layout(layer.size); @@ -164,12 +164,12 @@ impl View for StackView { } } - fn get_min_size(&mut self, size: Vec2) -> Vec2 { + fn required_size(&mut self, size: Vec2) -> Vec2 { // The min size is the max of all children's self.layers .iter_mut() - .map(|layer| layer.view.get_min_size(size)) + .map(|layer| layer.view.required_size(size)) .fold(Vec2::new(1, 1), Vec2::max) } diff --git a/src/views/text_view.rs b/src/views/text_view.rs index 13b41af..9a88578 100644 --- a/src/views/text_view.rs +++ b/src/views/text_view.rs @@ -272,7 +272,7 @@ impl View for TextView { self.last_size.is_none() } - fn get_min_size(&mut self, size: Vec2) -> Vec2 { + fn required_size(&mut self, size: Vec2) -> Vec2 { self.compute_rows(size); // This is what we'd like