2019-02-28 23:54:12 +00:00
|
|
|
use crate::direction;
|
|
|
|
use crate::event::*;
|
|
|
|
use crate::menu::{MenuItem, MenuTree};
|
|
|
|
use crate::rect::Rect;
|
|
|
|
use crate::theme::ColorStyle;
|
|
|
|
use crate::vec::Vec2;
|
|
|
|
use crate::view::{Position, View};
|
|
|
|
use crate::views::{MenuPopup, OnEventView};
|
|
|
|
use crate::Cursive;
|
|
|
|
use crate::Printer;
|
2019-03-01 00:04:14 +00:00
|
|
|
use std::rc::Rc;
|
|
|
|
use unicode_width::UnicodeWidthStr;
|
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
|
|
|
///
|
2020-01-06 23:39:30 +00:00
|
|
|
/// [`Cursive`]: crate::Cursive::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.
|
2018-01-09 13:37:40 +00:00
|
|
|
root: MenuTree,
|
2017-10-12 23:42:41 +00:00
|
|
|
|
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 {
|
2018-01-09 13:37:40 +00:00
|
|
|
root: MenuTree::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
|
|
|
|
}
|
|
|
|
|
2017-10-13 22:17:41 +00:00
|
|
|
/// True if some submenus are visible.
|
|
|
|
pub fn has_submenu(&self) -> bool {
|
|
|
|
self.state == State::Submenu
|
|
|
|
}
|
|
|
|
|
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.
|
2018-01-09 13:37:40 +00:00
|
|
|
pub fn add_subtree<S>(&mut self, title: S, menu: MenuTree) -> &mut Self
|
|
|
|
where
|
|
|
|
S: Into<String>,
|
|
|
|
{
|
|
|
|
let i = self.root.len();
|
2017-01-24 00:51:48 +00:00
|
|
|
self.insert_subtree(i, title, menu)
|
2017-01-23 23:42:36 +00:00
|
|
|
}
|
|
|
|
|
2018-01-09 13:37:40 +00:00
|
|
|
/// Adds a delimiter to the menubar.
|
|
|
|
pub fn add_delimiter(&mut self) -> &mut Self {
|
|
|
|
let i = self.root.len();
|
|
|
|
self.insert_delimiter(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds a leaf node to the menubar.
|
|
|
|
pub fn add_leaf<S, F>(&mut self, title: S, cb: F) -> &mut Self
|
|
|
|
where
|
|
|
|
S: Into<String>,
|
|
|
|
F: 'static + Fn(&mut Cursive),
|
|
|
|
{
|
|
|
|
let i = self.root.len();
|
|
|
|
self.insert_leaf(i, title, cb)
|
|
|
|
}
|
|
|
|
|
2017-01-23 23:42:36 +00:00
|
|
|
/// Insert a new item at the given position.
|
2018-01-09 13:37:40 +00:00
|
|
|
pub fn insert_subtree<S>(
|
2019-07-30 23:08:05 +00:00
|
|
|
&mut self,
|
|
|
|
i: usize,
|
|
|
|
title: S,
|
|
|
|
menu: MenuTree,
|
2018-01-09 13:37:40 +00:00
|
|
|
) -> &mut Self
|
|
|
|
where
|
|
|
|
S: Into<String>,
|
|
|
|
{
|
|
|
|
self.root.insert_subtree(i, title, menu);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Inserts a new delimiter at the given position.
|
|
|
|
///
|
|
|
|
/// It will show up as `|`.
|
|
|
|
pub fn insert_delimiter(&mut self, i: usize) -> &mut Self {
|
|
|
|
self.root.insert_delimiter(i);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Inserts a new leaf node at the given position.
|
|
|
|
///
|
|
|
|
/// It will be directly actionable.
|
|
|
|
pub fn insert_leaf<S, F>(&mut self, i: usize, title: S, cb: F) -> &mut Self
|
|
|
|
where
|
|
|
|
S: Into<String>,
|
|
|
|
F: 'static + Fn(&mut Cursive),
|
|
|
|
{
|
|
|
|
self.root.insert_leaf(i, title, cb);
|
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) {
|
2018-01-09 13:37:40 +00:00
|
|
|
self.root.clear();
|
2017-01-23 23:42:36 +00:00
|
|
|
self.focus = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the number of items in this menubar.
|
|
|
|
pub fn len(&self) -> usize {
|
2018-01-09 13:37:40 +00:00
|
|
|
self.root.len()
|
2017-01-23 23:42:36 +00:00
|
|
|
}
|
|
|
|
|
2017-01-27 22:51:46 +00:00
|
|
|
/// Returns `true` if this menubar is empty.
|
|
|
|
pub fn is_empty(&self) -> bool {
|
2018-01-09 13:37:40 +00:00
|
|
|
self.root.is_empty()
|
2017-01-27 22:51:46 +00:00
|
|
|
}
|
|
|
|
|
2017-01-23 23:42:36 +00:00
|
|
|
/// Returns the item at the given position.
|
|
|
|
///
|
|
|
|
/// Returns `None` if `i > self.len()`
|
2017-01-24 00:51:48 +00:00
|
|
|
pub fn get_subtree(&mut self, i: usize) -> Option<&mut MenuTree> {
|
2018-01-09 13:37:40 +00:00
|
|
|
self.root.get_subtree(i)
|
2017-01-23 23:42:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Looks for an item with the given label.
|
2017-01-24 00:51:48 +00:00
|
|
|
pub fn find_subtree(&mut self, label: &str) -> Option<&mut MenuTree> {
|
2018-01-09 13:37:40 +00:00
|
|
|
self.root.find_subtree(label)
|
2017-01-23 23:42:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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> {
|
2018-01-09 13:37:40 +00:00
|
|
|
self.root.find_position(label)
|
2017-01-23 23:42:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Remove the item at the given position.
|
|
|
|
pub fn remove(&mut self, i: usize) {
|
2018-01-09 13:37:40 +00:00
|
|
|
self.root.remove(i);
|
2017-01-23 23:42:36 +00:00
|
|
|
}
|
2017-10-12 23:42:41 +00:00
|
|
|
|
|
|
|
fn child_at(&self, x: usize) -> Option<usize> {
|
|
|
|
if x == 0 {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let mut offset = 1;
|
|
|
|
|
2018-01-09 13:37:40 +00:00
|
|
|
for (i, child) in self.root.children.iter().enumerate() {
|
|
|
|
offset += child.label().width() + 2;
|
2017-10-12 23:42:41 +00:00
|
|
|
if x < offset {
|
|
|
|
return Some(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2018-01-09 13:37:40 +00:00
|
|
|
fn select_child(&mut self, open_only: bool) -> EventResult {
|
|
|
|
match self.root.children[self.focus] {
|
|
|
|
MenuItem::Leaf(_, ref cb) if !open_only => {
|
2018-08-06 01:17:56 +00:00
|
|
|
// Go inactive after an action.
|
|
|
|
self.state = State::Inactive;
|
2018-01-09 13:37:40 +00:00
|
|
|
EventResult::Consumed(Some(cb.clone()))
|
|
|
|
}
|
|
|
|
MenuItem::Subtree(_, ref tree) => {
|
|
|
|
// First, we need a new Rc to send the callback,
|
|
|
|
// since we don't know when it will be called.
|
|
|
|
let menu = Rc::clone(tree);
|
|
|
|
|
|
|
|
self.state = State::Submenu;
|
|
|
|
let offset = Vec2::new(
|
|
|
|
self.root.children[..self.focus]
|
|
|
|
.iter()
|
|
|
|
.map(|child| child.label().width() + 2)
|
|
|
|
.sum(),
|
|
|
|
if self.autohide { 1 } else { 0 },
|
|
|
|
);
|
|
|
|
// Since the closure will be called multiple times,
|
|
|
|
// we also need a new Rc on every call.
|
|
|
|
EventResult::with_cb(move |s| {
|
|
|
|
show_child(s, offset, Rc::clone(&menu))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
_ => EventResult::Ignored,
|
|
|
|
}
|
2017-10-12 23:42:41 +00:00
|
|
|
}
|
2016-07-20 03:44:20 +00:00
|
|
|
}
|
2016-07-02 22:02:42 +00:00
|
|
|
|
2018-01-09 13:37:40 +00:00
|
|
|
fn show_child(s: &mut Cursive, offset: Vec2, menu: Rc<MenuTree>) {
|
2016-07-20 03:44:20 +00:00
|
|
|
// 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
|
2017-06-12 23:39:12 +00:00
|
|
|
// consume it before our OnEventView. This means sub-menus can properly
|
2016-07-20 03:44:20 +00:00
|
|
|
// be entered.)
|
2017-10-12 23:38:55 +00:00
|
|
|
s.screen_mut().add_layer_at(
|
|
|
|
Position::absolute(offset),
|
|
|
|
OnEventView::new(
|
|
|
|
MenuPopup::new(menu)
|
2019-03-04 18:31:36 +00:00
|
|
|
.on_dismiss(Cursive::select_menubar)
|
2017-10-12 23:38:55 +00:00
|
|
|
.on_action(|s| s.menubar().state = State::Inactive),
|
2019-03-01 00:04:14 +00:00
|
|
|
)
|
|
|
|
.on_event(Key::Right, |s| {
|
2017-06-12 23:39:12 +00:00
|
|
|
s.pop_layer();
|
|
|
|
s.select_menubar();
|
|
|
|
// Act as if we sent "Right" then "Down"
|
2018-05-18 00:37:39 +00:00
|
|
|
s.menubar().on_event(Event::Key(Key::Right)).process(s);
|
2017-06-12 23:39:12 +00:00
|
|
|
if let EventResult::Consumed(Some(cb)) =
|
2017-10-12 23:38:55 +00:00
|
|
|
s.menubar().on_event(Event::Key(Key::Down))
|
|
|
|
{
|
2017-06-12 23:39:12 +00:00
|
|
|
cb(s);
|
|
|
|
}
|
2019-03-01 00:04:14 +00:00
|
|
|
})
|
|
|
|
.on_event(Key::Left, |s| {
|
2018-08-22 20:33:29 +00:00
|
|
|
s.pop_layer();
|
|
|
|
s.select_menubar();
|
|
|
|
// Act as if we sent "Left" then "Down"
|
|
|
|
s.menubar().on_event(Event::Key(Key::Left)).process(s);
|
|
|
|
if let EventResult::Consumed(Some(cb)) =
|
|
|
|
s.menubar().on_event(Event::Key(Key::Down))
|
|
|
|
{
|
|
|
|
cb(s);
|
|
|
|
}
|
|
|
|
}),
|
2017-10-12 23:38:55 +00:00
|
|
|
);
|
2016-07-20 03:44:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl View for Menubar {
|
2019-02-28 23:55:02 +00:00
|
|
|
fn draw(&self, printer: &Printer<'_, '_>) {
|
2016-06-28 04:59:42 +00:00
|
|
|
// Draw the bar at the top
|
2018-01-17 17:35:57 +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;
|
2018-01-09 13:37:40 +00:00
|
|
|
for (i, item) in self.root.children.iter().enumerate() {
|
|
|
|
let title = item.label();
|
|
|
|
|
2016-07-02 22:02:42 +00:00
|
|
|
// We don't want to show HighlightInactive when we're not selected,
|
|
|
|
// because it's ugly on the menubar.
|
2017-10-12 23:38:55 +00:00
|
|
|
let selected =
|
|
|
|
(self.state != State::Inactive) && (i == self.focus);
|
2016-07-02 22:02:42 +00:00
|
|
|
printer.with_selection(selected, |printer| {
|
|
|
|
printer.print((offset, 0), &format!(" {} ", title));
|
|
|
|
});
|
2017-10-12 23:42:41 +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 {
|
2017-10-13 22:17:41 +00:00
|
|
|
Event::Key(Key::Esc) => {
|
2016-10-09 22:47:06 +00:00
|
|
|
self.hide();
|
2019-03-04 18:31:36 +00:00
|
|
|
return EventResult::with_cb(Cursive::clear);
|
2016-10-09 22:47:06 +00:00
|
|
|
}
|
2018-01-09 13:37:40 +00:00
|
|
|
Event::Key(Key::Left) => loop {
|
|
|
|
if self.focus > 0 {
|
|
|
|
self.focus -= 1;
|
|
|
|
} else {
|
|
|
|
self.focus = self.root.len() - 1;
|
|
|
|
}
|
|
|
|
if !self.root.children[self.focus].is_delimiter() {
|
|
|
|
break;
|
|
|
|
}
|
2017-10-12 23:38:55 +00:00
|
|
|
},
|
2018-01-09 13:37:40 +00:00
|
|
|
Event::Key(Key::Right) => loop {
|
|
|
|
if self.focus + 1 < self.root.len() {
|
|
|
|
self.focus += 1;
|
|
|
|
} else {
|
|
|
|
self.focus = 0;
|
|
|
|
}
|
|
|
|
if !self.root.children[self.focus].is_delimiter() {
|
|
|
|
break;
|
|
|
|
}
|
2017-10-12 23:38:55 +00:00
|
|
|
},
|
2018-01-09 13:37:40 +00:00
|
|
|
Event::Key(Key::Down) => {
|
|
|
|
return self.select_child(true);
|
|
|
|
}
|
|
|
|
Event::Key(Key::Enter) => {
|
|
|
|
return self.select_child(false);
|
2017-10-12 23:42:41 +00:00
|
|
|
}
|
|
|
|
Event::Mouse {
|
2017-10-13 22:22:59 +00:00
|
|
|
event: MouseEvent::Press(btn),
|
2017-10-12 23:42:41 +00:00
|
|
|
position,
|
|
|
|
offset,
|
2019-03-01 00:04:14 +00:00
|
|
|
} if position.fits(offset) && position.y == offset.y => {
|
2017-10-13 22:22:59 +00:00
|
|
|
if let Some(child) = position
|
2017-10-12 23:42:41 +00:00
|
|
|
.checked_sub(offset)
|
|
|
|
.and_then(|pos| self.child_at(pos.x))
|
2017-10-13 22:22:59 +00:00
|
|
|
{
|
2018-01-09 13:53:55 +00:00
|
|
|
if !self.root.children[child].is_delimiter() {
|
|
|
|
self.focus = child;
|
|
|
|
if btn == MouseButton::Left {
|
|
|
|
return self.select_child(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Event::Mouse {
|
|
|
|
event: MouseEvent::Release(btn),
|
|
|
|
position,
|
|
|
|
offset,
|
2019-03-01 00:04:14 +00:00
|
|
|
} if position.fits(offset) && position.y == offset.y => {
|
2018-01-09 13:53:55 +00:00
|
|
|
if let Some(child) = position
|
|
|
|
.checked_sub(offset)
|
|
|
|
.and_then(|pos| self.child_at(pos.x))
|
|
|
|
{
|
2018-06-11 06:29:10 +00:00
|
|
|
if self.focus == child
|
|
|
|
&& btn == MouseButton::Left
|
2018-01-09 14:20:07 +00:00
|
|
|
&& self.root.children[child].is_leaf()
|
|
|
|
{
|
|
|
|
return self.select_child(false);
|
2017-10-13 22:22:59 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-12 23:42:41 +00:00
|
|
|
}
|
|
|
|
Event::Mouse {
|
|
|
|
event: MouseEvent::Press(_),
|
2017-10-13 18:22:02 +00:00
|
|
|
..
|
2017-10-12 23:42:41 +00:00
|
|
|
} => {
|
|
|
|
self.hide();
|
2019-03-04 18:31:36 +00:00
|
|
|
return EventResult::with_cb(Cursive::clear);
|
2017-10-12 23:42:41 +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
|
|
|
|
}
|
|
|
|
|
2017-01-24 06:52:29 +00:00
|
|
|
fn required_size(&mut self, _: Vec2) -> Vec2 {
|
2016-07-20 03:44:20 +00:00
|
|
|
// 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)
|
2018-06-11 06:29:10 +00:00
|
|
|
let width = self
|
|
|
|
.root
|
2018-01-09 13:37:40 +00:00
|
|
|
.children
|
2016-07-20 03:44:20 +00:00
|
|
|
.iter()
|
2018-01-09 13:37:40 +00:00
|
|
|
.map(|item| item.label().len() + 2)
|
|
|
|
.sum();
|
2016-07-20 03:44:20 +00:00
|
|
|
|
|
|
|
Vec2::new(width, 1)
|
|
|
|
}
|
2018-03-22 22:13:51 +00:00
|
|
|
|
|
|
|
fn important_area(&self, _: Vec2) -> Rect {
|
|
|
|
if self.root.is_empty() {
|
|
|
|
return Rect::from((0, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
// X position is 1 (margin before the first item) + sum of widths
|
|
|
|
// And each item has a 2 cells padding.
|
|
|
|
let x = 1 + self.root.children[..self.focus]
|
|
|
|
.iter()
|
|
|
|
.map(|child| child.label().width() + 2)
|
|
|
|
.sum::<usize>();
|
|
|
|
|
|
|
|
let width = self.root.children[self.focus].label().width();
|
|
|
|
|
|
|
|
Rect::from_size((x, 0), (width, 1))
|
|
|
|
}
|
2016-07-10 02:05:51 +00:00
|
|
|
}
|