2017-01-24 06:01:25 +00:00
|
|
|
use Printer;
|
|
|
|
use With;
|
|
|
|
use direction::Direction;
|
2018-04-17 05:39:16 +00:00
|
|
|
use event::{AnyCb, Event, EventResult};
|
|
|
|
use rect::Rect;
|
2017-01-24 06:01:25 +00:00
|
|
|
use vec::Vec2;
|
2018-04-17 05:39:16 +00:00
|
|
|
use view::{Selector, View};
|
2017-01-24 06:01:25 +00:00
|
|
|
|
|
|
|
/// A blank view that forwards calls to closures.
|
|
|
|
///
|
|
|
|
/// You can use this view to easily draw your own interface.
|
|
|
|
pub struct Canvas<T> {
|
|
|
|
state: T,
|
|
|
|
|
2017-09-23 20:31:17 +00:00
|
|
|
draw: Box<Fn(&T, &Printer)>,
|
|
|
|
on_event: Box<FnMut(&mut T, Event) -> EventResult>,
|
|
|
|
required_size: Box<FnMut(&mut T, Vec2) -> Vec2>,
|
|
|
|
layout: Box<FnMut(&mut T, Vec2)>,
|
|
|
|
take_focus: Box<FnMut(&mut T, Direction) -> bool>,
|
2017-01-24 18:48:00 +00:00
|
|
|
needs_relayout: Box<Fn(&T) -> bool>,
|
2018-04-17 05:39:16 +00:00
|
|
|
focus_view: Box<FnMut(&mut T, &Selector) -> Result<(), ()>>,
|
|
|
|
call_on_any: Box<for<'a> FnMut(&mut T, &Selector, AnyCb<'a>)>,
|
|
|
|
important_area: Box<Fn(&T, Vec2) -> Rect>,
|
2017-01-24 06:01:25 +00:00
|
|
|
}
|
|
|
|
|
2017-10-12 23:38:55 +00:00
|
|
|
impl<T: 'static + View> Canvas<T> {
|
2017-09-23 20:31:17 +00:00
|
|
|
/// Creates a new Canvas around the given view.
|
|
|
|
///
|
|
|
|
/// By default, forwards all calls to the inner view.
|
|
|
|
pub fn wrap(view: T) -> Self {
|
|
|
|
Canvas::new(view)
|
|
|
|
.with_draw(T::draw)
|
|
|
|
.with_on_event(T::on_event)
|
|
|
|
.with_required_size(T::required_size)
|
|
|
|
.with_layout(T::layout)
|
|
|
|
.with_take_focus(T::take_focus)
|
|
|
|
.with_needs_relayout(T::needs_relayout)
|
2018-04-17 05:39:16 +00:00
|
|
|
.with_focus_view(T::focus_view)
|
|
|
|
.with_call_on_any(T::call_on_any)
|
|
|
|
.with_important_area(T::important_area)
|
2017-09-23 20:31:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-24 06:01:25 +00:00
|
|
|
impl<T> Canvas<T> {
|
|
|
|
/// Creates a new, empty Canvas.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
2017-01-24 06:03:39 +00:00
|
|
|
/// # use cursive::views::Canvas;
|
2017-01-24 06:01:25 +00:00
|
|
|
/// let canvas = Canvas::new(())
|
|
|
|
/// .with_draw(|printer, _| {
|
|
|
|
/// // Print the view
|
|
|
|
/// });
|
|
|
|
/// ```
|
|
|
|
pub fn new(state: T) -> Self {
|
|
|
|
Canvas {
|
2018-04-10 18:53:25 +00:00
|
|
|
state,
|
2017-01-24 06:01:25 +00:00
|
|
|
draw: Box::new(|_, _| ()),
|
|
|
|
on_event: Box::new(|_, _| EventResult::Ignored),
|
2017-01-24 06:52:29 +00:00
|
|
|
required_size: Box::new(|_, _| Vec2::new(1, 1)),
|
2017-01-24 06:01:25 +00:00
|
|
|
layout: Box::new(|_, _| ()),
|
|
|
|
take_focus: Box::new(|_, _| false),
|
2017-01-24 18:48:00 +00:00
|
|
|
needs_relayout: Box::new(|_| true),
|
2018-04-17 05:39:16 +00:00
|
|
|
focus_view: Box::new(|_, _| Err(())),
|
|
|
|
call_on_any: Box::new(|_, _, _| ()),
|
|
|
|
important_area: Box::new(|_, size| {
|
|
|
|
Rect::from_corners((0, 0), size)
|
|
|
|
}),
|
2017-01-24 06:01:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-24 18:48:00 +00:00
|
|
|
/// Gets a mutable reference to the inner state.
|
|
|
|
pub fn state_mut(&mut self) -> &mut T {
|
|
|
|
&mut self.state
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the closure for `draw(&Printer)`.
|
2017-01-24 06:01:25 +00:00
|
|
|
pub fn set_draw<F>(&mut self, f: F)
|
2017-10-12 23:38:55 +00:00
|
|
|
where
|
|
|
|
F: 'static + Fn(&T, &Printer),
|
2017-01-24 06:01:25 +00:00
|
|
|
{
|
|
|
|
self.draw = Box::new(f);
|
|
|
|
}
|
|
|
|
|
2017-01-24 18:48:00 +00:00
|
|
|
/// Sets the closure for `draw(&Printer)`.
|
2017-01-24 06:01:25 +00:00
|
|
|
///
|
|
|
|
/// Chainable variant.
|
|
|
|
pub fn with_draw<F>(self, f: F) -> Self
|
2017-10-12 23:38:55 +00:00
|
|
|
where
|
|
|
|
F: 'static + Fn(&T, &Printer),
|
2017-01-24 06:01:25 +00:00
|
|
|
{
|
|
|
|
self.with(|s| s.set_draw(f))
|
|
|
|
}
|
|
|
|
|
2017-01-24 18:48:00 +00:00
|
|
|
/// Sets the closure for `on_event(Event)`.
|
2017-01-24 06:01:25 +00:00
|
|
|
pub fn set_on_event<F>(&mut self, f: F)
|
2017-10-12 23:38:55 +00:00
|
|
|
where
|
|
|
|
F: 'static + FnMut(&mut T, Event) -> EventResult,
|
2017-01-24 06:01:25 +00:00
|
|
|
{
|
|
|
|
self.on_event = Box::new(f);
|
|
|
|
}
|
|
|
|
|
2017-01-24 18:48:00 +00:00
|
|
|
/// Sets the closure for `on_event(Event)`.
|
2017-01-24 06:01:25 +00:00
|
|
|
///
|
|
|
|
/// Chainable variant.
|
|
|
|
pub fn with_on_event<F>(self, f: F) -> Self
|
2017-10-12 23:38:55 +00:00
|
|
|
where
|
|
|
|
F: 'static + FnMut(&mut T, Event) -> EventResult,
|
2017-01-24 06:01:25 +00:00
|
|
|
{
|
|
|
|
self.with(|s| s.set_on_event(f))
|
|
|
|
}
|
|
|
|
|
2017-01-24 18:48:00 +00:00
|
|
|
/// Sets the closure for `required_size(Vec2)`.
|
2017-01-24 06:52:29 +00:00
|
|
|
pub fn set_required_size<F>(&mut self, f: F)
|
2017-10-12 23:38:55 +00:00
|
|
|
where
|
|
|
|
F: 'static + FnMut(&mut T, Vec2) -> Vec2,
|
2017-01-24 06:01:25 +00:00
|
|
|
{
|
2017-01-24 06:52:29 +00:00
|
|
|
self.required_size = Box::new(f);
|
2017-01-24 06:01:25 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 18:48:00 +00:00
|
|
|
/// Sets the closure for `required_size(Vec2)`.
|
2017-01-24 06:01:25 +00:00
|
|
|
///
|
|
|
|
/// Chainable variant.
|
2017-01-24 06:52:29 +00:00
|
|
|
pub fn with_required_size<F>(self, f: F) -> Self
|
2017-10-12 23:38:55 +00:00
|
|
|
where
|
|
|
|
F: 'static + FnMut(&mut T, Vec2) -> Vec2,
|
2017-01-24 06:01:25 +00:00
|
|
|
{
|
2017-01-24 06:52:29 +00:00
|
|
|
self.with(|s| s.set_required_size(f))
|
2017-01-24 06:01:25 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 18:48:00 +00:00
|
|
|
/// Sets the closure for `layout(Vec2)`.
|
2017-01-24 06:01:25 +00:00
|
|
|
pub fn set_layout<F>(&mut self, f: F)
|
2017-10-12 23:38:55 +00:00
|
|
|
where
|
|
|
|
F: 'static + FnMut(&mut T, Vec2),
|
2017-01-24 06:01:25 +00:00
|
|
|
{
|
|
|
|
self.layout = Box::new(f);
|
|
|
|
}
|
|
|
|
|
2017-01-24 18:48:00 +00:00
|
|
|
/// Sets the closure for `layout(Vec2)`.
|
2017-01-24 06:01:25 +00:00
|
|
|
///
|
|
|
|
/// Chainable variant.
|
|
|
|
pub fn with_layout<F>(self, f: F) -> Self
|
2017-10-12 23:38:55 +00:00
|
|
|
where
|
|
|
|
F: 'static + FnMut(&mut T, Vec2),
|
2017-01-24 06:01:25 +00:00
|
|
|
{
|
|
|
|
self.with(|s| s.set_layout(f))
|
|
|
|
}
|
|
|
|
|
2017-01-24 18:48:00 +00:00
|
|
|
/// Sets the closure for `take_focus(Direction)`.
|
2017-01-24 06:01:25 +00:00
|
|
|
pub fn set_take_focus<F>(&mut self, f: F)
|
2017-10-12 23:38:55 +00:00
|
|
|
where
|
|
|
|
F: 'static + FnMut(&mut T, Direction) -> bool,
|
2017-01-24 06:01:25 +00:00
|
|
|
{
|
|
|
|
self.take_focus = Box::new(f);
|
|
|
|
}
|
|
|
|
|
2017-01-24 18:48:00 +00:00
|
|
|
/// Sets the closure for `take_focus(Direction)`.
|
2017-01-24 06:01:25 +00:00
|
|
|
///
|
|
|
|
/// Chainable variant.
|
|
|
|
pub fn with_take_focus<F>(self, f: F) -> Self
|
2017-10-12 23:38:55 +00:00
|
|
|
where
|
|
|
|
F: 'static + FnMut(&mut T, Direction) -> bool,
|
2017-01-24 06:01:25 +00:00
|
|
|
{
|
|
|
|
self.with(|s| s.set_take_focus(f))
|
|
|
|
}
|
2017-01-24 18:48:00 +00:00
|
|
|
|
|
|
|
/// Sets the closure for `needs_relayout()`.
|
|
|
|
pub fn set_needs_relayout<F>(&mut self, f: F)
|
2017-10-12 23:38:55 +00:00
|
|
|
where
|
|
|
|
F: 'static + Fn(&T) -> bool,
|
2017-01-24 18:48:00 +00:00
|
|
|
{
|
|
|
|
self.needs_relayout = Box::new(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the closure for `needs_relayout()`.
|
|
|
|
///
|
|
|
|
/// Chainable variant.
|
|
|
|
pub fn with_needs_relayout<F>(self, f: F) -> Self
|
2017-10-12 23:38:55 +00:00
|
|
|
where
|
|
|
|
F: 'static + Fn(&T) -> bool,
|
2017-01-24 18:48:00 +00:00
|
|
|
{
|
|
|
|
self.with(|s| s.set_needs_relayout(f))
|
|
|
|
}
|
2018-04-17 05:39:16 +00:00
|
|
|
|
|
|
|
/// Sets the closure for `call_on_any()`.
|
|
|
|
pub fn set_call_on_any<F>(&mut self, f: F)
|
|
|
|
where
|
|
|
|
F: 'static + for<'a> FnMut(&mut T, &Selector, AnyCb<'a>),
|
|
|
|
{
|
|
|
|
self.call_on_any = Box::new(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the closure for `call_on_any()`.
|
|
|
|
///
|
|
|
|
/// Chainable variant.
|
|
|
|
pub fn with_call_on_any<F>(self, f: F) -> Self
|
|
|
|
where
|
|
|
|
F: 'static + for<'a> FnMut(&mut T, &Selector, AnyCb<'a>),
|
|
|
|
{
|
|
|
|
self.with(|s| s.set_call_on_any(f))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the closure for `important_area()`.
|
|
|
|
pub fn set_important_area<F>(&mut self, f: F)
|
|
|
|
where
|
|
|
|
F: 'static + Fn(&T, Vec2) -> Rect,
|
|
|
|
{
|
|
|
|
self.important_area = Box::new(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the closure for `important_area()`.
|
|
|
|
///
|
|
|
|
/// Chainable variant.
|
|
|
|
pub fn with_important_area<F>(self, f: F) -> Self
|
|
|
|
where
|
|
|
|
F: 'static + Fn(&T, Vec2) -> Rect,
|
|
|
|
{
|
|
|
|
self.with(|s| s.set_important_area(f))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the closure for `focus_view()`.
|
|
|
|
pub fn set_focus_view<F>(&mut self, f: F)
|
|
|
|
where
|
|
|
|
F: 'static + FnMut(&mut T, &Selector) -> Result<(), ()>,
|
|
|
|
{
|
|
|
|
self.focus_view = Box::new(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the closure for `focus_view()`.
|
|
|
|
///
|
|
|
|
/// Chainable variant.
|
|
|
|
pub fn with_focus_view<F>(self, f: F) -> Self
|
|
|
|
where
|
|
|
|
F: 'static + FnMut(&mut T, &Selector) -> Result<(), ()>,
|
|
|
|
{
|
|
|
|
self.with(|s| s.set_focus_view(f))
|
|
|
|
}
|
2017-01-24 06:01:25 +00:00
|
|
|
}
|
|
|
|
|
2017-12-12 02:54:40 +00:00
|
|
|
impl<T: 'static> View for Canvas<T> {
|
2017-01-24 06:01:25 +00:00
|
|
|
fn draw(&self, printer: &Printer) {
|
2017-09-23 20:31:17 +00:00
|
|
|
(self.draw)(&self.state, printer);
|
2017-01-24 06:01:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn on_event(&mut self, event: Event) -> EventResult {
|
2017-09-23 20:31:17 +00:00
|
|
|
(self.on_event)(&mut self.state, event)
|
2017-01-24 06:01:25 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 06:52:29 +00:00
|
|
|
fn required_size(&mut self, constraint: Vec2) -> Vec2 {
|
2017-09-23 20:31:17 +00:00
|
|
|
(self.required_size)(&mut self.state, constraint)
|
2017-01-24 06:01:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn layout(&mut self, size: Vec2) {
|
2017-09-23 20:31:17 +00:00
|
|
|
(self.layout)(&mut self.state, size);
|
2017-01-24 06:01:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn take_focus(&mut self, source: Direction) -> bool {
|
2017-09-23 20:31:17 +00:00
|
|
|
(self.take_focus)(&mut self.state, source)
|
2017-01-24 06:01:25 +00:00
|
|
|
}
|
2018-04-17 05:39:16 +00:00
|
|
|
|
|
|
|
fn needs_relayout(&self) -> bool {
|
|
|
|
(self.needs_relayout)(&self.state)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn focus_view(&mut self, selector: &Selector) -> Result<(), ()> {
|
|
|
|
(self.focus_view)(&mut self.state, selector)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn important_area(&self, view_size: Vec2) -> Rect {
|
|
|
|
(self.important_area)(&self.state, view_size)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn call_on_any<'a>(&mut self, selector: &Selector, cb: AnyCb<'a>) {
|
|
|
|
(self.call_on_any)(&mut self.state, selector, cb);
|
|
|
|
}
|
2017-01-24 06:01:25 +00:00
|
|
|
}
|