2015-05-15 19:16:58 +00:00
|
|
|
//! # Cursive
|
|
|
|
//!
|
2016-07-14 06:18:59 +00:00
|
|
|
//! [Cursive](https://github.com/gyscos/Cursive) is a TUI library built on top
|
|
|
|
//! of ncurses-rs.
|
2015-05-15 19:16:58 +00:00
|
|
|
//! It allows to easily build layouts for text-based applications.
|
|
|
|
//!
|
2016-07-14 06:18:59 +00:00
|
|
|
//! ## Getting started
|
|
|
|
//!
|
|
|
|
//! * Every application should start with a [`Cursive`](struct.Cursive.html)
|
|
|
|
//! object. It is the main entry-point to the library.
|
|
|
|
//! * A declarative phase then describes the structure of the UI by adding
|
|
|
|
//! views and configuring their behaviours.
|
|
|
|
//! * Finally, the event loop is started by calling
|
|
|
|
//! [`Cursive::run(&mut self)`](struct.Cursive.html#method.run).
|
|
|
|
//!
|
2016-07-15 06:25:32 +00:00
|
|
|
//! ## Views
|
|
|
|
//!
|
|
|
|
//! Views are the main components of a cursive interface.
|
2016-12-12 18:19:41 +00:00
|
|
|
//! The [`views`](./views/index.html) module contains many views to use in your
|
2016-07-15 06:25:32 +00:00
|
|
|
//! application; if you don't find what you need, you may also implement the
|
|
|
|
//! [`View`](view/trait.View.html) trait and build your own.
|
|
|
|
//!
|
|
|
|
//! ## Callbacks
|
|
|
|
//!
|
|
|
|
//! Cursive is a *reactive* UI: it *reacts* to events generated by user input.
|
|
|
|
//!
|
|
|
|
//! During the declarative phase, callbacks are set to trigger on specific
|
|
|
|
//! events. These functions usually take a `&mut Cursive` argument, allowing
|
|
|
|
//! them to modify the view tree at will.
|
|
|
|
//!
|
|
|
|
//! ## Examples
|
|
|
|
//!
|
2015-06-04 18:40:35 +00:00
|
|
|
//! ```no_run
|
2015-05-15 19:16:58 +00:00
|
|
|
//! extern crate cursive;
|
|
|
|
//!
|
2016-12-12 18:19:41 +00:00
|
|
|
//! use cursive::Cursive;
|
|
|
|
//! use cursive::views::TextView;
|
2015-05-15 19:16:58 +00:00
|
|
|
//!
|
|
|
|
//! fn main() {
|
|
|
|
//! let mut siv = Cursive::new();
|
|
|
|
//!
|
2016-12-12 18:19:41 +00:00
|
|
|
//! siv.add_layer(TextView::new("Hello World!\nPress q to quit."));
|
2015-05-15 19:16:58 +00:00
|
|
|
//!
|
2015-06-04 18:40:35 +00:00
|
|
|
//! siv.add_global_callback('q', |s| s.quit());
|
2015-05-15 19:16:58 +00:00
|
|
|
//!
|
|
|
|
//! siv.run();
|
|
|
|
//! }
|
|
|
|
//! ```
|
2016-09-25 00:19:09 +00:00
|
|
|
//!
|
|
|
|
//! ## Debugging
|
|
|
|
//!
|
|
|
|
//! The `Cursive` root initializes the terminal on creation, and do cleanups
|
|
|
|
//! on drop. While it is alive, printing to the terminal will not work
|
|
|
|
//! as expected, making debugging a bit harder.
|
|
|
|
//!
|
|
|
|
//! One solution is to redirect stderr to a file when running the application,
|
|
|
|
//! and log to it instead of stdout.
|
2016-12-12 18:19:41 +00:00
|
|
|
//!
|
|
|
|
//! Or you can use gdb as usual.
|
2016-07-12 03:26:33 +00:00
|
|
|
#![deny(missing_docs)]
|
2016-07-11 00:41:49 +00:00
|
|
|
|
2015-05-22 06:29:49 +00:00
|
|
|
extern crate toml;
|
2016-06-26 22:03:12 +00:00
|
|
|
extern crate unicode_segmentation;
|
2016-07-02 08:01:09 +00:00
|
|
|
extern crate unicode_width;
|
2016-08-02 07:32:16 +00:00
|
|
|
extern crate odds;
|
2017-01-11 01:39:21 +00:00
|
|
|
extern crate num;
|
2015-05-09 19:18:25 +00:00
|
|
|
|
2017-03-05 22:45:21 +00:00
|
|
|
#[cfg(feature = "termion")]
|
2016-12-14 06:10:00 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate chan;
|
|
|
|
|
2016-07-10 01:23:58 +00:00
|
|
|
macro_rules! println_stderr(
|
|
|
|
($($arg:tt)*) => { {
|
|
|
|
use ::std::io::Write;
|
|
|
|
let r = writeln!(&mut ::std::io::stderr(), $($arg)*);
|
|
|
|
r.expect("failed printing to stderr");
|
|
|
|
} }
|
|
|
|
);
|
|
|
|
|
2016-07-17 00:28:42 +00:00
|
|
|
macro_rules! new_default(
|
2016-09-25 00:06:33 +00:00
|
|
|
($c:ty) => {
|
2016-07-17 00:28:42 +00:00
|
|
|
impl Default for $c {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-26 06:54:33 +00:00
|
|
|
);
|
2016-07-17 00:28:42 +00:00
|
|
|
|
2016-09-29 05:45:27 +00:00
|
|
|
pub mod traits;
|
2016-07-10 01:23:58 +00:00
|
|
|
|
2015-05-15 00:41:17 +00:00
|
|
|
pub mod event;
|
2016-07-28 23:36:01 +00:00
|
|
|
#[macro_use]
|
2015-05-09 19:18:25 +00:00
|
|
|
pub mod view;
|
2016-07-28 23:36:01 +00:00
|
|
|
|
|
|
|
pub mod views;
|
2015-05-18 18:51:30 +00:00
|
|
|
pub mod vec;
|
2015-06-06 01:08:05 +00:00
|
|
|
pub mod theme;
|
2015-06-02 00:48:29 +00:00
|
|
|
pub mod align;
|
2016-06-28 04:59:42 +00:00
|
|
|
pub mod menu;
|
2016-07-15 03:27:15 +00:00
|
|
|
pub mod direction;
|
2016-07-29 06:05:08 +00:00
|
|
|
pub mod utils;
|
2016-06-28 04:59:42 +00:00
|
|
|
|
|
|
|
// This probably doesn't need to be public?
|
2016-07-14 06:18:59 +00:00
|
|
|
mod printer;
|
2016-07-13 04:01:11 +00:00
|
|
|
mod xy;
|
2016-07-16 20:25:21 +00:00
|
|
|
mod with;
|
2015-05-09 19:18:25 +00:00
|
|
|
|
2015-05-15 00:41:17 +00:00
|
|
|
mod div;
|
2015-05-28 05:13:51 +00:00
|
|
|
mod utf8;
|
2015-05-09 19:18:25 +00:00
|
|
|
|
2016-10-10 20:02:07 +00:00
|
|
|
#[doc(hidden)]
|
|
|
|
pub mod backend;
|
2016-06-30 00:36:20 +00:00
|
|
|
|
2016-07-13 04:01:11 +00:00
|
|
|
|
2016-10-09 22:48:51 +00:00
|
|
|
use backend::Backend;
|
2016-06-30 00:36:20 +00:00
|
|
|
|
2016-10-02 22:22:29 +00:00
|
|
|
use event::{Callback, Event, EventResult};
|
|
|
|
|
|
|
|
pub use printer::Printer;
|
|
|
|
|
2015-05-16 00:56:38 +00:00
|
|
|
use std::any::Any;
|
2015-05-15 01:38:58 +00:00
|
|
|
use std::collections::HashMap;
|
2015-06-06 01:08:05 +00:00
|
|
|
use std::path::Path;
|
2015-05-15 01:38:58 +00:00
|
|
|
|
2016-10-02 22:22:29 +00:00
|
|
|
use std::sync::mpsc;
|
|
|
|
|
2015-05-18 18:51:30 +00:00
|
|
|
use vec::Vec2;
|
2016-10-02 22:22:29 +00:00
|
|
|
use view::Finder;
|
2015-06-08 03:58:10 +00:00
|
|
|
use view::View;
|
2016-10-02 22:22:29 +00:00
|
|
|
pub use with::With;
|
|
|
|
pub use xy::XY;
|
2015-05-09 19:18:25 +00:00
|
|
|
|
2015-05-15 01:38:58 +00:00
|
|
|
|
2016-10-15 01:18:19 +00:00
|
|
|
/// Identifies a screen in the cursive root.
|
2015-05-15 01:38:58 +00:00
|
|
|
pub type ScreenId = usize;
|
2015-05-15 00:41:17 +00:00
|
|
|
|
|
|
|
/// Central part of the cursive library.
|
2015-05-16 00:56:38 +00:00
|
|
|
///
|
2015-05-15 00:41:17 +00:00
|
|
|
/// 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().
|
2015-05-15 01:38:58 +00:00
|
|
|
///
|
|
|
|
/// It uses a list of screen, with one screen active at a time.
|
2015-05-09 19:18:25 +00:00
|
|
|
pub struct Cursive {
|
2016-06-28 04:59:42 +00:00
|
|
|
theme: theme::Theme,
|
2016-07-28 23:36:01 +00:00
|
|
|
screens: Vec<views::StackView>,
|
2016-07-02 22:02:42 +00:00
|
|
|
global_callbacks: HashMap<Event, Callback>,
|
2016-07-28 23:36:01 +00:00
|
|
|
menubar: views::Menubar,
|
2015-05-15 01:38:58 +00:00
|
|
|
|
2016-10-12 00:12:00 +00:00
|
|
|
// Last layer sizes of the stack view.
|
|
|
|
// If it changed, clear the screen.
|
|
|
|
last_sizes: Vec<Vec2>,
|
|
|
|
|
2015-05-15 01:38:58 +00:00
|
|
|
active_screen: ScreenId,
|
2015-05-09 19:18:25 +00:00
|
|
|
|
|
|
|
running: bool,
|
2016-07-26 17:13:36 +00:00
|
|
|
|
2016-10-10 00:16:35 +00:00
|
|
|
backend: backend::Concrete,
|
2016-09-23 05:00:58 +00:00
|
|
|
|
2016-07-26 17:13:36 +00:00
|
|
|
cb_source: mpsc::Receiver<Box<Fn(&mut Cursive) + Send>>,
|
|
|
|
cb_sink: mpsc::Sender<Box<Fn(&mut Cursive) + Send>>,
|
2015-05-09 19:18:25 +00:00
|
|
|
}
|
|
|
|
|
2016-07-17 00:28:42 +00:00
|
|
|
new_default!(Cursive);
|
2016-06-28 05:40:11 +00:00
|
|
|
|
2015-05-09 19:18:25 +00:00
|
|
|
impl Cursive {
|
2016-10-09 22:48:51 +00:00
|
|
|
/// Creates a new Cursive root, and initialize the back-end.
|
2015-05-09 19:18:25 +00:00
|
|
|
pub fn new() -> Self {
|
2016-10-10 00:16:35 +00:00
|
|
|
let mut backend = backend::Concrete::init();
|
2016-06-30 00:36:20 +00:00
|
|
|
|
2015-06-06 01:08:05 +00:00
|
|
|
let theme = theme::load_default();
|
2016-09-24 23:51:42 +00:00
|
|
|
theme.activate(&mut backend);
|
2015-06-06 01:08:05 +00:00
|
|
|
// let theme = theme::load_theme("assets/style.toml").unwrap();
|
2015-05-22 06:29:49 +00:00
|
|
|
|
2016-07-26 17:13:36 +00:00
|
|
|
let (tx, rx) = mpsc::channel();
|
|
|
|
|
2015-05-15 01:38:58 +00:00
|
|
|
let mut res = Cursive {
|
2016-06-28 04:59:42 +00:00
|
|
|
theme: theme,
|
2015-05-15 01:38:58 +00:00
|
|
|
screens: Vec::new(),
|
2016-10-12 00:12:00 +00:00
|
|
|
last_sizes: Vec::new(),
|
2016-06-28 04:59:42 +00:00
|
|
|
global_callbacks: HashMap::new(),
|
2016-07-28 23:36:01 +00:00
|
|
|
menubar: views::Menubar::new(),
|
2015-05-15 01:38:58 +00:00
|
|
|
active_screen: 0,
|
2015-05-09 19:18:25 +00:00
|
|
|
running: true,
|
2016-07-26 17:13:36 +00:00
|
|
|
cb_source: rx,
|
|
|
|
cb_sink: tx,
|
2016-09-24 23:51:42 +00:00
|
|
|
backend: backend,
|
2015-05-15 01:38:58 +00:00
|
|
|
};
|
|
|
|
|
2016-07-28 23:36:01 +00:00
|
|
|
res.screens.push(views::StackView::new());
|
2015-05-15 01:38:58 +00:00
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
2016-07-26 17:13:36 +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.
|
2016-10-15 01:18:19 +00:00
|
|
|
///
|
|
|
|
/// Note that you currently need to call [`set_fps`] to force cursive to
|
|
|
|
/// regularly check for messages.
|
|
|
|
///
|
|
|
|
/// [`set_fps`]: #method.set_fps
|
2016-07-26 17:13:36 +00:00
|
|
|
pub fn cb_sink(&self) -> &mpsc::Sender<Box<Fn(&mut Cursive) + Send>> {
|
|
|
|
&self.cb_sink
|
|
|
|
}
|
|
|
|
|
2016-10-15 01:18:19 +00:00
|
|
|
/// Selects the menubar.
|
2016-07-02 22:02:42 +00:00
|
|
|
pub fn select_menubar(&mut self) {
|
2016-07-20 03:44:20 +00:00
|
|
|
self.menubar.take_focus(direction::Direction::none());
|
2016-06-28 04:59:42 +00:00
|
|
|
}
|
|
|
|
|
2016-10-15 01:18:19 +00:00
|
|
|
/// Sets the menubar autohide feature.
|
2016-06-28 04:59:42 +00:00
|
|
|
///
|
2016-10-09 22:56:03 +00:00
|
|
|
/// * When enabled (default), the menu is only visible when selected.
|
2016-06-28 04:59:42 +00:00
|
|
|
/// * When disabled, the menu is always visible and reserves the top row.
|
|
|
|
pub fn set_autohide_menu(&mut self, autohide: bool) {
|
2016-07-02 22:02:42 +00:00
|
|
|
self.menubar.autohide = autohide;
|
2016-06-28 04:59:42 +00:00
|
|
|
}
|
|
|
|
|
2016-10-15 01:18:19 +00:00
|
|
|
/// Access the menu tree used by the menubar.
|
2016-07-21 04:25:14 +00:00
|
|
|
///
|
|
|
|
/// This allows to add menu items to the menubar.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2016-10-11 17:11:40 +00:00
|
|
|
/// ```no_run
|
2016-07-21 04:25:14 +00:00
|
|
|
/// # extern crate cursive;
|
|
|
|
/// #
|
2016-09-29 05:45:27 +00:00
|
|
|
/// # use cursive::{Cursive, event};
|
|
|
|
/// # use cursive::views::{Dialog};
|
|
|
|
/// # use cursive::traits::*;
|
|
|
|
/// # use cursive::menu::*;
|
2016-07-21 04:25:14 +00:00
|
|
|
/// #
|
|
|
|
/// # fn main() {
|
|
|
|
/// let mut siv = Cursive::new();
|
|
|
|
///
|
|
|
|
/// siv.menubar()
|
2017-01-24 00:54:27 +00:00
|
|
|
/// .add_subtree("File",
|
2016-07-21 04:25:14 +00:00
|
|
|
/// MenuTree::new()
|
|
|
|
/// .leaf("New", |s| s.add_layer(Dialog::info("New file!")))
|
|
|
|
/// .subtree("Recent", MenuTree::new().with(|tree| {
|
|
|
|
/// for i in 1..100 {
|
2017-01-23 23:58:38 +00:00
|
|
|
/// tree.add_leaf(format!("Item {}", i), |_| ())
|
2016-07-21 04:25:14 +00:00
|
|
|
/// }
|
|
|
|
/// }))
|
|
|
|
/// .delimiter()
|
|
|
|
/// .with(|tree| {
|
|
|
|
/// for i in 1..10 {
|
2017-01-23 23:58:38 +00:00
|
|
|
/// tree.add_leaf(format!("Option {}", i), |_| ());
|
2016-07-21 04:25:14 +00:00
|
|
|
/// }
|
|
|
|
/// })
|
|
|
|
/// .delimiter()
|
|
|
|
/// .leaf("Quit", |s| s.quit()))
|
2017-01-24 00:54:27 +00:00
|
|
|
/// .add_subtree("Help",
|
2016-07-21 04:25:14 +00:00
|
|
|
/// 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"))));
|
|
|
|
///
|
2016-09-29 05:45:27 +00:00
|
|
|
/// siv.add_global_callback(event::Key::Esc, |s| s.select_menubar());
|
2016-07-21 04:25:14 +00:00
|
|
|
/// # }
|
|
|
|
/// ```
|
2016-07-28 23:36:01 +00:00
|
|
|
pub fn menubar(&mut self) -> &mut views::Menubar {
|
2016-07-02 22:02:42 +00:00
|
|
|
&mut self.menubar
|
2016-06-28 04:59:42 +00:00
|
|
|
}
|
|
|
|
|
2016-10-15 01:18:19 +00:00
|
|
|
/// Returns the currently used theme.
|
2015-06-06 01:08:05 +00:00
|
|
|
pub fn current_theme(&self) -> &theme::Theme {
|
|
|
|
&self.theme
|
|
|
|
}
|
|
|
|
|
2016-08-04 07:11:16 +00:00
|
|
|
/// Sets the current theme.
|
|
|
|
pub fn set_theme(&mut self, theme: theme::Theme) {
|
|
|
|
self.theme = theme;
|
2016-09-24 23:51:42 +00:00
|
|
|
self.theme.activate(&mut self.backend);
|
2016-10-09 22:47:06 +00:00
|
|
|
self.backend.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Clears the screen.
|
|
|
|
///
|
2016-10-15 01:18:19 +00:00
|
|
|
/// Users rarely have to call this directly.
|
2016-10-09 22:47:06 +00:00
|
|
|
pub fn clear(&self) {
|
|
|
|
self.backend.clear();
|
2016-08-04 07:11:16 +00:00
|
|
|
}
|
|
|
|
|
2015-06-06 01:08:05 +00:00
|
|
|
/// Loads a theme from the given file.
|
|
|
|
///
|
2016-07-24 23:53:12 +00:00
|
|
|
/// `filename` must point to a valid toml file.
|
2016-07-25 20:39:10 +00:00
|
|
|
pub fn load_theme_file<P: AsRef<Path>>(&mut self, filename: P)
|
|
|
|
-> Result<(), theme::Error> {
|
2016-08-04 07:11:16 +00:00
|
|
|
self.set_theme(try!(theme::load_theme_file(filename)));
|
2016-07-24 23:53:12 +00:00
|
|
|
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> {
|
2016-08-04 07:11:16 +00:00
|
|
|
self.set_theme(try!(theme::load_theme(content)));
|
2016-07-24 23:53:12 +00:00
|
|
|
Ok(())
|
2015-06-06 01:08:05 +00:00
|
|
|
}
|
|
|
|
|
2016-07-10 02:05:51 +00:00
|
|
|
/// Sets the refresh rate, in frames per second.
|
2015-05-22 23:28:05 +00:00
|
|
|
///
|
2016-07-10 02:05:51 +00:00
|
|
|
/// Regularly redraws everything, even when no input is given.
|
2016-10-15 01:18:19 +00:00
|
|
|
///
|
|
|
|
/// 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
|
2016-09-24 23:51:42 +00:00
|
|
|
pub fn set_fps(&mut self, fps: u32) {
|
|
|
|
self.backend.set_refresh_rate(fps)
|
2015-05-22 23:28:05 +00:00
|
|
|
}
|
|
|
|
|
2016-07-20 07:30:00 +00:00
|
|
|
/// Returns a reference to the currently active screen.
|
2016-07-28 23:36:01 +00:00
|
|
|
pub fn screen(&self) -> &views::StackView {
|
2016-07-20 07:30:00 +00:00
|
|
|
let id = self.active_screen;
|
|
|
|
&self.screens[id]
|
|
|
|
}
|
|
|
|
|
2015-05-15 01:38:58 +00:00
|
|
|
/// Returns a mutable reference to the currently active screen.
|
2016-07-28 23:36:01 +00:00
|
|
|
pub fn screen_mut(&mut self) -> &mut views::StackView {
|
2015-05-15 01:38:58 +00:00
|
|
|
let id = self.active_screen;
|
2017-01-19 19:11:57 +00:00
|
|
|
&mut self.screens[id]
|
2015-05-15 01:38:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds a new screen, and returns its ID.
|
|
|
|
pub fn add_screen(&mut self) -> ScreenId {
|
|
|
|
let res = self.screens.len();
|
2016-07-28 23:36:01 +00:00
|
|
|
self.screens.push(views::StackView::new());
|
2015-05-15 01:38:58 +00:00
|
|
|
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() {
|
2016-06-28 04:59:42 +00:00
|
|
|
panic!("Tried to set an invalid screen ID: {}, but only {} \
|
|
|
|
screens present.",
|
2016-03-15 22:37:57 +00:00
|
|
|
screen_id,
|
|
|
|
self.screens.len());
|
2015-05-09 19:18:25 +00:00
|
|
|
}
|
2015-05-15 01:38:58 +00:00
|
|
|
self.active_screen = screen_id;
|
|
|
|
}
|
|
|
|
|
2016-09-21 21:57:03 +00:00
|
|
|
/// Tries to find the view pointed to by the given selector.
|
2016-07-21 04:25:14 +00:00
|
|
|
///
|
2015-05-16 00:56:38 +00:00
|
|
|
/// If the view is not found, or if it is not of the asked type,
|
|
|
|
/// it returns None.
|
2016-07-21 04:25:14 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2016-10-11 17:11:40 +00:00
|
|
|
/// ```no_run
|
2016-07-21 04:25:14 +00:00
|
|
|
/// # extern crate cursive;
|
2016-09-29 05:45:27 +00:00
|
|
|
/// # use cursive::{Cursive, views, view};
|
|
|
|
/// # use cursive::traits::*;
|
2016-07-21 04:25:14 +00:00
|
|
|
/// # fn main() {
|
|
|
|
/// let mut siv = Cursive::new();
|
|
|
|
///
|
2016-09-29 05:45:27 +00:00
|
|
|
/// siv.add_layer(views::TextView::new("Text #1")
|
|
|
|
/// .with_id("text"));
|
2016-07-21 04:25:14 +00:00
|
|
|
///
|
|
|
|
/// siv.add_global_callback('p', |s| {
|
2016-09-29 05:45:27 +00:00
|
|
|
/// s.find::<views::TextView>(&view::Selector::Id("text"))
|
2016-07-21 04:25:14 +00:00
|
|
|
/// .unwrap()
|
|
|
|
/// .set_content("Text #2");
|
|
|
|
/// });
|
|
|
|
/// # }
|
|
|
|
/// ```
|
2016-09-23 05:10:14 +00:00
|
|
|
pub fn find<V: View + Any>(&mut self, sel: &view::Selector)
|
|
|
|
-> Option<&mut V> {
|
2016-09-21 18:33:44 +00:00
|
|
|
self.screen_mut().find(sel)
|
2015-05-16 00:56:38 +00:00
|
|
|
}
|
|
|
|
|
2016-07-28 23:36:01 +00:00
|
|
|
/// Convenient method to use `find` with a `view::Selector::Id`.
|
2016-07-21 04:25:14 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2016-10-11 17:11:40 +00:00
|
|
|
/// ```no_run
|
2016-07-21 04:25:14 +00:00
|
|
|
/// # extern crate cursive;
|
2016-09-29 05:45:27 +00:00
|
|
|
/// # use cursive::{Cursive, views};
|
|
|
|
/// # use cursive::traits::*;
|
2016-07-21 04:25:14 +00:00
|
|
|
/// # fn main() {
|
|
|
|
/// let mut siv = Cursive::new();
|
|
|
|
///
|
2016-09-29 05:45:27 +00:00
|
|
|
/// siv.add_layer(views::TextView::new("Text #1")
|
|
|
|
/// .with_id("text"));
|
2016-07-21 04:25:14 +00:00
|
|
|
///
|
|
|
|
/// siv.add_global_callback('p', |s| {
|
2016-09-29 05:45:27 +00:00
|
|
|
/// s.find_id::<views::TextView>("text")
|
2016-07-21 04:25:14 +00:00
|
|
|
/// .unwrap()
|
|
|
|
/// .set_content("Text #2");
|
|
|
|
/// });
|
|
|
|
/// # }
|
|
|
|
/// ```
|
2015-07-28 13:57:52 +00:00
|
|
|
pub fn find_id<V: View + Any>(&mut self, id: &str) -> Option<&mut V> {
|
2016-07-28 23:36:01 +00:00
|
|
|
self.find(&view::Selector::Id(id))
|
2015-07-28 13:57:52 +00:00
|
|
|
}
|
|
|
|
|
2016-07-10 02:05:51 +00:00
|
|
|
/// Adds a global callback.
|
|
|
|
///
|
|
|
|
/// Will be triggered on the given key press when no view catches it.
|
2016-07-21 04:25:14 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2016-10-11 17:11:40 +00:00
|
|
|
/// ```no_run
|
2016-07-21 04:25:14 +00:00
|
|
|
/// # extern crate cursive;
|
2016-09-29 05:45:27 +00:00
|
|
|
/// # use cursive::*;
|
2016-07-21 04:25:14 +00:00
|
|
|
/// # fn main() {
|
|
|
|
/// let mut siv = Cursive::new();
|
|
|
|
///
|
|
|
|
/// siv.add_global_callback('q', |s| s.quit());
|
|
|
|
/// # }
|
|
|
|
/// ```
|
2016-07-11 01:27:26 +00:00
|
|
|
pub fn add_global_callback<F, E: Into<Event>>(&mut self, event: E, cb: F)
|
2015-05-24 00:07:22 +00:00
|
|
|
where F: Fn(&mut Cursive) + 'static
|
2015-05-15 01:38:58 +00:00
|
|
|
{
|
2016-07-25 06:00:13 +00:00
|
|
|
self.global_callbacks.insert(event.into(), Callback::from_fn(cb));
|
2015-05-15 01:38:58 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 02:54:33 +00:00
|
|
|
/// Add a layer to the current screen.
|
2016-07-21 04:25:14 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2016-10-11 17:11:40 +00:00
|
|
|
/// ```no_run
|
2016-07-21 04:25:14 +00:00
|
|
|
/// # extern crate cursive;
|
2016-09-29 05:45:27 +00:00
|
|
|
/// # use cursive::*;
|
2016-07-21 04:25:14 +00:00
|
|
|
/// # fn main() {
|
|
|
|
/// let mut siv = Cursive::new();
|
|
|
|
///
|
2016-09-29 05:45:27 +00:00
|
|
|
/// siv.add_layer(views::TextView::new("Hello world!"));
|
2016-07-21 04:25:14 +00:00
|
|
|
/// # }
|
|
|
|
/// ```
|
2015-05-15 01:38:58 +00:00
|
|
|
pub fn add_layer<T: 'static + View>(&mut self, view: T) {
|
|
|
|
self.screen_mut().add_layer(view);
|
|
|
|
}
|
|
|
|
|
2017-01-24 02:54:33 +00:00
|
|
|
/// 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 T: 'static + View
|
|
|
|
{
|
|
|
|
self.screen_mut().add_fullscreen_layer(view);
|
|
|
|
}
|
|
|
|
|
2015-05-26 23:48:27 +00:00
|
|
|
/// Convenient method to remove a layer from the current screen.
|
2015-05-26 22:46:05 +00:00
|
|
|
pub fn pop_layer(&mut self) {
|
|
|
|
self.screen_mut().pop_layer();
|
2016-10-12 00:12:00 +00:00
|
|
|
self.clear();
|
2015-05-26 22:46:05 +00:00
|
|
|
}
|
|
|
|
|
2015-05-15 22:00:20 +00:00
|
|
|
// Handles a key event when it was ignored by the current view
|
2015-05-28 01:04:33 +00:00
|
|
|
fn on_event(&mut self, event: Event) {
|
|
|
|
let cb = match self.global_callbacks.get(&event) {
|
2015-05-15 01:38:58 +00:00
|
|
|
None => return,
|
|
|
|
Some(cb) => cb.clone(),
|
|
|
|
};
|
2015-05-16 00:56:38 +00:00
|
|
|
// Not from a view, so no viewpath here
|
2015-05-24 00:07:22 +00:00
|
|
|
cb(self);
|
2015-05-09 19:18:25 +00:00
|
|
|
}
|
|
|
|
|
2015-05-16 00:56:38 +00:00
|
|
|
/// Returns the size of the screen, in characters.
|
2015-05-15 22:00:20 +00:00
|
|
|
pub fn screen_size(&self) -> Vec2 {
|
2016-09-24 23:51:42 +00:00
|
|
|
let (x, y) = self.backend.screen_size();
|
2015-05-15 22:00:20 +00:00
|
|
|
|
|
|
|
Vec2 {
|
2015-05-25 21:46:29 +00:00
|
|
|
x: x as usize,
|
|
|
|
y: y as usize,
|
2015-05-15 22:00:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn layout(&mut self) {
|
|
|
|
let size = self.screen_size();
|
|
|
|
self.screen_mut().layout(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn draw(&mut self) {
|
2016-06-28 04:59:42 +00:00
|
|
|
// TODO: don't clone the theme
|
|
|
|
// Reference it or something
|
2016-10-12 00:12:00 +00:00
|
|
|
|
|
|
|
let sizes = self.screen().layer_sizes();
|
|
|
|
if &self.last_sizes != &sizes {
|
|
|
|
self.clear();
|
|
|
|
self.last_sizes = sizes;
|
|
|
|
}
|
|
|
|
|
2016-09-23 05:10:14 +00:00
|
|
|
let printer = Printer::new(self.screen_size(),
|
|
|
|
self.theme.clone(),
|
|
|
|
&self.backend);
|
2016-06-28 04:59:42 +00:00
|
|
|
|
|
|
|
// Draw the currently active screen
|
|
|
|
// If the menubar is active, nothing else can be.
|
2016-10-02 22:22:29 +00:00
|
|
|
let offset = if self.menubar.autohide { 0 } else { 1 };
|
2016-06-28 04:59:42 +00:00
|
|
|
// Draw the menubar?
|
2016-07-02 22:02:42 +00:00
|
|
|
if self.menubar.visible() {
|
2016-07-10 02:05:51 +00:00
|
|
|
let printer = printer.sub_printer(Vec2::zero(),
|
|
|
|
printer.size,
|
|
|
|
self.menubar.receive_events());
|
2016-07-02 22:02:42 +00:00
|
|
|
self.menubar.draw(&printer);
|
2016-06-28 04:59:42 +00:00
|
|
|
}
|
|
|
|
|
2016-07-03 02:37:38 +00:00
|
|
|
let selected = self.menubar.receive_events();
|
|
|
|
|
2016-07-10 02:05:51 +00:00
|
|
|
let printer =
|
|
|
|
printer.sub_printer(Vec2::new(0, offset), printer.size, !selected);
|
2016-09-23 05:00:58 +00:00
|
|
|
let id = self.active_screen;
|
2017-01-19 19:11:57 +00:00
|
|
|
self.screens[id].draw(&printer);
|
2016-07-03 02:37:38 +00:00
|
|
|
|
2015-05-28 01:04:33 +00:00
|
|
|
}
|
|
|
|
|
2016-12-15 19:24:48 +00:00
|
|
|
/// Returns `true` until [`quit(&mut self)`] is called.
|
|
|
|
///
|
|
|
|
/// [`quit(&mut self)`]: #method.quit
|
|
|
|
pub fn is_running(&self) -> bool {
|
|
|
|
self.running
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-15 00:41:17 +00:00
|
|
|
/// Runs the event loop.
|
2016-06-28 04:59:42 +00:00
|
|
|
///
|
2016-07-10 02:05:51 +00:00
|
|
|
/// It will wait for user input (key presses)
|
|
|
|
/// and trigger callbacks accordingly.
|
|
|
|
///
|
2016-12-15 19:24:48 +00:00
|
|
|
/// Calls [`step(&mut self)`] until [`quit(&mut self)`] is called.
|
|
|
|
///
|
|
|
|
/// [`step(&mut self)`]: #method.step
|
|
|
|
/// [`quit(&mut self)`]: #method.quit
|
2015-05-09 19:18:25 +00:00
|
|
|
pub fn run(&mut self) {
|
2015-05-24 08:22:27 +00:00
|
|
|
|
2015-05-15 23:06:48 +00:00
|
|
|
// And the big event loop begins!
|
2015-05-09 19:18:25 +00:00
|
|
|
while self.running {
|
2016-12-15 19:24:48 +00:00
|
|
|
self.step();
|
|
|
|
}
|
|
|
|
}
|
2016-07-26 17:13:36 +00:00
|
|
|
|
2016-12-15 19:24:48 +00:00
|
|
|
/// 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) {
|
|
|
|
if let Ok(cb) = self.cb_source.try_recv() {
|
|
|
|
cb(self);
|
|
|
|
}
|
2016-10-12 00:47:44 +00:00
|
|
|
|
2016-12-15 19:24:48 +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.backend.clear();
|
|
|
|
}
|
2015-05-28 01:04:33 +00:00
|
|
|
|
2016-12-15 19:24:48 +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 {
|
|
|
|
match self.screen_mut().on_event(event) {
|
|
|
|
// 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),
|
2015-05-09 19:18:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-16 00:56:38 +00:00
|
|
|
/// Stops the event loop.
|
2015-05-09 19:18:25 +00:00
|
|
|
pub fn quit(&mut self) {
|
|
|
|
self.running = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Cursive {
|
|
|
|
fn drop(&mut self) {
|
2016-09-24 23:51:42 +00:00
|
|
|
self.backend.finish();
|
2015-05-09 19:18:25 +00:00
|
|
|
}
|
|
|
|
}
|