Struct cursive::views::SelectView
source · pub struct SelectView<T = String> { /* private fields */ }
Expand description
View to select an item among a list.
It contains a list of values of type T, with associated labels.
Examples
let mut time_select = SelectView::new().h_align(HAlign::Center);
time_select.add_item("Short", 1);
time_select.add_item("Medium", 5);
time_select.add_item("Long", 10);
time_select.set_on_submit(|s, time| {
s.pop_layer();
let text = format!("You will wait for {} minutes...", time);
s.add_layer(Dialog::around(TextView::new(text))
.button("Quit", |s| s.quit()));
});
let mut siv = Cursive::dummy();
siv.add_layer(Dialog::around(time_select)
.title("How long is your wait?"));
Implementations
sourceimpl<T: 'static> SelectView<T>
impl<T: 'static> SelectView<T>
sourcepub fn set_autojump(&mut self, autojump: bool)
pub fn set_autojump(&mut self, autojump: bool)
Sets the “auto-jump” property for this view.
If enabled, when a key is pressed, the selection will jump to the next item beginning with the pressed letter.
sourcepub fn autojump(self) -> Self
pub fn autojump(self) -> Self
Sets the “auto-jump” property for this view.
If enabled, when a key is pressed, the selection will jump to the next item beginning with the pressed letter.
Chainable variant.
sourcepub fn set_enabled(&mut self, enabled: bool)
pub fn set_enabled(&mut self, enabled: bool)
Enable or disable this view.
sourcepub fn is_enabled(&self) -> bool
pub fn is_enabled(&self) -> bool
Returns true
if this view is enabled.
sourcepub fn set_on_select<F>(&mut self, cb: F)where
F: Fn(&mut Cursive, &T) + 'static,
pub fn set_on_select<F>(&mut self, cb: F)where
F: Fn(&mut Cursive, &T) + 'static,
Sets a callback to be used when an item is selected.
sourcepub fn on_select<F>(self, cb: F) -> Selfwhere
F: Fn(&mut Cursive, &T) + 'static,
pub fn on_select<F>(self, cb: F) -> Selfwhere
F: Fn(&mut Cursive, &T) + 'static,
Sets a callback to be used when an item is selected.
Chainable variant.
sourcepub fn set_on_submit<F, R, V: ?Sized>(&mut self, cb: F)where
F: 'static + Fn(&mut Cursive, &V) -> R,
T: Borrow<V>,
pub fn set_on_submit<F, R, V: ?Sized>(&mut self, cb: F)where
F: 'static + Fn(&mut Cursive, &V) -> R,
T: Borrow<V>,
Sets a callback to be used when <Enter>
is pressed.
Also happens if the user clicks an item.
The item currently selected will be given to the callback.
Here, V
can be T
itself, or a type that can be borrowed from T
.
sourcepub fn on_submit<F, V: ?Sized>(self, cb: F) -> Selfwhere
F: Fn(&mut Cursive, &V) + 'static,
T: Borrow<V>,
pub fn on_submit<F, V: ?Sized>(self, cb: F) -> Selfwhere
F: Fn(&mut Cursive, &V) + 'static,
T: Borrow<V>,
Sets a callback to be used when <Enter>
is pressed.
Also happens if the user clicks an item.
The item currently selected will be given to the callback.
Chainable variant.
sourcepub fn v_align(self, v: VAlign) -> Self
pub fn v_align(self, v: VAlign) -> Self
Sets the vertical alignment for this view. (If the view is given too much space vertically.)
sourcepub fn selection(&self) -> Option<Rc<T>>
pub fn selection(&self) -> Option<Rc<T>>
Returns the value of the currently selected item.
Returns None
if the list is empty.
sourcepub fn add_item<S: Into<StyledString>>(&mut self, label: S, value: T)
pub fn add_item<S: Into<StyledString>>(&mut self, label: S, value: T)
Adds a item to the list, with given label and value.
sourcepub fn get_item(&self, i: usize) -> Option<(&str, &T)>
pub fn get_item(&self, i: usize) -> Option<(&str, &T)>
Gets an item at given idx or None.
use cursive::Cursive;
use cursive::views::{SelectView, TextView};
let select = SelectView::new()
.item("Short", 1);
assert_eq!(select.get_item(0), Some(("Short", &1)));
sourcepub fn get_item_mut(&mut self, i: usize) -> Option<(&mut StyledString, &mut T)>
pub fn get_item_mut(&mut self, i: usize) -> Option<(&mut StyledString, &mut T)>
Gets a mut item at given idx or None.
sourcepub fn iter(&self) -> impl Iterator<Item = (&str, &T)>
pub fn iter(&self) -> impl Iterator<Item = (&str, &T)>
Iterate on the items in this view.
Returns an iterator with each item and their labels.
sourcepub fn remove_item(&mut self, id: usize) -> Callback
pub fn remove_item(&mut self, id: usize) -> Callback
Removes an item from the list.
Returns a callback in response to the selection change.
You should run this callback with a &mut Cursive
.
sourcepub fn insert_item<S>(&mut self, index: usize, label: S, value: T)where
S: Into<StyledString>,
pub fn insert_item<S>(&mut self, index: usize, label: S, value: T)where
S: Into<StyledString>,
Inserts an item at position index
, shifting all elements after it to
the right.
sourcepub fn add_all<S, I>(&mut self, iter: I)where
S: Into<StyledString>,
I: IntoIterator<Item = (S, T)>,
pub fn add_all<S, I>(&mut self, iter: I)where
S: Into<StyledString>,
I: IntoIterator<Item = (S, T)>,
Adds all items from from an iterator.
sourcepub fn with_all<S, I>(self, iter: I) -> Selfwhere
S: Into<String>,
I: IntoIterator<Item = (S, T)>,
pub fn with_all<S, I>(self, iter: I) -> Selfwhere
S: Into<String>,
I: IntoIterator<Item = (S, T)>,
Adds all items from from an iterator.
Chainable variant.
sourcepub fn selected_id(&self) -> Option<usize>
pub fn selected_id(&self) -> Option<usize>
Returns the id of the item currently selected.
Returns None
if the list is empty.
sourcepub fn set_selection(&mut self, i: usize) -> Callback
pub fn set_selection(&mut self, i: usize) -> Callback
Moves the selection to the given position.
Returns a callback in response to the selection change.
You should run this callback with a &mut Cursive
.
sourcepub fn selected(self, i: usize) -> Self
pub fn selected(self, i: usize) -> Self
Sets the selection to the given position.
Chainable variant.
Does not apply on_select
callbacks.
sourcepub fn select_up(&mut self, n: usize) -> Callback
pub fn select_up(&mut self, n: usize) -> Callback
Moves the selection up by the given number of rows.
Returns a callback in response to the selection change.
You should run this callback with a &mut Cursive
:
fn select_up(siv: &mut Cursive, view: &mut SelectView<()>) {
let cb = view.select_up(1);
cb(siv);
}
sourcepub fn select_down(&mut self, n: usize) -> Callback
pub fn select_down(&mut self, n: usize) -> Callback
Moves the selection down by the given number of rows.
Returns a callback in response to the selection change.
You should run this callback with a &mut Cursive
.
sourceimpl SelectView<String>
impl SelectView<String>
sourcepub fn add_item_str<S: Into<String>>(&mut self, label: S)
pub fn add_item_str<S: Into<String>>(&mut self, label: S)
Convenient method to use the label as value.
sourcepub fn insert_item_str<S>(&mut self, index: usize, label: S)where
S: Into<String>,
pub fn insert_item_str<S>(&mut self, index: usize, label: S)where
S: Into<String>,
Convenient method to use the label as value.
sourcepub fn add_all_str<S, I>(&mut self, iter: I)where
S: Into<String>,
I: IntoIterator<Item = S>,
pub fn add_all_str<S, I>(&mut self, iter: I)where
S: Into<String>,
I: IntoIterator<Item = S>,
Adds all strings from an iterator.
Examples
let mut select_view = SelectView::new();
select_view.add_all_str(vec!["a", "b", "c"]);
sourcepub fn with_all_str<S, I>(self, iter: I) -> Selfwhere
S: Into<String>,
I: IntoIterator<Item = S>,
pub fn with_all_str<S, I>(self, iter: I) -> Selfwhere
S: Into<String>,
I: IntoIterator<Item = S>,
Adds all strings from an iterator.
Chainable variant.
Trait Implementations
sourceimpl<T: 'static> Default for SelectView<T>
impl<T: 'static> Default for SelectView<T>
sourceimpl<T: 'static> View for SelectView<T>
impl<T: 'static> View for SelectView<T>
sourcefn draw(&self, printer: &Printer<'_, '_>)
fn draw(&self, printer: &Printer<'_, '_>)
sourcefn required_size(&mut self, _: Vec2) -> Vec2
fn required_size(&mut self, _: Vec2) -> Vec2
sourcefn on_event(&mut self, event: Event) -> EventResult
fn on_event(&mut self, event: Event) -> EventResult
sourcefn take_focus(&mut self, _: Direction) -> bool
fn take_focus(&mut self, _: Direction) -> bool
sourcefn layout(&mut self, size: Vec2)
fn layout(&mut self, size: Vec2)
sourcefn important_area(&self, size: Vec2) -> Rect
fn important_area(&self, size: Vec2) -> Rect
sourcefn needs_relayout(&self) -> bool
fn needs_relayout(&self) -> bool
true
if the view content changed since last layout phase. Read more