2015-05-15 19:16:58 +00:00
|
|
|
//! Defines various views to use when creating the layout.
|
2016-07-14 03:52:24 +00:00
|
|
|
//!
|
|
|
|
//! Views are the main building blocks of your UI.
|
|
|
|
//!
|
|
|
|
//! A view can delegate part or all of its responsabilities to child views,
|
|
|
|
//! forming a view tree. The root of this tree is a `StackView` handled
|
|
|
|
//! directly by the `Cursive` element.
|
|
|
|
//!
|
|
|
|
//! # Layout
|
|
|
|
//!
|
|
|
|
//! The layout phase is when the size and location of each view is computed.
|
|
|
|
//!
|
|
|
|
//! Each view is given an area of the screen by the `View::layout()` method.
|
|
|
|
//! With this, the view is free to plan its content, including calling
|
|
|
|
//! `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.
|
|
|
|
//!
|
|
|
|
//!
|
|
|
|
//! ### Contracts
|
|
|
|
//!
|
|
|
|
//! When building new Views, you should respect these contracts:
|
|
|
|
//!
|
|
|
|
//! * By default, `View::layout()` should be called before any call to
|
|
|
|
//! `View::draw()` with the same available size. The only exceptions is
|
|
|
|
//! when both following conditions are met:
|
2016-07-15 03:27:15 +00:00
|
|
|
//! * The available size has not changed since the last call to
|
|
|
|
//! `View::layout()`
|
2016-07-14 03:52:24 +00:00
|
|
|
//! * `View::needs_relayout()` returns `false`
|
|
|
|
//!
|
|
|
|
//! 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,
|
|
|
|
//! no matter what the request is. This means calling `View::layout()` with
|
|
|
|
//! a size returned by `get_min_size` is **never** an error.
|
2015-05-15 19:16:58 +00:00
|
|
|
|
2016-07-02 03:23:58 +00:00
|
|
|
#[macro_use]
|
|
|
|
mod view_wrapper;
|
2015-06-08 03:58:10 +00:00
|
|
|
|
2016-07-02 02:19:43 +00:00
|
|
|
// Essentials components
|
|
|
|
mod position;
|
|
|
|
mod view_path;
|
|
|
|
|
|
|
|
// Helper bases
|
|
|
|
mod scroll;
|
|
|
|
|
|
|
|
// Views
|
2015-05-16 00:56:38 +00:00
|
|
|
mod box_view;
|
2015-05-19 02:41:35 +00:00
|
|
|
mod button;
|
2016-07-17 00:22:24 +00:00
|
|
|
mod checkbox;
|
2015-06-08 03:58:10 +00:00
|
|
|
mod dialog;
|
|
|
|
mod edit_view;
|
2015-05-22 23:28:05 +00:00
|
|
|
mod full_view;
|
2015-05-23 17:33:29 +00:00
|
|
|
mod id_view;
|
2015-06-08 03:58:10 +00:00
|
|
|
mod key_event_view;
|
|
|
|
mod linear_layout;
|
2016-07-16 20:25:21 +00:00
|
|
|
mod list_view;
|
2016-07-20 03:44:20 +00:00
|
|
|
mod menubar;
|
2016-07-02 08:01:09 +00:00
|
|
|
mod menu_popup;
|
2016-07-25 20:35:46 +00:00
|
|
|
mod panel;
|
2016-07-25 06:00:13 +00:00
|
|
|
mod progress_bar;
|
2015-05-23 22:58:06 +00:00
|
|
|
mod shadow_view;
|
2015-05-31 04:32:24 +00:00
|
|
|
mod select_view;
|
2015-06-08 03:58:10 +00:00
|
|
|
mod sized_view;
|
|
|
|
mod stack_view;
|
|
|
|
mod text_view;
|
2016-07-02 03:23:58 +00:00
|
|
|
mod tracked_view;
|
2015-05-16 00:56:38 +00:00
|
|
|
|
2015-05-31 23:38:53 +00:00
|
|
|
|
2015-05-16 00:56:38 +00:00
|
|
|
use std::any::Any;
|
2015-05-09 19:18:25 +00:00
|
|
|
|
2016-07-13 08:19:05 +00:00
|
|
|
use XY;
|
2016-07-15 03:27:15 +00:00
|
|
|
use direction::Direction;
|
2016-03-15 22:37:57 +00:00
|
|
|
use event::{Event, EventResult};
|
2015-06-08 03:58:10 +00:00
|
|
|
use vec::Vec2;
|
2016-07-14 06:25:54 +00:00
|
|
|
use Printer;
|
2015-06-08 03:58:10 +00:00
|
|
|
|
2016-07-10 02:05:51 +00:00
|
|
|
pub use self::position::{Offset, Position};
|
2016-07-02 02:19:43 +00:00
|
|
|
|
2015-06-08 03:58:10 +00:00
|
|
|
pub use self::scroll::ScrollBase;
|
|
|
|
|
2016-07-25 06:00:13 +00:00
|
|
|
pub use self::id_view::{IdView, Identifiable};
|
2015-05-16 00:56:38 +00:00
|
|
|
pub use self::box_view::BoxView;
|
2015-05-19 02:41:35 +00:00
|
|
|
pub use self::button::Button;
|
2016-07-17 00:22:24 +00:00
|
|
|
pub use self::checkbox::Checkbox;
|
2015-06-08 03:58:10 +00:00
|
|
|
pub use self::dialog::Dialog;
|
|
|
|
pub use self::edit_view::EditView;
|
2015-05-22 23:28:05 +00:00
|
|
|
pub use self::full_view::FullView;
|
2015-06-08 03:58:10 +00:00
|
|
|
pub use self::key_event_view::KeyEventView;
|
|
|
|
pub use self::linear_layout::LinearLayout;
|
2016-07-16 20:25:21 +00:00
|
|
|
pub use self::list_view::ListView;
|
2016-07-20 03:44:20 +00:00
|
|
|
pub use self::menubar::Menubar;
|
2016-07-02 08:01:09 +00:00
|
|
|
pub use self::menu_popup::MenuPopup;
|
2015-06-08 03:58:10 +00:00
|
|
|
pub use self::view_path::ViewPath;
|
2016-07-25 20:35:46 +00:00
|
|
|
pub use self::panel::Panel;
|
2016-07-25 06:00:13 +00:00
|
|
|
pub use self::progress_bar::ProgressBar;
|
2015-05-31 04:32:24 +00:00
|
|
|
pub use self::select_view::SelectView;
|
2015-06-08 03:58:10 +00:00
|
|
|
pub use self::shadow_view::ShadowView;
|
2016-07-25 06:00:13 +00:00
|
|
|
pub use self::sized_view::SizedView;
|
2015-06-08 03:58:10 +00:00
|
|
|
pub use self::stack_view::StackView;
|
|
|
|
pub use self::text_view::TextView;
|
2016-07-02 03:23:58 +00:00
|
|
|
pub use self::tracked_view::TrackedView;
|
2015-06-08 03:58:10 +00:00
|
|
|
pub use self::view_wrapper::ViewWrapper;
|
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.
|
2016-03-15 22:37:57 +00:00
|
|
|
fn on_event(&mut self, Event) -> EventResult {
|
|
|
|
EventResult::Ignored
|
|
|
|
}
|
2015-05-23 17:33:29 +00:00
|
|
|
|
2016-07-10 02:05:51 +00:00
|
|
|
/// Returns the minimum size the view requires with the given restrictions.
|
2016-07-20 03:44:20 +00:00
|
|
|
///
|
|
|
|
/// 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.
|
|
|
|
fn get_min_size(&mut self, constraint: Vec2) -> Vec2 {
|
|
|
|
let _ = constraint;
|
2016-03-15 22:37:57 +00:00
|
|
|
Vec2::new(1, 1)
|
|
|
|
}
|
2015-05-23 17:33:29 +00:00
|
|
|
|
2016-07-11 00:41:49 +00:00
|
|
|
/// Returns `true` if the view content changed since last layout phase.
|
|
|
|
///
|
|
|
|
/// This is mostly an optimisation for views where the layout phase is
|
|
|
|
/// expensive.
|
|
|
|
///
|
|
|
|
/// * 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,
|
|
|
|
/// the children may cache the result themselves and speed up the
|
|
|
|
/// process anyway.
|
|
|
|
fn needs_relayout(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Called once the size for this view has been decided,
|
|
|
|
///
|
|
|
|
/// View groups should propagate the information to their children.
|
2016-06-25 23:36:22 +00:00
|
|
|
fn layout(&mut self, Vec2) {}
|
2015-05-23 17:33:29 +00:00
|
|
|
|
|
|
|
/// Draws the view with the given printer (includes bounds) and focus.
|
2016-07-16 06:44:38 +00:00
|
|
|
fn draw(&self, printer: &Printer);
|
2015-05-23 17:33:29 +00:00
|
|
|
|
|
|
|
/// Finds the view pointed to by the given path.
|
2016-07-11 00:41:49 +00:00
|
|
|
///
|
2015-05-23 17:33:29 +00:00
|
|
|
/// Returns None if the path doesn't lead to a view.
|
2016-03-15 22:37:57 +00:00
|
|
|
fn find(&mut self, &Selector) -> Option<&mut Any> {
|
|
|
|
None
|
|
|
|
}
|
2015-05-23 17:33:29 +00:00
|
|
|
|
|
|
|
/// This view is offered focus. Will it take it?
|
2016-07-15 03:27:15 +00:00
|
|
|
///
|
|
|
|
/// `source` indicates where the focus comes from.
|
|
|
|
/// When the source is unclear, `Front` is usually used.
|
|
|
|
fn take_focus(&mut self, source: Direction) -> bool {
|
|
|
|
let _ = source;
|
2016-03-15 22:37:57 +00:00
|
|
|
false
|
|
|
|
}
|
2015-05-23 17:33:29 +00:00
|
|
|
}
|
|
|
|
|
2016-07-21 05:08:06 +00:00
|
|
|
/// Cache around a one-dimensional layout result.
|
|
|
|
///
|
|
|
|
/// This is not a View, but something to help you if you create your own Views.
|
2016-07-13 08:19:05 +00:00
|
|
|
#[derive(PartialEq, Debug, Clone, Copy)]
|
|
|
|
pub struct SizeCache {
|
|
|
|
/// Cached value
|
|
|
|
pub value: usize,
|
|
|
|
/// `true` if the last size was constrained.
|
|
|
|
///
|
|
|
|
/// If unconstrained, any request larger than this value
|
|
|
|
/// would return the same size.
|
|
|
|
pub constrained: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SizeCache {
|
|
|
|
/// Creates a new sized cache
|
|
|
|
pub fn new(value: usize, constrained: bool) -> Self {
|
|
|
|
SizeCache {
|
|
|
|
value: value,
|
|
|
|
constrained: constrained,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` if `self` is still valid for the given `request`.
|
2016-07-14 05:11:03 +00:00
|
|
|
pub fn accept(self, request: usize) -> bool {
|
2016-07-13 08:19:05 +00:00
|
|
|
if request < self.value {
|
|
|
|
false
|
|
|
|
} else if request == self.value {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
!self.constrained
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new bi-dimensional cache.
|
|
|
|
///
|
|
|
|
/// * `size` must fit inside `req`.
|
|
|
|
/// * for each dimension, `constrained = (size == req)`
|
|
|
|
fn build(size: Vec2, req: Vec2) -> XY<Self> {
|
2016-07-17 00:08:17 +00:00
|
|
|
XY::new(SizeCache::new(size.x, size.x >= req.x),
|
|
|
|
SizeCache::new(size.y, size.y >= req.y))
|
2016-07-13 08:19:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-23 17:33:29 +00:00
|
|
|
/// Selects a single view (if any) in the tree.
|
|
|
|
pub enum Selector<'a> {
|
2016-07-20 06:47:27 +00:00
|
|
|
/// Selects a view from its ID.
|
2015-05-23 17:33:29 +00:00
|
|
|
Id(&'a str),
|
2016-07-20 06:47:27 +00:00
|
|
|
/// Selects a view from its path.
|
2015-05-23 17:33:29 +00:00
|
|
|
Path(&'a ViewPath),
|
|
|
|
}
|