Add ColorType::InheritParent

This commit is contained in:
Alexandre Bury 2020-12-18 15:31:22 -08:00
parent bbca563e12
commit ae0556c77d
2 changed files with 31 additions and 6 deletions

View File

@ -4,13 +4,14 @@ use crate::backend::Backend;
use crate::direction::Orientation; use crate::direction::Orientation;
use crate::rect::Rect; use crate::rect::Rect;
use crate::theme::{ use crate::theme::{
BorderStyle, ColorStyle, Effect, PaletteColor, Style, Theme, BorderStyle, ColorPair, ColorStyle, Effect, PaletteColor, Style, Theme,
}; };
use crate::utils::lines::simple::{prefix, suffix}; use crate::utils::lines::simple::{prefix, suffix};
use crate::with::With; use crate::with::With;
use crate::Vec2; use crate::Vec2;
use enumset::EnumSet; use enumset::EnumSet;
use std::cell::Cell;
use std::cmp::min; use std::cmp::min;
use unicode_segmentation::UnicodeSegmentation; use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr; use unicode_width::UnicodeWidthStr;
@ -59,6 +60,9 @@ pub struct Printer<'a, 'b> {
/// Currently used theme /// Currently used theme
pub theme: &'a Theme, pub theme: &'a Theme,
/// Current color pair used by the parent view.
current_color: Cell<ColorPair>,
/// Backend used to actually draw things /// Backend used to actually draw things
backend: &'b dyn Backend, backend: &'b dyn Backend,
} }
@ -83,6 +87,7 @@ impl<'a, 'b> Printer<'a, 'b> {
enabled: true, enabled: true,
theme, theme,
backend, backend,
current_color: Cell::new(ColorPair::from_256colors(0, 0)),
} }
} }
@ -286,6 +291,11 @@ impl<'a, 'b> Printer<'a, 'b> {
self.backend.print_at_rep(start, repetitions, c); self.backend.print_at_rep(start, repetitions, c);
} }
/// Returns the color currently used by the parent view.
pub fn current_color(&self) -> ColorPair {
self.current_color.get()
}
/// Call the given closure with a colored printer, /// Call the given closure with a colored printer,
/// that will apply the given color on prints. /// that will apply the given color on prints.
/// ///
@ -306,9 +316,16 @@ impl<'a, 'b> Printer<'a, 'b> {
where where
F: FnOnce(&Printer), F: FnOnce(&Printer),
{ {
let old = self.backend.set_color(c.resolve(&self.theme.palette)); let old = self.current_color.get();
let new = c.resolve(&self.theme.palette, old);
self.current_color.set(new);
self.backend.set_color(new);
f(self); f(self);
self.backend.set_color(old); self.backend.set_color(old);
self.current_color.set(old)
} }
/// Call the given closure with a styled printer, /// Call the given closure with a styled printer,

View File

@ -77,10 +77,14 @@ impl ColorStyle {
} }
/// Return the color pair that this style represents. /// Return the color pair that this style represents.
pub fn resolve(&self, palette: &Palette) -> ColorPair { pub fn resolve(
&self,
palette: &Palette,
previous: ColorPair,
) -> ColorPair {
ColorPair { ColorPair {
front: self.front.resolve(palette), front: self.front.resolve(palette, previous.front),
back: self.back.resolve(palette), back: self.back.resolve(palette, previous.back),
} }
} }
} }
@ -127,14 +131,18 @@ pub enum ColorType {
/// Uses a direct color, independent of the current palette. /// Uses a direct color, independent of the current palette.
Color(Color), Color(Color),
/// Re-use the color from the parent.
InheritParent,
} }
impl ColorType { impl ColorType {
/// Given a palette, resolve `self` to a concrete color. /// Given a palette, resolve `self` to a concrete color.
pub fn resolve(self, palette: &Palette) -> Color { pub fn resolve(self, palette: &Palette, previous: Color) -> Color {
match self { match self {
ColorType::Color(color) => color, ColorType::Color(color) => color,
ColorType::Palette(color) => color.resolve(palette), ColorType::Palette(color) => color.resolve(palette),
ColorType::InheritParent => previous,
} }
} }
} }