2015-05-23 17:33:29 +00:00
|
|
|
use std::any::Any;
|
|
|
|
|
2016-07-15 03:27:15 +00:00
|
|
|
use direction::Direction;
|
2015-05-19 02:41:35 +00:00
|
|
|
use vec::Vec2;
|
2016-07-02 07:47:38 +00:00
|
|
|
use view::{Selector, View};
|
2016-07-14 06:25:54 +00:00
|
|
|
use Printer;
|
2016-03-15 22:37:57 +00:00
|
|
|
use event::{Event, EventResult};
|
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.
|
2015-05-19 02:41:35 +00:00
|
|
|
pub trait ViewWrapper {
|
2016-07-11 02:11:21 +00:00
|
|
|
/// Get an immutable reference to the wrapped view.
|
2015-05-19 02:41:35 +00:00
|
|
|
fn get_view(&self) -> &View;
|
2016-07-11 02:11:21 +00:00
|
|
|
|
|
|
|
/// Get a mutable reference to the wrapped view.
|
2015-05-19 02:41:35 +00:00
|
|
|
fn get_view_mut(&mut self) -> &mut View;
|
|
|
|
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Wraps the `draw` method.
|
2015-05-31 04:05:34 +00:00
|
|
|
fn wrap_draw(&mut self, printer: &Printer) {
|
|
|
|
self.get_view_mut().draw(printer);
|
2015-05-19 02:41:35 +00:00
|
|
|
}
|
|
|
|
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Wraps the `get_min_size` method.
|
2016-07-10 01:23:58 +00:00
|
|
|
fn wrap_get_min_size(&mut self, req: Vec2) -> Vec2 {
|
|
|
|
self.get_view_mut().get_min_size(req)
|
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 {
|
|
|
|
self.get_view_mut().on_event(ch)
|
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) {
|
|
|
|
self.get_view_mut().layout(size);
|
|
|
|
}
|
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 {
|
|
|
|
self.get_view_mut().take_focus(source)
|
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.
|
2015-05-23 17:33:29 +00:00
|
|
|
fn wrap_find(&mut self, selector: &Selector) -> Option<&mut Any> {
|
|
|
|
self.get_view_mut().find(selector)
|
|
|
|
}
|
2016-07-11 00:41:49 +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 {
|
|
|
|
self.get_view().needs_relayout()
|
|
|
|
}
|
2015-05-19 02:41:35 +00:00
|
|
|
}
|
|
|
|
|
2016-06-25 23:36:22 +00:00
|
|
|
impl<T: ViewWrapper> View for T {
|
2015-05-31 04:05:34 +00:00
|
|
|
fn draw(&mut self, printer: &Printer) {
|
|
|
|
self.wrap_draw(printer);
|
2015-05-19 02:41:35 +00:00
|
|
|
}
|
|
|
|
|
2016-07-10 01:23:58 +00:00
|
|
|
fn get_min_size(&mut self, req: Vec2) -> Vec2 {
|
2015-05-19 02:41:35 +00:00
|
|
|
self.wrap_get_min_size(req)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
fn find(&mut self, selector: &Selector) -> Option<&mut Any> {
|
|
|
|
self.wrap_find(selector)
|
|
|
|
}
|
2016-07-11 00:41:49 +00:00
|
|
|
|
|
|
|
fn needs_relayout(&self) -> bool {
|
|
|
|
self.wrap_needs_relayout()
|
|
|
|
}
|
2015-05-19 02:41:35 +00:00
|
|
|
}
|
2015-05-19 22:54:11 +00:00
|
|
|
|
2016-07-11 02:11:21 +00:00
|
|
|
/// Convenient macro to implement the ViewWrapper trait.
|
2015-05-20 00:31:52 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// If the wrapped view is in a box, just name it in the macro:
|
|
|
|
///
|
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 BoxFooView {
|
|
|
|
/// content: Box<View>,
|
|
|
|
/// }
|
|
|
|
///
|
2015-06-04 18:40:35 +00:00
|
|
|
/// impl ViewWrapper for BoxFooView {
|
2015-05-20 00:31:52 +00:00
|
|
|
/// wrap_impl!(self.content);
|
|
|
|
/// }
|
2015-06-04 18:40:35 +00:00
|
|
|
/// # fn main() { }
|
2015-05-20 00:31:52 +00:00
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// If the content is directly a view, reference it:
|
|
|
|
///
|
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> {
|
2015-05-20 00:31:52 +00:00
|
|
|
/// wrap_impl!(&self.view);
|
|
|
|
/// }
|
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 {
|
2015-05-19 23:53:50 +00:00
|
|
|
(&self.$v:ident) => {
|
2015-05-19 23:40:32 +00:00
|
|
|
|
|
|
|
fn get_view(&self) -> &View {
|
|
|
|
&self.$v
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_view_mut(&mut self) -> &mut View {
|
|
|
|
&mut self.$v
|
|
|
|
}
|
|
|
|
};
|
|
|
|
(self.$v:ident) => {
|
2015-05-19 22:54:11 +00:00
|
|
|
|
|
|
|
fn get_view(&self) -> &View {
|
|
|
|
&*self.$v
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_view_mut(&mut self) -> &mut View {
|
|
|
|
&mut *self.$v
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|