2016-07-14 06:25:54 +00:00
|
|
|
use Printer;
|
2017-10-11 18:35:18 +00:00
|
|
|
use event::{Event, EventResult};
|
2016-07-01 06:38:01 +00:00
|
|
|
use theme::ColorStyle;
|
2016-10-02 22:22:29 +00:00
|
|
|
use vec::Vec2;
|
|
|
|
use view::{View, ViewWrapper};
|
2015-05-23 22:58:06 +00:00
|
|
|
|
2015-05-26 23:48:27 +00:00
|
|
|
/// Wrapper view that adds a shadow.
|
|
|
|
///
|
|
|
|
/// It reserves a 1 pixel border on each side.
|
2015-05-23 22:58:06 +00:00
|
|
|
pub struct ShadowView<T: View> {
|
2015-05-26 23:48:27 +00:00
|
|
|
view: T,
|
2016-07-03 02:46:23 +00:00
|
|
|
top_padding: bool,
|
|
|
|
left_padding: bool,
|
2015-05-23 22:58:06 +00:00
|
|
|
}
|
|
|
|
|
2016-06-25 23:36:22 +00:00
|
|
|
impl<T: View> ShadowView<T> {
|
2015-05-26 23:48:27 +00:00
|
|
|
/// Wraps the given view.
|
2015-05-23 22:58:06 +00:00
|
|
|
pub fn new(view: T) -> Self {
|
2016-07-02 22:02:42 +00:00
|
|
|
ShadowView {
|
|
|
|
view: view,
|
2016-07-03 02:46:23 +00:00
|
|
|
top_padding: true,
|
|
|
|
left_padding: true,
|
2016-07-02 22:02:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-17 05:05:28 +00:00
|
|
|
fn padding(&self) -> Vec2 {
|
2017-10-12 23:41:45 +00:00
|
|
|
self.top_left_padding() + (1, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn top_left_padding(&self) -> Vec2 {
|
2018-03-22 18:04:10 +00:00
|
|
|
Vec2::new(
|
|
|
|
self.left_padding as usize,
|
|
|
|
self.top_padding as usize,
|
|
|
|
)
|
2015-05-23 22:58:06 +00:00
|
|
|
}
|
2016-07-02 22:02:42 +00:00
|
|
|
|
2016-07-12 03:26:33 +00:00
|
|
|
/// If set, adds an empty column to the left of the view.
|
|
|
|
///
|
|
|
|
/// Default to true.
|
2016-07-03 02:46:23 +00:00
|
|
|
pub fn left_padding(mut self, value: bool) -> Self {
|
|
|
|
self.left_padding = value;
|
|
|
|
self
|
|
|
|
}
|
2016-07-12 03:26:33 +00:00
|
|
|
|
|
|
|
/// If set, adds an empty row at the top of the view.
|
|
|
|
///
|
|
|
|
/// Default to true.
|
2016-07-03 02:46:23 +00:00
|
|
|
pub fn top_padding(mut self, value: bool) -> Self {
|
|
|
|
self.top_padding = value;
|
2016-07-02 22:02:42 +00:00
|
|
|
self
|
|
|
|
}
|
2018-01-26 11:06:14 +00:00
|
|
|
|
2018-01-27 09:37:08 +00:00
|
|
|
inner_getters!(self.view: T);
|
2015-05-23 22:58:06 +00:00
|
|
|
}
|
|
|
|
|
2016-06-25 23:36:22 +00:00
|
|
|
impl<T: View> ViewWrapper for ShadowView<T> {
|
2016-09-20 00:11:00 +00:00
|
|
|
wrap_impl!(self.view: T);
|
2015-05-23 22:58:06 +00:00
|
|
|
|
2017-01-24 06:52:29 +00:00
|
|
|
fn wrap_required_size(&mut self, req: Vec2) -> Vec2 {
|
2016-07-17 05:05:28 +00:00
|
|
|
// Make sure req >= offset
|
2017-08-14 23:32:01 +00:00
|
|
|
let offset = self.padding();
|
2018-03-22 18:04:10 +00:00
|
|
|
self.view
|
|
|
|
.required_size(req.saturating_sub(offset)) + offset
|
2015-05-23 22:58:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn wrap_layout(&mut self, size: Vec2) {
|
2017-08-14 23:32:01 +00:00
|
|
|
let offset = self.padding();
|
|
|
|
self.view.layout(size.saturating_sub(offset));
|
2015-05-23 22:58:06 +00:00
|
|
|
}
|
|
|
|
|
2017-10-11 18:35:18 +00:00
|
|
|
fn wrap_on_event(&mut self, event: Event) -> EventResult {
|
2017-10-12 23:41:45 +00:00
|
|
|
let padding = self.top_left_padding();
|
|
|
|
self.view.on_event(event.relativized(padding))
|
2017-10-11 18:35:18 +00:00
|
|
|
}
|
2015-05-23 22:58:06 +00:00
|
|
|
|
2017-10-11 18:35:18 +00:00
|
|
|
fn wrap_draw(&self, printer: &Printer) {
|
|
|
|
if printer.size.y <= self.top_padding as usize
|
|
|
|
|| printer.size.x <= self.left_padding as usize
|
|
|
|
{
|
2016-07-17 05:05:28 +00:00
|
|
|
// Nothing to do if there's no place to draw.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-02 22:02:42 +00:00
|
|
|
// Skip the first row/column
|
2018-03-22 18:04:10 +00:00
|
|
|
let offset = Vec2::new(
|
|
|
|
self.left_padding as usize,
|
|
|
|
self.top_padding as usize,
|
|
|
|
);
|
2016-08-05 00:27:04 +00:00
|
|
|
let printer = &printer.offset(offset, true);
|
2016-10-10 00:42:57 +00:00
|
|
|
if printer.theme.shadow {
|
2016-08-05 00:27:04 +00:00
|
|
|
let h = printer.size.y;
|
|
|
|
let w = printer.size.x;
|
2015-05-23 22:58:06 +00:00
|
|
|
|
2017-08-14 23:32:01 +00:00
|
|
|
if h == 0 || w == 0 {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-17 17:35:57 +00:00
|
|
|
printer.with_color(ColorStyle::shadow(), |printer| {
|
2016-08-05 00:27:04 +00:00
|
|
|
printer.print_hline((1, h - 1), w - 1, " ");
|
|
|
|
printer.print_vline((w - 1, 1), h - 1, " ");
|
|
|
|
});
|
|
|
|
}
|
2017-01-24 02:54:33 +00:00
|
|
|
|
|
|
|
// Draw the view background
|
2017-10-11 18:35:18 +00:00
|
|
|
let printer = printer.sub_printer(
|
|
|
|
Vec2::zero(),
|
|
|
|
printer.size.saturating_sub((1, 1)),
|
|
|
|
true,
|
|
|
|
);
|
2017-01-24 02:54:33 +00:00
|
|
|
self.view.draw(&printer);
|
2015-05-23 22:58:06 +00:00
|
|
|
}
|
|
|
|
}
|