2016-07-02 08:01:09 +00:00
|
|
|
|
|
|
|
|
2016-07-02 22:02:42 +00:00
|
|
|
use Cursive;
|
2016-10-02 22:22:29 +00:00
|
|
|
use Printer;
|
2016-07-20 07:30:00 +00:00
|
|
|
use With;
|
2016-10-02 22:22:29 +00:00
|
|
|
use align::Align;
|
|
|
|
use event::{Callback, Event, EventResult, Key};
|
2016-07-02 08:01:09 +00:00
|
|
|
use menu::{MenuItem, MenuTree};
|
2016-10-02 22:22:29 +00:00
|
|
|
use std::cmp::min;
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
|
|
|
use unicode_width::UnicodeWidthStr;
|
|
|
|
use vec::Vec2;
|
2016-08-02 07:49:59 +00:00
|
|
|
use view::{Position, ScrollBase, View};
|
2016-07-28 23:36:01 +00:00
|
|
|
use views::KeyEventView;
|
2016-07-02 08:01:09 +00:00
|
|
|
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Popup that shows a list of items.
|
2016-07-02 08:01:09 +00:00
|
|
|
pub struct MenuPopup {
|
|
|
|
menu: Rc<MenuTree>,
|
|
|
|
focus: usize,
|
|
|
|
scrollbase: ScrollBase,
|
|
|
|
align: Align,
|
2016-07-02 22:02:42 +00:00
|
|
|
on_dismiss: Option<Callback>,
|
|
|
|
on_action: Option<Callback>,
|
2016-07-02 08:01:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MenuPopup {
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Creates a new `MenuPopup` using the given menu tree.
|
2016-07-02 08:01:09 +00:00
|
|
|
pub fn new(menu: Rc<MenuTree>) -> Self {
|
|
|
|
MenuPopup {
|
|
|
|
menu: menu,
|
|
|
|
focus: 0,
|
2016-08-02 07:49:59 +00:00
|
|
|
scrollbase: ScrollBase::new().scrollbar_offset(1).right_padding(0),
|
2016-07-02 08:01:09 +00:00
|
|
|
align: Align::top_left(),
|
2016-07-02 22:02:42 +00:00
|
|
|
on_dismiss: None,
|
|
|
|
on_action: None,
|
2016-07-02 08:01:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-20 07:30:00 +00:00
|
|
|
/// Sets the currently focused element.
|
|
|
|
pub fn set_focus(&mut self, focus: usize) {
|
|
|
|
self.focus = min(focus, self.menu.len());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the currently focused element.
|
|
|
|
///
|
|
|
|
/// Chainable variant.
|
|
|
|
pub fn focus(self, focus: usize) -> Self {
|
|
|
|
self.with(|s| s.set_focus(focus))
|
|
|
|
}
|
|
|
|
|
2016-07-03 02:37:38 +00:00
|
|
|
fn item_width(item: &MenuItem) -> usize {
|
|
|
|
match *item {
|
|
|
|
MenuItem::Delimiter => 1,
|
|
|
|
MenuItem::Leaf(ref title, _) => title.width(),
|
|
|
|
MenuItem::Subtree(ref title, _) => title.width() + 3,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-03 03:53:13 +00:00
|
|
|
fn scroll_up(&mut self, mut n: usize, cycle: bool) {
|
|
|
|
while n > 0 {
|
|
|
|
if self.focus > 0 {
|
|
|
|
self.focus -= 1;
|
|
|
|
} else if cycle {
|
|
|
|
self.focus = self.menu.children.len() - 1;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if !self.menu.children[self.focus].is_delimiter() {
|
|
|
|
n -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn scroll_down(&mut self, mut n: usize, cycle: bool) {
|
|
|
|
while n > 0 {
|
|
|
|
if self.focus + 1 < self.menu.children.len() {
|
|
|
|
self.focus += 1;
|
|
|
|
} else if cycle {
|
|
|
|
self.focus = 0;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if !self.menu.children[self.focus].is_delimiter() {
|
|
|
|
n -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-03 02:37:38 +00:00
|
|
|
|
2016-07-02 08:01:09 +00:00
|
|
|
/// Sets the alignment for this view.
|
2017-05-19 18:20:13 +00:00
|
|
|
///
|
|
|
|
/// Chainable variant.
|
|
|
|
pub fn align(self, align: Align) -> Self {
|
|
|
|
self.with(|s| s.set_align(align))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the alignment for this view.
|
|
|
|
pub fn set_align(&mut self, align: Align) {
|
2016-07-02 08:01:09 +00:00
|
|
|
self.align = align;
|
2017-05-19 18:20:13 +00:00
|
|
|
}
|
2016-07-02 08:01:09 +00:00
|
|
|
|
2017-05-19 18:20:13 +00:00
|
|
|
/// Sets a callback to be used when this view is actively dismissed.
|
|
|
|
///
|
|
|
|
/// (When the user hits <ESC>)
|
|
|
|
///
|
|
|
|
/// Chainable variant.
|
|
|
|
pub fn on_dismiss<F: 'static + Fn(&mut Cursive)>(self, f: F) -> Self {
|
|
|
|
self.with(|s| s.set_on_dismiss(f))
|
2016-07-02 08:01:09 +00:00
|
|
|
}
|
2016-07-02 22:02:42 +00:00
|
|
|
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Sets a callback to be used when this view is actively dismissed.
|
|
|
|
///
|
|
|
|
/// (When the user hits <ESC>)
|
2017-05-19 18:20:13 +00:00
|
|
|
pub fn set_on_dismiss<F: 'static + Fn(&mut Cursive)>(&mut self, f: F) {
|
2016-07-25 06:00:13 +00:00
|
|
|
self.on_dismiss = Some(Callback::from_fn(f));
|
2016-07-02 22:02:42 +00:00
|
|
|
}
|
|
|
|
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Sets a callback to be used when a leaf is activated.
|
|
|
|
///
|
|
|
|
/// Will also be called if a leaf from a subtree is activated.
|
|
|
|
///
|
|
|
|
/// Usually used to hide the parent view.
|
2017-05-19 18:20:13 +00:00
|
|
|
///
|
|
|
|
/// Chainable variant.
|
|
|
|
pub fn on_action<F: 'static + Fn(&mut Cursive)>(self, f: F) -> Self {
|
|
|
|
self.with(|s| s.set_on_action(f))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets a callback to be used when a leaf is activated.
|
|
|
|
///
|
|
|
|
/// Will also be called if a leaf from a subtree is activated.
|
|
|
|
///
|
|
|
|
/// Usually used to hide the parent view.
|
|
|
|
pub fn set_on_action<F: 'static + Fn(&mut Cursive)>(&mut self, f: F) {
|
2016-07-25 06:00:13 +00:00
|
|
|
self.on_action = Some(Callback::from_fn(f));
|
2016-07-02 22:02:42 +00:00
|
|
|
}
|
2016-07-03 02:37:38 +00:00
|
|
|
|
|
|
|
fn make_subtree_cb(&self, tree: &Rc<MenuTree>) -> EventResult {
|
|
|
|
let tree = tree.clone();
|
|
|
|
let max_width = 4 +
|
|
|
|
self.menu
|
2016-07-10 02:05:51 +00:00
|
|
|
.children
|
|
|
|
.iter()
|
|
|
|
.map(Self::item_width)
|
|
|
|
.max()
|
|
|
|
.unwrap_or(1);
|
2016-07-03 02:37:38 +00:00
|
|
|
let offset = Vec2::new(max_width, self.focus);
|
|
|
|
let action_cb = self.on_action.clone();
|
2016-07-12 03:26:33 +00:00
|
|
|
|
2016-07-03 02:37:38 +00:00
|
|
|
EventResult::with_cb(move |s| {
|
|
|
|
let action_cb = action_cb.clone();
|
|
|
|
s.screen_mut()
|
2016-07-10 02:05:51 +00:00
|
|
|
.add_layer_at(Position::parent(offset),
|
|
|
|
KeyEventView::new(MenuPopup::new(tree.clone())
|
|
|
|
.on_action(move |s| {
|
|
|
|
// This will happen when the subtree popup
|
|
|
|
// activates something;
|
|
|
|
// First, remove ourselve.
|
|
|
|
s.pop_layer();
|
2016-07-03 02:37:38 +00:00
|
|
|
if let Some(ref action_cb) = action_cb {
|
|
|
|
action_cb.clone()(s);
|
|
|
|
}
|
2016-07-10 02:05:51 +00:00
|
|
|
}))
|
|
|
|
.register(Key::Left, |s| s.pop_layer()));
|
2016-07-03 02:37:38 +00:00
|
|
|
})
|
|
|
|
}
|
2016-07-02 08:01:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl View for MenuPopup {
|
2016-07-16 06:44:38 +00:00
|
|
|
fn draw(&self, printer: &Printer) {
|
2016-07-17 08:20:41 +00:00
|
|
|
if printer.size.x < 2 || printer.size.y < 2 {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-02 08:01:09 +00:00
|
|
|
let h = self.menu.len();
|
|
|
|
let offset = self.align.v.get_offset(h, printer.size.y);
|
2016-07-10 02:05:51 +00:00
|
|
|
let printer =
|
|
|
|
&printer.sub_printer(Vec2::new(0, offset), printer.size, true);
|
2016-07-02 08:01:09 +00:00
|
|
|
|
|
|
|
// Start with a box
|
2016-08-05 00:27:04 +00:00
|
|
|
printer.print_box(Vec2::new(0, 0), printer.size, false);
|
2016-07-02 08:01:09 +00:00
|
|
|
|
|
|
|
// We're giving it a reduced size because of borders.
|
|
|
|
// But we're keeping the full width,
|
|
|
|
// to integrate horizontal delimiters in the frame.
|
|
|
|
let size = printer.size - (0, 2);
|
|
|
|
let printer = printer.sub_printer(Vec2::new(0, 1), size, true);
|
|
|
|
self.scrollbase.draw(&printer, |printer, i| {
|
|
|
|
printer.with_selection(i == self.focus, |printer| {
|
|
|
|
let item = &self.menu.children[i];
|
|
|
|
match *item {
|
|
|
|
MenuItem::Delimiter => {
|
|
|
|
printer.print_hdelim((0, 0), printer.size.x)
|
|
|
|
}
|
2016-07-03 02:37:38 +00:00
|
|
|
MenuItem::Subtree(ref label, _) => {
|
2016-07-20 03:44:20 +00:00
|
|
|
if printer.size.x < 4 {
|
|
|
|
return;
|
|
|
|
}
|
2016-07-03 02:37:38 +00:00
|
|
|
printer.print_hline((1, 0), printer.size.x - 2, " ");
|
|
|
|
printer.print((2, 0), label);
|
|
|
|
printer.print((printer.size.x - 4, 0), ">>");
|
|
|
|
}
|
2016-07-02 08:01:09 +00:00
|
|
|
MenuItem::Leaf(ref label, _) => {
|
2016-07-20 03:44:20 +00:00
|
|
|
if printer.size.x < 2 {
|
|
|
|
return;
|
|
|
|
}
|
2016-07-02 22:02:42 +00:00
|
|
|
printer.print_hline((1, 0), printer.size.x - 2, " ");
|
|
|
|
printer.print((2, 0), label);
|
2016-07-02 08:01:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-01-24 06:52:29 +00:00
|
|
|
fn required_size(&mut self, req: Vec2) -> Vec2 {
|
2016-07-02 08:01:09 +00:00
|
|
|
// We can't really shrink our items here, so it's not flexible.
|
2016-07-03 02:37:38 +00:00
|
|
|
let w = 4 +
|
2016-07-02 22:02:42 +00:00
|
|
|
self.menu
|
2016-07-10 02:05:51 +00:00
|
|
|
.children
|
|
|
|
.iter()
|
|
|
|
.map(Self::item_width)
|
|
|
|
.max()
|
|
|
|
.unwrap_or(1);
|
2016-07-02 22:02:42 +00:00
|
|
|
let h = 2 + self.menu.children.len();
|
2016-07-02 08:01:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
let scrolling = req.y < h;
|
|
|
|
|
2016-10-02 22:22:29 +00:00
|
|
|
let w = if scrolling { w + 1 } else { w };
|
2016-07-02 08:01:09 +00:00
|
|
|
|
|
|
|
Vec2::new(w, h)
|
|
|
|
}
|
2016-07-02 22:02:42 +00:00
|
|
|
|
|
|
|
fn on_event(&mut self, event: Event) -> EventResult {
|
|
|
|
match event {
|
|
|
|
Event::Key(Key::Esc) => {
|
|
|
|
let dismiss_cb = self.on_dismiss.clone();
|
|
|
|
return EventResult::with_cb(move |s| {
|
|
|
|
if let Some(ref cb) = dismiss_cb {
|
|
|
|
cb.clone()(s);
|
|
|
|
}
|
|
|
|
s.pop_layer();
|
|
|
|
});
|
|
|
|
}
|
2016-07-03 03:53:13 +00:00
|
|
|
Event::Key(Key::Up) => self.scroll_up(1, true),
|
|
|
|
Event::Key(Key::PageUp) => self.scroll_up(5, false),
|
|
|
|
Event::Key(Key::Down) => self.scroll_down(1, true),
|
|
|
|
Event::Key(Key::PageDown) => self.scroll_down(5, false),
|
|
|
|
|
|
|
|
Event::Key(Key::Home) => self.focus = 0,
|
2017-02-07 02:18:17 +00:00
|
|
|
Event::Key(Key::End) => self.focus = self.menu.children.len() - 1,
|
2016-07-03 03:53:13 +00:00
|
|
|
|
2017-02-07 02:18:17 +00:00
|
|
|
Event::Key(Key::Right) if self.menu.children[self.focus]
|
2016-07-10 02:05:51 +00:00
|
|
|
.is_subtree() => {
|
2016-07-03 02:37:38 +00:00
|
|
|
return match self.menu.children[self.focus] {
|
|
|
|
MenuItem::Subtree(_, ref tree) => {
|
|
|
|
self.make_subtree_cb(tree)
|
|
|
|
}
|
|
|
|
_ => panic!("Not a subtree???"),
|
|
|
|
|
|
|
|
};
|
2016-07-02 22:02:42 +00:00
|
|
|
}
|
2017-02-07 02:18:17 +00:00
|
|
|
Event::Key(Key::Enter) if !self.menu.children[self.focus]
|
2016-07-10 02:05:51 +00:00
|
|
|
.is_delimiter() => {
|
2016-07-02 22:02:42 +00:00
|
|
|
return match self.menu.children[self.focus] {
|
|
|
|
MenuItem::Leaf(_, ref cb) => {
|
|
|
|
|
|
|
|
let cb = cb.clone();
|
|
|
|
let action_cb = self.on_action.clone();
|
|
|
|
EventResult::with_cb(move |s| {
|
2016-07-03 02:37:38 +00:00
|
|
|
// Remove ourselves from the face of the earth
|
|
|
|
s.pop_layer();
|
|
|
|
// If we had prior orders, do it now.
|
2016-07-02 22:02:42 +00:00
|
|
|
if let Some(ref action_cb) = action_cb {
|
|
|
|
action_cb.clone()(s);
|
|
|
|
}
|
2016-07-03 02:37:38 +00:00
|
|
|
// And transmit his last words.
|
2016-07-02 22:02:42 +00:00
|
|
|
cb.clone()(s);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
MenuItem::Subtree(_, ref tree) => {
|
2016-07-03 02:37:38 +00:00
|
|
|
self.make_subtree_cb(tree)
|
2016-07-02 22:02:42 +00:00
|
|
|
}
|
|
|
|
_ => panic!("No delimiter here"),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => return EventResult::Ignored,
|
|
|
|
}
|
|
|
|
|
|
|
|
self.scrollbase.scroll_to(self.focus);
|
|
|
|
|
|
|
|
EventResult::Consumed(None)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn layout(&mut self, size: Vec2) {
|
2017-01-23 23:42:36 +00:00
|
|
|
self.scrollbase
|
|
|
|
.set_heights(size.y - 2, self.menu.children.len());
|
2016-07-02 22:02:42 +00:00
|
|
|
}
|
2016-07-02 08:01:09 +00:00
|
|
|
}
|