cursive/src/view/stack_view.rs

121 lines
3.3 KiB
Rust
Raw Normal View History

2015-05-23 17:33:29 +00:00
use std::any::Any;
use vec::Vec2;
use view::{Selector, ShadowView, View, Position};
2016-03-15 22:37:57 +00:00
use event::{Event, EventResult};
use printer::Printer;
use theme::ColorStyle;
2015-05-15 01:38:58 +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,
position: Position,
// Has it received the gift yet?
virgin: bool,
}
2016-06-28 05:40:11 +00:00
impl Default for StackView {
fn default() -> Self {
Self::new()
}
}
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:48:24 +00:00
/// Adds new view on top of the stack in the center of the screen.
pub fn add_layer<T: 'static + View>(&mut self, view: T) {
self.add_layer_at(Position::center(), view);
}
/// Adds a view on top of the stack.
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 {
view: Box::new(ShadowView::new(view)),
2016-03-15 22:37:57 +00:00
size: Vec2::new(0, 0),
position: position,
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
}
}
impl View for StackView {
fn draw(&mut self, printer: &Printer) {
let last = self.layers.len();
let mut previous = Vec2::zero();
printer.with_color(ColorStyle::Primary, |printer| {
for (i, v) in self.layers.iter_mut().enumerate() {
// Place the view
// Center the view
let offset = v.position.compute_offset(v.size, printer.size, previous);
previous = offset;
v.view.draw(&printer.sub_printer(offset, v.size, i + 1 == last));
}
});
}
2015-05-15 01:38:58 +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,
Some(v) => v.view.on_event(event),
2015-05-15 23:06:48 +00:00
}
}
fn layout(&mut self, size: Vec2) {
// 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 {
// Give each guy what he asks for, within the budget constraints.
layer.size = Vec2::min(size, layer.view.get_min_size(size));
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...?
// (TODO: try to make it during layer addition)
if layer.virgin {
layer.view.take_focus();
layer.virgin = false;
}
2015-05-15 01:38:58 +00:00
}
}
fn get_min_size(&self, size: Vec2) -> Vec2 {
2015-05-15 01:38:58 +00:00
// The min size is the max of all children's
self.layers
.iter()
.map(|layer| layer.view.get_min_size(size))
.fold(Vec2::new(1, 1), Vec2::max)
2015-05-15 01:38:58 +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-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
}
}