mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-23 17:35:00 +00:00
Added functions to register callbacks for ListView
- Added `on_selected()` and `set_on_selected()` funtions that are called when the focus is changed - Added `focused()` to return the index of the currently focused item.
This commit is contained in:
parent
8c366389a7
commit
9894dd9715
@ -1,9 +1,11 @@
|
|||||||
|
use Cursive;
|
||||||
use Printer;
|
use Printer;
|
||||||
use With;
|
use With;
|
||||||
use direction;
|
use direction;
|
||||||
use event::{Event, EventResult, Key};
|
use event::{Callback, Event, EventResult, Key};
|
||||||
|
|
||||||
use std::any::Any;
|
use std::any::Any;
|
||||||
|
use std::rc::Rc;
|
||||||
use vec::Vec2;
|
use vec::Vec2;
|
||||||
use view::ScrollBase;
|
use view::ScrollBase;
|
||||||
use view::Selector;
|
use view::Selector;
|
||||||
@ -35,6 +37,8 @@ pub struct ListView {
|
|||||||
children: Vec<Child>,
|
children: Vec<Child>,
|
||||||
scrollbase: ScrollBase,
|
scrollbase: ScrollBase,
|
||||||
focus: usize,
|
focus: usize,
|
||||||
|
// This callback is called when the selection is changed.
|
||||||
|
on_select: Option<Rc<Fn(&mut Cursive, &String)>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
new_default!(ListView);
|
new_default!(ListView);
|
||||||
@ -46,6 +50,7 @@ impl ListView {
|
|||||||
children: Vec::new(),
|
children: Vec::new(),
|
||||||
scrollbase: ScrollBase::new(),
|
scrollbase: ScrollBase::new(),
|
||||||
focus: 0,
|
focus: 0,
|
||||||
|
on_select: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,6 +85,29 @@ impl ListView {
|
|||||||
self.with(Self::add_delimiter)
|
self.with(Self::add_delimiter)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Sets a callback to be used when an item is selected.
|
||||||
|
pub fn set_on_select<F>(&mut self, cb: F)
|
||||||
|
where F: Fn(&mut Cursive, &String) + 'static
|
||||||
|
{
|
||||||
|
self.on_select = Some(Rc::new(cb));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets a callback to be used when an item is selected.
|
||||||
|
///
|
||||||
|
/// Chainable variant.
|
||||||
|
pub fn on_select<F>(self, cb: F) -> Self
|
||||||
|
where F: Fn(&mut Cursive, &String) + 'static
|
||||||
|
{
|
||||||
|
self.with(|s| s.set_on_select(cb))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the index of the currently focused item.
|
||||||
|
///
|
||||||
|
/// Panics if the list is empty.
|
||||||
|
pub fn focus(&self) -> usize {
|
||||||
|
self.focus
|
||||||
|
}
|
||||||
|
|
||||||
fn iter_mut<'a>(&'a mut self, from_focus: bool,
|
fn iter_mut<'a>(&'a mut self, from_focus: bool,
|
||||||
source: direction::Relative)
|
source: direction::Relative)
|
||||||
-> Box<Iterator<Item = (usize, &mut Child)> + 'a> {
|
-> Box<Iterator<Item = (usize, &mut Child)> + 'a> {
|
||||||
@ -121,7 +149,11 @@ impl ListView {
|
|||||||
self.focus = i;
|
self.focus = i;
|
||||||
self.scrollbase.scroll_to(self.focus);
|
self.scrollbase.scroll_to(self.focus);
|
||||||
|
|
||||||
EventResult::Consumed(None)
|
EventResult::Consumed(self.on_select.clone().map(|cb| {
|
||||||
|
let i = self.focus();
|
||||||
|
let focused_string = String::from(self.children[i].label());
|
||||||
|
Callback::from_fn(move |s| cb(s, &focused_string))
|
||||||
|
}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
|
|
||||||
|
|
||||||
use Cursive;
|
use Cursive;
|
||||||
use Printer;
|
use Printer;
|
||||||
use With;
|
use With;
|
||||||
|
Loading…
Reference in New Issue
Block a user