From b3f913391a66baf59d859a1ddc486449ba15b93e Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Sun, 17 Nov 2019 00:12:04 -0800 Subject: [PATCH] Add color option to Layer --- src/views/layer.rs | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/views/layer.rs b/src/views/layer.rs index 9ce87df..0e59a37 100644 --- a/src/views/layer.rs +++ b/src/views/layer.rs @@ -1,3 +1,4 @@ +use crate::theme::ColorStyle; use crate::view::{View, ViewWrapper}; use crate::Printer; @@ -9,12 +10,28 @@ use crate::Printer; #[derive(Debug)] pub struct Layer { view: T, + color: ColorStyle, } impl Layer { /// Wraps the given view. pub fn new(view: T) -> Self { - Layer { view } + Self::with_color(view, ColorStyle::primary()) + } + + /// Wraps the given view with a custom background color. + pub fn with_color(view: T, color: ColorStyle) -> Self { + Layer { view, color } + } + + /// Gets the current color. + pub fn color(&self) -> ColorStyle { + self.color + } + + /// Sets the background color. + pub fn set_color(&mut self, color: ColorStyle) { + self.color = color; } inner_getters!(self.view: T); @@ -24,9 +41,11 @@ impl ViewWrapper for Layer { wrap_impl!(self.view: T); fn wrap_draw(&self, printer: &Printer<'_, '_>) { - for y in 0..printer.size.y { - printer.print_hline((0, y), printer.size.x, " "); - } + printer.with_color(self.color, |printer| { + for y in 0..printer.size.y { + printer.print_hline((0, y), printer.size.x, " "); + } + }); self.view.draw(printer); } }