[−][src]Struct fui::cursive::Cursive
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]
pub fn new<F>(backend_init: F) -> Cursive where
F: FnOnce() -> Box<dyn Backend + 'static>,
[src]
F: FnOnce() -> Box<dyn Backend + 'static>,
Shortcut for Cursive::try_new
with non-failible init function.
You probably don't want to use this function directly. Instead,
Cursive::default()
or Cursive::ncurses()
may be what you're
looking for.
pub fn try_new<F, E>(backend_init: F) -> Result<Cursive, E> where
F: FnOnce() -> Result<Box<dyn Backend + 'static>, E>,
[src]
F: FnOnce() -> Result<Box<dyn Backend + 'static>, E>,
Creates a new Cursive root, and initialize the back-end.
- If you just want a cursive instance, use
Cursive::default()
. - If you want a specific backend, then:
Cursive::ncurses()
if thencurses-backend
feature is enabled (it is by default).Cursive::pancurses()
if thepancurses-backend
feature is enabled.Cursive::termion()
if thetermion-backend
feature is enabled.Cursive::crossterm()
if thecrossterm-backend
feature is enabled.Cursive::blt()
if theblt-backend
feature is enabled.Cursive::dummy()
for a dummy backend, mostly useful for tests.
- If you want to use a third-party backend, then
Cursive::new
is indeed the way to go:Cursive::new(bring::your::own::Backend::new)
Examples:
let siv = Cursive::new(backend::dummy::Backend::init); // equivalent to Cursive::dummy()
pub fn ncurses() -> Result<Cursive, Error>
[src]
Creates a new Cursive root using a ncurses backend.
pub fn dummy() -> Cursive
[src]
Creates a new Cursive root using a dummy backend.
Nothing will be output. This is mostly here for tests.
pub fn set_user_data<T>(&mut self, user_data: T) where
T: Any,
[src]
T: Any,
Sets some data to be stored in Cursive.
It can later on be accessed with Cursive::user_data()
pub fn user_data<T>(&mut self) -> Option<&mut T> where
T: Any,
[src]
T: Any,
Attempts to access the user-provided data.
If some data was set previously with the same type, returns a reference to it.
If nothing was set or if the type is different, returns None
.
pub fn with_user_data<F, T, R>(&mut self, f: F) -> Option<R> where
F: FnOnce(&mut T) -> R,
T: Any,
[src]
F: FnOnce(&mut T) -> R,
T: Any,
Runs the given closure on the stored user data, if any.
If no user data was supplied, or if the type is different, nothing will be run. Otherwise, the result will be returned.
pub fn show_debug_console(&mut self)
[src]
Show the debug console.
Currently, this will show logs if logger::init()
was called.
pub fn toggle_debug_console(&mut self)
[src]
Show the debug console, or hide it if it's already visible.
Examples
siv.add_global_callback('~', Cursive::toggle_debug_console);
pub fn cb_sink(&self) -> &Sender<Box<dyn CbFunc + 'static>>
[src]
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.
Examples
let mut siv = Cursive::dummy(); // quit() will be called during the next event cycle siv.cb_sink().send(Box::new(|s: &mut Cursive| s.quit())).unwrap();
pub fn select_menubar(&mut self)
[src]
Selects the menubar.
pub fn set_autohide_menu(&mut self, autohide: bool)
[src]
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 menubar(&mut self) -> &mut Menubar
[src]
Access the menu tree used by the menubar.
This allows to add menu items to the menubar.
Examples
let mut siv = Cursive::dummy(); 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 current_theme(&self) -> &Theme
[src]
Returns the currently used theme.
pub fn set_theme(&mut self, theme: Theme)
[src]
Sets the current theme.
pub fn clear(&mut self)
[src]
Clears the screen.
Users rarely have to call this directly.
pub fn load_theme_file<P>(&mut self, filename: P) -> Result<(), Error> where
P: AsRef<Path>,
[src]
P: AsRef<Path>,
Loads a theme from the given file.
filename
must point to a valid toml file.
pub fn load_toml(&mut self, content: &str) -> Result<(), Error>
[src]
Loads a theme from the given string content.
Content must be valid toml.
pub fn set_fps(&mut self, fps: u32)
[src]
Sets the refresh rate, in frames per second.
Note that the actual frequency is not guaranteed.
Between 0 and 30. Call with fps = 0
to disable (default value).
pub fn set_autorefresh(&mut self, autorefresh: bool)
[src]
Enables or disables automatic refresh of the screen.
This is a shortcut to call set_fps
with 30
or 0
depending on
autorefresh
.
pub fn screen(&self) -> &StackView
[src]
Returns a reference to the currently active screen.
pub fn screen_mut(&mut self) -> &mut StackView
[src]
Returns a mutable reference to the currently active screen.
pub fn active_screen(&self) -> usize
[src]
Returns the id of the currently active screen.
pub fn add_screen(&mut self) -> usize
[src]
Adds a new screen, and returns its ID.
pub fn add_active_screen(&mut self) -> usize
[src]
Convenient method to create a new screen, and set it as active.
pub fn set_screen(&mut self, screen_id: usize)
[src]
Sets the active screen. Panics if no such screen exist.
pub fn call_on<V, F, R>(&mut self, sel: &Selector, callback: F) -> Option<R> where
F: FnOnce(&mut V) -> R,
V: View + Any,
[src]
F: FnOnce(&mut V) -> R,
V: View + Any,
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
fn main() { let mut siv = Cursive::dummy(); 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"); }, ); }); }
pub fn call_on_id<V, F, R>(&mut self, id: &str, callback: F) -> Option<R> where
F: FnOnce(&mut V) -> R,
V: View + Any,
[src]
F: FnOnce(&mut V) -> R,
V: View + Any,
Tries to find the view identified by the given id.
Convenient method to use call_on
with a view::Selector::Id
.
Examples
let mut siv = Cursive::dummy(); 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 find_id<V>(
&mut self,
id: &str
) -> Option<OwningHandle<OwningRef<Rc<RefCell<V>>, RefCell<V>>, RefMut<'static, V>>> where
V: View + Any,
[src]
&mut self,
id: &str
) -> Option<OwningHandle<OwningRef<Rc<RefCell<V>>, RefCell<V>>, RefMut<'static, V>>> where
V: View + Any,
Convenient method to find a view wrapped in IdView
.
This looks for a IdView<V>
with the given ID, and return
a ViewRef
to the wrapped view. The ViewRef
implements
DerefMut<Target=T>
, so you can treat it just like a &mut T
.
Examples
use cursive::traits::Identifiable; siv.add_layer(TextView::new("foo").with_id("id")); // Could be called in a callback let mut view: ViewRef<TextView> = siv.find_id("id").unwrap(); view.set_content("bar");
Note that you must specify the exact type for the view you're after; for example, using the
wrong item type in a SelectView
will not find anything:
use cursive::traits::Identifiable; let select = SelectView::new().item("zero", 0u32).item("one", 1u32); siv.add_layer(select.with_id("select")); // Specifying a wrong type will not return anything. assert!(siv.find_id::<SelectView<String>>("select").is_none()); // Omitting the type will use the default type, in this case `String`. assert!(siv.find_id::<SelectView>("select").is_none()); // But with the correct type, it works fine. assert!(siv.find_id::<SelectView<u32>>("select").is_some());
pub fn focus_id(&mut self, id: &str) -> Result<(), ()>
[src]
Moves the focus to the view identified by id
.
Convenient method to call focus
with a view::Selector::Id
.
pub fn focus(&mut self, sel: &Selector) -> Result<(), ()>
[src]
Moves the focus to the view identified by sel
.
pub fn add_global_callback<F, E>(&mut self, event: E, cb: F) where
E: Into<Event>,
F: FnMut(&mut Cursive) + 'static,
[src]
E: Into<Event>,
F: FnMut(&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::dummy(); siv.add_global_callback('q', |s| s.quit());
pub fn clear_global_callbacks<E>(&mut self, event: E) where
E: Into<Event>,
[src]
E: Into<Event>,
Removes any callback tied to the given event.
Examples
let mut siv = Cursive::dummy(); siv.add_global_callback('q', |s| s.quit()); siv.clear_global_callbacks('q');
pub fn add_layer<T>(&mut self, view: T) where
T: IntoBoxedView,
[src]
T: IntoBoxedView,
Add a layer to the current screen.
Examples
let mut siv = Cursive::dummy(); siv.add_layer(views::TextView::new("Hello world!"));
pub fn add_fullscreen_layer<T>(&mut self, view: T) where
T: IntoBoxedView,
[src]
T: IntoBoxedView,
Adds a new full-screen layer to the current screen.
Fullscreen layers have no shadow.
pub fn pop_layer(&mut self) -> Option<Box<dyn View + 'static>>
[src]
Convenient method to remove a layer from the current screen.
pub fn reposition_layer(&mut self, layer: LayerPosition, position: XY<Offset>)
[src]
Convenient stub forwarding layer repositioning.
pub fn on_event(&mut self, event: Event)
[src]
Processes an event.
- If the menubar is active, it will be handled the event.
- The view tree will be handled the event.
- If ignored, global_callbacks will be checked for this event.
pub fn screen_size(&self) -> XY<usize>
[src]
Returns the size of the screen, in characters.
pub fn is_running(&self) -> bool
[src]
Returns true
until quit(&mut self)
is called.
pub fn run(&mut self)
[src]
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.
pub fn step(&mut self) -> bool
[src]
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.
Returns true
if an input event or callback was received
during this step, and false
otherwise.
pub fn refresh(&mut self)
[src]
Refresh the screen with the current view tree state.
pub fn quit(&mut self)
[src]
Stops the event loop.
pub fn noop(&mut self)
[src]
Does not do anything.
pub fn backend_name(&self) -> &str
[src]
Return the name of the backend used.
Mostly used for debugging.
Trait Implementations
Auto Trait Implementations
Blanket Implementations
impl<T> From for T
[src]
impl<T, U> Into for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Borrow for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> BorrowMut for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> With for T
[src]
fn with<F>(self, f: F) -> Self where
F: FnOnce(&mut Self),
[src]
F: FnOnce(&mut Self),
Calls the given closure on self
.
fn try_with<E, F>(self, f: F) -> Result<Self, E> where
F: FnOnce(&mut Self) -> Result<(), E>,
[src]
F: FnOnce(&mut Self) -> Result<(), E>,
Calls the given closure on self
.
fn with_if<F>(self, condition: bool, f: F) -> Self where
F: FnOnce(&mut Self),
[src]
F: FnOnce(&mut Self),
Calls the given closure if condition == true
.