mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-23 17:35:00 +00:00
Add Canvas::wrap(View)
Behaves like a dynamic ViewWrapper
This commit is contained in:
parent
daab316210
commit
6fa5b18cc6
@ -11,14 +11,29 @@ use view::View;
|
|||||||
pub struct Canvas<T> {
|
pub struct Canvas<T> {
|
||||||
state: T,
|
state: T,
|
||||||
|
|
||||||
draw: Box<Fn(&Printer, &T)>,
|
draw: Box<Fn(&T, &Printer)>,
|
||||||
on_event: Box<FnMut(Event, &mut T) -> EventResult>,
|
on_event: Box<FnMut(&mut T, Event) -> EventResult>,
|
||||||
required_size: Box<FnMut(Vec2, &mut T) -> Vec2>,
|
required_size: Box<FnMut(&mut T, Vec2) -> Vec2>,
|
||||||
layout: Box<FnMut(Vec2, &mut T)>,
|
layout: Box<FnMut(&mut T, Vec2)>,
|
||||||
take_focus: Box<FnMut(Direction, &mut T) -> bool>,
|
take_focus: Box<FnMut(&mut T, Direction) -> bool>,
|
||||||
needs_relayout: Box<Fn(&T) -> bool>,
|
needs_relayout: Box<Fn(&T) -> bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl <T: 'static + View> Canvas<T> {
|
||||||
|
/// 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T> Canvas<T> {
|
impl<T> Canvas<T> {
|
||||||
/// Creates a new, empty Canvas.
|
/// Creates a new, empty Canvas.
|
||||||
///
|
///
|
||||||
@ -50,7 +65,7 @@ impl<T> Canvas<T> {
|
|||||||
|
|
||||||
/// Sets the closure for `draw(&Printer)`.
|
/// Sets the closure for `draw(&Printer)`.
|
||||||
pub fn set_draw<F>(&mut self, f: F)
|
pub fn set_draw<F>(&mut self, f: F)
|
||||||
where F: 'static + Fn(&Printer, &T)
|
where F: 'static + Fn(&T, &Printer)
|
||||||
{
|
{
|
||||||
self.draw = Box::new(f);
|
self.draw = Box::new(f);
|
||||||
}
|
}
|
||||||
@ -59,14 +74,14 @@ impl<T> Canvas<T> {
|
|||||||
///
|
///
|
||||||
/// Chainable variant.
|
/// Chainable variant.
|
||||||
pub fn with_draw<F>(self, f: F) -> Self
|
pub fn with_draw<F>(self, f: F) -> Self
|
||||||
where F: 'static + Fn(&Printer, &T)
|
where F: 'static + Fn(&T, &Printer)
|
||||||
{
|
{
|
||||||
self.with(|s| s.set_draw(f))
|
self.with(|s| s.set_draw(f))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the closure for `on_event(Event)`.
|
/// Sets the closure for `on_event(Event)`.
|
||||||
pub fn set_on_event<F>(&mut self, f: F)
|
pub fn set_on_event<F>(&mut self, f: F)
|
||||||
where F: 'static + FnMut(Event, &mut T) -> EventResult
|
where F: 'static + FnMut(&mut T, Event) -> EventResult
|
||||||
{
|
{
|
||||||
self.on_event = Box::new(f);
|
self.on_event = Box::new(f);
|
||||||
}
|
}
|
||||||
@ -75,14 +90,14 @@ impl<T> Canvas<T> {
|
|||||||
///
|
///
|
||||||
/// Chainable variant.
|
/// Chainable variant.
|
||||||
pub fn with_on_event<F>(self, f: F) -> Self
|
pub fn with_on_event<F>(self, f: F) -> Self
|
||||||
where F: 'static + FnMut(Event, &mut T) -> EventResult
|
where F: 'static + FnMut(&mut T, Event) -> EventResult
|
||||||
{
|
{
|
||||||
self.with(|s| s.set_on_event(f))
|
self.with(|s| s.set_on_event(f))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the closure for `required_size(Vec2)`.
|
/// Sets the closure for `required_size(Vec2)`.
|
||||||
pub fn set_required_size<F>(&mut self, f: F)
|
pub fn set_required_size<F>(&mut self, f: F)
|
||||||
where F: 'static + FnMut(Vec2, &mut T) -> Vec2
|
where F: 'static + FnMut(&mut T, Vec2) -> Vec2
|
||||||
{
|
{
|
||||||
self.required_size = Box::new(f);
|
self.required_size = Box::new(f);
|
||||||
}
|
}
|
||||||
@ -91,14 +106,14 @@ impl<T> Canvas<T> {
|
|||||||
///
|
///
|
||||||
/// Chainable variant.
|
/// Chainable variant.
|
||||||
pub fn with_required_size<F>(self, f: F) -> Self
|
pub fn with_required_size<F>(self, f: F) -> Self
|
||||||
where F: 'static + FnMut(Vec2, &mut T) -> Vec2
|
where F: 'static + FnMut(&mut T, Vec2) -> Vec2
|
||||||
{
|
{
|
||||||
self.with(|s| s.set_required_size(f))
|
self.with(|s| s.set_required_size(f))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the closure for `layout(Vec2)`.
|
/// Sets the closure for `layout(Vec2)`.
|
||||||
pub fn set_layout<F>(&mut self, f: F)
|
pub fn set_layout<F>(&mut self, f: F)
|
||||||
where F: 'static + FnMut(Vec2, &mut T)
|
where F: 'static + FnMut(&mut T, Vec2)
|
||||||
{
|
{
|
||||||
self.layout = Box::new(f);
|
self.layout = Box::new(f);
|
||||||
}
|
}
|
||||||
@ -107,14 +122,14 @@ impl<T> Canvas<T> {
|
|||||||
///
|
///
|
||||||
/// Chainable variant.
|
/// Chainable variant.
|
||||||
pub fn with_layout<F>(self, f: F) -> Self
|
pub fn with_layout<F>(self, f: F) -> Self
|
||||||
where F: 'static + FnMut(Vec2, &mut T)
|
where F: 'static + FnMut(&mut T, Vec2)
|
||||||
{
|
{
|
||||||
self.with(|s| s.set_layout(f))
|
self.with(|s| s.set_layout(f))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the closure for `take_focus(Direction)`.
|
/// Sets the closure for `take_focus(Direction)`.
|
||||||
pub fn set_take_focus<F>(&mut self, f: F)
|
pub fn set_take_focus<F>(&mut self, f: F)
|
||||||
where F: 'static + FnMut(Direction, &mut T) -> bool
|
where F: 'static + FnMut(&mut T, Direction) -> bool
|
||||||
{
|
{
|
||||||
self.take_focus = Box::new(f);
|
self.take_focus = Box::new(f);
|
||||||
}
|
}
|
||||||
@ -123,7 +138,7 @@ impl<T> Canvas<T> {
|
|||||||
///
|
///
|
||||||
/// Chainable variant.
|
/// Chainable variant.
|
||||||
pub fn with_take_focus<F>(self, f: F) -> Self
|
pub fn with_take_focus<F>(self, f: F) -> Self
|
||||||
where F: 'static + FnMut(Direction, &mut T) -> bool
|
where F: 'static + FnMut(&mut T, Direction) -> bool
|
||||||
{
|
{
|
||||||
self.with(|s| s.set_take_focus(f))
|
self.with(|s| s.set_take_focus(f))
|
||||||
}
|
}
|
||||||
@ -148,22 +163,22 @@ impl<T> Canvas<T> {
|
|||||||
|
|
||||||
impl<T> View for Canvas<T> {
|
impl<T> View for Canvas<T> {
|
||||||
fn draw(&self, printer: &Printer) {
|
fn draw(&self, printer: &Printer) {
|
||||||
(self.draw)(printer, &self.state);
|
(self.draw)(&self.state, printer);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_event(&mut self, event: Event) -> EventResult {
|
fn on_event(&mut self, event: Event) -> EventResult {
|
||||||
(self.on_event)(event, &mut self.state)
|
(self.on_event)(&mut self.state, event)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn required_size(&mut self, constraint: Vec2) -> Vec2 {
|
fn required_size(&mut self, constraint: Vec2) -> Vec2 {
|
||||||
(self.required_size)(constraint, &mut self.state)
|
(self.required_size)(&mut self.state, constraint)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn layout(&mut self, size: Vec2) {
|
fn layout(&mut self, size: Vec2) {
|
||||||
(self.layout)(size, &mut self.state);
|
(self.layout)(&mut self.state, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn take_focus(&mut self, source: Direction) -> bool {
|
fn take_focus(&mut self, source: Direction) -> bool {
|
||||||
(self.take_focus)(source, &mut self.state)
|
(self.take_focus)(&mut self.state, source)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user