2015-05-15 19:16:58 +00:00
|
|
|
//! Defines various views to use when creating the layout.
|
|
|
|
|
2015-05-19 22:54:11 +00:00
|
|
|
#[macro_use] mod view_wrapper;
|
2015-05-16 00:56:38 +00:00
|
|
|
mod box_view;
|
|
|
|
mod stack_view;
|
|
|
|
mod text_view;
|
|
|
|
mod key_event_view;
|
|
|
|
mod view_path;
|
2015-05-16 21:02:15 +00:00
|
|
|
mod dialog;
|
2015-05-19 02:41:35 +00:00
|
|
|
mod button;
|
|
|
|
mod sized_view;
|
2015-05-22 23:28:05 +00:00
|
|
|
mod full_view;
|
2015-05-23 17:33:29 +00:00
|
|
|
mod id_view;
|
2015-05-23 22:58:06 +00:00
|
|
|
mod shadow_view;
|
2015-05-26 22:46:05 +00:00
|
|
|
mod edit_view;
|
2015-05-31 04:32:24 +00:00
|
|
|
mod select_view;
|
2015-05-16 00:56:38 +00:00
|
|
|
|
|
|
|
use std::any::Any;
|
2015-05-09 19:18:25 +00:00
|
|
|
|
2015-05-16 00:56:38 +00:00
|
|
|
pub use self::view_path::ViewPath;
|
|
|
|
pub use self::key_event_view::KeyEventView;
|
|
|
|
pub use self::box_view::BoxView;
|
|
|
|
pub use self::stack_view::StackView;
|
|
|
|
pub use self::text_view::TextView;
|
2015-05-16 21:02:15 +00:00
|
|
|
pub use self::dialog::Dialog;
|
2015-05-19 02:41:35 +00:00
|
|
|
pub use self::button::Button;
|
|
|
|
pub use self::sized_view::SizedView;
|
|
|
|
pub use self::view_wrapper::ViewWrapper;
|
2015-05-22 23:28:05 +00:00
|
|
|
pub use self::full_view::FullView;
|
2015-05-23 17:33:29 +00:00
|
|
|
pub use self::id_view::IdView;
|
2015-05-23 22:58:06 +00:00
|
|
|
pub use self::shadow_view::ShadowView;
|
2015-05-26 22:46:05 +00:00
|
|
|
pub use self::edit_view::EditView;
|
2015-05-31 04:32:24 +00:00
|
|
|
pub use self::select_view::SelectView;
|
2015-05-09 19:18:25 +00:00
|
|
|
|
2015-05-28 01:04:33 +00:00
|
|
|
use event::{Event,EventResult};
|
2015-05-19 02:41:35 +00:00
|
|
|
use vec::{Vec2,ToVec2};
|
2015-05-15 18:58:47 +00:00
|
|
|
use printer::Printer;
|
2015-05-09 19:18:25 +00:00
|
|
|
|
2015-05-23 17:33:29 +00:00
|
|
|
/// Main trait defining a view behaviour.
|
|
|
|
pub trait View {
|
|
|
|
/// Called when a key was pressed. Default implementation just ignores it.
|
2015-05-28 01:04:33 +00:00
|
|
|
fn on_event(&mut self, Event) -> EventResult { EventResult::Ignored }
|
2015-05-23 17:33:29 +00:00
|
|
|
|
|
|
|
/// Returns the minimum size the view requires under the given restrictions.
|
|
|
|
fn get_min_size(&self, SizeRequest) -> Vec2 { Vec2::new(1,1) }
|
|
|
|
|
|
|
|
/// Called once the size for this view has been decided, so it can
|
|
|
|
/// propagate the information to its children.
|
|
|
|
fn layout(&mut self, Vec2) { }
|
|
|
|
|
|
|
|
/// Draws the view with the given printer (includes bounds) and focus.
|
2015-05-31 04:05:34 +00:00
|
|
|
fn draw(&mut self, printer: &Printer);
|
2015-05-23 17:33:29 +00:00
|
|
|
|
|
|
|
/// Finds the view pointed to by the given path.
|
|
|
|
/// Returns None if the path doesn't lead to a view.
|
|
|
|
fn find(&mut self, &Selector) -> Option<&mut Any> { None }
|
|
|
|
|
|
|
|
/// This view is offered focus. Will it take it?
|
|
|
|
fn take_focus(&mut self) -> bool { false }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Selects a single view (if any) in the tree.
|
|
|
|
pub enum Selector<'a> {
|
|
|
|
Id(&'a str),
|
|
|
|
Path(&'a ViewPath),
|
|
|
|
}
|
|
|
|
|
2015-05-15 00:41:17 +00:00
|
|
|
/// Describe constraints on a view layout in one dimension.
|
2015-05-15 01:38:58 +00:00
|
|
|
#[derive(PartialEq,Clone,Copy)]
|
2015-05-15 00:41:17 +00:00
|
|
|
pub enum DimensionRequest {
|
|
|
|
/// The view must use exactly the attached size.
|
2015-05-25 21:46:29 +00:00
|
|
|
Fixed(usize),
|
2015-05-15 00:41:17 +00:00
|
|
|
/// The view is free to choose its size if it stays under the limit.
|
2015-05-25 21:46:29 +00:00
|
|
|
AtMost(usize),
|
2015-05-15 00:41:17 +00:00
|
|
|
/// No clear restriction apply.
|
|
|
|
Unknown,
|
2015-05-09 19:18:25 +00:00
|
|
|
}
|
|
|
|
|
2015-05-19 02:41:35 +00:00
|
|
|
impl DimensionRequest {
|
2015-05-20 18:11:55 +00:00
|
|
|
/// Returns a new request, reduced from the original by the given offset.
|
2015-05-25 21:46:29 +00:00
|
|
|
pub fn reduced(self, offset: usize) -> Self {
|
2015-05-19 02:41:35 +00:00
|
|
|
match self {
|
|
|
|
DimensionRequest::Fixed(w) => DimensionRequest::Fixed(w - offset),
|
|
|
|
DimensionRequest::AtMost(w) => DimensionRequest::AtMost(w - offset),
|
|
|
|
DimensionRequest::Unknown => DimensionRequest::Unknown,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-15 00:41:17 +00:00
|
|
|
/// Describes constraints on a view layout.
|
2015-05-15 01:38:58 +00:00
|
|
|
#[derive(PartialEq,Clone,Copy)]
|
2015-05-15 00:41:17 +00:00
|
|
|
pub struct SizeRequest {
|
|
|
|
/// Restriction on the view width
|
|
|
|
pub w: DimensionRequest,
|
|
|
|
/// Restriction on the view height
|
|
|
|
pub h: DimensionRequest,
|
2015-05-09 19:18:25 +00:00
|
|
|
}
|
|
|
|
|
2015-05-19 02:41:35 +00:00
|
|
|
impl SizeRequest {
|
2015-05-20 18:11:55 +00:00
|
|
|
/// Returns a new SizeRequest, reduced from the original by the given offset.
|
2015-05-19 02:41:35 +00:00
|
|
|
pub fn reduced<T: ToVec2>(self, offset: T) -> Self {
|
|
|
|
let ov = offset.to_vec2();
|
|
|
|
SizeRequest {
|
|
|
|
w: self.w.reduced(ov.x),
|
|
|
|
h: self.h.reduced(ov.y),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-20 18:11:55 +00:00
|
|
|
/// Creates a new dummy request, with no restriction.
|
2015-05-19 02:41:35 +00:00
|
|
|
pub fn dummy() -> Self {
|
|
|
|
SizeRequest {
|
|
|
|
w: DimensionRequest::Unknown,
|
|
|
|
h: DimensionRequest::Unknown,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-16 00:56:38 +00:00
|
|
|
|