2016-07-29 06:05:08 +00:00
|
|
|
//! Base elements required to build views.
|
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
|
2017-01-24 06:52:29 +00:00
|
|
|
//! can use `View::required_size()` on them.
|
2016-07-14 03:52:24 +00:00
|
|
|
//!
|
|
|
|
//!
|
|
|
|
//! ### 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()`.
|
|
|
|
//!
|
2017-01-24 06:52:29 +00:00
|
|
|
//! * The value returned by `required_size` should be an actually viable size,
|
2016-07-14 03:52:24 +00:00
|
|
|
//! no matter what the request is. This means calling `View::layout()` with
|
2017-01-24 06:52:29 +00:00
|
|
|
//! a size returned by `required_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;
|
2016-08-04 04:55:41 +00:00
|
|
|
mod size_cache;
|
|
|
|
mod size_constraint;
|
2016-07-02 02:19:43 +00:00
|
|
|
mod view_path;
|
|
|
|
|
|
|
|
// Helper bases
|
|
|
|
mod scroll;
|
2016-08-04 04:55:41 +00:00
|
|
|
mod identifiable;
|
|
|
|
mod boxable;
|
2015-05-16 00:56:38 +00:00
|
|
|
|
2018-03-14 20:58:57 +00:00
|
|
|
mod into_boxed_view;
|
|
|
|
|
|
|
|
pub use self::into_boxed_view::IntoBoxedView;
|
2016-10-02 22:22:29 +00:00
|
|
|
pub use self::boxable::Boxable;
|
|
|
|
pub use self::identifiable::Identifiable;
|
2016-07-10 02:05:51 +00:00
|
|
|
pub use self::position::{Offset, Position};
|
2017-01-21 19:44:40 +00:00
|
|
|
pub use self::scroll::{ScrollBase, ScrollStrategy};
|
2016-08-04 04:55:41 +00:00
|
|
|
pub use self::size_cache::SizeCache;
|
|
|
|
pub use self::size_constraint::SizeConstraint;
|
2015-06-08 03:58:10 +00:00
|
|
|
pub use self::view_path::ViewPath;
|
|
|
|
pub use self::view_wrapper::ViewWrapper;
|
2017-02-07 02:18:17 +00:00
|
|
|
use Printer;
|
|
|
|
use direction::Direction;
|
|
|
|
use event::{Event, EventResult};
|
2017-10-12 23:38:55 +00:00
|
|
|
use std::any::Any;
|
2016-10-02 22:22:29 +00:00
|
|
|
use vec::Vec2;
|
2017-03-26 04:56:31 +00:00
|
|
|
use views::IdView;
|
2015-05-09 19:18:25 +00:00
|
|
|
|
2018-01-22 19:50:25 +00:00
|
|
|
/// A view that can be downcasted to its concrete type.
|
2018-03-14 19:32:07 +00:00
|
|
|
///
|
|
|
|
/// This trait is automatically implemented for any `T: View`.
|
2018-03-14 21:59:41 +00:00
|
|
|
pub trait AnyView {
|
2018-01-22 19:50:25 +00:00
|
|
|
/// Downcast self to a `Any`.
|
|
|
|
fn as_any(&self) -> &Any;
|
|
|
|
|
|
|
|
/// Downcast self to a mutable `Any`.
|
|
|
|
fn as_any_mut(&mut self) -> &mut Any;
|
2018-03-14 18:18:28 +00:00
|
|
|
|
|
|
|
/// Returns a boxed any from a boxed self.
|
|
|
|
///
|
|
|
|
/// Can be used before `Box::downcast()`.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # use cursive::views::TextView;
|
|
|
|
/// # use cursive::view::AnyView;
|
|
|
|
/// # fn main() {
|
|
|
|
/// let boxed: Box<AnyView> = Box::new(TextView::new("text"));
|
|
|
|
/// let text: Box<TextView> = boxed.as_boxed_any().downcast().unwrap();
|
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
fn as_boxed_any(self: Box<Self>) -> Box<Any>;
|
2018-01-22 19:50:25 +00:00
|
|
|
}
|
|
|
|
|
2018-01-22 19:55:56 +00:00
|
|
|
impl<T: View> AnyView for T {
|
2018-01-22 19:50:25 +00:00
|
|
|
/// Downcast self to a `Any`.
|
2018-01-22 19:55:56 +00:00
|
|
|
fn as_any(&self) -> &Any {
|
|
|
|
self
|
|
|
|
}
|
2018-01-22 19:50:25 +00:00
|
|
|
|
|
|
|
/// Downcast self to a mutable `Any`.
|
2018-01-22 19:55:56 +00:00
|
|
|
fn as_any_mut(&mut self) -> &mut Any {
|
|
|
|
self
|
|
|
|
}
|
2018-03-14 18:18:28 +00:00
|
|
|
|
|
|
|
fn as_boxed_any(self: Box<Self>) -> Box<Any> {
|
|
|
|
self
|
|
|
|
}
|
2018-01-22 19:50:25 +00:00
|
|
|
}
|
|
|
|
|
2015-05-23 17:33:29 +00:00
|
|
|
/// Main trait defining a view behaviour.
|
2018-03-14 19:32:07 +00:00
|
|
|
///
|
|
|
|
/// This is what you should implement to define a custom View.
|
2018-03-14 21:59:41 +00:00
|
|
|
pub trait View: Any + AnyView {
|
2016-09-21 01:32:31 +00:00
|
|
|
/// 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.
|
2016-09-21 01:32:31 +00:00
|
|
|
///
|
|
|
|
/// Default implementation always return `(1,1)`.
|
2017-01-24 06:52:29 +00:00
|
|
|
fn required_size(&mut self, constraint: Vec2) -> Vec2 {
|
2016-07-20 03:44:20 +00:00
|
|
|
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.
|
2017-01-24 06:52:29 +00:00
|
|
|
/// * If they call `required_size` or `layout` with stable parameters,
|
2016-07-11 00:41:49 +00:00
|
|
|
/// 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
|
|
|
|
2017-07-03 08:27:55 +00:00
|
|
|
/// Runs a closure on the view identified by the given selector.
|
2016-07-11 00:41:49 +00:00
|
|
|
///
|
2017-10-15 04:18:50 +00:00
|
|
|
/// See [`Finder::call_on`] for a nicer interface, implemented for all
|
|
|
|
/// views.
|
2016-09-21 21:57:03 +00:00
|
|
|
///
|
2017-03-27 03:50:50 +00:00
|
|
|
/// [`Finder::call_on`]: trait.Finder.html#method.call_on
|
2016-09-21 21:57:03 +00:00
|
|
|
///
|
2017-07-03 08:27:55 +00:00
|
|
|
/// If the selector doesn't find a match, the closure will not be run.
|
2016-09-21 01:32:31 +00:00
|
|
|
///
|
2017-07-03 08:27:55 +00:00
|
|
|
/// Default implementation is a no-op.
|
2017-03-27 03:50:50 +00:00
|
|
|
fn call_on_any<'a>(&mut self, _: &Selector, _: Box<FnMut(&mut Any) + 'a>) {
|
2017-02-07 02:18:17 +00:00
|
|
|
// TODO: FnMut -> FnOnce once it works
|
2016-03-15 22:37:57 +00:00
|
|
|
}
|
2015-05-23 17:33:29 +00:00
|
|
|
|
2017-03-25 21:50:52 +00:00
|
|
|
/// Moves the focus to the view identified by the given selector.
|
|
|
|
///
|
|
|
|
/// Returns `Ok(())` if the view was found and selected.
|
2017-07-03 08:27:55 +00:00
|
|
|
///
|
|
|
|
/// Default implementation simply returns `Err(())`.
|
2017-03-25 21:50:52 +00:00
|
|
|
fn focus_view(&mut self, &Selector) -> Result<(), ()> {
|
|
|
|
Err(())
|
|
|
|
}
|
|
|
|
|
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.
|
2016-09-21 01:32:31 +00:00
|
|
|
///
|
|
|
|
/// Default implementation always return `false`.
|
2016-07-15 03:27:15 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-03-27 03:50:50 +00:00
|
|
|
/// Provides `call_on<V: View>` to views.
|
2016-09-21 21:57:03 +00:00
|
|
|
///
|
2017-03-27 03:50:50 +00:00
|
|
|
/// This trait is mostly a wrapper around [`View::call_on_any`].
|
2016-09-21 21:57:03 +00:00
|
|
|
///
|
|
|
|
/// It provides a nicer interface to find a view when you know its type.
|
|
|
|
///
|
2017-03-27 03:50:50 +00:00
|
|
|
/// [`View::call_on_any`]: ./trait.View.html#method.call_on_any
|
2016-09-21 18:33:44 +00:00
|
|
|
pub trait Finder {
|
2016-09-21 21:57:03 +00:00
|
|
|
/// Tries to find the view pointed to by the given selector.
|
|
|
|
///
|
|
|
|
/// If the view is not found, or if it is not of the asked type,
|
|
|
|
/// it returns None.
|
2017-03-27 03:50:50 +00:00
|
|
|
fn call_on<V, F, R>(&mut self, sel: &Selector, callback: F) -> Option<R>
|
2017-10-12 23:38:55 +00:00
|
|
|
where
|
|
|
|
V: View + Any,
|
|
|
|
F: FnOnce(&mut V) -> R;
|
2016-09-21 21:57:03 +00:00
|
|
|
|
2017-03-27 03:50:50 +00:00
|
|
|
/// Convenient method to use `call_on` with a `view::Selector::Id`.
|
2017-02-07 02:18:17 +00:00
|
|
|
fn find_id<V, F, R>(&mut self, id: &str, callback: F) -> Option<R>
|
2017-10-12 23:38:55 +00:00
|
|
|
where
|
|
|
|
V: View + Any,
|
|
|
|
F: FnOnce(&mut V) -> R,
|
2017-02-07 02:18:17 +00:00
|
|
|
{
|
2017-03-27 03:50:50 +00:00
|
|
|
self.call_on(&Selector::Id(id), callback)
|
2016-09-21 21:57:03 +00:00
|
|
|
}
|
2016-09-21 18:33:44 +00:00
|
|
|
}
|
|
|
|
|
2016-09-23 05:10:14 +00:00
|
|
|
impl<T: View> Finder for T {
|
2017-03-27 03:50:50 +00:00
|
|
|
fn call_on<V, F, R>(&mut self, sel: &Selector, callback: F) -> Option<R>
|
2017-10-12 23:38:55 +00:00
|
|
|
where
|
|
|
|
V: View + Any,
|
|
|
|
F: FnOnce(&mut V) -> R,
|
2017-02-07 02:18:17 +00:00
|
|
|
{
|
|
|
|
let mut result = None;
|
|
|
|
{
|
|
|
|
let result_ref = &mut result;
|
|
|
|
|
|
|
|
let mut callback = Some(callback);
|
2017-12-30 22:03:42 +00:00
|
|
|
let callback = |v: &mut Any| {
|
|
|
|
if let Some(callback) = callback.take() {
|
|
|
|
if v.is::<V>() {
|
|
|
|
*result_ref =
|
|
|
|
v.downcast_mut::<V>().map(|v| callback(v));
|
|
|
|
} else if v.is::<IdView<V>>() {
|
|
|
|
*result_ref = v.downcast_mut::<IdView<V>>()
|
|
|
|
.and_then(|v| v.with_view_mut(callback));
|
|
|
|
}
|
2017-02-07 02:18:17 +00:00
|
|
|
}
|
|
|
|
};
|
2017-03-27 03:50:50 +00:00
|
|
|
self.call_on_any(sel, Box::new(callback));
|
2017-02-07 02:18:17 +00:00
|
|
|
}
|
|
|
|
result
|
2016-09-21 18:33:44 +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),
|
|
|
|
}
|