2016-07-10 02:05:51 +00:00
|
|
|
use Cursive;
|
2016-07-14 06:25:54 +00:00
|
|
|
use Printer;
|
2016-10-02 22:22:29 +00:00
|
|
|
use direction;
|
2016-06-28 04:59:42 +00:00
|
|
|
use event::*;
|
2016-10-02 22:22:29 +00:00
|
|
|
use menu::MenuTree;
|
2016-06-28 04:59:42 +00:00
|
|
|
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
2017-01-23 23:42:36 +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;
|
|
|
|
use view::{Position, View};
|
|
|
|
use views::{KeyEventView, MenuPopup};
|
2016-07-04 23:04:32 +00:00
|
|
|
|
2016-07-02 22:02:42 +00:00
|
|
|
/// Current state of the menubar
|
|
|
|
#[derive(PartialEq, Debug)]
|
|
|
|
enum State {
|
|
|
|
/// The menubar is inactive.
|
|
|
|
Inactive,
|
|
|
|
/// The menubar is actively selected.
|
|
|
|
///
|
|
|
|
/// It will receive input.
|
|
|
|
Selected,
|
|
|
|
/// The menubar is still visible, but a submenu is open.
|
|
|
|
///
|
|
|
|
/// It will not receive input.
|
|
|
|
Submenu,
|
|
|
|
}
|
|
|
|
|
2016-07-20 03:28:34 +00:00
|
|
|
/// Shows a single-line list of items, with pop-up menus when one is selected.
|
|
|
|
///
|
2016-07-21 04:25:14 +00:00
|
|
|
/// The [`Cursive`] root already includes a menubar
|
|
|
|
/// that you just need to configure.
|
2016-07-20 03:28:34 +00:00
|
|
|
///
|
2016-07-20 03:50:21 +00:00
|
|
|
/// [`Cursive`]: ../struct.Cursive.html#method.menubar
|
2016-06-28 04:59:42 +00:00
|
|
|
pub struct Menubar {
|
2016-07-20 03:28:34 +00:00
|
|
|
/// Menu items in this menubar.
|
2017-01-23 23:42:36 +00:00
|
|
|
menus: Vec<(String, Rc<MenuTree>)>,
|
2016-07-20 03:28:34 +00:00
|
|
|
/// TODO: move this out of this view.
|
2016-06-28 04:59:42 +00:00
|
|
|
pub autohide: bool,
|
2016-07-20 03:28:34 +00:00
|
|
|
focus: usize,
|
|
|
|
|
|
|
|
// TODO: make Menubar impl View and take out the State management
|
2016-07-02 22:02:42 +00:00
|
|
|
state: State,
|
2016-06-28 04:59:42 +00:00
|
|
|
}
|
|
|
|
|
2016-07-17 00:28:42 +00:00
|
|
|
new_default!(Menubar);
|
2016-07-03 02:47:11 +00:00
|
|
|
|
2016-06-28 04:59:42 +00:00
|
|
|
impl Menubar {
|
2016-07-20 03:28:34 +00:00
|
|
|
/// Creates a new, empty menubar.
|
2016-06-28 04:59:42 +00:00
|
|
|
pub fn new() -> Self {
|
|
|
|
Menubar {
|
2016-07-02 22:02:42 +00:00
|
|
|
menus: Vec::new(),
|
2016-06-28 04:59:42 +00:00
|
|
|
autohide: true,
|
2016-07-02 22:02:42 +00:00
|
|
|
state: State::Inactive,
|
|
|
|
focus: 0,
|
2016-06-28 04:59:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-20 03:28:34 +00:00
|
|
|
/// Hides the menubar.
|
2016-07-16 08:07:37 +00:00
|
|
|
fn hide(&mut self) {
|
|
|
|
self.state = State::Inactive;
|
|
|
|
}
|
|
|
|
|
2016-07-20 03:28:34 +00:00
|
|
|
/// True if we should be receiving events.
|
2016-07-02 22:02:42 +00:00
|
|
|
pub fn receive_events(&self) -> bool {
|
|
|
|
self.state == State::Selected
|
|
|
|
}
|
|
|
|
|
2016-07-20 03:28:34 +00:00
|
|
|
/// Returns `true` if we should be drawn.
|
2016-07-02 22:02:42 +00:00
|
|
|
pub fn visible(&self) -> bool {
|
|
|
|
!self.autohide || self.state != State::Inactive
|
|
|
|
}
|
|
|
|
|
2016-07-20 03:28:34 +00:00
|
|
|
/// Adds a new item to the menubar.
|
|
|
|
///
|
|
|
|
/// The item will use the given title, and on selection, will open a
|
|
|
|
/// popup-menu with the given menu tree.
|
2016-07-02 22:02:42 +00:00
|
|
|
pub fn add(&mut self, title: &str, menu: MenuTree) -> &mut Self {
|
2017-01-23 23:42:36 +00:00
|
|
|
let i = self.menus.len();
|
|
|
|
self.insert(i, title, menu)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Insert a new item at the given position.
|
|
|
|
pub fn insert(&mut self, i: usize, title: &str, menu: MenuTree)
|
|
|
|
-> &mut Self {
|
|
|
|
self.menus.insert(i, (title.to_string(), Rc::new(menu)));
|
2016-07-02 22:02:42 +00:00
|
|
|
self
|
|
|
|
}
|
2017-01-23 23:42:36 +00:00
|
|
|
|
|
|
|
/// Removes all menu items from this menubar.
|
|
|
|
pub fn clear(&mut self) {
|
|
|
|
self.menus.clear();
|
|
|
|
self.focus = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the number of items in this menubar.
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.menus.len()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the item at the given position.
|
|
|
|
///
|
|
|
|
/// Returns `None` if `i > self.len()`
|
|
|
|
pub fn get(&mut self, i: usize) -> Option<&mut MenuTree> {
|
|
|
|
self.menus
|
|
|
|
.get_mut(i)
|
|
|
|
.map(|&mut (_, ref mut tree)| Rc::make_mut(tree))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Looks for an item with the given label.
|
|
|
|
pub fn find(&mut self, label: &str) -> Option<&mut MenuTree> {
|
|
|
|
// Look for the menu with the correct label,
|
|
|
|
// then call Rc::make_mut on the tree.
|
|
|
|
// If another Rc on this tree existed, this will clone
|
|
|
|
// the tree and keep the forked version.
|
|
|
|
self.menus
|
|
|
|
.iter_mut()
|
|
|
|
.find(|&&mut (ref l, _)| l == label)
|
|
|
|
.map(|&mut (_, ref mut tree)| Rc::make_mut(tree))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the position of the item with the given label.
|
|
|
|
///
|
|
|
|
/// Returns `None` if no such label was found.
|
|
|
|
pub fn find_position(&mut self, label: &str) -> Option<usize> {
|
|
|
|
self.menus
|
|
|
|
.iter()
|
|
|
|
.position(|&(ref l, _)| l == label)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Remove the item at the given position.
|
|
|
|
pub fn remove(&mut self, i: usize) {
|
|
|
|
self.menus.remove(i);
|
|
|
|
}
|
2016-07-20 03:44:20 +00:00
|
|
|
}
|
2016-07-02 22:02:42 +00:00
|
|
|
|
2016-07-20 03:44:20 +00:00
|
|
|
fn show_child(s: &mut Cursive, offset: (usize, usize), menu: Rc<MenuTree>) {
|
|
|
|
// Adds a new layer located near the item title with the menu popup.
|
|
|
|
// Also adds two key callbacks on this new view, to handle `left` and
|
|
|
|
// `right` key presses.
|
|
|
|
// (If the view itself listens for a `left` or `right` press, it will
|
|
|
|
// consume it before our KeyEventView. This means sub-menus can properly
|
|
|
|
// be entered.)
|
|
|
|
s.screen_mut()
|
|
|
|
.add_layer_at(Position::absolute(offset),
|
|
|
|
KeyEventView::new(MenuPopup::new(menu)
|
|
|
|
.on_dismiss(|s| s.select_menubar())
|
|
|
|
.on_action(|s| {
|
|
|
|
s.menubar().state = State::Inactive
|
|
|
|
}))
|
|
|
|
.register(Key::Right, |s| {
|
|
|
|
s.pop_layer();
|
|
|
|
s.select_menubar();
|
|
|
|
// Act as if we sent "Right" then "Down"
|
2016-10-12 00:12:00 +00:00
|
|
|
s.menubar().on_event(Event::Key(Key::Right)).process(s);
|
2017-01-23 23:42:36 +00:00
|
|
|
if let EventResult::Consumed(Some(cb)) =
|
|
|
|
s.menubar()
|
|
|
|
.on_event(Event::Key(Key::Down)) {
|
2016-07-20 03:44:20 +00:00
|
|
|
cb(s);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.register(Key::Left, |s| {
|
|
|
|
s.pop_layer();
|
|
|
|
s.select_menubar();
|
|
|
|
// Act as if we sent "Left" then "Down"
|
2016-10-12 00:12:00 +00:00
|
|
|
s.menubar().on_event(Event::Key(Key::Left)).process(s);
|
2017-01-23 23:42:36 +00:00
|
|
|
if let EventResult::Consumed(Some(cb)) =
|
|
|
|
s.menubar()
|
|
|
|
.on_event(Event::Key(Key::Down)) {
|
2016-07-20 03:44:20 +00:00
|
|
|
cb(s);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
impl View for Menubar {
|
|
|
|
fn draw(&self, printer: &Printer) {
|
2016-06-28 04:59:42 +00:00
|
|
|
// Draw the bar at the top
|
2016-07-01 06:38:01 +00:00
|
|
|
printer.with_color(ColorStyle::Primary, |printer| {
|
2016-06-28 04:59:42 +00:00
|
|
|
printer.print_hline((0, 0), printer.size.x, " ");
|
|
|
|
});
|
|
|
|
|
|
|
|
// TODO: draw the rest
|
2016-07-02 22:02:42 +00:00
|
|
|
let mut offset = 1;
|
|
|
|
for (i, &(ref title, _)) in self.menus.iter().enumerate() {
|
|
|
|
// We don't want to show HighlightInactive when we're not selected,
|
|
|
|
// because it's ugly on the menubar.
|
|
|
|
let selected = (self.state != State::Inactive) &&
|
|
|
|
(i == self.focus);
|
|
|
|
printer.with_selection(selected, |printer| {
|
|
|
|
printer.print((offset, 0), &format!(" {} ", title));
|
2016-07-04 23:04:32 +00:00
|
|
|
offset += title.width() + 2;
|
2016-07-02 22:02:42 +00:00
|
|
|
});
|
|
|
|
}
|
2016-06-28 04:59:42 +00:00
|
|
|
}
|
|
|
|
|
2016-07-20 03:44:20 +00:00
|
|
|
fn on_event(&mut self, event: Event) -> EventResult {
|
2016-07-02 22:02:42 +00:00
|
|
|
match event {
|
2016-10-09 22:47:06 +00:00
|
|
|
Event::Key(Key::Esc) => {
|
|
|
|
self.hide();
|
|
|
|
return EventResult::with_cb(|s| s.clear());
|
|
|
|
}
|
2016-07-03 02:37:38 +00:00
|
|
|
Event::Key(Key::Left) => {
|
|
|
|
if self.focus > 0 {
|
|
|
|
self.focus -= 1
|
|
|
|
} else {
|
|
|
|
self.focus = self.menus.len() - 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Event::Key(Key::Right) => {
|
|
|
|
if self.focus + 1 < self.menus.len() {
|
|
|
|
self.focus += 1
|
|
|
|
} else {
|
|
|
|
self.focus = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Event::Key(Key::Down) |
|
2016-07-02 22:02:42 +00:00
|
|
|
Event::Key(Key::Enter) => {
|
|
|
|
// First, we need a new Rc to send the callback,
|
|
|
|
// since we don't know when it will be called.
|
|
|
|
let menu = self.menus[self.focus].1.clone();
|
|
|
|
self.state = State::Submenu;
|
|
|
|
let offset = (self.menus[..self.focus]
|
2017-01-23 23:42:36 +00:00
|
|
|
.iter()
|
|
|
|
.map(|&(ref title, _)| title.width() + 2)
|
|
|
|
.fold(0, |a, b| a + b),
|
2016-10-02 22:22:29 +00:00
|
|
|
if self.autohide { 1 } else { 0 });
|
2016-07-10 02:05:51 +00:00
|
|
|
// Since the closure will be called multiple times,
|
|
|
|
// we also need a new Rc on every call.
|
2016-07-20 03:44:20 +00:00
|
|
|
return EventResult::with_cb(move |s| {
|
2016-07-10 02:05:51 +00:00
|
|
|
show_child(s, offset, menu.clone())
|
2016-07-20 03:44:20 +00:00
|
|
|
});
|
2016-07-02 22:02:42 +00:00
|
|
|
}
|
2016-07-20 03:44:20 +00:00
|
|
|
_ => return EventResult::Ignored,
|
2016-07-02 22:02:42 +00:00
|
|
|
}
|
2016-07-20 03:44:20 +00:00
|
|
|
EventResult::Consumed(None)
|
2016-06-28 04:59:42 +00:00
|
|
|
}
|
2016-07-10 02:05:51 +00:00
|
|
|
|
2016-07-20 03:44:20 +00:00
|
|
|
fn take_focus(&mut self, _: direction::Direction) -> bool {
|
|
|
|
self.state = State::Selected;
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_min_size(&mut self, _: Vec2) -> Vec2 {
|
|
|
|
// TODO: scroll the options if the screen is too small?
|
2016-07-10 02:05:51 +00:00
|
|
|
|
2016-07-20 03:44:20 +00:00
|
|
|
// We add 2 to the length of every label for marin.
|
|
|
|
// Also, we add 1 at the beginning.
|
|
|
|
// (See the `draw()` method)
|
|
|
|
let width = self.menus
|
|
|
|
.iter()
|
|
|
|
.map(|&(ref title, _)| title.len() + 2)
|
|
|
|
.fold(1, |a, b| a + b);
|
|
|
|
|
|
|
|
Vec2::new(width, 1)
|
|
|
|
}
|
2016-07-10 02:05:51 +00:00
|
|
|
}
|