pub struct MultipleChoiceView<T = String> { /* private fields */ }Expand description
View to make a selection from multiple items.
It contains a list of values of type T, with associated labels.
Implementations§
Source§impl<T: 'static + Send + Sync> MultipleChoiceView<T>
impl<T: 'static + Send + Sync> MultipleChoiceView<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_choice_indicators(&mut self, selection_indicators: [&'static str; 2])
pub fn set_choice_indicators(&mut self, selection_indicators: [&'static str; 2])
Sets the visual indicators for chosen and not chosen items.
The first indicator represents the not chosen variant.
§Examples
use cursive_core::traits::Nameable;
use cursive_multiple_choice_view::MultipleChoiceView;
let mut multiple_choice_view: MultipleChoiceView<String> = MultipleChoiceView::new();
multiple_choice_view.set_choice_indicators(["☐", "☒"]);Sourcepub fn with_choice_indicators(
self,
selection_indicators: [&'static str; 2],
) -> Self
pub fn with_choice_indicators( self, selection_indicators: [&'static str; 2], ) -> Self
Sets the visual indicators for chosen and not chosen items.
The first indicator represents the not chosen variant.
Chainable variant.
§Examples
use cursive_core::traits::Nameable;
use cursive_multiple_choice_view::MultipleChoiceView;
let multiple_choice_view: MultipleChoiceView<String> = MultipleChoiceView::new()
.with_choice_indicators(["☐", "☒"]);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 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<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::TextView;
use cursive_multiple_choice_view::MultipleChoiceView;
let text_view = TextView::new("").with_name("text");
let multiple_choice_view = MultipleChoiceView::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;
use cursive_multiple_choice_view::MultipleChoiceView;
let multiple_choice_view = MultipleChoiceView::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_multiple_choice_view::MultipleChoiceView;
let multiple_choice_view = MultipleChoiceView::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_multiple_choice_view::MultipleChoiceView;
let mut multiple_choice_view = MultipleChoiceView::new();
multiple_choice_view.add_item("Item 1", 1);
multiple_choice_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::TextView;
use cursive_multiple_choice_view::MultipleChoiceView;
use cursive_core::Cursive;
let select = MultipleChoiceView::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 MultipleChoiceView::selection()).
If T does not implement Clone, check MultipleChoiceView::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 MultipleChoiceView::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 add_item_with_choice<S: Into<StyledString>>(
&mut self,
label: S,
value: T,
chosen: bool,
)
pub fn add_item_with_choice<S: Into<StyledString>>( &mut self, label: S, value: T, chosen: bool, )
Adds a item to the list, with given label, value and whether it should be marked as chosen.
§Examples
use cursive_multiple_choice_view::MultipleChoiceView;
let mut multiple_choice_view = MultipleChoiceView::new();
multiple_choice_view.add_item_with_choice("Item 1", 1, true);
multiple_choice_view.add_item_with_choice("Item 2", 2, false);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.
pub fn insert_item_with_choice<S>(
&mut self,
index: usize,
label: S,
value: T,
chosen: bool,
)where
S: Into<StyledString>,
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_multiple_choice_view::MultipleChoiceView;
let multiple_choice_view = MultipleChoiceView::new()
.item("Item 1", 1)
.item("Item 2", 2)
.item("Surprise item", 42);pub fn item_with_choice<S: Into<StyledString>>( self, label: S, value: T, chosen: bool, ) -> Self
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_multiple_choice_view::MultipleChoiceView;
// Create a MultipleChoiceView with 100 items
let multiple_choice_view =
MultipleChoiceView::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_multiple_choice_view::MultipleChoiceView;
let multiple_choice_view = MultipleChoiceView::new()
.item("Item 1", 1)
.item("Item 2", 2)
.item("Item 3", 3);
assert_eq!(multiple_choice_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_multiple_choice_view::MultipleChoiceView;
let mut multiple_choice_view = MultipleChoiceView::new();
assert!(multiple_choice_view.is_empty());
multiple_choice_view.add_item("Item 1", 1);
multiple_choice_view.add_item("Item 2", 2);
assert!(!multiple_choice_view.is_empty());
multiple_choice_view.clear();
assert!(multiple_choice_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 get_choice(&self) -> Vec<Arc<T>>
pub fn get_choice(&self) -> Vec<Arc<T>>
Get all chosen items
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 MultipleChoiceView<()>) {
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 MultipleChoiceView<String>
impl MultipleChoiceView<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_str_with_choice<S: Into<String>>(
&mut self,
label: S,
chosen: bool,
)
pub fn add_item_str_with_choice<S: Into<String>>( &mut self, label: S, chosen: bool, )
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_multiple_choice_view::MultipleChoiceView;
let multiple_choice_view = MultipleChoiceView::new()
.item_str("Paris")
.item_str("New York")
.item_str("Tokyo");Sourcepub fn item_str_with_choice<S: Into<String>>(
self,
label: S,
chosen: bool,
) -> Self
pub fn item_str_with_choice<S: Into<String>>( self, label: S, chosen: bool, ) -> Self
Chainable variant of add_item_str_with_choice.
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 multiple_choice_view = MultipleChoiceView::new();
multiple_choice_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_multiple_choice_view::MultipleChoiceView;
let text = "..."; // Maybe read some config file
let multiple_choice_view = MultipleChoiceView::new().with_all_str(text.lines());Source§impl<T> MultipleChoiceView<T>where
T: Ord + 'static,
impl<T> MultipleChoiceView<T>where
T: Ord + 'static,
Trait Implementations§
Source§impl<T: 'static + Send + Sync> View for MultipleChoiceView<T>
impl<T: 'static + Send + Sync> View for MultipleChoiceView<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<'_>,
_: &mut dyn FnMut(&mut (dyn View + 'static)),
)
fn call_on_any( &mut self, _: &Selector<'_>, _: &mut dyn FnMut(&mut (dyn View + 'static)), )
Source§fn focus_view(&mut self, _: &Selector<'_>) -> Result<EventResult, ViewNotFound>
fn focus_view(&mut self, _: &Selector<'_>) -> Result<EventResult, ViewNotFound>
Auto Trait Implementations§
impl<T> Freeze for MultipleChoiceView<T>
impl<T = String> !RefUnwindSafe for MultipleChoiceView<T>
impl<T> Send for MultipleChoiceView<T>
impl<T> Sync for MultipleChoiceView<T>
impl<T> Unpin for MultipleChoiceView<T>
impl<T = String> !UnwindSafe for MultipleChoiceView<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>(self, size: S) -> ResizedView<Self>
fn fixed_size<S>(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>(self, size: S) -> ResizedView<Self>
fn max_size<S>(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>(self, size: S) -> ResizedView<Self>
fn min_size<S>(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.