Remove viewpath in callbacks

This commit is contained in:
Alexandre Bury 2015-05-23 17:07:22 -07:00
parent 5a4c34361c
commit 081b7545b9
10 changed files with 25 additions and 39 deletions

View File

@ -9,7 +9,7 @@ fn main() {
// Creates a dialog with a single "Quit" button // Creates a dialog with a single "Quit" button
siv.add_layer(Dialog::new(TextView::new("Hello Dialog!")) siv.add_layer(Dialog::new(TextView::new("Hello Dialog!"))
.title("Cursive") .title("Cursive")
.button("Quit", |s,_| s.quit())); .button("Quit", |s| s.quit()));
siv.run(); siv.run();
} }

View File

@ -7,7 +7,7 @@ fn main() {
let mut siv = Cursive::new(); let mut siv = Cursive::new();
// We can quit by pressing q // 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.")); siv.add_layer(TextView::new("Hello World!\nPress q to quit the application."));

View File

@ -13,7 +13,7 @@ fn main() {
// We want to refresh the page even when no input is given. // We want to refresh the page even when no input is given.
siv.set_fps(10); 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. // A channel will communicate data from our running task to the UI.
let (tx,rx) = mpsc::channel(); let (tx,rx) = mpsc::channel();

View File

@ -15,7 +15,7 @@ fn main() {
file.read_to_string(&mut content).unwrap(); file.read_to_string(&mut content).unwrap();
// We can quit by pressing q // 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, // The text is too long to fit on a line, so the view will wrap lines,
// and will adapt to the terminal size. // and will adapt to the terminal size.

View File

@ -6,7 +6,7 @@ use cursive::view::{IdView,TextView,Dialog,Selector,KeyEventView};
fn show_popup(siv: &mut Cursive) { fn show_popup(siv: &mut Cursive) {
siv.add_layer(Dialog::new(TextView::new("Tak!")) 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. // Look for a view tagged "text". We _know_ it's there, so unwrap it.
let view = s.find::<TextView>(&Selector::Id("text")).unwrap(); let view = s.find::<TextView>(&Selector::Id("text")).unwrap();
let content: String = view.get_content().chars().rev().collect(); 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."; 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. // 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), // 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. // so that we can't call it when the popup is already visible.
siv.add_layer(KeyEventView::new(IdView::new("text", TextView::new(content))) 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); show_popup(&mut siv);

View File

@ -3,11 +3,10 @@
use std::rc::Rc; use std::rc::Rc;
use ::Cursive; use ::Cursive;
use view::ViewPath;
/// Callback is a function that can be triggered by an event. /// Callback is a function that can be triggered by an event.
/// It has a mutable access to the cursive root. /// It has a mutable access to the cursive root.
pub type Callback = Box<Fn(&mut Cursive, &ViewPath)>; pub type Callback = Box<Fn(&mut Cursive)>;
/// Answer to an event notification. /// Answer to an event notification.
/// The event can be consumed or ignored. /// The event can be consumed or ignored.
@ -15,18 +14,5 @@ pub enum EventResult {
/// The event was ignored. The parent can keep handling it. /// The event was ignored. The parent can keep handling it.
Ignored, Ignored,
/// The event was consumed. An optionnal callback to run is attached. /// The event was consumed. An optionnal callback to run is attached.
Consumed(Option<Rc<Callback>>, ViewPath), Consumed(Option<Rc<Callback>>),
}
impl EventResult {
/// Convenient method to create EventResult::Consumed
/// from the given callback and empty ViewPath.
pub fn callback(cb: Rc<Callback>) -> 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())
}
} }

View File

@ -41,7 +41,7 @@ use std::collections::HashMap;
use vec::Vec2; use vec::Vec2;
use view::View; use view::View;
use printer::Printer; use printer::Printer;
use view::{StackView,ViewPath,Selector}; use view::{StackView,Selector};
use event::{EventResult,Callback}; 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. /// Adds a global callback, triggered on the given key press when no view catches it.
pub fn add_global_callback<F>(&mut self, key: i32, cb: F) pub fn add_global_callback<F>(&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))); self.global_callbacks.insert(key, Rc::new(Box::new(cb)));
} }
@ -166,7 +166,7 @@ impl Cursive {
Some(cb) => cb.clone(), Some(cb) => cb.clone(),
}; };
// Not from a view, so no viewpath here // Not from a view, so no viewpath here
cb(self, &ViewPath::new()); cb(self);
} }
/// Returns the size of the screen, in characters. /// 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. // If the event was ignored, it is our turn to play with it.
match self.screen_mut().on_key_event(ch) { match self.screen_mut().on_key_event(ch) {
EventResult::Ignored => self.on_key_event(ch), EventResult::Ignored => self.on_key_event(ch),
EventResult::Consumed(None, _) => (), EventResult::Consumed(None) => (),
EventResult::Consumed(Some(cb), path) => cb(self, &path), EventResult::Consumed(Some(cb)) => cb(self),
} }
} }
} }

