2016-07-02 08:01:09 +00:00
|
|
|
use align::{Align, HAlign, VAlign};
|
2016-10-02 22:22:29 +00:00
|
|
|
use direction::Direction;
|
2017-10-12 21:32:48 +00:00
|
|
|
use event::{Callback, Event, EventResult, Key, MouseButton, MouseEvent};
|
2016-10-02 22:22:29 +00:00
|
|
|
use menu::MenuTree;
|
2018-03-22 21:31:28 +00:00
|
|
|
use rect::Rect;
|
2016-10-02 22:22:29 +00:00
|
|
|
use std::borrow::Borrow;
|
|
|
|
use std::cell::Cell;
|
|
|
|
use std::cmp::min;
|
|
|
|
use std::rc::Rc;
|
2016-07-17 01:46:18 +00:00
|
|
|
use theme::ColorStyle;
|
2016-07-04 23:04:32 +00:00
|
|
|
use unicode_width::UnicodeWidthStr;
|
2016-10-02 22:22:29 +00:00
|
|
|
use vec::Vec2;
|
2018-07-25 02:38:24 +00:00
|
|
|
use view::{Position, View};
|
2016-10-02 22:22:29 +00:00
|
|
|
use views::MenuPopup;
|
2018-05-18 00:37:39 +00:00
|
|
|
use Cursive;
|
|
|
|
use Printer;
|
|
|
|
use With;
|
2016-07-04 23:04:32 +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-07-21 05:08:06 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2018-04-01 23:39:03 +00:00
|
|
|
/// ```rust
|
2016-07-21 05:08:06 +00:00
|
|
|
/// # extern crate cursive;
|
2016-09-29 05:45:27 +00:00
|
|
|
/// # use cursive::Cursive;
|
|
|
|
/// # use cursive::views::{SelectView, Dialog, TextView};
|
2016-07-21 05:08:06 +00:00
|
|
|
/// # use cursive::align::HAlign;
|
|
|
|
/// # fn main() {
|
|
|
|
/// let mut time_select = SelectView::new().h_align(HAlign::Center);
|
|
|
|
/// time_select.add_item("Short", 1);
|
|
|
|
/// time_select.add_item("Medium", 5);
|
|
|
|
/// time_select.add_item("Long", 10);
|
|
|
|
///
|
2016-07-30 08:27:34 +00:00
|
|
|
/// time_select.set_on_submit(|s, time| {
|
2016-07-21 05:08:06 +00:00
|
|
|
/// s.pop_layer();
|
|
|
|
/// let text = format!("You will wait for {} minutes...", time);
|
2016-10-02 22:15:30 +00:00
|
|
|
/// s.add_layer(Dialog::around(TextView::new(text))
|
2016-07-21 05:08:06 +00:00
|
|
|
/// .button("Quit", |s| s.quit()));
|
|
|
|
/// });
|
|
|
|
///
|
2018-04-01 23:39:03 +00:00
|
|
|
/// let mut siv = Cursive::dummy();
|
2016-10-02 22:15:30 +00:00
|
|
|
/// siv.add_layer(Dialog::around(time_select)
|
2016-09-29 05:45:27 +00:00
|
|
|
/// .title("How long is your wait?"));
|
2016-07-21 05:08:06 +00:00
|
|
|
/// # }
|
|
|
|
///
|
|
|
|
/// ```
|
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>>,
|
2016-07-17 01:46:18 +00:00
|
|
|
enabled: bool,
|
2016-07-20 06:47:27 +00:00
|
|
|
// the focus needs to be manipulable from callbacks
|
|
|
|
focus: Rc<Cell<usize>>,
|
2016-07-30 08:27:34 +00:00
|
|
|
// This is a custom callback to include a &T.
|
|
|
|
// It will be called whenever "Enter" is pressed.
|
|
|
|
on_submit: Option<Rc<Fn(&mut Cursive, &T)>>,
|
|
|
|
// This callback is called when the selection is changed.
|
|
|
|
on_select: Option<Rc<Fn(&mut Cursive, &T)>>,
|
2015-06-02 21:23:51 +00:00
|
|
|
align: Align,
|
2016-07-20 06:47:27 +00:00
|
|
|
// `true` if we show a one-line view, with popup on selection.
|
|
|
|
popup: bool,
|
2016-07-20 07:30:00 +00:00
|
|
|
// We need the last offset to place the popup window
|
|
|
|
// We "cache" it during the draw, so we need interior mutability.
|
|
|
|
last_offset: Cell<Vec2>,
|
|
|
|
last_size: Vec2,
|
2015-05-31 04:05:34 +00:00
|
|
|
}
|
|
|
|
|
2017-08-14 23:32:01 +00:00
|
|
|
impl<T: 'static> Default for SelectView<T> {
|
2017-07-17 23:43:50 +00:00
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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(),
|
2016-07-17 01:46:18 +00:00
|
|
|
enabled: true,
|
2016-07-20 06:47:27 +00:00
|
|
|
focus: Rc::new(Cell::new(0)),
|
2016-07-30 08:27:34 +00:00
|
|
|
on_select: None,
|
|
|
|
on_submit: None,
|
2015-06-02 21:23:51 +00:00
|
|
|
align: Align::top_left(),
|
2016-07-20 06:47:27 +00:00
|
|
|
popup: false,
|
2016-07-20 07:30:00 +00:00
|
|
|
last_offset: Cell::new(Vec2::zero()),
|
|
|
|
last_size: Vec2::zero(),
|
2015-05-31 04:05:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-20 06:47:27 +00:00
|
|
|
/// Turns `self` into a popup select view.
|
|
|
|
///
|
|
|
|
/// Chainable variant.
|
|
|
|
pub fn popup(self) -> Self {
|
|
|
|
self.with(|s| s.set_popup(true))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Turns `self` into a popup select view.
|
|
|
|
pub fn set_popup(&mut self, popup: bool) {
|
|
|
|
self.popup = popup;
|
|
|
|
}
|
|
|
|
|
2016-07-17 01:46:18 +00:00
|
|
|
/// Disables this view.
|
|
|
|
///
|
|
|
|
/// A disabled view cannot be selected.
|
|
|
|
pub fn disable(&mut self) {
|
|
|
|
self.enabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Disables this view.
|
|
|
|
///
|
|
|
|
/// Chainable variant.
|
|
|
|
pub fn disabled(self) -> Self {
|
|
|
|
self.with(Self::disable)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Re-enables this view.
|
|
|
|
pub fn enable(&mut self) {
|
|
|
|
self.enabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Enable or disable this view.
|
|
|
|
pub fn set_enabled(&mut self, enabled: bool) {
|
|
|
|
self.enabled = enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` if this view is enabled.
|
|
|
|
pub fn is_enabled(&self) -> bool {
|
|
|
|
self.enabled
|
|
|
|
}
|
|
|
|
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Sets a callback to be used when an item is selected.
|
2015-07-28 19:54:32 +00:00
|
|
|
pub fn set_on_select<F>(&mut self, cb: F)
|
2017-08-23 23:48:54 +00:00
|
|
|
where
|
|
|
|
F: Fn(&mut Cursive, &T) + 'static,
|
2015-07-28 19:54:32 +00:00
|
|
|
{
|
2016-07-30 08:27:34 +00:00
|
|
|
self.on_select = Some(Rc::new(cb));
|
2015-07-28 19:54:32 +00:00
|
|
|
}
|
|
|
|
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Sets a callback to be used when an item is selected.
|
|
|
|
///
|
|
|
|
/// Chainable variant.
|
2016-07-30 08:27:34 +00:00
|
|
|
pub fn on_select<F>(self, cb: F) -> Self
|
2017-08-23 23:48:54 +00:00
|
|
|
where
|
|
|
|
F: Fn(&mut Cursive, &T) + 'static,
|
2015-06-01 03:47:04 +00:00
|
|
|
{
|
2016-07-30 08:27:34 +00:00
|
|
|
self.with(|s| s.set_on_select(cb))
|
|
|
|
}
|
2015-06-01 03:47:04 +00:00
|
|
|
|
2016-07-30 08:27:34 +00:00
|
|
|
/// Sets a callback to be used when `<Enter>` is pressed.
|
|
|
|
///
|
|
|
|
/// The item currently selected will be given to the callback.
|
2016-09-28 22:06:16 +00:00
|
|
|
///
|
|
|
|
/// Here, `V` can be `T` itself, or a type that can be borrowed from `T`.
|
2017-12-15 07:45:14 +00:00
|
|
|
pub fn set_on_submit<F, R, V: ?Sized>(&mut self, cb: F)
|
2017-08-23 23:48:54 +00:00
|
|
|
where
|
2017-12-15 07:45:14 +00:00
|
|
|
F: 'static + Fn(&mut Cursive, &V) -> R,
|
2017-08-23 23:48:54 +00:00
|
|
|
T: Borrow<V>,
|
2016-07-30 08:27:34 +00:00
|
|
|
{
|
2017-12-15 07:45:14 +00:00
|
|
|
self.on_submit = Some(Rc::new(move |s, t| {
|
|
|
|
cb(s, t.borrow());
|
|
|
|
}));
|
2016-07-30 08:27:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets a callback to be used when `<Enter>` is pressed.
|
|
|
|
///
|
|
|
|
/// The item currently selected will be given to the callback.
|
|
|
|
///
|
|
|
|
/// Chainable variant.
|
2016-09-28 22:06:16 +00:00
|
|
|
pub fn on_submit<F, V: ?Sized>(self, cb: F) -> Self
|
2017-08-23 23:48:54 +00:00
|
|
|
where
|
|
|
|
F: Fn(&mut Cursive, &V) + 'static,
|
|
|
|
T: Borrow<V>,
|
2016-07-30 08:27:34 +00:00
|
|
|
{
|
|
|
|
self.with(|s| s.set_on_submit(cb))
|
2015-06-01 03:47:04 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-06-28 05:10:59 +00:00
|
|
|
/// Returns the value of the currently selected item.
|
|
|
|
///
|
2018-07-23 03:26:17 +00:00
|
|
|
/// Returns `None` if the list is empty.
|
2018-07-23 03:25:13 +00:00
|
|
|
pub fn selection(&self) -> Option<Rc<T>> {
|
|
|
|
let focus = self.focus();
|
|
|
|
if self.len() <= focus {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(Rc::clone(&self.items[focus].value))
|
|
|
|
}
|
2015-05-31 04:05:34 +00:00
|
|
|
}
|
|
|
|
|
2016-09-05 22:33:52 +00:00
|
|
|
/// Removes all items from this view.
|
|
|
|
pub fn clear(&mut self) {
|
|
|
|
self.items.clear();
|
2017-03-27 20:07:22 +00:00
|
|
|
self.focus.set(0);
|
2016-09-05 22:33:52 +00:00
|
|
|
}
|
|
|
|
|
2015-05-31 04:32:24 +00:00
|
|
|
/// Adds a item to the list, with given label and value.
|
2016-07-30 08:52:34 +00:00
|
|
|
pub fn add_item<S: Into<String>>(&mut self, label: S, value: T) {
|
|
|
|
self.items.push(Item::new(label.into(), value));
|
2015-05-31 23:38:53 +00:00
|
|
|
}
|
|
|
|
|
2018-01-26 12:25:19 +00:00
|
|
|
/// Gets an item at given idx or None.
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use cursive::Cursive;
|
|
|
|
/// use cursive::views::{SelectView, TextView};
|
|
|
|
/// let select = SelectView::new()
|
|
|
|
/// .item("Short", 1);
|
|
|
|
/// assert_eq!(select.get_item(0), Some(("Short", &1)));
|
|
|
|
/// ```
|
|
|
|
pub fn get_item(&self, i: usize) -> Option<(&str, &T)> {
|
2018-01-26 16:26:21 +00:00
|
|
|
self.items
|
|
|
|
.get(i)
|
|
|
|
.map(|item| (item.label.as_ref(), &*item.value))
|
2018-01-26 12:25:19 +00:00
|
|
|
}
|
|
|
|
|
2018-01-26 16:24:44 +00:00
|
|
|
/// Gets a mut item at given idx or None.
|
|
|
|
pub fn get_item_mut(&mut self, i: usize) -> Option<(&mut String, &mut T)> {
|
|
|
|
if i >= self.items.len() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
let item = &mut self.items[i];
|
|
|
|
if let Some(t) = Rc::get_mut(&mut item.value) {
|
|
|
|
let label = &mut item.label;
|
|
|
|
Some((label, t))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-01 00:00:48 +00:00
|
|
|
/// Removes an item from the list.
|
2018-03-15 18:42:49 +00:00
|
|
|
///
|
|
|
|
/// Returns a callback in response to the selection change.
|
|
|
|
///
|
2018-03-15 18:50:22 +00:00
|
|
|
/// You should run this callback with a `&mut Cursive`.
|
2018-03-15 18:42:49 +00:00
|
|
|
pub fn remove_item(&mut self, id: usize) -> Callback {
|
2016-09-01 00:00:48 +00:00
|
|
|
self.items.remove(id);
|
|
|
|
let focus = self.focus();
|
2016-09-01 18:28:30 +00:00
|
|
|
if focus >= id && focus > 0 {
|
2016-09-01 00:00:48 +00:00
|
|
|
self.focus.set(focus - 1);
|
|
|
|
}
|
2018-03-15 18:42:49 +00:00
|
|
|
|
2018-05-18 00:37:39 +00:00
|
|
|
self.make_select_cb().unwrap_or_else(Callback::dummy)
|
2016-09-01 00:00:48 +00:00
|
|
|
}
|
|
|
|
|
2018-03-25 07:30:48 +00:00
|
|
|
/// Inserts an item at position `index`, shifting all elements after it to
|
|
|
|
/// the right.
|
|
|
|
pub fn insert_item<S>(&mut self, index: usize, label: S, value: T)
|
|
|
|
where
|
|
|
|
S: Into<String>,
|
|
|
|
{
|
|
|
|
self.items.insert(index, Item::new(label.into(), value));
|
|
|
|
}
|
|
|
|
|
2015-05-31 23:38:53 +00:00
|
|
|
/// Chainable variant of add_item
|
2016-07-30 08:52:34 +00:00
|
|
|
pub fn item<S: Into<String>>(self, label: S, value: T) -> Self {
|
|
|
|
self.with(|s| s.add_item(label, value))
|
|
|
|
}
|
2015-05-31 04:05:34 +00:00
|
|
|
|
2016-07-30 08:52:34 +00:00
|
|
|
/// Adds all items from from an iterator.
|
|
|
|
pub fn add_all<S, I>(&mut self, iter: I)
|
2017-08-23 23:48:54 +00:00
|
|
|
where
|
|
|
|
S: Into<String>,
|
|
|
|
I: IntoIterator<Item = (S, T)>,
|
2016-07-30 08:52:34 +00:00
|
|
|
{
|
|
|
|
for (s, t) in iter {
|
|
|
|
self.add_item(s, t);
|
|
|
|
}
|
2015-05-31 04:05:34 +00:00
|
|
|
}
|
|
|
|
|
2016-07-30 08:52:34 +00:00
|
|
|
/// Adds all items from from an iterator.
|
|
|
|
///
|
|
|
|
/// Chainable variant.
|
|
|
|
pub fn with_all<S, I>(self, iter: I) -> Self
|
2017-08-23 23:48:54 +00:00
|
|
|
where
|
|
|
|
S: Into<String>,
|
|
|
|
I: IntoIterator<Item = (S, T)>,
|
2016-07-30 08:52:34 +00:00
|
|
|
{
|
|
|
|
self.with(|s| s.add_all(iter))
|
2015-05-31 04:05:34 +00:00
|
|
|
}
|
2016-07-17 01:46:18 +00:00
|
|
|
|
|
|
|
fn draw_item(&self, printer: &Printer, i: usize) {
|
|
|
|
let l = self.items[i].label.width();
|
|
|
|
let x = self.align.h.get_offset(l, printer.size.x);
|
|
|
|
printer.print_hline((0, 0), x, " ");
|
|
|
|
printer.print((x, 0), &self.items[i].label);
|
2016-07-17 08:20:41 +00:00
|
|
|
if l < printer.size.x {
|
2017-08-14 23:32:01 +00:00
|
|
|
assert!((l + x) <= printer.size.x);
|
|
|
|
printer.print_hline((x + l, 0), printer.size.x - (l + x), " ");
|
2016-07-17 08:20:41 +00:00
|
|
|
}
|
2016-07-17 01:46:18 +00:00
|
|
|
}
|
2016-07-20 06:47:27 +00:00
|
|
|
|
2016-09-01 00:00:48 +00:00
|
|
|
/// Returns the id of the item currently selected.
|
2016-09-01 00:05:58 +00:00
|
|
|
///
|
|
|
|
/// Returns `None` if the list is empty.
|
|
|
|
pub fn selected_id(&self) -> Option<usize> {
|
|
|
|
if self.items.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(self.focus())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the number of items in this list.
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.items.len()
|
|
|
|
}
|
|
|
|
|
2016-09-21 18:57:38 +00:00
|
|
|
/// Returns `true` if this list has no item.
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.items.is_empty()
|
|
|
|
}
|
|
|
|
|
2016-09-01 00:05:58 +00:00
|
|
|
fn focus(&self) -> usize {
|
2016-07-20 06:47:27 +00:00
|
|
|
self.focus.get()
|
|
|
|
}
|
|
|
|
|
2017-03-27 20:14:03 +00:00
|
|
|
/// Moves the selection to the given position.
|
2018-03-15 18:42:49 +00:00
|
|
|
///
|
|
|
|
/// Returns a callback in response to the selection change.
|
|
|
|
///
|
2018-03-15 18:50:22 +00:00
|
|
|
/// You should run this callback with a `&mut Cursive`.
|
2018-03-15 18:42:49 +00:00
|
|
|
pub fn set_selection(&mut self, i: usize) -> Callback {
|
2018-03-15 16:36:15 +00:00
|
|
|
// TODO: Check if `i >= self.len()` ?
|
|
|
|
// assert!(i < self.len(), "SelectView: trying to select out-of-bound");
|
|
|
|
// Or just cap the ID?
|
2018-04-10 18:53:25 +00:00
|
|
|
let i = if self.is_empty() {
|
2018-03-15 16:36:15 +00:00
|
|
|
0
|
|
|
|
} else {
|
|
|
|
min(i, self.len() - 1)
|
|
|
|
};
|
2017-03-27 20:14:03 +00:00
|
|
|
self.focus.set(i);
|
2018-03-15 18:42:49 +00:00
|
|
|
|
2018-05-18 00:37:39 +00:00
|
|
|
self.make_select_cb().unwrap_or_else(Callback::dummy)
|
2017-03-27 20:14:03 +00:00
|
|
|
}
|
|
|
|
|
2018-02-12 03:26:44 +00:00
|
|
|
/// Sets the selection to the given position.
|
|
|
|
///
|
|
|
|
/// Chainable variant.
|
2018-03-15 18:42:49 +00:00
|
|
|
///
|
|
|
|
/// Does not apply `on_select` callbacks.
|
2018-02-12 03:26:44 +00:00
|
|
|
pub fn selected(self, i: usize) -> Self {
|
2018-03-15 18:42:49 +00:00
|
|
|
self.with(|s| {
|
|
|
|
s.set_selection(i);
|
|
|
|
})
|
2018-02-12 03:26:44 +00:00
|
|
|
}
|
|
|
|
|
2017-08-23 23:48:54 +00:00
|
|
|
/// Moves the selection up by the given number of rows.
|
2018-03-15 18:42:49 +00:00
|
|
|
///
|
|
|
|
/// Returns a callback in response to the selection change.
|
|
|
|
///
|
|
|
|
/// You should run this callback with a `&mut Cursive`:
|
|
|
|
///
|
|
|
|
/// ```rust
|
2018-03-15 18:57:12 +00:00
|
|
|
/// # use cursive::Cursive;
|
|
|
|
/// # use cursive::views::SelectView;
|
2018-03-15 18:42:49 +00:00
|
|
|
/// # fn main() {}
|
|
|
|
/// fn select_up(siv: &mut Cursive, view: &mut SelectView<()>) {
|
|
|
|
/// let cb = view.select_up(1);
|
|
|
|
/// cb(siv);
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub fn select_up(&mut self, n: usize) -> Callback {
|
2017-08-23 23:48:54 +00:00
|
|
|
self.focus_up(n);
|
2018-05-18 00:37:39 +00:00
|
|
|
self.make_select_cb().unwrap_or_else(Callback::dummy)
|
2017-08-23 23:48:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Moves the selection down by the given number of rows.
|
2018-03-15 18:42:49 +00:00
|
|
|
///
|
|
|
|
/// Returns a callback in response to the selection change.
|
|
|
|
///
|
|
|
|
/// You should run this callback with a `&mut Cursive`.
|
|
|
|
pub fn select_down(&mut self, n: usize) -> Callback {
|
2017-08-23 23:48:54 +00:00
|
|
|
self.focus_down(n);
|
2018-05-18 00:37:39 +00:00
|
|
|
self.make_select_cb().unwrap_or_else(Callback::dummy)
|
2017-08-23 23:48:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn focus_up(&mut self, n: usize) {
|
|
|
|
let focus = self.focus().saturating_sub(n);
|
|
|
|
self.focus.set(focus);
|
2016-07-20 06:47:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn focus_down(&mut self, n: usize) {
|
2018-05-18 00:37:39 +00:00
|
|
|
let focus = min(self.focus() + n, self.items.len().saturating_sub(1));
|
2016-07-20 06:47:27 +00:00
|
|
|
self.focus.set(focus);
|
|
|
|
}
|
2017-10-12 21:32:48 +00:00
|
|
|
|
|
|
|
fn submit(&mut self) -> EventResult {
|
|
|
|
let cb = self.on_submit.clone().unwrap();
|
|
|
|
// We return a Callback Rc<|s| cb(s, &*v)>
|
2018-07-23 03:25:13 +00:00
|
|
|
EventResult::Consumed(
|
|
|
|
self.selection()
|
|
|
|
.map(|v| Callback::from_fn(move |s| cb(s, &v))),
|
|
|
|
)
|
2017-10-12 21:32:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn on_event_regular(&mut self, event: Event) -> EventResult {
|
|
|
|
match event {
|
|
|
|
Event::Key(Key::Up) if self.focus() > 0 => self.focus_up(1),
|
|
|
|
Event::Key(Key::Down) if self.focus() + 1 < self.items.len() => {
|
|
|
|
self.focus_down(1)
|
|
|
|
}
|
|
|
|
Event::Key(Key::PageUp) => self.focus_up(10),
|
|
|
|
Event::Key(Key::PageDown) => self.focus_down(10),
|
|
|
|
Event::Key(Key::Home) => self.focus.set(0),
|
2018-05-18 00:37:39 +00:00
|
|
|
Event::Key(Key::End) => {
|
|
|
|
self.focus.set(self.items.len().saturating_sub(1))
|
|
|
|
}
|
2017-10-15 04:18:50 +00:00
|
|
|
Event::Mouse {
|
2018-07-25 02:38:24 +00:00
|
|
|
event: MouseEvent::Press(_),
|
2017-10-12 21:32:48 +00:00
|
|
|
position,
|
|
|
|
offset,
|
2018-07-20 02:44:59 +00:00
|
|
|
}
|
|
|
|
if position
|
|
|
|
.checked_sub(offset)
|
|
|
|
.map(|position| {
|
2018-07-25 02:38:24 +00:00
|
|
|
position < self.last_size && position.y < self.len()
|
2018-08-22 20:33:29 +00:00
|
|
|
}).unwrap_or(false) =>
|
2017-10-15 04:18:50 +00:00
|
|
|
{
|
2018-07-25 02:38:24 +00:00
|
|
|
self.focus.set(position.y - offset.y)
|
2017-10-12 21:32:48 +00:00
|
|
|
}
|
|
|
|
Event::Mouse {
|
|
|
|
event: MouseEvent::Release(MouseButton::Left),
|
|
|
|
position,
|
|
|
|
offset,
|
2018-07-25 02:38:24 +00:00
|
|
|
}
|
2018-08-22 20:33:29 +00:00
|
|
|
if self.on_submit.is_some() && position
|
|
|
|
.checked_sub(offset)
|
|
|
|
.map(|position| {
|
|
|
|
position < self.last_size && position.y == self.focus()
|
|
|
|
}).unwrap_or(false) =>
|
2018-07-25 02:38:24 +00:00
|
|
|
{
|
|
|
|
return self.submit();
|
2017-10-12 21:32:48 +00:00
|
|
|
}
|
|
|
|
Event::Key(Key::Enter) if self.on_submit.is_some() => {
|
|
|
|
return self.submit();
|
|
|
|
}
|
|
|
|
Event::Char(c) => {
|
|
|
|
// Starting from the current focus,
|
|
|
|
// find the first item that match the char.
|
|
|
|
// 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());
|
2018-06-11 06:29:10 +00:00
|
|
|
if let Some((i, _)) = iter
|
|
|
|
.enumerate()
|
2017-10-15 04:18:50 +00:00
|
|
|
.skip(self.focus() + 1)
|
|
|
|
.find(|&(_, item)| item.label.starts_with(c))
|
2017-10-12 21:32:48 +00:00
|
|
|
{
|
|
|
|
// Apply modulo in case we have a hit
|
|
|
|
// from the chained iterator
|
|
|
|
self.focus.set(i % self.items.len());
|
2017-10-26 18:23:00 +00:00
|
|
|
} else {
|
|
|
|
return EventResult::Ignored;
|
2017-10-12 21:32:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => return EventResult::Ignored,
|
|
|
|
}
|
|
|
|
|
2018-03-15 18:42:49 +00:00
|
|
|
EventResult::Consumed(self.make_select_cb())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a callback from selection change.
|
|
|
|
fn make_select_cb(&self) -> Option<Callback> {
|
2018-07-23 03:25:13 +00:00
|
|
|
self.on_select.clone().and_then(|cb| {
|
|
|
|
self.selection()
|
|
|
|
.map(|v| Callback::from_fn(move |s| cb(s, &v)))
|
2018-03-15 18:42:49 +00:00
|
|
|
})
|
2017-10-12 21:32:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn open_popup(&mut self) -> EventResult {
|
|
|
|
// Build a shallow menu tree to mimick the items array.
|
|
|
|
// TODO: cache it?
|
|
|
|
let mut tree = MenuTree::new();
|
|
|
|
for (i, item) in self.items.iter().enumerate() {
|
2017-10-13 18:22:02 +00:00
|
|
|
let focus = Rc::clone(&self.focus);
|
2017-10-12 21:32:48 +00:00
|
|
|
let on_submit = self.on_submit.as_ref().cloned();
|
2017-10-13 18:22:02 +00:00
|
|
|
let value = Rc::clone(&item.value);
|
2017-10-12 21:32:48 +00:00
|
|
|
tree.add_leaf(item.label.clone(), move |s| {
|
2018-03-15 16:36:15 +00:00
|
|
|
// TODO: What if an item was removed in the meantime?
|
2017-10-12 21:32:48 +00:00
|
|
|
focus.set(i);
|
|
|
|
if let Some(ref on_submit) = on_submit {
|
|
|
|
on_submit(s, &value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// Let's keep the tree around,
|
|
|
|
// the callback will want to use it.
|
|
|
|
let tree = Rc::new(tree);
|
|
|
|
|
|
|
|
let focus = self.focus();
|
|
|
|
// This is the offset for the label text.
|
|
|
|
// We'll want to show the popup so that the text matches.
|
|
|
|
// It'll be soo cool.
|
|
|
|
let item_length = self.items[focus].label.len();
|
|
|
|
let text_offset = (self.last_size.x.saturating_sub(item_length)) / 2;
|
|
|
|
// The total offset for the window is:
|
|
|
|
// * the last absolute offset at which we drew this view
|
|
|
|
// * shifted to the right of the text offset
|
|
|
|
// * shifted to the top of the focus (so the line matches)
|
|
|
|
// * shifted top-left of the border+padding of the popup
|
|
|
|
let offset = self.last_offset.get();
|
|
|
|
let offset = offset + (text_offset, 0);
|
|
|
|
let offset = offset.saturating_sub((0, focus));
|
|
|
|
let offset = offset.saturating_sub((2, 1));
|
2017-10-12 23:42:17 +00:00
|
|
|
|
|
|
|
// And now, we can return the callback that will create the popup.
|
2017-10-12 21:32:48 +00:00
|
|
|
EventResult::with_cb(move |s| {
|
|
|
|
// The callback will want to work with a fresh Rc
|
2017-10-13 18:22:02 +00:00
|
|
|
let tree = Rc::clone(&tree);
|
2017-10-12 21:32:48 +00:00
|
|
|
// We'll relativise the absolute position,
|
|
|
|
// So that we are locked to the parent view.
|
|
|
|
// A nice effect is that window resizes will keep both
|
|
|
|
// layers together.
|
|
|
|
let current_offset = s.screen().offset();
|
|
|
|
let offset = offset.signed() - current_offset;
|
|
|
|
// And finally, put the view in view!
|
|
|
|
s.screen_mut().add_layer_at(
|
|
|
|
Position::parent(offset),
|
|
|
|
MenuPopup::new(tree).focus(focus),
|
|
|
|
);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// A popup view only does one thing: open the popup on Enter.
|
|
|
|
fn on_event_popup(&mut self, event: Event) -> EventResult {
|
|
|
|
match event {
|
|
|
|
// TODO: add Left/Right support for quick-switch?
|
|
|
|
Event::Key(Key::Enter) => self.open_popup(),
|
|
|
|
Event::Mouse {
|
2017-10-12 23:42:17 +00:00
|
|
|
event: MouseEvent::Release(MouseButton::Left),
|
2017-10-12 21:32:48 +00:00
|
|
|
position,
|
|
|
|
offset,
|
2018-07-20 02:44:59 +00:00
|
|
|
}
|
|
|
|
if position.fits_in_rect(offset, self.last_size) =>
|
2017-10-15 04:18:50 +00:00
|
|
|
{
|
2017-10-12 21:32:48 +00:00
|
|
|
self.open_popup()
|
|
|
|
}
|
|
|
|
_ => EventResult::Ignored,
|
|
|
|
}
|
|
|
|
}
|
2015-05-31 04:05:34 +00:00
|
|
|
}
|
|
|
|
|
2015-05-31 04:32:24 +00:00
|
|
|
impl SelectView<String> {
|
2016-06-28 05:10:59 +00:00
|
|
|
/// Convenient method to use the label as value.
|
2016-07-30 08:52:34 +00:00
|
|
|
pub fn add_item_str<S: Into<String>>(&mut self, label: S) {
|
|
|
|
let label = label.into();
|
|
|
|
self.add_item(label.clone(), label);
|
2015-05-31 23:58:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Chainable variant of add_item_str
|
2016-07-30 08:52:34 +00:00
|
|
|
pub fn item_str<S: Into<String>>(self, label: S) -> Self {
|
|
|
|
self.with(|s| s.add_item_str(label))
|
|
|
|
}
|
|
|
|
|
2018-03-25 07:30:48 +00:00
|
|
|
/// Convenient method to use the label as value.
|
2018-05-18 00:37:39 +00:00
|
|
|
pub fn insert_item_str<S>(&mut self, index: usize, label: S)
|
|
|
|
where
|
|
|
|
S: Into<String>,
|
|
|
|
{
|
2018-03-25 07:30:48 +00:00
|
|
|
let label = label.into();
|
|
|
|
self.insert_item(index, label.clone(), label);
|
|
|
|
}
|
|
|
|
|
2016-07-30 08:52:34 +00:00
|
|
|
/// Adds all strings from an iterator.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # use cursive::views::SelectView;
|
|
|
|
/// let mut select_view = SelectView::new();
|
|
|
|
/// select_view.add_all_str(vec!["a", "b", "c"]);
|
|
|
|
/// ```
|
|
|
|
pub fn add_all_str<S, I>(&mut self, iter: I)
|
2017-08-23 23:48:54 +00:00
|
|
|
where
|
|
|
|
S: Into<String>,
|
|
|
|
I: IntoIterator<Item = S>,
|
2016-07-30 08:52:34 +00:00
|
|
|
{
|
|
|
|
for s in iter {
|
|
|
|
self.add_item_str(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds all strings from an iterator.
|
|
|
|
///
|
|
|
|
/// Chainable variant.
|
|
|
|
pub fn with_all_str<S, I>(self, iter: I) -> Self
|
2017-08-23 23:48:54 +00:00
|
|
|
where
|
|
|
|
S: Into<String>,
|
|
|
|
I: IntoIterator<Item = S>,
|
2016-07-30 08:52:34 +00:00
|
|
|
{
|
|
|
|
self.with(|s| s.add_all_str(iter))
|
2015-05-31 04:05:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-25 23:36:22 +00:00
|
|
|
impl<T: 'static> View for SelectView<T> {
|
2016-07-16 06:44:38 +00:00
|
|
|
fn draw(&self, printer: &Printer) {
|
2016-07-20 07:30:00 +00:00
|
|
|
self.last_offset.set(printer.offset);
|
|
|
|
|
2016-07-20 06:47:27 +00:00
|
|
|
if self.popup {
|
2018-07-25 02:38:24 +00:00
|
|
|
// Popup-select only draw the active element.
|
|
|
|
// We'll draw the full list in a popup if needed.
|
2016-07-20 06:47:27 +00:00
|
|
|
let style = if !self.enabled {
|
2018-01-17 17:35:57 +00:00
|
|
|
ColorStyle::secondary()
|
2016-07-20 06:47:27 +00:00
|
|
|
} else if !printer.focused {
|
2018-01-17 17:35:57 +00:00
|
|
|
ColorStyle::primary()
|
2016-07-20 06:47:27 +00:00
|
|
|
} else {
|
2018-01-17 17:35:57 +00:00
|
|
|
ColorStyle::highlight()
|
2016-07-20 06:47:27 +00:00
|
|
|
};
|
2017-08-14 23:32:01 +00:00
|
|
|
let x = match printer.size.x.checked_sub(1) {
|
|
|
|
Some(x) => x,
|
|
|
|
None => return,
|
|
|
|
};
|
2016-07-20 06:47:27 +00:00
|
|
|
|
|
|
|
printer.with_color(style, |printer| {
|
|
|
|
// Prepare the entire background
|
2017-08-14 23:32:01 +00:00
|
|
|
printer.print_hline((1, 0), x, " ");
|
2016-07-20 06:47:27 +00:00
|
|
|
// Draw the borders
|
|
|
|
printer.print((0, 0), "<");
|
2017-08-14 23:32:01 +00:00
|
|
|
printer.print((x, 0), ">");
|
2016-07-20 06:47:27 +00:00
|
|
|
|
|
|
|
let label = &self.items[self.focus()].label;
|
|
|
|
|
|
|
|
// And center the text?
|
2017-08-14 23:32:01 +00:00
|
|
|
let offset = HAlign::Center.get_offset(label.len(), x + 1);
|
2016-07-20 06:47:27 +00:00
|
|
|
|
|
|
|
printer.print((offset, 0), label);
|
|
|
|
});
|
|
|
|
} else {
|
2018-07-25 02:38:24 +00:00
|
|
|
// Non-popup mode: we always print the entire list.
|
2016-07-20 06:47:27 +00:00
|
|
|
let h = self.items.len();
|
|
|
|
let offset = self.align.v.get_offset(h, printer.size.y);
|
2018-04-03 23:38:54 +00:00
|
|
|
let printer = &printer.offset((0, offset));
|
2016-07-20 06:47:27 +00:00
|
|
|
|
2018-07-25 02:38:24 +00:00
|
|
|
for i in 0..self.len() {
|
|
|
|
printer.offset((0, i)).with_selection(
|
|
|
|
i == self.focus(),
|
|
|
|
|printer| {
|
|
|
|
if i != self.focus() && !self.enabled {
|
|
|
|
printer.with_color(
|
|
|
|
ColorStyle::secondary(),
|
|
|
|
|printer| self.draw_item(printer, i),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
self.draw_item(printer, i);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2016-07-20 06:47:27 +00:00
|
|
|
}
|
2015-05-31 04:05:34 +00:00
|
|
|
}
|
|
|
|
|
2018-07-25 02:38:24 +00:00
|
|
|
fn required_size(&mut self, _: Vec2) -> Vec2 {
|
2016-07-02 07:47:38 +00:00
|
|
|
// Items here are not compressible.
|
|
|
|
// So no matter what the horizontal requirements are,
|
|
|
|
// we'll still return our longest item.
|
2018-06-11 06:29:10 +00:00
|
|
|
let w = self
|
|
|
|
.items
|
2016-07-10 02:05:51 +00:00
|
|
|
.iter()
|
|
|
|
.map(|item| item.label.width())
|
|
|
|
.max()
|
|
|
|
.unwrap_or(1);
|
2016-07-20 06:47:27 +00:00
|
|
|
if self.popup {
|
|
|
|
Vec2::new(w + 2, 1)
|
|
|
|
} else {
|
|
|
|
let h = self.items.len();
|
2015-05-31 23:38:53 +00:00
|
|
|
|
2018-07-25 02:38:24 +00:00
|
|
|
Vec2::new(w, h)
|
2016-07-20 06:47:27 +00:00
|
|
|
}
|
2015-05-31 04:05:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn on_event(&mut self, event: Event) -> EventResult {
|
2016-07-20 06:47:27 +00:00
|
|
|
if self.popup {
|
2017-10-12 21:32:48 +00:00
|
|
|
self.on_event_popup(event)
|
2016-07-20 06:47:27 +00:00
|
|
|
} else {
|
2017-10-12 21:32:48 +00:00
|
|
|
self.on_event_regular(event)
|
2015-05-31 04:05:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-15 03:27:15 +00:00
|
|
|
fn take_focus(&mut self, _: Direction) -> bool {
|
2016-07-17 01:46:18 +00:00
|
|
|
self.enabled && !self.items.is_empty()
|
2015-05-31 04:05:34 +00:00
|
|
|
}
|
2015-05-31 23:38:53 +00:00
|
|
|
|
|
|
|
fn layout(&mut self, size: Vec2) {
|
2016-07-20 07:30:00 +00:00
|
|
|
self.last_size = size;
|
2015-05-31 23:38:53 +00:00
|
|
|
}
|
2018-03-22 21:31:28 +00:00
|
|
|
|
|
|
|
fn important_area(&self, size: Vec2) -> Rect {
|
|
|
|
self.selected_id()
|
|
|
|
.map(|i| Rect::from_size((0, i), (size.x, 1)))
|
2018-05-18 00:35:57 +00:00
|
|
|
.unwrap_or_else(|| Rect::from((0, 0)))
|
2018-03-22 21:31:28 +00:00
|
|
|
}
|
2015-05-31 04:05:34 +00:00
|
|
|
}
|
2016-07-30 08:18:12 +00:00
|
|
|
|
|
|
|
struct Item<T> {
|
|
|
|
label: String,
|
|
|
|
value: Rc<T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Item<T> {
|
2016-07-30 08:52:34 +00:00
|
|
|
fn new(label: String, value: T) -> Self {
|
2016-07-30 08:18:12 +00:00
|
|
|
Item {
|
2018-05-18 00:35:57 +00:00
|
|
|
label,
|
2016-07-30 08:18:12 +00:00
|
|
|
value: Rc::new(value),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|