2015-05-15 01:38:58 +00:00
|
|
|
use std::cmp::max;
|
2015-05-15 00:41:17 +00:00
|
|
|
|
2015-05-15 18:58:47 +00:00
|
|
|
use vec2::Vec2;
|
2015-05-15 01:38:58 +00:00
|
|
|
use view::{View,SizeRequest};
|
|
|
|
use event::EventResult;
|
2015-05-15 18:58:47 +00:00
|
|
|
use printer::Printer;
|
2015-05-15 01:38:58 +00:00
|
|
|
|
2015-05-15 00:41:17 +00:00
|
|
|
/// Simple stack of views.
|
|
|
|
/// Only the top-most view is active and can receive input.
|
|
|
|
pub struct StackView {
|
|
|
|
layers: Vec<Box<View>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl StackView {
|
|
|
|
/// Creates a new empty StackView
|
|
|
|
pub fn new() -> Self {
|
|
|
|
StackView {
|
|
|
|
layers: Vec::new(),
|
|
|
|
}
|
|
|
|
}
|
2015-05-15 00:48:24 +00:00
|
|
|
|
2015-05-15 01:38:58 +00:00
|
|
|
/// Add new view on top of the stack.
|
|
|
|
pub fn add_layer<T: 'static + View>(&mut self, view: T) {
|
|
|
|
self.layers.push(Box::new(view));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Remove the top-most layer.
|
|
|
|
pub fn pop_layer(&mut self) {
|
|
|
|
self.layers.pop();
|
2015-05-15 00:48:24 +00:00
|
|
|
}
|
2015-05-15 00:41:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl View for StackView {
|
2015-05-15 18:58:47 +00:00
|
|
|
fn draw(&self, printer: &Printer) {
|
2015-05-15 00:48:24 +00:00
|
|
|
match self.layers.last() {
|
|
|
|
None => (),
|
2015-05-15 18:58:47 +00:00
|
|
|
Some(v) => v.draw(printer),
|
2015-05-15 00:48:24 +00:00
|
|
|
}
|
2015-05-15 00:41:17 +00:00
|
|
|
}
|
2015-05-15 01:38:58 +00:00
|
|
|
|
|
|
|
fn on_key_event(&mut self, ch: i32) -> EventResult {
|
|
|
|
match self.layers.last_mut() {
|
|
|
|
None => EventResult::Ignored,
|
|
|
|
Some(v) => v.on_key_event(ch),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-15 18:58:47 +00:00
|
|
|
fn get_min_size(&self, size: SizeRequest) -> Vec2 {
|
2015-05-15 01:38:58 +00:00
|
|
|
// The min size is the max of all children's
|
2015-05-15 18:58:47 +00:00
|
|
|
let mut s = Vec2::new(1,1);
|
2015-05-15 01:38:58 +00:00
|
|
|
|
|
|
|
for view in self.layers.iter() {
|
|
|
|
let vs = view.get_min_size(size);
|
2015-05-15 18:58:47 +00:00
|
|
|
s.x = max(s.x, vs.x);
|
|
|
|
s.y = max(s.y, vs.y);
|
2015-05-15 01:38:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s
|
|
|
|
}
|
2015-05-15 00:41:17 +00:00
|
|
|
}
|