2015-05-31 23:38:53 +00:00
|
|
|
use std::cmp::min;
|
|
|
|
|
2015-05-31 04:05:34 +00:00
|
|
|
use color;
|
2015-05-31 23:38:53 +00:00
|
|
|
use view::{View,IdView,SizeRequest,DimensionRequest};
|
2015-05-31 04:05:34 +00:00
|
|
|
use event::{Event,EventResult,Key};
|
|
|
|
use vec::Vec2;
|
|
|
|
use printer::Printer;
|
2015-05-31 23:38:53 +00:00
|
|
|
use super::scroll::ScrollBase;
|
2015-05-31 04:05:34 +00:00
|
|
|
|
|
|
|
struct Item<T> {
|
|
|
|
label: String,
|
|
|
|
value: T,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl <T> Item<T> {
|
|
|
|
fn new(label: &str, value: T) -> Self {
|
|
|
|
Item {
|
|
|
|
label: label.to_string(),
|
|
|
|
value: value,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-31 04:32:24 +00:00
|
|
|
/// 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> {
|
2015-05-31 04:05:34 +00:00
|
|
|
items: Vec<Item<T>>,
|
|
|
|
focus: usize,
|
2015-05-31 23:38:53 +00:00
|
|
|
scrollbase: ScrollBase,
|
2015-05-31 04:05:34 +00:00
|
|
|
}
|
|
|
|
|
2015-05-31 04:32:24 +00:00
|
|
|
impl <T> SelectView<T> {
|
|
|
|
/// Creates a new empty SelectView.
|
2015-05-31 04:05:34 +00:00
|
|
|
pub fn new() -> Self {
|
2015-05-31 04:32:24 +00:00
|
|
|
SelectView {
|
2015-05-31 04:05:34 +00:00
|
|
|
items: Vec::new(),
|
|
|
|
focus: 0,
|
2015-05-31 23:38:53 +00:00
|
|
|
scrollbase: ScrollBase::new(),
|
2015-05-31 04:05:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-31 04:32:24 +00:00
|
|
|
/// Returns the value of the currently selected item. Panics if the list is empty.
|
2015-05-31 04:05:34 +00:00
|
|
|
pub fn selection(&self) -> &T {
|
|
|
|
&self.items[self.focus].value
|
|
|
|
}
|
|
|
|
|
2015-05-31 04:32:24 +00:00
|
|
|
/// Adds a item to the list, with given label and value.
|
2015-05-31 23:38:53 +00:00
|
|
|
pub fn add_item(&mut self, label: &str, value: T) {
|
2015-05-31 04:05:34 +00:00
|
|
|
self.items.push(Item::new(label,value));
|
2015-05-31 23:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Chainable variant of add_item
|
|
|
|
pub fn item(mut self, label: &str, value: T) -> Self {
|
|
|
|
self.add_item(label, value);
|
2015-05-31 04:05:34 +00:00
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2015-05-31 04:32:24 +00:00
|
|
|
/// Wraps this view into an IdView with the given id.
|
2015-05-31 04:05:34 +00:00
|
|
|
pub fn with_id(self, label: &str) -> IdView<Self> {
|
|
|
|
IdView::new(label, self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-31 04:32:24 +00:00
|
|
|
impl SelectView<String> {
|
|
|
|
/// For String-based SelectView, this is a convenient method to use the label as value.
|
2015-05-31 23:58:55 +00:00
|
|
|
pub fn add_item_str(&mut self, label: &str) {
|
|
|
|
self.add_item(label, label.to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Chainable variant of add_item_str
|
2015-05-31 04:05:34 +00:00
|
|
|
pub fn item_str(self, label: &str) -> Self {
|
|
|
|
self.item(label, label.to_string())
|
|
|
|
}
|
2015-05-31 23:38:53 +00:00
|
|
|
|
2015-05-31 04:05:34 +00:00
|
|
|
}
|
|
|
|
|
2015-05-31 04:32:24 +00:00
|
|
|
impl <T> View for SelectView<T> {
|
2015-05-31 04:05:34 +00:00
|
|
|
fn draw(&mut self, printer: &Printer) {
|
2015-05-31 23:38:53 +00:00
|
|
|
|
|
|
|
self.scrollbase.draw(printer, |printer,i| {
|
|
|
|
let style = if i == self.focus {
|
|
|
|
if printer.focused { color::HIGHLIGHT }
|
|
|
|
else { color::HIGHLIGHT_INACTIVE }
|
|
|
|
} else {
|
|
|
|
color::PRIMARY
|
|
|
|
};
|
|
|
|
printer.with_color(style, |printer| printer.print((0,0), &self.items[i].label));
|
|
|
|
});
|
2015-05-31 04:05:34 +00:00
|
|
|
}
|
|
|
|
|
2015-05-31 23:38:53 +00:00
|
|
|
fn get_min_size(&self, req: SizeRequest) -> Vec2 {
|
2015-05-31 04:05:34 +00:00
|
|
|
let w = self.items.iter().map(|item| item.label.len()).max().unwrap_or(1);
|
|
|
|
let h = self.items.len();
|
2015-05-31 23:38:53 +00:00
|
|
|
|
|
|
|
let scrolling = if let DimensionRequest::Fixed(r_h) = req.h {
|
|
|
|
r_h < h
|
|
|
|
} else if let DimensionRequest::AtMost(r_h) = req.h {
|
|
|
|
r_h < h
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
|
|
|
|
|
|
|
let w = if scrolling { w + 1 } else { w };
|
|
|
|
|
2015-05-31 04:05:34 +00:00
|
|
|
Vec2::new(w,h)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn on_event(&mut self, event: Event) -> EventResult {
|
|
|
|
match event {
|
2015-05-31 23:38:53 +00:00
|
|
|
Event::KeyEvent(Key::Up) if self.focus > 0 => self.focus -= 1,
|
|
|
|
Event::KeyEvent(Key::Down) if self.focus + 1 < self.items.len() => self.focus += 1,
|
|
|
|
Event::KeyEvent(Key::PageUp) => self.focus -= min(self.focus,10),
|
|
|
|
Event::KeyEvent(Key::PageDown) => self.focus = min(self.focus+10,self.items.len()-1),
|
|
|
|
Event::KeyEvent(Key::Home) => self.focus = 0,
|
|
|
|
Event::KeyEvent(Key::End) => self.focus = self.items.len()-1,
|
|
|
|
Event::CharEvent(c) => {
|
|
|
|
// Starting from the current focus, find the first item that match the char.
|
|
|
|
if let Some((i,_)) = self.items.iter().enumerate()
|
|
|
|
.skip(self.focus+1)
|
|
|
|
.find(|&(_,item)| item.label.starts_with(c))
|
|
|
|
{
|
|
|
|
self.focus = i;
|
|
|
|
}
|
2015-05-31 04:05:34 +00:00
|
|
|
},
|
|
|
|
_ => return EventResult::Ignored,
|
|
|
|
}
|
|
|
|
|
2015-05-31 23:38:53 +00:00
|
|
|
self.scrollbase.scroll_to(self.focus);
|
|
|
|
|
2015-05-31 04:05:34 +00:00
|
|
|
EventResult::Consumed(None)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn take_focus(&mut self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
2015-05-31 23:38:53 +00:00
|
|
|
|
|
|
|
fn layout(&mut self, size: Vec2) {
|
|
|
|
self.scrollbase.set_heights(size.y, self.items.len());
|
|
|
|
}
|
2015-05-31 04:05:34 +00:00
|
|
|
}
|