View File

@ -3,7 +3,7 @@ use std::rc::Rc;
use color; use color;
use ::Cursive; use ::Cursive;
use vec::Vec2; use vec::Vec2;
use view::{View,ViewPath,SizeRequest}; use view::{View,SizeRequest};
use event::{Callback,EventResult}; use event::{Callback,EventResult};
use printer::Printer; use printer::Printer;
@ -17,7 +17,7 @@ pub struct Button {
impl Button { impl Button {
/// Creates a new button with the given content and callback. /// Creates a new button with the given content and callback.
pub fn new<F>(label: &str, cb: F) -> Self pub fn new<F>(label: &str, cb: F) -> Self
where F: Fn(&mut Cursive, &ViewPath) + 'static where F: Fn(&mut Cursive) + 'static
{ {
Button { Button {
label: label.to_string(), label: label.to_string(),
@ -47,7 +47,7 @@ impl View for Button {
fn on_key_event(&mut self, ch: i32) -> EventResult { fn on_key_event(&mut self, ch: i32) -> EventResult {
match ch { match ch {
// 10 is the ascii code for '\n', that is the return key // 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, _ => EventResult::Ignored,
} }
} }

View File

@ -52,7 +52,7 @@ impl Dialog {
/// ///
/// Consumes and returns self for easy chaining. /// Consumes and returns self for easy chaining.
pub fn button<'a, F>(mut self, label: &'a str, cb: F) -> Self 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))); 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. /// Shortcut method to add a button that will dismiss the dialog.
pub fn dismiss_button<'a>(self, label: &'a str) -> Self { 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. /// Sets the title of the dialog.
@ -165,7 +165,7 @@ impl View for Dialog {
ncurses::KEY_DOWN => { ncurses::KEY_DOWN => {
// Default to leftmost button when going down. // Default to leftmost button when going down.
self.focus = Focus::Button(0); self.focus = Focus::Button(0);
EventResult::Consumed(None, ViewPath::new()) EventResult::Consumed(None)
}, },
_ => EventResult::Ignored, _ => EventResult::Ignored,
}, },
@ -178,7 +178,7 @@ impl View for Dialog {
ncurses::KEY_UP => { ncurses::KEY_UP => {
if self.content.take_focus() { if self.content.take_focus() {
self.focus = Focus::Content; self.focus = Focus::Content;
EventResult::consume() EventResult::Consumed(None)
} else { } else {
EventResult::Ignored EventResult::Ignored
} }
@ -186,11 +186,11 @@ impl View for Dialog {
// Left and Right move to other buttons // Left and Right move to other buttons
ncurses::KEY_RIGHT if i+1 < self.buttons.len() => { ncurses::KEY_RIGHT if i+1 < self.buttons.len() => {
self.focus = Focus::Button(i+1); self.focus = Focus::Button(i+1);
EventResult::consume() EventResult::Consumed(None)
}, },
ncurses::KEY_LEFT if i > 0 => { ncurses::KEY_LEFT if i > 0 => {
self.focus = Focus::Button(i-1); self.focus = Focus::Button(i-1);
EventResult::consume() EventResult::Consumed(None)
}, },
_ => EventResult::Ignored, _ => EventResult::Ignored,
}, },

View File

@ -24,7 +24,7 @@ impl KeyEventView {
/// Registers a callback when the given key is ignored by the child. /// Registers a callback when the given key is ignored by the child.
pub fn register<F>(mut self, key: i32, cb: F) -> Self pub fn register<F>(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))); self.callbacks.insert(key, Rc::new(Box::new(cb)));
@ -40,7 +40,7 @@ impl ViewWrapper for KeyEventView {
match self.content.on_key_event(ch) { match self.content.on_key_event(ch) {
EventResult::Ignored => match self.callbacks.get(&ch) { EventResult::Ignored => match self.callbacks.get(&ch) {
None => EventResult::Ignored, None => EventResult::Ignored,
Some(cb) => EventResult::Consumed(Some(cb.clone()), ViewPath::new()), Some(cb) => EventResult::Consumed(Some(cb.clone())),
}, },
res => res, res => res,
} }