2017-10-12 23:33:24 +00:00
|
|
|
use backend;
|
2017-10-12 23:38:55 +00:00
|
|
|
use direction;
|
2017-10-12 23:33:24 +00:00
|
|
|
use event::{Callback, Event, EventResult};
|
2017-10-12 23:38:55 +00:00
|
|
|
use printer::Printer;
|
2017-10-12 23:33:24 +00:00
|
|
|
use std::any::Any;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::path::Path;
|
|
|
|
use std::sync::mpsc;
|
|
|
|
use theme;
|
|
|
|
use vec::Vec2;
|
2018-03-14 21:59:41 +00:00
|
|
|
use view::{self, Finder, IntoBoxedView, Position, View};
|
2018-02-08 00:25:00 +00:00
|
|
|
use views::{self, LayerPosition};
|
2017-10-12 23:33:24 +00:00
|
|
|
|
|
|
|
/// Identifies a screen in the cursive root.
|
|
|
|
pub type ScreenId = usize;
|
|
|
|
|
2018-02-11 19:01:23 +00:00
|
|
|
/// Asynchronous callback function trait.
|
|
|
|
///
|
|
|
|
/// Every `FnOnce(&mut Cursive) -> () + Send` automatically
|
|
|
|
/// implements this.
|
|
|
|
///
|
|
|
|
/// This is a workaround only because `Box<FnOnce()>` is not
|
|
|
|
/// working and `FnBox` is unstable.
|
|
|
|
pub trait CbFunc: Send {
|
|
|
|
/// Calls the function.
|
|
|
|
fn call_box(self: Box<Self>, &mut Cursive);
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F: FnOnce(&mut Cursive) -> () + Send> CbFunc for F {
|
|
|
|
fn call_box(self: Box<Self>, siv: &mut Cursive) {
|
|
|
|
(*self)(siv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-01 23:39:03 +00:00
|
|
|
#[cfg(feature = "termion")]
|
|
|
|
impl Default for Cursive {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::termion()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(all(not(feature = "termion"), feature = "pancurses"))]
|
|
|
|
impl Default for Cursive {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::pancurses()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(all(not(feature = "termion"), not(feature = "pancurses"), feature = "bear-lib-terminal"))]
|
|
|
|
impl Default for Cursive {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::blt()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(all(not(feature = "termion"), not(feature = "pancurses"), not(feature = "bear-lib-terminal"), feature = "ncurses"))]
|
|
|
|
impl Default for Cursive {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::ncurses()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-12 23:33:24 +00:00
|
|
|
/// Central part of the cursive library.
|
|
|
|
///
|
|
|
|
/// It initializes ncurses on creation and cleans up on drop.
|
|
|
|
/// To use it, you should populate it with views, layouts and callbacks,
|
|
|
|
/// then start the event loop with run().
|
|
|
|
///
|
|
|
|
/// It uses a list of screen, with one screen active at a time.
|
|
|
|
pub struct Cursive {
|
|
|
|
theme: theme::Theme,
|
|
|
|
screens: Vec<views::StackView>,
|
2018-01-11 17:47:44 +00:00
|
|
|
global_callbacks: HashMap<Event, Vec<Callback>>,
|
2017-10-12 23:33:24 +00:00
|
|
|
menubar: views::Menubar,
|
|
|
|
|
|
|
|
// Last layer sizes of the stack view.
|
|
|
|
// If it changed, clear the screen.
|
|
|
|
last_sizes: Vec<Vec2>,
|
|
|
|
|
|
|
|
active_screen: ScreenId,
|
|
|
|
|
|
|
|
running: bool,
|
|
|
|
|
2018-04-01 22:35:37 +00:00
|
|
|
backend: Box<backend::Backend>,
|
2017-10-12 23:33:24 +00:00
|
|
|
|
2018-02-11 19:01:23 +00:00
|
|
|
cb_source: mpsc::Receiver<Box<CbFunc>>,
|
|
|
|
cb_sink: mpsc::Sender<Box<CbFunc>>,
|
2017-10-12 23:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Cursive {
|
|
|
|
/// Creates a new Cursive root, and initialize the back-end.
|
2018-04-01 23:39:03 +00:00
|
|
|
pub fn new(backend: Box<backend::Backend>) -> Self {
|
2017-10-12 23:33:24 +00:00
|
|
|
let theme = theme::load_default();
|
|
|
|
// theme.activate(&mut backend);
|
|
|
|
// let theme = theme::load_theme("assets/style.toml").unwrap();
|
|
|
|
|
|
|
|
let (tx, rx) = mpsc::channel();
|
|
|
|
|
2018-01-11 17:20:23 +00:00
|
|
|
Cursive {
|
2017-10-12 23:33:24 +00:00
|
|
|
theme: theme,
|
2018-01-11 17:20:23 +00:00
|
|
|
screens: vec![views::StackView::new()],
|
2017-10-12 23:33:24 +00:00
|
|
|
last_sizes: Vec::new(),
|
|
|
|
global_callbacks: HashMap::new(),
|
|
|
|
menubar: views::Menubar::new(),
|
|
|
|
active_screen: 0,
|
|
|
|
running: true,
|
|
|
|
cb_source: rx,
|
|
|
|
cb_sink: tx,
|
|
|
|
backend: backend,
|
2018-01-11 17:20:23 +00:00
|
|
|
}
|
2017-10-12 23:33:24 +00:00
|
|
|
}
|
|
|
|
|
2018-04-01 23:39:03 +00:00
|
|
|
/// Creates a new Cursive root using a ncurses backend.
|
|
|
|
#[cfg(feature = "ncurses")]
|
|
|
|
pub fn ncurses() -> Self {
|
|
|
|
Self::new(backend::curses::n::Backend::init())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new Cursive root using a pancurses backend.
|
|
|
|
#[cfg(feature = "pancurses")]
|
|
|
|
pub fn pancurses() -> Self {
|
|
|
|
Self::new(backend::curses::pan::Backend::init())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new Cursive root using a termion backend.
|
|
|
|
#[cfg(feature = "termion")]
|
|
|
|
pub fn termion() -> Self {
|
|
|
|
Self::new(backend::termion::Backend::init())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new Cursive root using a bear-lib-terminal backend.
|
|
|
|
#[cfg(feature = "bear-lib-terminal")]
|
|
|
|
pub fn blt() -> Self {
|
|
|
|
Self::new(backend::blt::Backend::init())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new Cursive root using a dummy backend.
|
|
|
|
///
|
|
|
|
/// Nothing will be output. This is mostly here for tests.
|
|
|
|
pub fn dummy() -> Self {
|
|
|
|
Self::new(backend::dummy::Backend::init())
|
|
|
|
}
|
|
|
|
|
2017-10-12 23:33:24 +00:00
|
|
|
/// Returns a sink for asynchronous callbacks.
|
|
|
|
///
|
|
|
|
/// Returns the sender part of a channel, that allows to send
|
|
|
|
/// callbacks to `self` from other threads.
|
|
|
|
///
|
|
|
|
/// Callbacks will be executed in the order
|
|
|
|
/// of arrival on the next event cycle.
|
|
|
|
///
|
|
|
|
/// Note that you currently need to call [`set_fps`] to force cursive to
|
|
|
|
/// regularly check for messages.
|
|
|
|
///
|
2018-02-11 19:01:23 +00:00
|
|
|
/// # Examples
|
|
|
|
///
|
2018-04-01 23:39:03 +00:00
|
|
|
/// ```rust
|
2018-02-11 19:01:23 +00:00
|
|
|
/// # extern crate cursive;
|
|
|
|
/// # use cursive::*;
|
|
|
|
/// # fn main() {
|
2018-04-01 23:39:03 +00:00
|
|
|
/// let mut siv = Cursive::dummy();
|
2018-02-11 19:01:23 +00:00
|
|
|
/// siv.set_fps(10);
|
|
|
|
///
|
|
|
|
/// // quit() will be called during the next event cycle
|
|
|
|
/// siv.cb_sink().send(Box::new(|s: &mut Cursive| s.quit()));
|
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
///
|
2017-10-12 23:33:24 +00:00
|
|
|
/// [`set_fps`]: #method.set_fps
|
2018-02-11 19:01:23 +00:00
|
|
|
pub fn cb_sink(&self) -> &mpsc::Sender<Box<CbFunc>> {
|
2017-10-12 23:33:24 +00:00
|
|
|
&self.cb_sink
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Selects the menubar.
|
|
|
|
pub fn select_menubar(&mut self) {
|
2018-03-22 18:04:10 +00:00
|
|
|
self.menubar
|
|
|
|
.take_focus(direction::Direction::none());
|
2017-10-12 23:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the menubar autohide feature.
|
|
|
|
///
|
|
|
|
/// * When enabled (default), the menu is only visible when selected.
|
|
|
|
/// * When disabled, the menu is always visible and reserves the top row.
|
|
|
|
pub fn set_autohide_menu(&mut self, autohide: bool) {
|
|
|
|
self.menubar.autohide = autohide;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Access the menu tree used by the menubar.
|
|
|
|
///
|
|
|
|
/// This allows to add menu items to the menubar.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2018-04-01 23:39:03 +00:00
|
|
|
/// ```rust
|
2017-10-12 23:33:24 +00:00
|
|
|
/// # extern crate cursive;
|
|
|
|
/// #
|
|
|
|
/// # use cursive::{Cursive, event};
|
|
|
|
/// # use cursive::views::{Dialog};
|
|
|
|
/// # use cursive::traits::*;
|
|
|
|
/// # use cursive::menu::*;
|
|
|
|
/// #
|
|
|
|
/// # fn main() {
|
2018-04-01 23:39:03 +00:00
|
|
|
/// let mut siv = Cursive::dummy();
|
2017-10-12 23:33:24 +00:00
|
|
|
///
|
|
|
|
/// siv.menubar()
|
|
|
|
/// .add_subtree("File",
|
|
|
|
/// MenuTree::new()
|
|
|
|
/// .leaf("New", |s| s.add_layer(Dialog::info("New file!")))
|
|
|
|
/// .subtree("Recent", MenuTree::new().with(|tree| {
|
|
|
|
/// for i in 1..100 {
|
|
|
|
/// tree.add_leaf(format!("Item {}", i), |_| ())
|
|
|
|
/// }
|
|
|
|
/// }))
|
|
|
|
/// .delimiter()
|
|
|
|
/// .with(|tree| {
|
|
|
|
/// for i in 1..10 {
|
|
|
|
/// tree.add_leaf(format!("Option {}", i), |_| ());
|
|
|
|
/// }
|
|
|
|
/// })
|
|
|
|
/// .delimiter()
|
|
|
|
/// .leaf("Quit", |s| s.quit()))
|
|
|
|
/// .add_subtree("Help",
|
|
|
|
/// MenuTree::new()
|
|
|
|
/// .subtree("Help",
|
|
|
|
/// MenuTree::new()
|
|
|
|
/// .leaf("General", |s| {
|
|
|
|
/// s.add_layer(Dialog::info("Help message!"))
|
|
|
|
/// })
|
|
|
|
/// .leaf("Online", |s| {
|
|
|
|
/// s.add_layer(Dialog::info("Online help?"))
|
|
|
|
/// }))
|
|
|
|
/// .leaf("About",
|
|
|
|
/// |s| s.add_layer(Dialog::info("Cursive v0.0.0"))));
|
|
|
|
///
|
|
|
|
/// siv.add_global_callback(event::Key::Esc, |s| s.select_menubar());
|
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
pub fn menubar(&mut self) -> &mut views::Menubar {
|
|
|
|
&mut self.menubar
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the currently used theme.
|
|
|
|
pub fn current_theme(&self) -> &theme::Theme {
|
|
|
|
&self.theme
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the current theme.
|
|
|
|
pub fn set_theme(&mut self, theme: theme::Theme) {
|
|
|
|
self.theme = theme;
|
|
|
|
self.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Clears the screen.
|
|
|
|
///
|
|
|
|
/// Users rarely have to call this directly.
|
|
|
|
pub fn clear(&self) {
|
2018-01-17 17:35:57 +00:00
|
|
|
self.backend
|
2018-01-21 19:06:33 +00:00
|
|
|
.clear(self.theme.palette[theme::PaletteColor::Background]);
|
2017-10-12 23:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Loads a theme from the given file.
|
|
|
|
///
|
|
|
|
/// `filename` must point to a valid toml file.
|
|
|
|
pub fn load_theme_file<P: AsRef<Path>>(
|
2018-01-22 22:37:27 +00:00
|
|
|
&mut self, filename: P
|
2017-10-12 23:33:24 +00:00
|
|
|
) -> Result<(), theme::Error> {
|
|
|
|
self.set_theme(try!(theme::load_theme_file(filename)));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Loads a theme from the given string content.
|
|
|
|
///
|
|
|
|
/// Content must be valid toml.
|
|
|
|
pub fn load_theme(&mut self, content: &str) -> Result<(), theme::Error> {
|
|
|
|
self.set_theme(try!(theme::load_theme(content)));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the refresh rate, in frames per second.
|
|
|
|
///
|
|
|
|
/// Regularly redraws everything, even when no input is given.
|
|
|
|
///
|
|
|
|
/// You currently need this to regularly check
|
|
|
|
/// for events sent using [`cb_sink`].
|
|
|
|
///
|
|
|
|
/// Between 0 and 1000. Call with `fps = 0` to disable (default value).
|
|
|
|
///
|
|
|
|
/// [`cb_sink`]: #method.cb_sink
|
|
|
|
pub fn set_fps(&mut self, fps: u32) {
|
|
|
|
self.backend.set_refresh_rate(fps)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a reference to the currently active screen.
|
|
|
|
pub fn screen(&self) -> &views::StackView {
|
|
|
|
let id = self.active_screen;
|
|
|
|
&self.screens[id]
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a mutable reference to the currently active screen.
|
|
|
|
pub fn screen_mut(&mut self) -> &mut views::StackView {
|
|
|
|
let id = self.active_screen;
|
|
|
|
&mut self.screens[id]
|
|
|
|
}
|
|
|
|
|
2018-01-11 17:20:23 +00:00
|
|
|
/// Returns the id of the currently active screen.
|
|
|
|
pub fn active_screen(&self) -> ScreenId {
|
|
|
|
self.active_screen
|
|
|
|
}
|
|
|
|
|
2017-10-12 23:33:24 +00:00
|
|
|
/// Adds a new screen, and returns its ID.
|
|
|
|
pub fn add_screen(&mut self) -> ScreenId {
|
|
|
|
let res = self.screens.len();
|
|
|
|
self.screens.push(views::StackView::new());
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convenient method to create a new screen, and set it as active.
|
|
|
|
pub fn add_active_screen(&mut self) -> ScreenId {
|
|
|
|
let res = self.add_screen();
|
|
|
|
self.set_screen(res);
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the active screen. Panics if no such screen exist.
|
|
|
|
pub fn set_screen(&mut self, screen_id: ScreenId) {
|
|
|
|
if screen_id >= self.screens.len() {
|
|
|
|
panic!(
|
|
|
|
"Tried to set an invalid screen ID: {}, but only {} \
|
|
|
|
screens present.",
|
|
|
|
screen_id,
|
|
|
|
self.screens.len()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
self.active_screen = screen_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Tries to find the view pointed to by the given selector.
|
|
|
|
///
|
|
|
|
/// Runs a closure on the view once it's found, and return the
|
|
|
|
/// result.
|
|
|
|
///
|
|
|
|
/// If the view is not found, or if it is not of the asked type,
|
|
|
|
/// returns None.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2018-04-01 23:39:03 +00:00
|
|
|
/// ```rust
|
2017-10-12 23:33:24 +00:00
|
|
|
/// # extern crate cursive;
|
|
|
|
/// # use cursive::{Cursive, views, view};
|
|
|
|
/// # use cursive::traits::*;
|
|
|
|
/// # fn main() {
|
2017-10-15 04:18:50 +00:00
|
|
|
/// fn main() {
|
2018-04-01 23:39:03 +00:00
|
|
|
/// let mut siv = Cursive::dummy();
|
2017-10-15 04:18:50 +00:00
|
|
|
///
|
|
|
|
/// siv.add_layer(views::TextView::new("Text #1").with_id("text"));
|
|
|
|
///
|
|
|
|
/// siv.add_global_callback('p', |s| {
|
|
|
|
/// s.call_on(
|
|
|
|
/// &view::Selector::Id("text"),
|
|
|
|
/// |view: &mut views::TextView| {
|
|
|
|
/// view.set_content("Text #2");
|
|
|
|
/// },
|
|
|
|
/// );
|
2017-10-12 23:33:24 +00:00
|
|
|
/// });
|
2017-10-15 04:18:50 +00:00
|
|
|
///
|
|
|
|
/// }
|
2017-10-12 23:33:24 +00:00
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
pub fn call_on<V, F, R>(
|
2018-01-22 22:37:27 +00:00
|
|
|
&mut self, sel: &view::Selector, callback: F
|
2017-10-12 23:33:24 +00:00
|
|
|
) -> Option<R>
|
|
|
|
where
|
|
|
|
V: View + Any,
|
|
|
|
F: FnOnce(&mut V) -> R,
|
|
|
|
{
|
|
|
|
self.screen_mut().call_on(sel, callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Tries to find the view identified by the given id.
|
|
|
|
///
|
|
|
|
/// Convenient method to use `call_on` with a `view::Selector::Id`.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2018-04-01 23:39:03 +00:00
|
|
|
/// ```rust
|
2017-10-12 23:33:24 +00:00
|
|
|
/// # extern crate cursive;
|
|
|
|
/// # use cursive::{Cursive, views};
|
|
|
|
/// # use cursive::traits::*;
|
|
|
|
/// # fn main() {
|
2018-04-01 23:39:03 +00:00
|
|
|
/// let mut siv = Cursive::dummy();
|
2017-10-12 23:33:24 +00:00
|
|
|
///
|
|
|
|
/// siv.add_layer(views::TextView::new("Text #1")
|
|
|
|
/// .with_id("text"));
|
|
|
|
///
|
|
|
|
/// siv.add_global_callback('p', |s| {
|
|
|
|
/// s.call_on_id("text", |view: &mut views::TextView| {
|
|
|
|
/// view.set_content("Text #2");
|
|
|
|
/// });
|
|
|
|
/// });
|
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
pub fn call_on_id<V, F, R>(&mut self, id: &str, callback: F) -> Option<R>
|
|
|
|
where
|
|
|
|
V: View + Any,
|
|
|
|
F: FnOnce(&mut V) -> R,
|
|
|
|
{
|
|
|
|
self.call_on(&view::Selector::Id(id), callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convenient method to find a view wrapped in [`IdView`].
|
|
|
|
///
|
|
|
|
/// This looks for a `IdView<V>` with the given ID, and return
|
2018-01-09 08:26:05 +00:00
|
|
|
/// a [`ViewRef`] to the wrapped view. The `ViewRef` implements
|
|
|
|
/// `DerefMut<Target=T>`, so you can treat it just like a `&mut T`.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2018-04-01 23:39:03 +00:00
|
|
|
/// ```rust
|
2018-01-09 08:26:05 +00:00
|
|
|
/// # use cursive::Cursive;
|
2018-01-09 09:10:05 +00:00
|
|
|
/// # use cursive::views::{TextView, ViewRef};
|
2018-04-01 23:39:03 +00:00
|
|
|
/// # let mut siv = Cursive::dummy();
|
2018-01-09 09:10:05 +00:00
|
|
|
/// use cursive::traits::Identifiable;
|
2018-01-09 08:26:05 +00:00
|
|
|
///
|
|
|
|
/// siv.add_layer(TextView::new("foo").with_id("id"));
|
|
|
|
///
|
|
|
|
/// // Could be called in a callback
|
2018-01-09 09:10:05 +00:00
|
|
|
/// let mut view: ViewRef<TextView> = siv.find_id("id").unwrap();
|
|
|
|
/// view.set_content("bar");
|
2018-01-09 08:26:05 +00:00
|
|
|
/// ```
|
2017-10-12 23:33:24 +00:00
|
|
|
///
|
|
|
|
/// [`IdView`]: views/struct.IdView.html
|
2018-01-09 08:26:05 +00:00
|
|
|
/// [`ViewRef`]: views/type.ViewRef.html
|
2017-10-12 23:33:24 +00:00
|
|
|
pub fn find_id<V>(&mut self, id: &str) -> Option<views::ViewRef<V>>
|
|
|
|
where
|
|
|
|
V: View + Any,
|
|
|
|
{
|
|
|
|
self.call_on_id(id, views::IdView::<V>::get_mut)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Moves the focus to the view identified by `id`.
|
|
|
|
///
|
|
|
|
/// Convenient method to call `focus` with a `view::Selector::Id`.
|
|
|
|
pub fn focus_id(&mut self, id: &str) -> Result<(), ()> {
|
|
|
|
self.focus(&view::Selector::Id(id))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Moves the focus to the view identified by `sel`.
|
|
|
|
pub fn focus(&mut self, sel: &view::Selector) -> Result<(), ()> {
|
|
|
|
self.screen_mut().focus_view(sel)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds a global callback.
|
|
|
|
///
|
|
|
|
/// Will be triggered on the given key press when no view catches it.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2018-04-01 23:39:03 +00:00
|
|
|
/// ```rust
|
2017-10-12 23:33:24 +00:00
|
|
|
/// # extern crate cursive;
|
|
|
|
/// # use cursive::*;
|
|
|
|
/// # fn main() {
|
2018-04-01 23:39:03 +00:00
|
|
|
/// let mut siv = Cursive::dummy();
|
2017-10-12 23:33:24 +00:00
|
|
|
///
|
|
|
|
/// siv.add_global_callback('q', |s| s.quit());
|
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
pub fn add_global_callback<F, E: Into<Event>>(&mut self, event: E, cb: F)
|
|
|
|
where
|
|
|
|
F: Fn(&mut Cursive) + 'static,
|
|
|
|
{
|
|
|
|
self.global_callbacks
|
2018-01-11 17:47:44 +00:00
|
|
|
.entry(event.into())
|
2018-01-22 22:47:56 +00:00
|
|
|
.or_insert_with(Vec::new)
|
2018-01-11 17:47:44 +00:00
|
|
|
.push(Callback::from_fn(cb));
|
2017-10-12 23:33:24 +00:00
|
|
|
}
|
|
|
|
|
2018-03-05 23:24:40 +00:00
|
|
|
/// Removes any callback tied to the given event.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2018-04-01 23:39:03 +00:00
|
|
|
/// ```rust
|
2018-03-05 23:24:40 +00:00
|
|
|
/// # extern crate cursive;
|
|
|
|
/// # use cursive::*;
|
|
|
|
/// # fn main() {
|
2018-04-01 23:39:03 +00:00
|
|
|
/// let mut siv = Cursive::dummy();
|
2018-03-05 23:24:40 +00:00
|
|
|
///
|
|
|
|
/// siv.add_global_callback('q', |s| s.quit());
|
|
|
|
/// siv.clear_global_callbacks('q');
|
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
pub fn clear_global_callbacks<E>(&mut self, event: E)
|
|
|
|
where
|
|
|
|
E: Into<Event>,
|
|
|
|
{
|
|
|
|
let event = event.into();
|
|
|
|
self.global_callbacks.remove(&event);
|
|
|
|
}
|
|
|
|
|
2017-10-12 23:33:24 +00:00
|
|
|
/// Add a layer to the current screen.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2018-04-01 23:39:03 +00:00
|
|
|
/// ```rust
|
2017-10-12 23:33:24 +00:00
|
|
|
/// # extern crate cursive;
|
|
|
|
/// # use cursive::*;
|
|
|
|
/// # fn main() {
|
2018-04-01 23:39:03 +00:00
|
|
|
/// let mut siv = Cursive::dummy();
|
2017-10-12 23:33:24 +00:00
|
|
|
///
|
|
|
|
/// siv.add_layer(views::TextView::new("Hello world!"));
|
|
|
|
/// # }
|
|
|
|
/// ```
|
2018-03-14 20:39:30 +00:00
|
|
|
pub fn add_layer<T>(&mut self, view: T)
|
|
|
|
where
|
2018-03-14 20:58:57 +00:00
|
|
|
T: IntoBoxedView,
|
2018-03-14 20:39:30 +00:00
|
|
|
{
|
2017-10-12 23:33:24 +00:00
|
|
|
self.screen_mut().add_layer(view);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds a new full-screen layer to the current screen.
|
|
|
|
///
|
|
|
|
/// Fullscreen layers have no shadow.
|
|
|
|
pub fn add_fullscreen_layer<T>(&mut self, view: T)
|
|
|
|
where
|
2018-03-14 20:58:57 +00:00
|
|
|
T: IntoBoxedView,
|
2017-10-12 23:33:24 +00:00
|
|
|
{
|
|
|
|
self.screen_mut().add_fullscreen_layer(view);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convenient method to remove a layer from the current screen.
|
2018-03-14 21:59:41 +00:00
|
|
|
pub fn pop_layer(&mut self) -> Option<Box<View>> {
|
2018-02-08 00:25:00 +00:00
|
|
|
self.screen_mut().pop_layer()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convenient stub forwarding layer repositioning.
|
|
|
|
pub fn reposition_layer(
|
|
|
|
&mut self, layer: LayerPosition, position: Position
|
|
|
|
) {
|
2018-03-22 18:04:10 +00:00
|
|
|
self.screen_mut()
|
|
|
|
.reposition_layer(layer, position);
|
2017-10-12 23:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handles a key event when it was ignored by the current view
|
|
|
|
fn on_event(&mut self, event: Event) {
|
2018-01-11 17:47:44 +00:00
|
|
|
let cb_list = match self.global_callbacks.get(&event) {
|
2017-10-12 23:33:24 +00:00
|
|
|
None => return,
|
2018-01-11 17:47:44 +00:00
|
|
|
Some(cb_list) => cb_list.clone(),
|
2017-10-12 23:33:24 +00:00
|
|
|
};
|
|
|
|
// Not from a view, so no viewpath here
|
2018-01-11 17:47:44 +00:00
|
|
|
for cb in cb_list {
|
|
|
|
cb(self);
|
|
|
|
}
|
2017-10-12 23:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the size of the screen, in characters.
|
|
|
|
pub fn screen_size(&self) -> Vec2 {
|
2018-04-03 01:08:12 +00:00
|
|
|
self.backend.screen_size()
|
2017-10-12 23:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn layout(&mut self) {
|
|
|
|
let size = self.screen_size();
|
2018-03-22 18:04:10 +00:00
|
|
|
let offset = if self.menubar.autohide {
|
|
|
|
0
|
|
|
|
} else {
|
|
|
|
1
|
|
|
|
};
|
2017-10-13 22:17:41 +00:00
|
|
|
let size = size.saturating_sub((0, offset));
|
2017-10-12 23:33:24 +00:00
|
|
|
self.screen_mut().layout(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn draw(&mut self) {
|
|
|
|
let sizes = self.screen().layer_sizes();
|
|
|
|
if self.last_sizes != sizes {
|
|
|
|
self.clear();
|
|
|
|
self.last_sizes = sizes;
|
|
|
|
}
|
|
|
|
|
|
|
|
let printer =
|
2018-04-10 18:53:25 +00:00
|
|
|
Printer::new(self.screen_size(), &self.theme, &*self.backend);
|
2017-10-12 23:33:24 +00:00
|
|
|
|
2018-02-08 00:25:00 +00:00
|
|
|
let selected = self.menubar.receive_events();
|
|
|
|
|
|
|
|
// Print the stackview background before the menubar
|
2018-03-22 18:04:10 +00:00
|
|
|
let offset = if self.menubar.autohide {
|
|
|
|
0
|
|
|
|
} else {
|
|
|
|
1
|
|
|
|
};
|
2018-02-08 00:25:00 +00:00
|
|
|
let id = self.active_screen;
|
2018-04-03 23:38:54 +00:00
|
|
|
let sv_printer = printer.offset((0, offset)).focused(!selected);
|
2018-02-08 00:25:00 +00:00
|
|
|
|
|
|
|
self.screens[id].draw_bg(&sv_printer);
|
|
|
|
|
2017-10-12 23:33:24 +00:00
|
|
|
// Draw the currently active screen
|
|
|
|
// If the menubar is active, nothing else can be.
|
|
|
|
// Draw the menubar?
|
|
|
|
if self.menubar.visible() {
|
2018-04-03 23:38:54 +00:00
|
|
|
let printer = printer.focused(self.menubar.receive_events());
|
2017-10-12 23:33:24 +00:00
|
|
|
self.menubar.draw(&printer);
|
|
|
|
}
|
|
|
|
|
2018-02-08 00:25:00 +00:00
|
|
|
// finally draw stackview layers
|
|
|
|
// using variables from above
|
|
|
|
self.screens[id].draw_fg(&sv_printer);
|
2017-10-12 23:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` until [`quit(&mut self)`] is called.
|
|
|
|
///
|
|
|
|
/// [`quit(&mut self)`]: #method.quit
|
|
|
|
pub fn is_running(&self) -> bool {
|
|
|
|
self.running
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Runs the event loop.
|
|
|
|
///
|
|
|
|
/// It will wait for user input (key presses)
|
|
|
|
/// and trigger callbacks accordingly.
|
|
|
|
///
|
|
|
|
/// Calls [`step(&mut self)`] until [`quit(&mut self)`] is called.
|
|
|
|
///
|
|
|
|
/// After this function returns, you can call
|
|
|
|
/// it again and it will start a new loop.
|
|
|
|
///
|
|
|
|
/// [`step(&mut self)`]: #method.step
|
|
|
|
/// [`quit(&mut self)`]: #method.quit
|
|
|
|
pub fn run(&mut self) {
|
|
|
|
self.running = true;
|
|
|
|
|
|
|
|
// And the big event loop begins!
|
|
|
|
while self.running {
|
|
|
|
self.step();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Performs a single step from the event loop.
|
|
|
|
///
|
|
|
|
/// Useful if you need tighter control on the event loop.
|
|
|
|
/// Otherwise, [`run(&mut self)`] might be more convenient.
|
|
|
|
///
|
|
|
|
/// [`run(&mut self)`]: #method.run
|
|
|
|
pub fn step(&mut self) {
|
2018-02-11 19:01:14 +00:00
|
|
|
while let Ok(cb) = self.cb_source.try_recv() {
|
2018-02-11 19:01:23 +00:00
|
|
|
cb.call_box(self);
|
2017-10-12 23:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Do we need to redraw everytime?
|
|
|
|
// Probably, actually.
|
|
|
|
// TODO: Do we need to re-layout everytime?
|
|
|
|
self.layout();
|
|
|
|
|
|
|
|
// TODO: Do we need to redraw every view every time?
|
|
|
|
// (Is this getting repetitive? :p)
|
|
|
|
self.draw();
|
|
|
|
self.backend.refresh();
|
|
|
|
|
|
|
|
// Wait for next event.
|
|
|
|
// (If set_fps was called, this returns -1 now and then)
|
|
|
|
let event = self.backend.poll_event();
|
|
|
|
if event == Event::Exit {
|
|
|
|
self.quit();
|
|
|
|
}
|
|
|
|
|
|
|
|
if event == Event::WindowResize {
|
|
|
|
self.clear();
|
|
|
|
}
|
|
|
|
|
2017-10-13 22:17:41 +00:00
|
|
|
if let Event::Mouse {
|
2018-03-22 18:04:10 +00:00
|
|
|
event,
|
|
|
|
position,
|
|
|
|
..
|
2017-10-13 22:17:41 +00:00
|
|
|
} = event
|
|
|
|
{
|
|
|
|
if event.grabs_focus() && !self.menubar.autohide
|
|
|
|
&& !self.menubar.has_submenu()
|
|
|
|
&& position.y == 0
|
|
|
|
{
|
|
|
|
self.select_menubar();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-12 23:33:24 +00:00
|
|
|
// Event dispatch order:
|
|
|
|
// * Focused element:
|
|
|
|
// * Menubar (if active)
|
|
|
|
// * Current screen (top layer)
|
|
|
|
// * Global callbacks
|
|
|
|
if self.menubar.receive_events() {
|
|
|
|
self.menubar.on_event(event).process(self);
|
|
|
|
} else {
|
2018-03-22 18:04:10 +00:00
|
|
|
let offset = if self.menubar.autohide {
|
|
|
|
0
|
|
|
|
} else {
|
|
|
|
1
|
|
|
|
};
|
|
|
|
match self.screen_mut()
|
|
|
|
.on_event(event.relativized((0, offset)))
|
|
|
|
{
|
2017-10-12 23:33:24 +00:00
|
|
|
// If the event was ignored,
|
|
|
|
// it is our turn to play with it.
|
|
|
|
EventResult::Ignored => self.on_event(event),
|
|
|
|
EventResult::Consumed(None) => (),
|
|
|
|
EventResult::Consumed(Some(cb)) => cb(self),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Stops the event loop.
|
|
|
|
pub fn quit(&mut self) {
|
|
|
|
self.running = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Cursive {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.backend.finish();
|
|
|
|
}
|
|
|
|
}
|