mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-12 20:23:35 +00:00
Add some tests to view::position
This commit is contained in:
parent
e3ac2a0482
commit
7d16f70d67
@ -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<Self> {
|
||||
IdView::new(id, self)
|
||||
}
|
||||
}
|
||||
|
||||
/// Any `View` implements this trait.
|
||||
impl<T: View> Identifiable for T {}
|
||||
|
@ -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
|
||||
|
@ -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<S, A, P>(&self, size: S, available: A, parent: P)
|
||||
-> Vec2
|
||||
where S: Into<Vec2>,
|
||||
A: Into<Vec2>,
|
||||
P: Into<Vec2>
|
||||
{
|
||||
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)));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user