mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-23 17:35:00 +00:00
Remove viewpath in callbacks
This commit is contained in:
parent
5a4c34361c
commit
081b7545b9
@ -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();
|
||||
}
|
||||
|
@ -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."));
|
||||
|
||||
|
@ -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();
|
||||
|
@ -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.
|
||||
|
@ -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::<TextView>(&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);
|
||||
|
||||
|
18
src/event.rs
18
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<Fn(&mut Cursive, &ViewPath)>;
|
||||
pub type Callback = Box<Fn(&mut Cursive)>;
|
||||
|
||||
/// 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<Rc<Callback>>, ViewPath),
|
||||
}
|
||||
|
||||
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())
|
||||
}
|
||||
Consumed(Option<Rc<Callback>>),
|
||||
}
|
||||
|
10
src/lib.rs
10
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<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)));
|
||||
}
|
||||
@ -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),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<F>(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,
|
||||
}
|
||||
}
|
||||
|
@ -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,
|
||||
},
|
||||
|
@ -24,7 +24,7 @@ impl KeyEventView {
|
||||
|
||||
/// Registers a callback when the given key is ignored by the child.
|
||||
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)));
|
||||
|
||||
@ -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,
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user