Rename View::get_min_size -> required_size

This commit is contained in:
Alexandre Bury 2017-01-23 22:52:29 -08:00
parent 451d27b58e
commit d7eaa5a086
18 changed files with 78 additions and 78 deletions

View File

@ -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 {

View File

@ -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<T: ViewWrapper> 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 {

View File

@ -152,10 +152,10 @@ impl<T: View> BoxView<T> {
impl<T: View> ViewWrapper for BoxView<T> {
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)));
}
}

View File

@ -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)
}

View File

@ -13,7 +13,7 @@ pub struct Canvas<T> {
draw: Box<Fn(&Printer, &T)>,
on_event: Box<FnMut(Event, &mut T) -> EventResult>,
get_min_size: Box<FnMut(Vec2, &mut T) -> Vec2>,
required_size: Box<FnMut(Vec2, &mut T) -> Vec2>,
layout: Box<FnMut(Vec2, &mut T)>,
take_focus: Box<FnMut(Direction, &mut T) -> bool>,
}
@ -35,7 +35,7 @@ impl<T> Canvas<T> {
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<T> Canvas<T> {
self.with(|s| s.set_on_event(f))
}
/// Sets the closure for `get_min_size(Vec2)`
pub fn set_get_min_size<F>(&mut self, f: F)
/// Sets the closure for `required_size(Vec2)`
pub fn set_required_size<F>(&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<F>(self, f: F) -> Self
pub fn with_required_size<F>(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 <T> View for Canvas<T> {
(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) {

View File

@ -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)
}

View File

@ -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);
}

View File

@ -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<Vec2> = 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<Vec2> = 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<Vec2> = 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);

View File

@ -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);

View File

@ -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

View File

@ -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.

View File

@ -18,9 +18,9 @@ impl<V: View> Panel<V> {
impl<V: View> ViewWrapper for Panel<V> {
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) {

View File

@ -139,7 +139,7 @@ impl<T> RadioButton<T> {
}
impl<T> View for RadioButton<T> {
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 {

View File

@ -377,7 +377,7 @@ impl<T: 'static> View for SelectView<T> {
}
}
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.

View File

@ -47,10 +47,10 @@ impl<T: View> ShadowView<T> {
impl<T: View> ViewWrapper for ShadowView<T> {
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) {

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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