2016-07-25 20:35:46 +00:00
|
|
|
use Printer;
|
2017-10-13 03:43:33 +00:00
|
|
|
use event::{Event, EventResult};
|
2018-03-22 22:21:54 +00:00
|
|
|
use rect::Rect;
|
2016-07-25 20:35:46 +00:00
|
|
|
use vec::Vec2;
|
|
|
|
use view::{View, ViewWrapper};
|
|
|
|
|
|
|
|
/// Draws a border around a wrapped view.
|
2017-12-12 01:31:55 +00:00
|
|
|
#[derive(Debug)]
|
2016-07-25 20:35:46 +00:00
|
|
|
pub struct Panel<V: View> {
|
|
|
|
view: V,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<V: View> Panel<V> {
|
|
|
|
/// Creates a new panel around the given view.
|
|
|
|
pub fn new(view: V) -> Self {
|
2016-07-25 20:39:10 +00:00
|
|
|
Panel { view: view }
|
2016-07-25 20:35:46 +00:00
|
|
|
}
|
2018-01-26 11:06:14 +00:00
|
|
|
|
2018-01-27 09:37:08 +00:00
|
|
|
inner_getters!(self.view: V);
|
2016-07-25 20:35:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<V: View> ViewWrapper for Panel<V> {
|
2016-09-20 00:11:00 +00:00
|
|
|
wrap_impl!(self.view: V);
|
2016-07-25 20:35:46 +00:00
|
|
|
|
2017-10-13 03:43:33 +00:00
|
|
|
fn wrap_on_event(&mut self, event: Event) -> EventResult {
|
|
|
|
self.view.on_event(event.relativized((1, 1)))
|
|
|
|
}
|
|
|
|
|
2017-01-24 06:52:29 +00:00
|
|
|
fn wrap_required_size(&mut self, req: Vec2) -> Vec2 {
|
2016-07-25 20:35:46 +00:00
|
|
|
// TODO: make borders conditional?
|
2017-08-14 23:32:01 +00:00
|
|
|
let req = req.saturating_sub((2, 2));
|
2017-03-06 22:53:10 +00:00
|
|
|
|
|
|
|
self.view.required_size(req) + (2, 2)
|
2016-07-25 20:35:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn wrap_draw(&self, printer: &Printer) {
|
2016-08-05 00:27:04 +00:00
|
|
|
printer.print_box((0, 0), printer.size, true);
|
2018-03-22 22:21:54 +00:00
|
|
|
let size = printer.size.saturating_sub((2, 2));
|
|
|
|
let printer = printer.sub_printer((1, 1), size, true);
|
|
|
|
self.view.draw(&printer);
|
2017-03-15 23:35:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn wrap_layout(&mut self, size: Vec2) {
|
2017-08-14 23:32:01 +00:00
|
|
|
self.view.layout(size.saturating_sub((2, 2)));
|
2016-07-25 20:35:46 +00:00
|
|
|
}
|
2018-03-22 22:21:54 +00:00
|
|
|
|
|
|
|
fn wrap_important_area(&self, size: Vec2) -> Rect {
|
|
|
|
let inner_size = size.saturating_sub((2, 2));
|
|
|
|
self.view.important_area(inner_size) + (1, 1)
|
|
|
|
}
|
2016-07-25 20:35:46 +00:00
|
|
|
}
|