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::new();
siv.add_layer(Dialog::around(time_select).title("How long is your wait?"));Implementations§
Source§impl<T: 'static + Send + Sync> SelectView<T>
impl<T: 'static + Send + Sync> SelectView<T>
Sourcepub fn set_enabled(&mut self, enabled: bool)
pub fn set_enabled(&mut self, enabled: bool)
Enable or disable this view.
Sourcepub fn with_enabled(self, is_enabled: bool) -> Self
pub fn with_enabled(self, is_enabled: bool) -> Self
Enable or disable this view.
Chainable variant.
Sourcepub fn is_enabled(&self) -> bool
pub fn is_enabled(&self) -> bool
Returns true if this view is enabled.
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_inactive_highlight(&mut self, inactive_highlight: bool)
pub fn set_inactive_highlight(&mut self, inactive_highlight: bool)
Sets the “inactive highlight” property for this view.
- If true (the default), the selected row will be highlighted when the view is not focused.
- If false, the selected row will be printed like the others if inactive.
Sourcepub fn with_inactive_highlight(self, inactive_highlight: bool) -> Self
pub fn with_inactive_highlight(self, inactive_highlight: bool) -> Self
Sets the “inactive highlight” property for this view.
- If true (the default), the selected row will be highlighted when the view is not focused.
- If false, the selected row will be printed like the others if inactive.
Chainable variant.
Sourcepub fn get_inactive_highlight(&self) -> bool
pub fn get_inactive_highlight(&self) -> bool
Returns the current status of the “inactive highlight” property.
Sourcepub fn decorators<S: Into<String>>(self, start: S, end: S) -> Self
pub fn decorators<S: Into<String>>(self, start: S, end: S) -> Self
Use custom decorators around the popup button instead of “<” and “>”.
Chainable variant.
Sourcepub fn set_decorators<S: Into<String>>(&mut self, start: S, end: S)
pub fn set_decorators<S: Into<String>>(&mut self, start: S, end: S)
Use custom decorators around the popup button instead of “<” and “>”.
Sourcepub fn set_on_select<F>(&mut self, cb: F)
pub fn set_on_select<F>(&mut self, cb: F)
Sets a callback to be used when an item is selected.
Sourcepub fn on_select_cb<F: Fn(&mut Cursive, &T) + 'static + Send + Sync>(
cb: F,
) -> Arc<dyn Fn(&mut Cursive, &T) + Send + Sync>
pub fn on_select_cb<F: Fn(&mut Cursive, &T) + 'static + Send + Sync>( cb: F, ) -> Arc<dyn Fn(&mut Cursive, &T) + Send + Sync>
Helper method to store a callback of the correct type for Self::set_on_select.
This is mostly useful when using this view in a template.
Sourcepub fn set_on_select_cb(
&mut self,
cb: Arc<dyn Fn(&mut Cursive, &T) + Send + Sync>,
)
pub fn set_on_select_cb( &mut self, cb: Arc<dyn Fn(&mut Cursive, &T) + Send + Sync>, )
Helper method to call Self::set_on_select with a variable from a config.
This is mostly useful when writing a cursive blueprint for this view.
Sourcepub fn on_select<F>(self, cb: F) -> Self
pub fn on_select<F>(self, cb: F) -> Self
Sets a callback to be used when an item is selected.
Chainable variant.
§Examples
use cursive_core::traits::Nameable;
use cursive_core::views::{SelectView, TextView};
let text_view = TextView::new("").with_name("text");
let select_view = SelectView::new()
.item("One", 1)
.item("Two", 2)
.on_select(|s, item| {
let content = match *item {
1 => "Content number one",
2 => "Content number two! Much better!",
_ => unreachable!("no such item"),
};
// Update the textview with the currently selected item.
s.call_on_name("text", |v: &mut TextView| {
v.set_content(content);
})
.unwrap();
});Sourcepub fn set_on_submit<F, V: ?Sized>(&mut self, cb: F)
pub fn set_on_submit<F, V: ?Sized>(&mut self, cb: F)
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) -> Self
pub fn on_submit<F, V: ?Sized>(self, cb: F) -> Self
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.
§Examples
use cursive_core::views::{Dialog, SelectView};
let select_view = SelectView::new()
.item("One", 1)
.item("Two", 2)
.on_submit(|s, item| {
let content = match *item {
1 => "Content number one",
2 => "Content number two! Much better!",
_ => unreachable!("no such item"),
};
// Show a popup whenever the user presses <Enter>.
s.add_layer(Dialog::info(content));
});Sourcepub fn align(self, align: Align) -> Self
pub fn align(self, align: Align) -> Self
Sets the alignment for this view.
§Examples
use cursive_core::align;
use cursive_core::views::SelectView;
let select_view = SelectView::new()
.item("One", 1)
.align(align::Align::top_center());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<Arc<T>>
pub fn selection(&self) -> Option<Arc<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.
§Examples
use cursive_core::views::SelectView;
let mut select_view = SelectView::new();
select_view.add_item("Item 1", 1);
select_view.add_item("Item 2", 2);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_core::views::{SelectView, TextView};
use cursive_core::Cursive;
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_mut(&mut self) -> impl Iterator<Item = (&mut StyledString, &mut T)>where
T: Clone,
pub fn iter_mut(&mut self) -> impl Iterator<Item = (&mut StyledString, &mut T)>where
T: Clone,
Iterate mutably on the items in this view.
Returns an iterator with each item and their labels.
In some cases some items will need to be cloned (for example if a
Arc<T> is still alive after calling SelectView::selection()).
If T does not implement Clone, check SelectView::try_iter_mut().
Sourcepub fn try_iter_mut(
&mut self,
) -> impl Iterator<Item = (&mut StyledString, Option<&mut T>)>
pub fn try_iter_mut( &mut self, ) -> impl Iterator<Item = (&mut StyledString, Option<&mut T>)>
Try to iterate mutably on the items in this view.
Returns an iterator with each item and their labels.
Some items may not be returned mutably, for example if a Arc<T> is
still alive after calling SelectView::selection().
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 item<S: Into<StyledString>>(self, label: S, value: T) -> Self
pub fn item<S: Into<StyledString>>(self, label: S, value: T) -> Self
Chainable variant of add_item
§Examples
use cursive_core::views::SelectView;
let select_view = SelectView::new()
.item("Item 1", 1)
.item("Item 2", 2)
.item("Surprise item", 42);Sourcepub fn with_all<S, I>(self, iter: I) -> Self
pub fn with_all<S, I>(self, iter: I) -> Self
Adds all items from from an iterator.
Chainable variant.
§Examples
use cursive_core::views::SelectView;
// Create a SelectView with 100 items
let select_view =
SelectView::new().with_all((1u8..100).into_iter().map(|i| (format!("Item {}", i), i)));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 len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of items in this list.
§Examples
use cursive_core::views::SelectView;
let select_view = SelectView::new()
.item("Item 1", 1)
.item("Item 2", 2)
.item("Item 3", 3);
assert_eq!(select_view.len(), 3);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if this list has no item.
§Examples
use cursive_core::views::SelectView;
let mut select_view = SelectView::new();
assert!(select_view.is_empty());
select_view.add_item("Item 1", 1);
select_view.add_item("Item 2", 2);
assert!(!select_view.is_empty());
select_view.clear();
assert!(select_view.is_empty());Sourcepub fn sort_by_label(&mut self)
pub fn sort_by_label(&mut self)
Sort the current items lexicographically by their label.
Note that this does not change the current focus index, which means that the current selection will likely be changed by the sorting.
This sort is stable: items with identical label will not be reordered.
Sourcepub fn sort_by<F>(&mut self, compare: F)
pub fn sort_by<F>(&mut self, compare: F)
Sort the current items with the given comparator function.
Note that this does not change the current focus index, which means that the current selection will likely be changed by the sorting.
The given comparator function must define a total order for the items.
If the comparator function does not define a total order, then the order after the sort is unspecified.
This sort is stable: equal items will not be reordered.
Sourcepub fn sort_by_key<K, F>(&mut self, key_of: F)
pub fn sort_by_key<K, F>(&mut self, key_of: F)
Sort the current items with the given key extraction function.
Note that this does not change the current focus index, which means that the current selection will likely be changed by the sorting.
This sort is stable: items with equal keys will not be reordered.
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.
Source§impl 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 add_item_styled<S: Into<StyledString>>(&mut self, label: S)
pub fn add_item_styled<S: Into<StyledString>>(&mut self, label: S)
Convenient method to use the label unstyled text as value.
Sourcepub fn item_str<S: Into<String>>(self, label: S) -> Self
pub fn item_str<S: Into<String>>(self, label: S) -> Self
Chainable variant of add_item_str.
§Examples
use cursive_core::views::SelectView;
let select_view = SelectView::new()
.item_str("Paris")
.item_str("New York")
.item_str("Tokyo");Sourcepub fn item_styled<S: Into<StyledString>>(self, label: S) -> Self
pub fn item_styled<S: Into<StyledString>>(self, label: S) -> Self
Chainable variant of add_item_styled.
Sourcepub fn insert_item_str<S>(&mut self, index: usize, label: S)
pub fn insert_item_str<S>(&mut self, index: usize, label: S)
Convenient method to use the label as value.
Sourcepub fn add_all_str<S, I>(&mut self, iter: I)
pub fn add_all_str<S, I>(&mut self, iter: I)
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) -> Self
pub fn with_all_str<S, I>(self, iter: I) -> Self
Adds all strings from an iterator.
Chainable variant.
§Examples
use cursive_core::views::SelectView;
let text = "..."; // Maybe read some config file
let select_view = SelectView::new().with_all_str(text.lines());Source§impl<T> SelectView<T>where
T: Ord + 'static,
impl<T> SelectView<T>where
T: Ord + 'static,
Trait Implementations§
Source§impl<T: 'static + Send + Sync> View for SelectView<T>
impl<T: 'static + Send + Sync> View for SelectView<T>
Source§fn draw(&self, printer: &Printer<'_, '_>)
fn draw(&self, printer: &Printer<'_, '_>)
Source§fn required_size(&mut self, _: Vec2) -> Vec2
fn required_size(&mut self, _: Vec2) -> Vec2
Source§fn on_event(&mut self, event: Event) -> EventResult
fn on_event(&mut self, event: Event) -> EventResult
Source§fn take_focus(&mut self, source: Direction) -> Result<EventResult, CannotFocus>
fn take_focus(&mut self, source: Direction) -> Result<EventResult, CannotFocus>
Source§fn layout(&mut self, size: Vec2)
fn layout(&mut self, size: Vec2)
Source§fn important_area(&self, size: Vec2) -> Rect
fn important_area(&self, size: Vec2) -> Rect
Source§fn needs_relayout(&self) -> bool
fn needs_relayout(&self) -> bool
Source§fn call_on_any(&mut self, _: &Selector<'_>, _: AnyCb<'_>)
fn call_on_any(&mut self, _: &Selector<'_>, _: AnyCb<'_>)
Source§fn focus_view(&mut self, _: &Selector<'_>) -> Result<EventResult, ViewNotFound>
fn focus_view(&mut self, _: &Selector<'_>) -> Result<EventResult, ViewNotFound>
Auto Trait Implementations§
impl<T = String> !Freeze for SelectView<T>
impl<T = String> !RefUnwindSafe for SelectView<T>
impl<T> Send for SelectView<T>
impl<T> Sync for SelectView<T>
impl<T> Unpin for SelectView<T>
impl<T = String> !UnwindSafe for SelectView<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Finder for Twhere
T: View,
impl<T> Finder for Twhere
T: View,
Source§fn call_on_all<V, F>(&mut self, sel: &Selector<'_>, callback: F)
fn call_on_all<V, F>(&mut self, sel: &Selector<'_>, callback: F)
sel. Read moreSource§fn call_on<V, F, R>(&mut self, sel: &Selector<'_>, callback: F) -> Option<R>
fn call_on<V, F, R>(&mut self, sel: &Selector<'_>, callback: F) -> Option<R>
sel. Read moreSource§fn call_on_name<V, F, R>(&mut self, name: &str, callback: F) -> Option<R>
fn call_on_name<V, F, R>(&mut self, name: &str, callback: F) -> Option<R>
call_on with a view::Selector::Name.Source§impl<T> IntoBoxedView for Twhere
T: View,
impl<T> IntoBoxedView for Twhere
T: View,
Source§fn into_boxed_view(self) -> Box<dyn View>
fn into_boxed_view(self) -> Box<dyn View>
Box<View>.Source§impl<T> Resizable for Twhere
T: View,
impl<T> Resizable for Twhere
T: View,
Source§fn resized(
self,
width: SizeConstraint,
height: SizeConstraint,
) -> ResizedView<Self>
fn resized( self, width: SizeConstraint, height: SizeConstraint, ) -> ResizedView<Self>
self in a ResizedView with the given size constraints.Source§fn fixed_size<S: Into<Vec2>>(self, size: S) -> ResizedView<Self>
fn fixed_size<S: Into<Vec2>>(self, size: S) -> ResizedView<Self>
self into a fixed-size ResizedView.Source§fn fixed_width(self, width: usize) -> ResizedView<Self>
fn fixed_width(self, width: usize) -> ResizedView<Self>
self into a fixed-width ResizedView.Source§fn fixed_height(self, height: usize) -> ResizedView<Self>
fn fixed_height(self, height: usize) -> ResizedView<Self>
self into a fixed-width ResizedView.Source§fn full_screen(self) -> ResizedView<Self>
fn full_screen(self) -> ResizedView<Self>
self into a full-screen ResizedView.Source§fn full_width(self) -> ResizedView<Self>
fn full_width(self) -> ResizedView<Self>
self into a full-width ResizedView.Source§fn full_height(self) -> ResizedView<Self>
fn full_height(self) -> ResizedView<Self>
self into a full-height ResizedView.Source§fn max_size<S: Into<Vec2>>(self, size: S) -> ResizedView<Self>
fn max_size<S: Into<Vec2>>(self, size: S) -> ResizedView<Self>
self into a limited-size ResizedView.Source§fn max_width(self, max_width: usize) -> ResizedView<Self>
fn max_width(self, max_width: usize) -> ResizedView<Self>
self into a limited-width ResizedView.Source§fn max_height(self, max_height: usize) -> ResizedView<Self>
fn max_height(self, max_height: usize) -> ResizedView<Self>
self into a limited-height ResizedView.Source§fn min_size<S: Into<Vec2>>(self, size: S) -> ResizedView<Self>
fn min_size<S: Into<Vec2>>(self, size: S) -> ResizedView<Self>
self into a ResizedView at least sized size.Source§fn min_width(self, min_width: usize) -> ResizedView<Self>
fn min_width(self, min_width: usize) -> ResizedView<Self>
self in a ResizedView at least min_width wide.Source§fn min_height(self, min_height: usize) -> ResizedView<Self>
fn min_height(self, min_height: usize) -> ResizedView<Self>
self in a ResizedView at least min_height tall.Source§impl<T> Scrollable for Twhere
T: View,
impl<T> Scrollable for Twhere
T: View,
Source§fn scrollable(self) -> ScrollView<Self>
fn scrollable(self) -> ScrollView<Self>
self in a ScrollView.