2016-07-02 22:02:42 +00:00
|
|
|
use std::borrow::Cow;
|
|
|
|
|
2016-07-02 07:47:38 +00:00
|
|
|
use view::{View, ViewWrapper};
|
2015-05-23 22:58:06 +00:00
|
|
|
use printer::Printer;
|
|
|
|
use vec::Vec2;
|
2016-07-01 06:38:01 +00:00
|
|
|
use theme::ColorStyle;
|
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-02 22:02:42 +00:00
|
|
|
topleft_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,
|
|
|
|
topleft_padding: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn padding(&self) -> (usize, usize) {
|
|
|
|
if self.topleft_padding {
|
|
|
|
(2, 2)
|
|
|
|
} else {
|
|
|
|
(1, 1)
|
|
|
|
}
|
2015-05-23 22:58:06 +00:00
|
|
|
}
|
2016-07-02 22:02:42 +00:00
|
|
|
|
|
|
|
pub fn no_topleft_padding(mut self) -> Self {
|
|
|
|
self.topleft_padding = false;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2015-05-23 22:58:06 +00:00
|
|
|
}
|
|
|
|
|
2016-06-25 23:36:22 +00:00
|
|
|
impl<T: View> ViewWrapper for ShadowView<T> {
|
2015-05-23 22:58:06 +00:00
|
|
|
wrap_impl!(&self.view);
|
|
|
|
|
2016-07-02 07:47:38 +00:00
|
|
|
fn wrap_get_min_size(&self, req: Vec2) -> Vec2 {
|
2016-07-02 22:02:42 +00:00
|
|
|
let offset = self.padding();
|
|
|
|
self.view.get_min_size(req - offset) + offset
|
2015-05-23 22:58:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn wrap_layout(&mut self, size: Vec2) {
|
2016-07-02 22:02:42 +00:00
|
|
|
let offset = self.padding();
|
|
|
|
self.view.layout(size - offset);
|
2015-05-23 22:58:06 +00:00
|
|
|
}
|
|
|
|
|
2015-05-31 04:05:34 +00:00
|
|
|
fn wrap_draw(&mut self, printer: &Printer) {
|
2015-05-23 22:58:06 +00:00
|
|
|
|
2016-07-02 22:02:42 +00:00
|
|
|
// Skip the first row/column
|
|
|
|
let printer = if self.topleft_padding {
|
|
|
|
Cow::Owned(printer.sub_printer(Vec2::new(1, 1), printer.size, true))
|
|
|
|
} else {
|
|
|
|
Cow::Borrowed(printer)
|
|
|
|
};
|
|
|
|
|
2016-06-30 00:36:20 +00:00
|
|
|
// Draw the view background
|
2016-07-02 22:02:42 +00:00
|
|
|
for y in 0..printer.size.y - 1 {
|
|
|
|
printer.print_hline((0, y), printer.size.x - 1, " ");
|
2016-06-30 00:36:20 +00:00
|
|
|
}
|
2015-05-23 22:58:06 +00:00
|
|
|
|
2016-07-02 22:02:42 +00:00
|
|
|
self.view.draw(&printer.sub_printer(Vec2::zero(),
|
|
|
|
printer.size - (1, 1),
|
|
|
|
true));
|
2015-05-23 22:58:06 +00:00
|
|
|
|
2016-07-02 22:02:42 +00:00
|
|
|
let h = printer.size.y;
|
|
|
|
let w = printer.size.x;
|
2015-05-23 22:58:06 +00:00
|
|
|
|
2016-07-01 06:38:01 +00:00
|
|
|
printer.with_color(ColorStyle::Shadow, |printer| {
|
2016-07-02 22:02:42 +00:00
|
|
|
printer.print_hline((1, h-1), w - 1, " ");
|
|
|
|
printer.print_vline((w-1, 1), h - 1, " ");
|
2015-05-23 22:58:06 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|