diff --git a/src/view/identifiable.rs b/src/view/identifiable.rs index 7c4fe0c..bd35979 100644 --- a/src/view/identifiable.rs +++ b/src/view/identifiable.rs @@ -6,9 +6,12 @@ use view::View; /// [`IdView`]: ../views/struct.IdView.html pub trait Identifiable: View + Sized { /// Wraps this view into an IdView with the given id. + /// + /// This is just a shortcut for `IdView::new(id, self)` fn with_id(self, id: &str) -> IdView { IdView::new(id, self) } } +/// Any `View` implements this trait. impl Identifiable for T {} diff --git a/src/view/mod.rs b/src/view/mod.rs index 38fc83e..61484b2 100644 --- a/src/view/mod.rs +++ b/src/view/mod.rs @@ -71,7 +71,9 @@ pub use self::boxable::Boxable; /// Main trait defining a view behaviour. pub trait View { - /// Called when a key was pressed. Default implementation just ignores it. + /// Called when a key was pressed. + /// + /// Default implementation just ignores it. fn on_event(&mut self, Event) -> EventResult { EventResult::Ignored } @@ -81,6 +83,8 @@ pub trait View { /// If the view is flexible (it has multiple size options), it can try /// to return one that fits the given `constraint`. /// 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 { let _ = constraint; Vec2::new(1, 1) @@ -112,6 +116,8 @@ pub trait View { /// Finds the view pointed to by the given path. /// /// Returns None if the path doesn't lead to a view. + /// + /// Default implementation always return `None`. fn find(&mut self, &Selector) -> Option<&mut Any> { None } @@ -120,6 +126,8 @@ pub trait View { /// /// `source` indicates where the focus comes from. /// When the source is unclear, `Front` is usually used. + /// + /// Default implementation always return `false`. fn take_focus(&mut self, source: Direction) -> bool { let _ = source; false diff --git a/src/view/position.rs b/src/view/position.rs index ffa2257..88023aa 100644 --- a/src/view/position.rs +++ b/src/view/position.rs @@ -29,8 +29,16 @@ impl Position { /// and a parent with the absolute coordinates `parent`, drawing the /// child with its top-left corner at the returned coordinates will /// position him appropriately. - pub fn compute_offset(&self, size: Vec2, available: Vec2, parent: Vec2) - -> Vec2 { + pub fn compute_offset(&self, size: S, available: A, parent: P) + -> Vec2 + where S: Into, + A: Into, + P: Into + { + let available = available.into(); + let size = size.into(); + let parent = parent.into(); + Vec2::new(self.x.compute_offset(size.x, available.x, parent.x), self.y.compute_offset(size.y, available.y, parent.y)) } @@ -67,3 +75,21 @@ impl Offset { } } } + +#[cfg(test)] +mod tests { + + use vec::Vec2; + use super::{Offset, Position}; + + #[test] + fn test_center() { + let center = Position::center(); + assert_eq!(Vec2::new(2, 1), center.compute_offset((1,1), (5,3), (0,0))); + assert_eq!(Vec2::new(2, 0), center.compute_offset((1,3), (5,3), (0,0))); + assert_eq!(Vec2::new(1, 1), center.compute_offset((3,1), (5,3), (0,0))); + assert_eq!(Vec2::new(0, 1), center.compute_offset((5,1), (5,3), (0,0))); + assert_eq!(Vec2::new(0, 0), center.compute_offset((5,3), (5,3), (0,0))); + assert_eq!(Vec2::new(0, 0), center.compute_offset((5,3), (3,1), (0,0))); + } +}