cursive/src/view/shadow_view.rs

49 lines
1.2 KiB
Rust
Raw Normal View History

use view::{View, ViewWrapper};
use printer::Printer;
use vec::Vec2;
use theme::ColorStyle;
2015-05-26 23:48:27 +00:00
/// Wrapper view that adds a shadow.
///
/// It reserves a 1 pixel border on each side.
pub struct ShadowView<T: View> {
2015-05-26 23:48:27 +00:00
view: T,
}
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.
pub fn new(view: T) -> Self {
2016-03-15 22:37:57 +00:00
ShadowView { view: view }
}
}
2016-06-25 23:36:22 +00:00
impl<T: View> ViewWrapper for ShadowView<T> {
wrap_impl!(&self.view);
fn wrap_get_min_size(&self, req: Vec2) -> Vec2 {
self.view.get_min_size(req - (2, 2)) + (2, 2)
}
fn wrap_layout(&mut self, size: Vec2) {
2016-03-15 22:37:57 +00:00
self.view.layout(size - (2, 2));
}
fn wrap_draw(&mut self, printer: &Printer) {
// Draw the view background
for y in 1..printer.size.y - 1 {
printer.print_hline((1, y), printer.size.x - 2, " ");
}
self.view.draw(&printer.sub_printer(Vec2::new(1, 1), printer.size - (2, 2), true));
2016-03-15 22:37:57 +00:00
let h = printer.size.y - 1;
let w = printer.size.x - 1;
printer.with_color(ColorStyle::Shadow, |printer| {
printer.print_hline((2, h), w - 1, " ");
printer.print_vline((w, 2), h - 1, " ");
});
}
}