From 081b7545b93ac844693b41cccf2ad3cfe18d4317 Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Sat, 23 May 2015 17:07:22 -0700 Subject: [PATCH] Remove viewpath in callbacks --- examples/dialog.rs | 2 +- examples/hello_world.rs | 2 +- examples/logs.rs | 2 +- examples/lorem.rs | 2 +- examples/mutation.rs | 6 +++--- src/event.rs | 18 ++---------------- src/lib.rs | 10 +++++----- src/view/button.rs | 6 +++--- src/view/dialog.rs | 12 ++++++------ src/view/key_event_view.rs | 4 ++-- 10 files changed, 25 insertions(+), 39 deletions(-) diff --git a/examples/dialog.rs b/examples/dialog.rs index ee9ca2d..03467c9 100644 --- a/examples/dialog.rs +++ b/examples/dialog.rs @@ -9,7 +9,7 @@ fn main() { // Creates a dialog with a single "Quit" button siv.add_layer(Dialog::new(TextView::new("Hello Dialog!")) .title("Cursive") - .button("Quit", |s,_| s.quit())); + .button("Quit", |s| s.quit())); siv.run(); } diff --git a/examples/hello_world.rs b/examples/hello_world.rs index 627ce39..26461b4 100644 --- a/examples/hello_world.rs +++ b/examples/hello_world.rs @@ -7,7 +7,7 @@ fn main() { let mut siv = Cursive::new(); // We can quit by pressing q - siv.add_global_callback('q' as i32, |s,_| s.quit()); + siv.add_global_callback('q' as i32, |s| s.quit()); siv.add_layer(TextView::new("Hello World!\nPress q to quit the application.")); diff --git a/examples/logs.rs b/examples/logs.rs index b26364f..68e7016 100644 --- a/examples/logs.rs +++ b/examples/logs.rs @@ -13,7 +13,7 @@ fn main() { // We want to refresh the page even when no input is given. siv.set_fps(10); - siv.add_global_callback('q' as i32, |s,_| s.quit()); + siv.add_global_callback('q' as i32, |s| s.quit()); // A channel will communicate data from our running task to the UI. let (tx,rx) = mpsc::channel(); diff --git a/examples/lorem.rs b/examples/lorem.rs index 878f8e8..7dfec6b 100644 --- a/examples/lorem.rs +++ b/examples/lorem.rs @@ -15,7 +15,7 @@ fn main() { file.read_to_string(&mut content).unwrap(); // We can quit by pressing q - siv.add_global_callback('q' as i32, |s,_| s.quit()); + siv.add_global_callback('q' as i32, |s| s.quit()); // The text is too long to fit on a line, so the view will wrap lines, // and will adapt to the terminal size. diff --git a/examples/mutation.rs b/examples/mutation.rs index bddeca0..cf2b6cc 100644 --- a/examples/mutation.rs +++ b/examples/mutation.rs @@ -6,7 +6,7 @@ use cursive::view::{IdView,TextView,Dialog,Selector,KeyEventView}; fn show_popup(siv: &mut Cursive) { siv.add_layer(Dialog::new(TextView::new("Tak!")) - .button("Change", |s,_| { + .button("Change", |s| { // Look for a view tagged "text". We _know_ it's there, so unwrap it. let view = s.find::(&Selector::Id("text")).unwrap(); let content: String = view.get_content().chars().rev().collect(); @@ -21,13 +21,13 @@ fn main() { let content = "Press Q to quit the application.\n\nPress P to bring back the popup."; - siv.add_global_callback('q' as i32, |s,_| s.quit()); + siv.add_global_callback('q' as i32, |s| s.quit()); // Let's wrap the view to give it a recognizable ID, so we can look for it. // We add the P callback on the textview only (and not globally), // so that we can't call it when the popup is already visible. siv.add_layer(KeyEventView::new(IdView::new("text", TextView::new(content))) - .register('p' as i32, |s,_| show_popup(s))); + .register('p' as i32, |s| show_popup(s))); show_popup(&mut siv); diff --git a/src/event.rs b/src/event.rs index 35bd49a..5157a08 100644 --- a/src/event.rs +++ b/src/event.rs @@ -3,11 +3,10 @@ use std::rc::Rc; use ::Cursive; -use view::ViewPath; /// Callback is a function that can be triggered by an event. /// It has a mutable access to the cursive root. -pub type Callback = Box; +pub type Callback = Box; /// Answer to an event notification. /// The event can be consumed or ignored. @@ -15,18 +14,5 @@ pub enum EventResult { /// The event was ignored. The parent can keep handling it. Ignored, /// The event was consumed. An optionnal callback to run is attached. - Consumed(Option>, ViewPath), -} - -impl EventResult { - /// Convenient method to create EventResult::Consumed - /// from the given callback and empty ViewPath. - pub fn callback(cb: Rc) -> Self { - EventResult::Consumed(Some(cb), ViewPath::new()) - } - - /// Convenient method to create EventResult::Consumed with no callback. - pub fn consume() -> Self { - EventResult::Consumed(None, ViewPath::new()) - } + Consumed(Option>), } diff --git a/src/lib.rs b/src/lib.rs index 5706a5f..c52912b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -41,7 +41,7 @@ use std::collections::HashMap; use vec::Vec2; use view::View; use printer::Printer; -use view::{StackView,ViewPath,Selector}; +use view::{StackView,Selector}; use event::{EventResult,Callback}; @@ -149,7 +149,7 @@ impl Cursive { /// Adds a global callback, triggered on the given key press when no view catches it. pub fn add_global_callback(&mut self, key: i32, cb: F) - where F: Fn(&mut Cursive, &ViewPath) + 'static + where F: Fn(&mut Cursive) + 'static { self.global_callbacks.insert(key, Rc::new(Box::new(cb))); } @@ -166,7 +166,7 @@ impl Cursive { Some(cb) => cb.clone(), }; // Not from a view, so no viewpath here - cb(self, &ViewPath::new()); + cb(self); } /// Returns the size of the screen, in characters. @@ -218,8 +218,8 @@ impl Cursive { // If the event was ignored, it is our turn to play with it. match self.screen_mut().on_key_event(ch) { EventResult::Ignored => self.on_key_event(ch), - EventResult::Consumed(None, _) => (), - EventResult::Consumed(Some(cb), path) => cb(self, &path), + EventResult::Consumed(None) => (), + EventResult::Consumed(Some(cb)) => cb(self), } } } diff --git a/src/view/button.rs b/src/view/button.rs index ed3df73..c715748 100644 --- a/src/view/button.rs +++ b/src/view/button.rs @@ -3,7 +3,7 @@ use std::rc::Rc; use color; use ::Cursive; use vec::Vec2; -use view::{View,ViewPath,SizeRequest}; +use view::{View,SizeRequest}; use event::{Callback,EventResult}; use printer::Printer; @@ -17,7 +17,7 @@ pub struct Button { impl Button { /// Creates a new button with the given content and callback. pub fn new(label: &str, cb: F) -> Self - where F: Fn(&mut Cursive, &ViewPath) + 'static + where F: Fn(&mut Cursive) + 'static { Button { label: label.to_string(), @@ -47,7 +47,7 @@ impl View for Button { fn on_key_event(&mut self, ch: i32) -> EventResult { match ch { // 10 is the ascii code for '\n', that is the return key - 10 => EventResult::callback(self.callback.clone()), + 10 => EventResult::Consumed(Some(self.callback.clone())), _ => EventResult::Ignored, } } diff --git a/src/view/dialog.rs b/src/view/dialog.rs index e4254b1..adea002 100644 --- a/src/view/dialog.rs +++ b/src/view/dialog.rs @@ -52,7 +52,7 @@ impl Dialog { /// /// Consumes and returns self for easy chaining. pub fn button<'a, F>(mut self, label: &'a str, cb: F) -> Self - where F: Fn(&mut Cursive, &ViewPath) + 'static + where F: Fn(&mut Cursive) + 'static { self.buttons.push(SizedView::new(Button::new(label, cb))); @@ -61,7 +61,7 @@ impl Dialog { /// Shortcut method to add a button that will dismiss the dialog. pub fn dismiss_button<'a>(self, label: &'a str) -> Self { - self.button(label, |s, _| s.screen_mut().pop_layer()) + self.button(label, |s| s.screen_mut().pop_layer()) } /// Sets the title of the dialog. @@ -165,7 +165,7 @@ impl View for Dialog { ncurses::KEY_DOWN => { // Default to leftmost button when going down. self.focus = Focus::Button(0); - EventResult::Consumed(None, ViewPath::new()) + EventResult::Consumed(None) }, _ => EventResult::Ignored, }, @@ -178,7 +178,7 @@ impl View for Dialog { ncurses::KEY_UP => { if self.content.take_focus() { self.focus = Focus::Content; - EventResult::consume() + EventResult::Consumed(None) } else { EventResult::Ignored } @@ -186,11 +186,11 @@ impl View for Dialog { // Left and Right move to other buttons ncurses::KEY_RIGHT if i+1 < self.buttons.len() => { self.focus = Focus::Button(i+1); - EventResult::consume() + EventResult::Consumed(None) }, ncurses::KEY_LEFT if i > 0 => { self.focus = Focus::Button(i-1); - EventResult::consume() + EventResult::Consumed(None) }, _ => EventResult::Ignored, }, diff --git a/src/view/key_event_view.rs b/src/view/key_event_view.rs index e3d73a4..d82d878 100644 --- a/src/view/key_event_view.rs +++ b/src/view/key_event_view.rs @@ -24,7 +24,7 @@ impl KeyEventView { /// Registers a callback when the given key is ignored by the child. pub fn register(mut self, key: i32, cb: F) -> Self - where F: Fn(&mut Cursive, &ViewPath) + 'static + where F: Fn(&mut Cursive) + 'static { self.callbacks.insert(key, Rc::new(Box::new(cb))); @@ -40,7 +40,7 @@ impl ViewWrapper for KeyEventView { match self.content.on_key_event(ch) { EventResult::Ignored => match self.callbacks.get(&ch) { None => EventResult::Ignored, - Some(cb) => EventResult::Consumed(Some(cb.clone()), ViewPath::new()), + Some(cb) => EventResult::Consumed(Some(cb.clone())), }, res => res, }