2015-05-23 17:33:29 +00:00
|
|
|
use std::any::Any;
|
2015-05-15 00:41:17 +00:00
|
|
|
|
2015-05-18 18:51:30 +00:00
|
|
|
use vec::Vec2;
|
2016-06-28 05:10:59 +00:00
|
|
|
use view::{DimensionRequest, Selector, ShadowView, SizeRequest, View};
|
2016-03-15 22:37:57 +00:00
|
|
|
use event::{Event, EventResult};
|
2015-05-15 18:58:47 +00:00
|
|
|
use printer::Printer;
|
2016-06-30 00:36:20 +00:00
|
|
|
use theme::ColorPair;
|
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 {
|
2015-05-15 23:06:48 +00:00
|
|
|
layers: Vec<Layer>,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Layer {
|
|
|
|
view: Box<View>,
|
|
|
|
size: Vec2,
|
2015-05-31 04:53:25 +00:00
|
|
|
// Has it received the gift yet?
|
|
|
|
virgin: bool,
|
2015-05-15 00:41:17 +00:00
|
|
|
}
|
|
|
|
|
2016-06-28 05:40:11 +00:00
|
|
|
impl Default for StackView {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-15 00:41:17 +00:00
|
|
|
impl StackView {
|
|
|
|
/// Creates a new empty StackView
|
|
|
|
pub fn new() -> Self {
|
2016-03-15 22:37:57 +00:00
|
|
|
StackView { layers: Vec::new() }
|
2015-05-15 00:41:17 +00:00
|
|
|
}
|
2015-05-15 00:48:24 +00:00
|
|
|
|
2015-05-15 01:38:58 +00:00
|
|
|
/// Add new view on top of the stack.
|
2015-05-31 04:53:25 +00:00
|
|
|
pub fn add_layer<T: 'static + View>(&mut self, view: T) {
|
2015-05-15 23:06:48 +00:00
|
|
|
self.layers.push(Layer {
|
2015-05-23 22:58:06 +00:00
|
|
|
view: Box::new(ShadowView::new(view)),
|
2016-03-15 22:37:57 +00:00
|
|
|
size: Vec2::new(0, 0),
|
2015-05-31 04:53:25 +00:00
|
|
|
virgin: true,
|
2015-05-15 23:06:48 +00:00
|
|
|
});
|
2015-05-15 01:38:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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-31 04:05:34 +00:00
|
|
|
fn draw(&mut self, printer: &Printer) {
|
2015-05-31 04:53:25 +00:00
|
|
|
let last = self.layers.len();
|
2016-06-30 00:36:20 +00:00
|
|
|
printer.with_color(ColorPair::Primary, |printer| {
|
|
|
|
for (i, v) in self.layers.iter_mut().enumerate() {
|
|
|
|
// Center the view
|
|
|
|
let size = v.size;
|
|
|
|
let offset = (printer.size - size) / 2;
|
|
|
|
// TODO: only draw focus for the top view
|
|
|
|
v.view.draw(&printer.sub_printer(offset, size, i + 1 == last));
|
|
|
|
}
|
|
|
|
});
|
2015-05-15 00:41:17 +00:00
|
|
|
}
|
2015-05-15 01:38:58 +00:00
|
|
|
|
2015-05-28 01:04:33 +00:00
|
|
|
fn on_event(&mut self, event: Event) -> EventResult {
|
2015-05-15 01:38:58 +00:00
|
|
|
match self.layers.last_mut() {
|
|
|
|
None => EventResult::Ignored,
|
2015-05-28 01:04:33 +00:00
|
|
|
Some(v) => v.view.on_event(event),
|
2015-05-15 23:06:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn layout(&mut self, size: Vec2) {
|
|
|
|
let req = SizeRequest {
|
|
|
|
w: DimensionRequest::AtMost(size.x),
|
|
|
|
h: DimensionRequest::AtMost(size.y),
|
|
|
|
};
|
2016-06-28 05:40:11 +00:00
|
|
|
for layer in &mut self.layers {
|
2015-05-22 06:29:49 +00:00
|
|
|
layer.size = Vec2::min(size, layer.view.get_min_size(req));
|
2015-05-15 23:06:48 +00:00
|
|
|
layer.view.layout(layer.size);
|
2016-06-28 05:10:59 +00:00
|
|
|
// We do it here instead of when adding a new layer because...?
|
2015-05-31 04:53:25 +00:00
|
|
|
if layer.virgin {
|
|
|
|
layer.view.take_focus();
|
|
|
|
layer.virgin = false;
|
|
|
|
}
|
2015-05-15 01:38:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2016-03-15 22:37:57 +00:00
|
|
|
let mut s = Vec2::new(1, 1);
|
2015-05-15 01:38:58 +00:00
|
|
|
|
2016-06-28 05:40:11 +00:00
|
|
|
for layer in &self.layers {
|
2015-05-15 23:06:48 +00:00
|
|
|
let vs = layer.view.get_min_size(size);
|
2015-05-22 07:43:58 +00:00
|
|
|
s = Vec2::max(s, vs);
|
2015-05-15 01:38:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s
|
|
|
|
}
|
2015-05-19 22:54:11 +00:00
|
|
|
|
|
|
|
fn take_focus(&mut self) -> bool {
|
|
|
|
match self.layers.last_mut() {
|
|
|
|
None => false,
|
2016-03-15 22:37:57 +00:00
|
|
|
Some(mut v) => v.view.take_focus(),
|
2015-05-19 22:54:11 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-23 17:33:29 +00:00
|
|
|
|
|
|
|
fn find(&mut self, selector: &Selector) -> Option<&mut Any> {
|
2016-06-28 05:40:11 +00:00
|
|
|
for layer in &mut self.layers {
|
2015-05-23 17:33:29 +00:00
|
|
|
if let Some(any) = layer.view.find(selector) {
|
|
|
|
return Some(any);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
2015-05-15 00:41:17 +00:00
|
|
|
}
|