2016-10-02 22:22:29 +00:00
|
|
|
use Printer;
|
2015-05-15 00:41:17 +00:00
|
|
|
|
2016-07-15 03:27:15 +00:00
|
|
|
use direction::Direction;
|
2016-10-02 22:22:29 +00:00
|
|
|
use event::{Event, EventResult};
|
|
|
|
use std::any::Any;
|
|
|
|
use theme::ColorStyle;
|
2015-05-18 18:51:30 +00:00
|
|
|
use vec::Vec2;
|
2016-07-28 23:36:01 +00:00
|
|
|
use view::{Offset, Position, Selector, View};
|
|
|
|
use views::ShadowView;
|
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>,
|
2016-07-20 07:30:00 +00:00
|
|
|
last_size: Vec2,
|
2016-10-09 22:47:06 +00:00
|
|
|
needs_clear: bool,
|
2015-05-15 23:06:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Layer {
|
|
|
|
view: Box<View>,
|
|
|
|
size: Vec2,
|
2016-07-02 02:19:43 +00:00
|
|
|
position: Position,
|
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-07-17 00:28:42 +00:00
|
|
|
new_default!(StackView);
|
2016-06-28 05:40:11 +00:00
|
|
|
|
2015-05-15 00:41:17 +00:00
|
|
|
impl StackView {
|
|
|
|
/// Creates a new empty StackView
|
|
|
|
pub fn new() -> Self {
|
2016-07-20 07:30:00 +00:00
|
|
|
StackView {
|
|
|
|
layers: Vec::new(),
|
|
|
|
last_size: Vec2::zero(),
|
2016-10-09 22:47:06 +00:00
|
|
|
needs_clear: false,
|
2016-07-20 07:30:00 +00:00
|
|
|
}
|
2015-05-15 00:41:17 +00:00
|
|
|
}
|
2015-05-15 00:48:24 +00:00
|
|
|
|
2016-07-02 02:19:43 +00:00
|
|
|
/// Adds new view on top of the stack in the center of the screen.
|
2015-05-31 04:53:25 +00:00
|
|
|
pub fn add_layer<T: 'static + View>(&mut self, view: T) {
|
2016-07-02 02:19:43 +00:00
|
|
|
self.add_layer_at(Position::center(), view);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds a view on top of the stack.
|
2016-07-02 22:02:42 +00:00
|
|
|
pub fn add_layer_at<T: 'static + View>(&mut self, position: Position,
|
|
|
|
view: T) {
|
2015-05-15 23:06:48 +00:00
|
|
|
self.layers.push(Layer {
|
2016-07-03 02:46:23 +00:00
|
|
|
// Skip padding for absolute/parent-placed views
|
|
|
|
view: Box::new(ShadowView::new(view)
|
2016-07-10 02:05:51 +00:00
|
|
|
.top_padding(position.y == Offset::Center)
|
|
|
|
.left_padding(position.x == Offset::Center)),
|
2016-03-15 22:37:57 +00:00
|
|
|
size: Vec2::new(0, 0),
|
2016-07-02 02:19:43 +00:00
|
|
|
position: position,
|
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();
|
2016-10-09 22:47:06 +00:00
|
|
|
self.needs_clear = true;
|
2015-05-15 00:48:24 +00:00
|
|
|
}
|
2016-07-20 07:30:00 +00:00
|
|
|
|
|
|
|
/// Computes the offset of the current top view.
|
|
|
|
pub fn offset(&self) -> Vec2 {
|
|
|
|
let mut previous = Vec2::zero();
|
|
|
|
for layer in &self.layers {
|
|
|
|
let offset = layer.position
|
|
|
|
.compute_offset(layer.size, self.last_size, previous);
|
|
|
|
previous = offset;
|
|
|
|
}
|
|
|
|
previous
|
|
|
|
}
|
2015-05-15 00:41:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl View for StackView {
|
2016-07-16 06:44:38 +00:00
|
|
|
fn draw(&self, printer: &Printer) {
|
2016-10-09 22:47:06 +00:00
|
|
|
if self.needs_clear && printer.is_new() {
|
|
|
|
printer.clear();
|
|
|
|
}
|
2015-05-31 04:53:25 +00:00
|
|
|
let last = self.layers.len();
|
2016-07-02 02:19:43 +00:00
|
|
|
let mut previous = Vec2::zero();
|
2016-07-01 06:38:01 +00:00
|
|
|
printer.with_color(ColorStyle::Primary, |printer| {
|
2016-07-16 06:44:38 +00:00
|
|
|
for (i, v) in self.layers.iter().enumerate() {
|
2016-07-02 02:19:43 +00:00
|
|
|
// Place the view
|
2016-06-30 00:36:20 +00:00
|
|
|
// Center the view
|
2016-07-03 02:46:23 +00:00
|
|
|
let offset = v.position
|
2016-07-10 02:05:51 +00:00
|
|
|
.compute_offset(v.size, printer.size, previous);
|
2016-07-03 02:37:38 +00:00
|
|
|
|
2016-07-02 02:19:43 +00:00
|
|
|
previous = offset;
|
2016-07-02 22:02:42 +00:00
|
|
|
v.view
|
2016-07-10 02:05:51 +00:00
|
|
|
.draw(&printer.sub_printer(offset, v.size, i + 1 == last));
|
2016-06-30 00:36:20 +00:00
|
|
|
}
|
|
|
|
});
|
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) {
|
2016-07-20 07:30:00 +00:00
|
|
|
self.last_size = size;
|
|
|
|
|
2016-07-02 02:19:43 +00:00
|
|
|
// The call has been made, we can't ask for more space anymore.
|
|
|
|
// Let's make do with what we have.
|
|
|
|
|
2016-06-28 05:40:11 +00:00
|
|
|
for layer in &mut self.layers {
|
2016-07-02 02:19:43 +00:00
|
|
|
// Give each guy what he asks for, within the budget constraints.
|
2016-10-02 22:33:55 +00:00
|
|
|
let size = Vec2::min(size, layer.view.get_min_size(size));
|
|
|
|
if !layer.size.fits_in(size) {
|
2016-10-09 22:47:06 +00:00
|
|
|
self.needs_clear = true;
|
2016-10-02 22:33:55 +00:00
|
|
|
}
|
|
|
|
layer.size = size;
|
2015-05-15 23:06:48 +00:00
|
|
|
layer.view.layout(layer.size);
|
2016-07-02 02:19:43 +00:00
|
|
|
|
2016-06-28 05:10:59 +00:00
|
|
|
// We do it here instead of when adding a new layer because...?
|
2016-07-02 02:19:43 +00:00
|
|
|
// (TODO: try to make it during layer addition)
|
2015-05-31 04:53:25 +00:00
|
|
|
if layer.virgin {
|
2016-07-15 03:27:15 +00:00
|
|
|
layer.view.take_focus(Direction::none());
|
2015-05-31 04:53:25 +00:00
|
|
|
layer.virgin = false;
|
|
|
|
}
|
2015-05-15 01:38:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-10 01:23:58 +00:00
|
|
|
fn get_min_size(&mut self, size: Vec2) -> Vec2 {
|
2015-05-15 01:38:58 +00:00
|
|
|
// The min size is the max of all children's
|
|
|
|
|
2016-07-02 07:47:38 +00:00
|
|
|
self.layers
|
2016-07-10 01:23:58 +00:00
|
|
|
.iter_mut()
|
2016-07-02 07:47:38 +00:00
|
|
|
.map(|layer| layer.view.get_min_size(size))
|
|
|
|
.fold(Vec2::new(1, 1), Vec2::max)
|
2015-05-15 01:38:58 +00:00
|
|
|
}
|
2015-05-19 22:54:11 +00:00
|
|
|
|
2016-07-15 03:27:15 +00:00
|
|
|
fn take_focus(&mut self, source: Direction) -> bool {
|
2015-05-19 22:54:11 +00:00
|
|
|
match self.layers.last_mut() {
|
|
|
|
None => false,
|
2016-07-15 03:27:15 +00:00
|
|
|
Some(mut v) => v.view.take_focus(source),
|
2015-05-19 22:54:11 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-23 17:33:29 +00:00
|
|
|
|
2016-09-21 18:33:44 +00:00
|
|
|
fn find_any(&mut self, selector: &Selector) -> Option<&mut Any> {
|
|
|
|
self.layers
|
|
|
|
.iter_mut()
|
|
|
|
.filter_map(|l| l.view.find_any(selector))
|
|
|
|
.next()
|
2015-05-23 17:33:29 +00:00
|
|
|
}
|
2015-05-15 00:41:17 +00:00
|
|
|
}
|