2016-07-02 08:01:09 +00:00
|
|
|
use std::rc::Rc;
|
|
|
|
|
|
|
|
use unicode_width::UnicodeWidthStr;
|
|
|
|
|
2016-07-02 22:02:42 +00:00
|
|
|
use Cursive;
|
2016-07-02 08:01:09 +00:00
|
|
|
use menu::{MenuItem, MenuTree};
|
|
|
|
use printer::Printer;
|
|
|
|
use view::View;
|
2016-07-02 22:02:42 +00:00
|
|
|
use view::Position;
|
2016-07-02 08:01:09 +00:00
|
|
|
use view::scroll::ScrollBase;
|
|
|
|
use align::Align;
|
|
|
|
use vec::Vec2;
|
2016-07-02 22:02:42 +00:00
|
|
|
use event::{Callback, Event, EventResult, Key};
|
2016-07-02 08:01:09 +00:00
|
|
|
|
|
|
|
/// fd
|
|
|
|
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 {
|
|
|
|
pub fn new(menu: Rc<MenuTree>) -> Self {
|
|
|
|
MenuPopup {
|
|
|
|
menu: menu,
|
|
|
|
focus: 0,
|
|
|
|
scrollbase: ScrollBase::new(),
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the alignment for this view.
|
|
|
|
pub fn align(mut self, align: Align) -> Self {
|
|
|
|
self.align = align;
|
|
|
|
|
|
|
|
self
|
|
|
|
}
|
2016-07-02 22:02:42 +00:00
|
|
|
|
|
|
|
pub fn on_dismiss<F: 'static + Fn(&mut Cursive)>(mut self, f: F) -> Self {
|
|
|
|
self.on_dismiss = Some(Rc::new(f));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn on_action<F: 'static + Fn(&mut Cursive)>(mut self, f: F) -> Self {
|
|
|
|
self.on_action = Some(Rc::new(f));
|
|
|
|
self
|
|
|
|
}
|
2016-07-02 08:01:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl View for MenuPopup {
|
|
|
|
fn draw(&mut self, printer: &Printer) {
|
|
|
|
let h = self.menu.len();
|
|
|
|
let offset = self.align.v.get_offset(h, printer.size.y);
|
|
|
|
let printer = &printer.sub_printer(Vec2::new(0, offset),
|
|
|
|
printer.size,
|
|
|
|
true);
|
|
|
|
|
|
|
|
// Start with a box
|
|
|
|
printer.print_box(Vec2::new(0, 0), printer.size);
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
MenuItem::Subtree(ref label, _) |
|
|
|
|
MenuItem::Leaf(ref label, _) => {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_min_size(&self, req: Vec2) -> Vec2 {
|
|
|
|
// We can't really shrink our items here, so it's not flexible.
|
2016-07-02 22:02:42 +00:00
|
|
|
let w = 2 +
|
|
|
|
self.menu
|
2016-07-02 08:01:09 +00:00
|
|
|
.children
|
|
|
|
.iter()
|
2016-07-02 22:02:42 +00:00
|
|
|
.map(|item| 2 + item.label().width())
|
2016-07-02 08:01:09 +00:00
|
|
|
.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;
|
|
|
|
|
|
|
|
let w = if scrolling {
|
|
|
|
w + 2
|
|
|
|
} else {
|
|
|
|
w
|
|
|
|
};
|
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
Event::Key(Key::Up) if self.focus > 0 => self.focus -= 1,
|
|
|
|
Event::Key(Key::Down) if self.focus + 1 <
|
|
|
|
self.menu.children.len() => {
|
|
|
|
self.focus += 1
|
|
|
|
}
|
|
|
|
Event::Key(Key::Enter) if !self.menu.children[self.focus]
|
|
|
|
.is_delimiter() => {
|
|
|
|
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| {
|
|
|
|
if let Some(ref action_cb) = action_cb {
|
|
|
|
action_cb.clone()(s);
|
|
|
|
}
|
|
|
|
s.pop_layer();
|
|
|
|
cb.clone()(s);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
MenuItem::Subtree(_, ref tree) => {
|
|
|
|
let tree = tree.clone();
|
|
|
|
let offset = Vec2::new(10, self.focus + 1);
|
|
|
|
EventResult::with_cb(move |s| {
|
|
|
|
s.screen_mut()
|
|
|
|
.add_layer_at(Position::parent(offset),
|
|
|
|
MenuPopup::new(tree.clone()));
|
|
|
|
})
|
|
|
|
}
|
|
|
|
_ => panic!("No delimiter here"),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => return EventResult::Ignored,
|
|
|
|
}
|
|
|
|
|
|
|
|
self.scrollbase.scroll_to(self.focus);
|
|
|
|
|
|
|
|
EventResult::Consumed(None)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn layout(&mut self, size: Vec2) {
|
|
|
|
self.scrollbase.set_heights(size.y, self.menu.children.len());
|
|
|
|
}
|
2016-07-02 08:01:09 +00:00
|
|
|
}
|