2016-10-02 22:22:29 +00:00
|
|
|
use Printer;
|
2016-07-15 03:27:15 +00:00
|
|
|
use direction::Direction;
|
2016-10-02 22:22:29 +00:00
|
|
|
use event::{Event, EventResult};
|
|
|
|
use std::any::Any;
|
2015-05-19 02:41:35 +00:00
|
|
|
use vec::Vec2;
|
2016-07-02 07:47:38 +00:00
|
|
|
use view::{Selector, View};
|
2015-05-19 02:41:35 +00:00
|
|
|
|
2015-05-23 17:33:29 +00:00
|
|
|
/// Generic wrapper around a view.
|
|
|
|
///
|
|
|
|
/// Default implementation forwards all calls to the child view.
|
|
|
|
/// Overrides some methods as desired.
|
2016-09-01 18:56:11 +00:00
|
|
|
///
|
2017-02-07 02:18:17 +00:00
|
|
|
/// You can use the [`wrap_impl!`] macro to define `with_view` and
|
|
|
|
/// `with_view_mut` for you.
|
2016-09-01 18:56:11 +00:00
|
|
|
///
|
|
|
|
/// [`wrap_impl!`]: ../macro.wrap_impl.html
|
2015-05-19 02:41:35 +00:00
|
|
|
pub trait ViewWrapper {
|
2016-09-20 00:11:00 +00:00
|
|
|
/// Type that this view wraps.
|
2017-09-24 19:24:23 +00:00
|
|
|
type V: View + ?Sized;
|
2016-09-20 00:11:00 +00:00
|
|
|
|
2017-09-25 01:50:45 +00:00
|
|
|
/// Runs a function on the inner view, returning the result.
|
2017-09-24 19:24:23 +00:00
|
|
|
///
|
2017-09-25 01:50:45 +00:00
|
|
|
/// Returns `None` if the inner view is unavailable. This should only
|
|
|
|
/// happen with some views if they are already borrowed by another call.
|
2017-09-24 19:24:23 +00:00
|
|
|
fn with_view<F, R>(&self, f: F) -> Option<R>
|
|
|
|
where
|
|
|
|
F: FnOnce(&Self::V) -> R;
|
2016-07-11 02:11:21 +00:00
|
|
|
|
2017-09-25 01:50:45 +00:00
|
|
|
/// Runs a function on the inner view, returning the result.
|
2017-09-24 19:24:23 +00:00
|
|
|
///
|
2017-09-25 01:50:45 +00:00
|
|
|
/// Returns `None` if the inner view is unavailable. This should only
|
|
|
|
/// happen with some views if they are already borrowed by another call.
|
2017-02-08 23:33:43 +00:00
|
|
|
fn with_view_mut<F, R>(&mut self, f: F) -> Option<R>
|
2017-09-24 19:24:23 +00:00
|
|
|
where
|
|
|
|
F: FnOnce(&mut Self::V) -> R;
|
2015-05-19 02:41:35 +00:00
|
|
|
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Wraps the `draw` method.
|
2016-07-16 06:44:38 +00:00
|
|
|
fn wrap_draw(&self, printer: &Printer) {
|
2017-02-07 02:18:17 +00:00
|
|
|
self.with_view(|v| v.draw(printer));
|
2015-05-19 02:41:35 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 06:52:29 +00:00
|
|
|
/// Wraps the `required_size` method.
|
|
|
|
fn wrap_required_size(&mut self, req: Vec2) -> Vec2 {
|
2017-09-24 19:24:23 +00:00
|
|
|
self.with_view_mut(|v| v.required_size(req))
|
|
|
|
.unwrap_or_else(Vec2::zero)
|
2015-05-19 02:41:35 +00:00
|
|
|
}
|
|
|
|
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Wraps the `on_event` method.
|
2015-05-28 01:04:33 +00:00
|
|
|
fn wrap_on_event(&mut self, ch: Event) -> EventResult {
|
2017-10-12 23:38:55 +00:00
|
|
|
self.with_view_mut(|v| v.on_event(ch))
|
|
|
|
.unwrap_or(EventResult::Ignored)
|
2015-05-19 02:41:35 +00:00
|
|
|
}
|
|
|
|
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Wraps the `layout` method.
|
2015-05-19 02:41:35 +00:00
|
|
|
fn wrap_layout(&mut self, size: Vec2) {
|
2017-02-07 02:18:17 +00:00
|
|
|
self.with_view_mut(|v| v.layout(size));
|
2015-05-19 02:41:35 +00:00
|
|
|
}
|
2015-05-20 00:31:52 +00:00
|
|
|
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Wraps the `take_focus` method.
|
2016-07-15 03:27:15 +00:00
|
|
|
fn wrap_take_focus(&mut self, source: Direction) -> bool {
|
2017-10-12 23:38:55 +00:00
|
|
|
self.with_view_mut(|v| v.take_focus(source))
|
|
|
|
.unwrap_or(false)
|
2015-05-20 00:31:52 +00:00
|
|
|
}
|
2015-05-23 17:33:29 +00:00
|
|
|
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Wraps the `find` method.
|
2017-10-12 23:38:55 +00:00
|
|
|
fn wrap_call_on_any<'a>(
|
|
|
|
&mut self, selector: &Selector, callback: Box<FnMut(&mut Any) + 'a>
|
|
|
|
) {
|
2017-03-27 03:50:50 +00:00
|
|
|
self.with_view_mut(|v| v.call_on_any(selector, callback));
|
2015-05-23 17:33:29 +00:00
|
|
|
}
|
2016-07-11 00:41:49 +00:00
|
|
|
|
2017-03-25 21:50:52 +00:00
|
|
|
/// Wraps the `focus_view` method.
|
|
|
|
fn wrap_focus_view(&mut self, selector: &Selector) -> Result<(), ()> {
|
2017-10-12 23:38:55 +00:00
|
|
|
self.with_view_mut(|v| v.focus_view(selector))
|
|
|
|
.unwrap_or(Err(()))
|
2017-03-25 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Wraps the `needs_relayout` method.
|
2016-07-11 00:41:49 +00:00
|
|
|
fn wrap_needs_relayout(&self) -> bool {
|
2017-02-08 23:33:43 +00:00
|
|
|
self.with_view(|v| v.needs_relayout()).unwrap_or(true)
|
2016-07-11 00:41:49 +00:00
|
|
|
}
|
2015-05-19 02:41:35 +00:00
|
|
|
}
|
|
|
|
|
2017-09-25 01:50:45 +00:00
|
|
|
// Some types easily implement ViewWrapper.
|
|
|
|
// This includes Box<T: View>
|
2017-09-24 19:24:23 +00:00
|
|
|
use std::ops::{Deref, DerefMut};
|
|
|
|
impl<U: View + ?Sized, T: Deref<Target = U> + DerefMut> ViewWrapper for T {
|
|
|
|
type V = U;
|
|
|
|
|
|
|
|
fn with_view<F, R>(&self, f: F) -> Option<R>
|
|
|
|
where
|
|
|
|
F: FnOnce(&Self::V) -> R,
|
|
|
|
{
|
|
|
|
Some(f(self.deref()))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn with_view_mut<F, R>(&mut self, f: F) -> Option<R>
|
|
|
|
where
|
|
|
|
F: FnOnce(&mut Self::V) -> R,
|
|
|
|
{
|
|
|
|
Some(f(self.deref_mut()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-25 01:50:45 +00:00
|
|
|
// The main point of implementing ViewWrapper is to have View for free.
|
2016-06-25 23:36:22 +00:00
|
|
|
impl<T: ViewWrapper> View for T {
|
2016-07-16 06:44:38 +00:00
|
|
|
fn draw(&self, printer: &Printer) {
|
2015-05-31 04:05:34 +00:00
|
|
|
self.wrap_draw(printer);
|
2015-05-19 02:41:35 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 06:52:29 +00:00
|
|
|
fn required_size(&mut self, req: Vec2) -> Vec2 {
|
|
|
|
self.wrap_required_size(req)
|
2015-05-19 02:41:35 +00:00
|
|
|
}
|
|
|
|
|
2015-05-28 01:04:33 +00:00
|
|
|
fn on_event(&mut self, ch: Event) -> EventResult {
|
|
|
|
self.wrap_on_event(ch)
|
2015-05-19 02:41:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn layout(&mut self, size: Vec2) {
|
|
|
|
self.wrap_layout(size);
|
|
|
|
}
|
2015-05-20 00:31:52 +00:00
|
|
|
|
2016-07-15 03:27:15 +00:00
|
|
|
fn take_focus(&mut self, source: Direction) -> bool {
|
|
|
|
self.wrap_take_focus(source)
|
2015-05-20 00:31:52 +00:00
|
|
|
}
|
2015-05-23 17:33:29 +00:00
|
|
|
|
2017-10-12 23:38:55 +00:00
|
|
|
fn call_on_any<'a>(
|
|
|
|
&mut self, selector: &Selector, callback: Box<FnMut(&mut Any) + 'a>
|
|
|
|
) {
|
2017-03-27 03:50:50 +00:00
|
|
|
self.wrap_call_on_any(selector, callback)
|
2015-05-23 17:33:29 +00:00
|
|
|
}
|
2016-07-11 00:41:49 +00:00
|
|
|
|
|
|
|
fn needs_relayout(&self) -> bool {
|
|
|
|
self.wrap_needs_relayout()
|
|
|
|
}
|
2017-03-25 21:50:52 +00:00
|
|
|
|
|
|
|
fn focus_view(&mut self, selector: &Selector) -> Result<(), ()> {
|
|
|
|
self.wrap_focus_view(selector)
|
|
|
|
}
|
2015-05-19 02:41:35 +00:00
|
|
|
}
|
2015-05-19 22:54:11 +00:00
|
|
|
|
2016-09-01 18:56:11 +00:00
|
|
|
/// Convenient macro to implement the [`ViewWrapper`] trait.
|
|
|
|
///
|
2017-02-07 02:18:17 +00:00
|
|
|
/// It defines the `with_view` and `with_view_mut` implementations,
|
2016-09-20 00:11:00 +00:00
|
|
|
/// as well as the `type V` declaration.
|
|
|
|
///
|
2016-09-01 18:56:11 +00:00
|
|
|
/// [`ViewWrapper`]: view/trait.ViewWrapper.html
|
2015-05-20 00:31:52 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2015-06-08 03:58:10 +00:00
|
|
|
/// ```no_run
|
2015-06-04 18:40:35 +00:00
|
|
|
/// # #[macro_use] extern crate cursive;
|
|
|
|
/// # use cursive::view::{View,ViewWrapper};
|
2015-05-20 00:31:52 +00:00
|
|
|
/// struct FooView<T: View> {
|
|
|
|
/// view: T,
|
|
|
|
/// }
|
|
|
|
///
|
2015-06-04 18:40:35 +00:00
|
|
|
/// impl <T: View> ViewWrapper for FooView<T> {
|
2016-09-20 00:11:00 +00:00
|
|
|
/// wrap_impl!(self.view: T);
|
2015-05-20 00:31:52 +00:00
|
|
|
/// }
|
2015-06-04 18:40:35 +00:00
|
|
|
/// # fn main() { }
|
2015-05-20 00:31:52 +00:00
|
|
|
/// ```
|
2015-05-19 22:54:11 +00:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! wrap_impl {
|
2016-09-24 23:56:42 +00:00
|
|
|
(self.$v:ident: $t:ty) => {
|
2016-09-20 00:11:00 +00:00
|
|
|
type V = $t;
|
2015-05-19 23:40:32 +00:00
|
|
|
|
2017-02-08 23:33:43 +00:00
|
|
|
fn with_view<F, R>(&self, f: F) -> Option<R>
|
|
|
|
where F: FnOnce(&Self::V) -> R
|
|
|
|
{
|
|
|
|
Some(f(&self.$v))
|
2015-05-19 23:40:32 +00:00
|
|
|
}
|
|
|
|
|
2017-02-08 23:33:43 +00:00
|
|
|
fn with_view_mut<F, R>(&mut self, f: F) -> Option<R>
|
2017-02-07 02:18:17 +00:00
|
|
|
where F: FnOnce(&mut Self::V) -> R
|
|
|
|
{
|
2017-02-08 23:33:43 +00:00
|
|
|
Some(f(&mut self.$v))
|
2015-05-19 23:40:32 +00:00
|
|
|
}
|
|
|
|
};
|
2015-05-19 22:54:11 +00:00
|
|
|
}
|