2015-05-31 23:38:53 +00:00
|
|
|
use std::cmp::min;
|
2015-06-01 03:47:04 +00:00
|
|
|
use std::rc::Rc;
|
2016-03-15 22:37:57 +00:00
|
|
|
use ncurses::chtype;
|
2015-05-31 23:38:53 +00:00
|
|
|
|
2015-06-06 01:08:05 +00:00
|
|
|
use theme::ColorPair;
|
2016-03-15 22:37:57 +00:00
|
|
|
use Cursive;
|
2015-06-02 21:23:51 +00:00
|
|
|
use align::*;
|
2016-03-15 22:37:57 +00:00
|
|
|
use view::{View, IdView, SizeRequest, DimensionRequest};
|
|
|
|
use event::{Event, EventResult, Key};
|
2015-05-31 04:05:34 +00:00
|
|
|
use vec::Vec2;
|
|
|
|
use printer::Printer;
|
2015-05-31 23:38:53 +00:00
|
|
|
use super::scroll::ScrollBase;
|
2015-05-31 04:05:34 +00:00
|
|
|
|
|
|
|
struct Item<T> {
|
|
|
|
label: String,
|
2015-06-01 03:47:04 +00:00
|
|
|
value: Rc<T>,
|
2015-05-31 04:05:34 +00:00
|
|
|
}
|
|
|
|
|
2016-06-25 23:36:22 +00:00
|
|
|
impl<T> Item<T> {
|
2015-05-31 04:05:34 +00:00
|
|
|
fn new(label: &str, value: T) -> Self {
|
|
|
|
Item {
|
|
|
|
label: label.to_string(),
|
2015-06-01 03:47:04 +00:00
|
|
|
value: Rc::new(value),
|
2015-05-31 04:05:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-31 04:32:24 +00:00
|
|
|
/// View to select an item among a list.
|
|
|
|
///
|
|
|
|
/// It contains a list of values of type T, with associated labels.
|
2016-03-15 22:37:57 +00:00
|
|
|
pub struct SelectView<T = String> {
|
2015-05-31 04:05:34 +00:00
|
|
|
items: Vec<Item<T>>,
|
|
|
|
focus: usize,
|
2015-05-31 23:38:53 +00:00
|
|
|
scrollbase: ScrollBase,
|
2016-03-15 22:37:57 +00:00
|
|
|
select_cb: Option<Rc<Box<Fn(&mut Cursive, &T)>>>,
|
2015-06-02 21:23:51 +00:00
|
|
|
align: Align,
|
2015-05-31 04:05:34 +00:00
|
|
|
}
|
|
|
|
|
2016-06-25 23:36:22 +00:00
|
|
|
impl<T: 'static> SelectView<T> {
|
2015-05-31 04:32:24 +00:00
|
|
|
/// Creates a new empty SelectView.
|
2015-05-31 04:05:34 +00:00
|
|
|
pub fn new() -> Self {
|
2015-05-31 04:32:24 +00:00
|
|
|
SelectView {
|
2015-05-31 04:05:34 +00:00
|
|
|
items: Vec::new(),
|
|
|
|
focus: 0,
|
2015-05-31 23:38:53 +00:00
|
|
|
scrollbase: ScrollBase::new(),
|
2015-06-01 03:47:04 +00:00
|
|
|
select_cb: None,
|
2015-06-02 21:23:51 +00:00
|
|
|
align: Align::top_left(),
|
2015-05-31 04:05:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-28 19:54:32 +00:00
|
|
|
pub fn set_on_select<F>(&mut self, cb: F)
|
|
|
|
where F: Fn(&mut Cursive, &T) + 'static
|
|
|
|
{
|
|
|
|
self.select_cb = Some(Rc::new(Box::new(cb)));
|
|
|
|
}
|
|
|
|
|
2015-06-01 03:47:04 +00:00
|
|
|
/// Sets a function to be called when an item is selected (when ENTER is pressed).
|
|
|
|
pub fn on_select<F>(mut self, cb: F) -> Self
|
2015-07-28 19:54:32 +00:00
|
|
|
where F: Fn(&mut Cursive, &T) + 'static
|
2015-06-01 03:47:04 +00:00
|
|
|
{
|
2015-07-28 19:54:32 +00:00
|
|
|
self.set_on_select(cb);
|
2015-06-01 03:47:04 +00:00
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2015-06-03 22:36:51 +00:00
|
|
|
/// Sets the alignment for this view.
|
2015-06-02 21:23:51 +00:00
|
|
|
pub fn align(mut self, align: Align) -> Self {
|
|
|
|
self.align = align;
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2015-06-03 22:36:51 +00:00
|
|
|
/// Sets the vertical alignment for this view.
|
2016-06-26 16:45:53 +00:00
|
|
|
/// (If the view is given too much space vertically.)
|
2015-06-02 21:23:51 +00:00
|
|
|
pub fn v_align(mut self, v: VAlign) -> Self {
|
|
|
|
self.align.v = v;
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2015-06-03 22:36:51 +00:00
|
|
|
/// Sets the horizontal alignment for this view.
|
2015-06-02 21:23:51 +00:00
|
|
|
pub fn h_align(mut self, h: HAlign) -> Self {
|
|
|
|
self.align.h = h;
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2015-05-31 04:32:24 +00:00
|
|
|
/// Returns the value of the currently selected item. Panics if the list is empty.
|
2015-06-01 03:47:04 +00:00
|
|
|
pub fn selection(&self) -> Rc<T> {
|
|
|
|
self.items[self.focus].value.clone()
|
2015-05-31 04:05:34 +00:00
|
|
|
}
|
|
|
|
|
2015-05-31 04:32:24 +00:00
|
|
|
/// Adds a item to the list, with given label and value.
|
2015-05-31 23:38:53 +00:00
|
|
|
pub fn add_item(&mut self, label: &str, value: T) {
|
2016-03-15 22:37:57 +00:00
|
|
|
self.items.push(Item::new(label, value));
|
2015-05-31 23:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Chainable variant of add_item
|
|
|
|
pub fn item(mut self, label: &str, value: T) -> Self {
|
|
|
|
self.add_item(label, value);
|
2015-05-31 04:05:34 +00:00
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2015-05-31 04:32:24 +00:00
|
|
|
/// Wraps this view into an IdView with the given id.
|
2015-05-31 04:05:34 +00:00
|
|
|
pub fn with_id(self, label: &str) -> IdView<Self> {
|
|
|
|
IdView::new(label, self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-31 04:32:24 +00:00
|
|
|
impl SelectView<String> {
|
|
|
|
/// For String-based SelectView, this is a convenient method to use the label as value.
|
2015-05-31 23:58:55 +00:00
|
|
|
pub fn add_item_str(&mut self, label: &str) {
|
|
|
|
self.add_item(label, label.to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Chainable variant of add_item_str
|
2015-05-31 04:05:34 +00:00
|
|
|
pub fn item_str(self, label: &str) -> Self {
|
|
|
|
self.item(label, label.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-25 23:36:22 +00:00
|
|
|
impl<T: 'static> View for SelectView<T> {
|
2015-05-31 04:05:34 +00:00
|
|
|
fn draw(&mut self, printer: &Printer) {
|
2015-05-31 23:38:53 +00:00
|
|
|
|
2015-06-02 21:23:51 +00:00
|
|
|
let h = self.items.len();
|
|
|
|
let offset = self.align.v.get_offset(h, printer.size.y);
|
2016-03-15 22:37:57 +00:00
|
|
|
let printer = &printer.sub_printer(Vec2::new(0, offset), printer.size, true);
|
2015-06-02 21:23:51 +00:00
|
|
|
|
2016-03-15 22:37:57 +00:00
|
|
|
self.scrollbase.draw(printer, |printer, i| {
|
2015-05-31 23:38:53 +00:00
|
|
|
let style = if i == self.focus {
|
2016-03-15 22:37:57 +00:00
|
|
|
if printer.focused {
|
|
|
|
ColorPair::Highlight
|
|
|
|
} else {
|
|
|
|
ColorPair::HighlightInactive
|
|
|
|
}
|
2015-05-31 23:38:53 +00:00
|
|
|
} else {
|
2015-06-06 01:08:05 +00:00
|
|
|
ColorPair::Primary
|
2015-05-31 23:38:53 +00:00
|
|
|
};
|
2015-06-01 22:48:31 +00:00
|
|
|
printer.with_color(style, |printer| {
|
2015-06-02 21:23:51 +00:00
|
|
|
let l = self.items[i].label.chars().count();
|
|
|
|
let x = self.align.h.get_offset(l, printer.size.x);
|
2016-03-15 22:37:57 +00:00
|
|
|
printer.print_hline((0, 0), x, ' ' as chtype);
|
|
|
|
printer.print((x, 0), &self.items[i].label);
|
|
|
|
printer.print_hline((x + l, 0), printer.size.x - l - x, ' ' as chtype);
|
2015-06-01 22:48:31 +00:00
|
|
|
});
|
2015-05-31 23:38:53 +00:00
|
|
|
});
|
2015-05-31 04:05:34 +00:00
|
|
|
}
|
|
|
|
|
2015-05-31 23:38:53 +00:00
|
|
|
fn get_min_size(&self, req: SizeRequest) -> Vec2 {
|
2015-05-31 04:05:34 +00:00
|
|
|
let w = self.items.iter().map(|item| item.label.len()).max().unwrap_or(1);
|
|
|
|
let h = self.items.len();
|
2015-05-31 23:38:53 +00:00
|
|
|
|
|
|
|
let scrolling = if let DimensionRequest::Fixed(r_h) = req.h {
|
|
|
|
r_h < h
|
|
|
|
} else if let DimensionRequest::AtMost(r_h) = req.h {
|
|
|
|
r_h < h
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
|
|
|
|
2015-06-01 22:48:31 +00:00
|
|
|
// Add 2 spaces for the scrollbar if we need
|
2016-03-15 22:37:57 +00:00
|
|
|
let w = if scrolling {
|
|
|
|
w + 2
|
|
|
|
} else {
|
|
|
|
w
|
|
|
|
};
|
2015-05-31 23:38:53 +00:00
|
|
|
|
2016-03-15 22:37:57 +00:00
|
|
|
Vec2::new(w, h)
|
2015-05-31 04:05:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn on_event(&mut self, event: Event) -> EventResult {
|
|
|
|
match event {
|
2015-05-31 23:38:53 +00:00
|
|
|
Event::KeyEvent(Key::Up) if self.focus > 0 => self.focus -= 1,
|
|
|
|
Event::KeyEvent(Key::Down) if self.focus + 1 < self.items.len() => self.focus += 1,
|
2016-03-15 22:37:57 +00:00
|
|
|
Event::KeyEvent(Key::PageUp) => self.focus -= min(self.focus, 10),
|
2016-06-25 23:36:22 +00:00
|
|
|
Event::KeyEvent(Key::PageDown) => {
|
|
|
|
self.focus = min(self.focus + 10, self.items.len() - 1)
|
|
|
|
}
|
2015-05-31 23:38:53 +00:00
|
|
|
Event::KeyEvent(Key::Home) => self.focus = 0,
|
2016-03-15 22:37:57 +00:00
|
|
|
Event::KeyEvent(Key::End) => self.focus = self.items.len() - 1,
|
2015-06-01 03:47:04 +00:00
|
|
|
Event::KeyEvent(Key::Enter) if self.select_cb.is_some() => {
|
|
|
|
if let Some(ref cb) = self.select_cb {
|
|
|
|
let cb = cb.clone();
|
|
|
|
let v = self.selection();
|
|
|
|
return EventResult::Consumed(Some(Rc::new(Box::new(move |s| cb(s, &*v)))));
|
|
|
|
}
|
2016-03-15 22:37:57 +00:00
|
|
|
}
|
2015-05-31 23:38:53 +00:00
|
|
|
Event::CharEvent(c) => {
|
|
|
|
// Starting from the current focus, find the first item that match the char.
|
2015-06-01 02:09:30 +00:00
|
|
|
// Cycle back to the beginning of the list when we reach the end.
|
|
|
|
// This is achieved by chaining twice the iterator
|
|
|
|
let iter = self.items.iter().chain(self.items.iter());
|
2016-03-15 22:37:57 +00:00
|
|
|
if let Some((i, _)) = iter.enumerate()
|
|
|
|
.skip(self.focus + 1)
|
|
|
|
.find(|&(_, item)| item.label.starts_with(c)) {
|
2015-06-01 02:09:30 +00:00
|
|
|
// Apply modulo in case we have a hit from the chained iterator
|
|
|
|
self.focus = i % self.items.len();
|
2015-05-31 23:38:53 +00:00
|
|
|
}
|
2016-03-15 22:37:57 +00:00
|
|
|
}
|
2015-05-31 04:05:34 +00:00
|
|
|
_ => return EventResult::Ignored,
|
|
|
|
}
|
|
|
|
|
2015-05-31 23:38:53 +00:00
|
|
|
self.scrollbase.scroll_to(self.focus);
|
|
|
|
|
2015-05-31 04:05:34 +00:00
|
|
|
EventResult::Consumed(None)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn take_focus(&mut self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
2015-05-31 23:38:53 +00:00
|
|
|
|
|
|
|
fn layout(&mut self, size: Vec2) {
|
|
|
|
self.scrollbase.set_heights(size.y, self.items.len());
|
|
|
|
}
|
2015-05-31 04:05:34 +00:00
|
|
|
}
|