mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-23 17:35:00 +00:00
Rename ListView -> SelectView
And added documentation.
This commit is contained in:
parent
969650ab1a
commit
6b781684ef
@ -46,5 +46,5 @@ name = "key_codes"
|
|||||||
path = "examples/key_codes.rs"
|
path = "examples/key_codes.rs"
|
||||||
|
|
||||||
[[example]]
|
[[example]]
|
||||||
name = "list"
|
name = "select"
|
||||||
path = "examples/list.rs"
|
path = "examples/select.rs"
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
extern crate cursive;
|
extern crate cursive;
|
||||||
|
|
||||||
use cursive::Cursive;
|
use cursive::Cursive;
|
||||||
use cursive::view::{Dialog,ListView,TextView,Selector};
|
use cursive::view::{Dialog,SelectView,TextView,Selector};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut siv = Cursive::new();
|
let mut siv = Cursive::new();
|
||||||
|
|
||||||
siv.add_layer(Dialog::new(ListView::new()
|
siv.add_layer(Dialog::new(SelectView::new()
|
||||||
.item_str("Berlin")
|
.item_str("Berlin")
|
||||||
.item_str("London")
|
.item_str("London")
|
||||||
.item_str("New York")
|
.item_str("New York")
|
||||||
@ -14,7 +14,7 @@ fn main() {
|
|||||||
.with_id("city"))
|
.with_id("city"))
|
||||||
.title("Where are you from?")
|
.title("Where are you from?")
|
||||||
.button("Ok", |s| {
|
.button("Ok", |s| {
|
||||||
let city = s.find::<ListView>(&Selector::Id("city")).unwrap().selection().to_string();
|
let city = s.find::<SelectView>(&Selector::Id("city")).unwrap().selection().to_string();
|
||||||
s.pop_layer();
|
s.pop_layer();
|
||||||
s.add_layer(Dialog::new(TextView::new(&format!("{} is a great city!", city)))
|
s.add_layer(Dialog::new(TextView::new(&format!("{} is a great city!", city)))
|
||||||
.button("Quit", |s| s.quit()));
|
.button("Quit", |s| s.quit()));
|
@ -13,7 +13,7 @@ mod full_view;
|
|||||||
mod id_view;
|
mod id_view;
|
||||||
mod shadow_view;
|
mod shadow_view;
|
||||||
mod edit_view;
|
mod edit_view;
|
||||||
mod list_view;
|
mod select_view;
|
||||||
|
|
||||||
use std::any::Any;
|
use std::any::Any;
|
||||||
|
|
||||||
@ -30,7 +30,7 @@ pub use self::full_view::FullView;
|
|||||||
pub use self::id_view::IdView;
|
pub use self::id_view::IdView;
|
||||||
pub use self::shadow_view::ShadowView;
|
pub use self::shadow_view::ShadowView;
|
||||||
pub use self::edit_view::EditView;
|
pub use self::edit_view::EditView;
|
||||||
pub use self::list_view::ListView;
|
pub use self::select_view::SelectView;
|
||||||
|
|
||||||
use event::{Event,EventResult};
|
use event::{Event,EventResult};
|
||||||
use vec::{Vec2,ToVec2};
|
use vec::{Vec2,ToVec2};
|
||||||
|
@ -18,41 +18,49 @@ impl <T> Item<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ListView<T=String> {
|
/// View to select an item among a list.
|
||||||
|
///
|
||||||
|
/// It contains a list of values of type T, with associated labels.
|
||||||
|
pub struct SelectView<T=String> {
|
||||||
items: Vec<Item<T>>,
|
items: Vec<Item<T>>,
|
||||||
focus: usize,
|
focus: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <T> ListView<T> {
|
impl <T> SelectView<T> {
|
||||||
|
/// Creates a new empty SelectView.
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
ListView {
|
SelectView {
|
||||||
items: Vec::new(),
|
items: Vec::new(),
|
||||||
focus: 0,
|
focus: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the value of the currently selected item. Panics if the list is empty.
|
||||||
pub fn selection(&self) -> &T {
|
pub fn selection(&self) -> &T {
|
||||||
&self.items[self.focus].value
|
&self.items[self.focus].value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Adds a item to the list, with given label and value.
|
||||||
pub fn item(mut self, label: &str, value: T) -> Self {
|
pub fn item(mut self, label: &str, value: T) -> Self {
|
||||||
self.items.push(Item::new(label,value));
|
self.items.push(Item::new(label,value));
|
||||||
|
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Wraps this view into an IdView with the given id.
|
||||||
pub fn with_id(self, label: &str) -> IdView<Self> {
|
pub fn with_id(self, label: &str) -> IdView<Self> {
|
||||||
IdView::new(label, self)
|
IdView::new(label, self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ListView<String> {
|
impl SelectView<String> {
|
||||||
|
/// For String-based SelectView, this is a convenient method to use the label as value.
|
||||||
pub fn item_str(self, label: &str) -> Self {
|
pub fn item_str(self, label: &str) -> Self {
|
||||||
self.item(label, label.to_string())
|
self.item(label, label.to_string())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <T> View for ListView<T> {
|
impl <T> View for SelectView<T> {
|
||||||
fn draw(&mut self, printer: &Printer) {
|
fn draw(&mut self, printer: &Printer) {
|
||||||
for (i,item) in self.items.iter().enumerate() {
|
for (i,item) in self.items.iter().enumerate() {
|
||||||
let style = if i == self.focus { if printer.focused { color::HIGHLIGHT } else { color::HIGHLIGHT_INACTIVE } } else { color::PRIMARY };
|
let style = if i == self.focus { if printer.focused { color::HIGHLIGHT } else { color::HIGHLIGHT_INACTIVE } } else { color::PRIMARY };
|
Loading…
Reference in New Issue
Block a user