From afc681bcf455d31f0d0782df71fb497d6fa7fa78 Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Tue, 24 Jan 2017 10:48:00 -0800 Subject: [PATCH] Add `Canvas::state_mut(&mut self)` And missing Canvas::needs_relayout closure. --- src/views/canvas.rs | 46 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/src/views/canvas.rs b/src/views/canvas.rs index b24e2f8..fab5346 100644 --- a/src/views/canvas.rs +++ b/src/views/canvas.rs @@ -16,6 +16,7 @@ pub struct Canvas { required_size: Box Vec2>, layout: Box, take_focus: Box bool>, + needs_relayout: Box bool>, } impl Canvas { @@ -38,17 +39,23 @@ impl Canvas { required_size: Box::new(|_, _| Vec2::new(1, 1)), layout: Box::new(|_, _| ()), take_focus: Box::new(|_, _| false), + needs_relayout: Box::new(|_| true), } } - /// Sets the closure for `draw(&Printer)` + /// 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)`. pub fn set_draw(&mut self, f: F) where F: 'static + Fn(&Printer, &T) { self.draw = Box::new(f); } - /// Sets the closure for `draw(&Printer)` + /// Sets the closure for `draw(&Printer)`. /// /// Chainable variant. pub fn with_draw(self, f: F) -> Self @@ -57,14 +64,14 @@ impl Canvas { 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(&mut self, f: F) where F: 'static + FnMut(Event, &mut T) -> EventResult { self.on_event = Box::new(f); } - /// Sets the closure for `on_event(Event)` + /// Sets the closure for `on_event(Event)`. /// /// Chainable variant. pub fn with_on_event(self, f: F) -> Self @@ -73,14 +80,14 @@ impl Canvas { 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(&mut self, f: F) where F: 'static + FnMut(Vec2, &mut T) -> Vec2 { self.required_size = Box::new(f); } - /// Sets the closure for `required_size(Vec2)` + /// Sets the closure for `required_size(Vec2)`. /// /// Chainable variant. pub fn with_required_size(self, f: F) -> Self @@ -89,14 +96,14 @@ impl Canvas { self.with(|s| s.set_required_size(f)) } - /// Sets the closure for `layout(Vec2)` + /// Sets the closure for `layout(Vec2)`. pub fn set_layout(&mut self, f: F) where F: 'static + FnMut(Vec2, &mut T) { self.layout = Box::new(f); } - /// Sets the closure for `layout(Vec2)` + /// Sets the closure for `layout(Vec2)`. /// /// Chainable variant. pub fn with_layout(self, f: F) -> Self @@ -105,14 +112,14 @@ impl Canvas { 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(&mut self, f: F) where F: 'static + FnMut(Direction, &mut T) -> bool { self.take_focus = Box::new(f); } - /// Sets the closure for `take_focus(Direction)` + /// Sets the closure for `take_focus(Direction)`. /// /// Chainable variant. pub fn with_take_focus(self, f: F) -> Self @@ -120,9 +127,26 @@ impl Canvas { { self.with(|s| s.set_take_focus(f)) } + + /// Sets the closure for `needs_relayout()`. + pub fn set_needs_relayout(&mut self, f: F) + where F: 'static + Fn(&T) -> bool + { + self.needs_relayout = Box::new(f); + } + + + /// Sets the closure for `needs_relayout()`. + /// + /// Chainable variant. + pub fn with_needs_relayout(self, f: F) -> Self + where F: 'static + Fn(&T) -> bool + { + self.with(|s| s.set_needs_relayout(f)) + } } -impl View for Canvas { +impl View for Canvas { fn draw(&self, printer: &Printer) { (self.draw)(printer, &self.state); }