2015-05-15 01:38:58 +00:00
|
|
|
use std::cmp::max;
|
2015-05-23 17:33:29 +00:00
|
|
|
use std::any::Any;
|
2015-05-15 00:41:17 +00:00
|
|
|
|
2015-05-22 06:29:49 +00:00
|
|
|
use ncurses;
|
|
|
|
|
|
|
|
use color;
|
2015-05-18 18:51:30 +00:00
|
|
|
use vec::Vec2;
|
2015-05-23 17:33:29 +00:00
|
|
|
use view::{View,SizeRequest,DimensionRequest,Selector};
|
2015-05-15 01:38:58 +00:00
|
|
|
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 {
|
2015-05-15 23:06:48 +00:00
|
|
|
layers: Vec<Layer>,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Layer {
|
|
|
|
view: Box<View>,
|
|
|
|
size: Vec2,
|
2015-05-22 06:29:49 +00:00
|
|
|
win: Option<ncurses::WINDOW>,
|
2015-05-15 00:41:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2015-05-15 23:06:48 +00:00
|
|
|
self.layers.push(Layer {
|
|
|
|
view: Box::new(view),
|
|
|
|
size: Vec2::new(0,0),
|
2015-05-22 06:29:49 +00:00
|
|
|
win: None,
|
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-22 23:28:05 +00:00
|
|
|
fn draw(&mut self, printer: &Printer, focused: bool) {
|
2015-05-22 06:29:49 +00:00
|
|
|
ncurses::wrefresh(printer.win);
|
2015-05-22 23:28:05 +00:00
|
|
|
for v in self.layers.iter_mut() {
|
2015-05-22 06:29:49 +00:00
|
|
|
// Center the view
|
|
|
|
v.view.draw(&Printer::new(v.win.unwrap(), v.size), focused);
|
|
|
|
|
|
|
|
let h = v.size.y;
|
|
|
|
let w = v.size.x;
|
|
|
|
let x = (printer.size.x - w) / 2;
|
|
|
|
let y = (printer.size.y - h) / 2;
|
|
|
|
|
|
|
|
|
2015-05-23 17:33:29 +00:00
|
|
|
let printer = printer.style(color::HIGHLIGHT);
|
2015-05-22 06:29:49 +00:00
|
|
|
printer.print_hline((x+1,y+h), w, ' ' as u64);
|
|
|
|
printer.print_vline((x+w,y+1), h, ' ' as u64);
|
|
|
|
|
|
|
|
// v.view.draw(&printer.sub_printer(offset, v.size), focused);
|
|
|
|
ncurses::wrefresh(v.win.unwrap());
|
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,
|
2015-05-15 23:06:48 +00:00
|
|
|
Some(v) => v.view.on_key_event(ch),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn layout(&mut self, size: Vec2) {
|
|
|
|
let req = SizeRequest {
|
|
|
|
w: DimensionRequest::AtMost(size.x),
|
|
|
|
h: DimensionRequest::AtMost(size.y),
|
|
|
|
};
|
|
|
|
for layer in self.layers.iter_mut() {
|
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);
|
2015-05-22 06:29:49 +00:00
|
|
|
|
|
|
|
let h = layer.size.y as i32;
|
|
|
|
let w = layer.size.x as i32;
|
|
|
|
let x = (size.x as i32 - w) / 2;
|
|
|
|
let y = (size.y as i32 - h) / 2;
|
|
|
|
let win = ncurses::newwin(h, w, y, x);
|
|
|
|
ncurses::wbkgd(win, ncurses::COLOR_PAIR(color::PRIMARY));
|
|
|
|
|
|
|
|
match layer.win {
|
|
|
|
None => (),
|
|
|
|
Some(w) => { ncurses::delwin(w); },
|
|
|
|
}
|
|
|
|
layer.win = Some(win);
|
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
|
2015-05-15 18:58:47 +00:00
|
|
|
let mut s = Vec2::new(1,1);
|
2015-05-15 01:38:58 +00:00
|
|
|
|
2015-05-15 23:06:48 +00:00
|
|
|
for layer in self.layers.iter() {
|
|
|
|
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,
|
|
|
|
Some(mut v) => v.view.take_focus()
|
|
|
|
}
|
|
|
|
}
|
2015-05-23 17:33:29 +00:00
|
|
|
|
|
|
|
fn find(&mut self, selector: &Selector) -> Option<&mut Any> {
|
|
|
|
for layer in self.layers.iter_mut() {
|
|
|
|
if let Some(any) = layer.view.find(selector) {
|
|
|
|
return Some(any);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
2015-05-15 00:41:17 +00:00
|
|
|
}
|