Struct cursive::Cursive
[−]
[src]
pub struct Cursive { /* fields omitted */ }
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.
Methods
impl Cursive
[src]
fn new() -> Self
Creates a new Cursive root, and initialize the back-end.
fn cb_sink(&self) -> &Sender<Box<Fn(&mut Cursive) + Send>>
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.
Selects the menubar.
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.
Access the menu tree used by the menubar.
This allows to add menu items to the menubar.
Examples
let mut siv = Cursive::new(); 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());
fn current_theme(&self) -> &Theme
Returns the currently used theme.
fn set_theme(&mut self, theme: Theme)
Sets the current theme.
fn clear(&self)
Clears the screen.
Users rarely have to call this directly.
fn load_theme_file<P: AsRef<Path>>(&mut self, filename: P) -> Result<(), Error>
Loads a theme from the given file.
filename
must point to a valid toml file.
fn load_theme(&mut self, content: &str) -> Result<(), Error>
Loads a theme from the given string content.
Content must be valid toml.
fn set_fps(&mut self, fps: u32)
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).
fn screen(&self) -> &StackView
Returns a reference to the currently active screen.
fn screen_mut(&mut self) -> &mut StackView
Returns a mutable reference to the currently active screen.
fn add_screen(&mut self) -> ScreenId
Adds a new screen, and returns its ID.
fn add_active_screen(&mut self) -> ScreenId
Convenient method to create a new screen, and set it as active.
fn set_screen(&mut self, screen_id: ScreenId)
Sets the active screen. Panics if no such screen exist.
fn find<V: View + Any>(&mut self, sel: &Selector) -> Option<&mut V>
Tries to find the view pointed to by the given selector.
If the view is not found, or if it is not of the asked type, it returns None.
Examples
let mut siv = Cursive::new(); siv.add_layer(views::TextView::new("Text #1") .with_id("text")); siv.add_global_callback('p', |s| { s.find::<views::TextView>(&view::Selector::Id("text")) .unwrap() .set_content("Text #2"); });
fn find_id<V: View + Any>(&mut self, id: &str) -> Option<&mut V>
Convenient method to use find
with a view::Selector::Id
.
Examples
let mut siv = Cursive::new(); siv.add_layer(views::TextView::new("Text #1") .with_id("text")); siv.add_global_callback('p', |s| { s.find_id::<views::TextView>("text") .unwrap() .set_content("Text #2"); });
fn add_global_callback<F, E: Into<Event>>(&mut self, event: E, cb: F) where F: Fn(&mut Cursive) + 'static
Adds a global callback.
Will be triggered on the given key press when no view catches it.
Examples
let mut siv = Cursive::new(); siv.add_global_callback('q', |s| s.quit());
fn add_layer<T: 'static + View>(&mut self, view: T)
Add a layer to the current screen.
Examples
let mut siv = Cursive::new(); siv.add_layer(views::TextView::new("Hello world!"));
fn add_fullscreen_layer<T>(&mut self, view: T) where T: 'static + View
Adds a new full-screen layer to the current screen.
Fullscreen layers have no shadow.
fn pop_layer(&mut self)
Convenient method to remove a layer from the current screen.
fn screen_size(&self) -> Vec2
Returns the size of the screen, in characters.
fn is_running(&self) -> bool
Returns true
until quit(&mut self)
is called.
fn run(&mut self)
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.
fn step(&mut self)
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.
fn quit(&mut self)
Stops the event loop.