2018-01-08 15:11:12 +00:00
|
|
|
use super::Color;
|
2018-01-17 17:35:57 +00:00
|
|
|
use enum_map::EnumMap;
|
2018-01-21 19:06:33 +00:00
|
|
|
use toml;
|
2018-01-08 15:11:12 +00:00
|
|
|
|
2018-06-11 06:24:58 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::ops::{Index, IndexMut};
|
|
|
|
|
2018-01-08 15:11:12 +00:00
|
|
|
/// Color configuration for the application.
|
|
|
|
///
|
|
|
|
/// Assign each color role an actual color.
|
2018-01-21 19:06:33 +00:00
|
|
|
///
|
|
|
|
/// It implements `Index` and `IndexMut` to access and modify this mapping:
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
2018-06-11 06:24:58 +00:00
|
|
|
/// # use cursive::theme::Palette;
|
2018-01-21 19:06:33 +00:00
|
|
|
/// use cursive::theme::PaletteColor::*;
|
|
|
|
/// use cursive::theme::Color::*;
|
|
|
|
/// use cursive::theme::BaseColor::*;
|
|
|
|
///
|
2018-06-11 06:24:58 +00:00
|
|
|
/// let mut palette = Palette::default();
|
2018-01-21 19:06:33 +00:00
|
|
|
///
|
|
|
|
/// assert_eq!(palette[Background], Dark(Blue));
|
|
|
|
/// palette[Shadow] = Light(Red);
|
|
|
|
/// ```
|
2018-06-11 06:24:58 +00:00
|
|
|
#[derive(PartialEq, Eq, Clone, Debug)]
|
|
|
|
pub struct Palette {
|
|
|
|
basic: EnumMap<PaletteColor, Color>,
|
|
|
|
custom: HashMap<String, PaletteNode>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A node in the palette tree.
|
|
|
|
///
|
2018-06-11 06:35:31 +00:00
|
|
|
/// This describes a value attached to a custom keyword in the palette.
|
|
|
|
///
|
2018-06-11 06:24:58 +00:00
|
|
|
/// This can either be a color, or a nested namespace with its own mapping.
|
|
|
|
#[derive(PartialEq, Eq, Clone, Debug)]
|
2018-06-11 06:35:31 +00:00
|
|
|
pub enum PaletteNode {
|
|
|
|
/// A single color.
|
2018-06-11 06:24:58 +00:00
|
|
|
Color(Color),
|
2018-06-11 06:35:31 +00:00
|
|
|
/// A group of values bundled in the same namespace.
|
|
|
|
///
|
|
|
|
/// Namespaces can be merged in the palette with `Palette::merge`.
|
2018-06-11 06:24:58 +00:00
|
|
|
Namespace(HashMap<String, PaletteNode>),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Basic usage: only use basic colors
|
|
|
|
impl Index<PaletteColor> for Palette {
|
|
|
|
type Output = Color;
|
|
|
|
|
|
|
|
fn index(&self, palette_color: PaletteColor) -> &Color {
|
|
|
|
&self.basic[palette_color]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We can alter existing color if needed (but why?...)
|
|
|
|
impl IndexMut<PaletteColor> for Palette {
|
|
|
|
fn index_mut(&mut self, palette_color: PaletteColor) -> &mut Color {
|
|
|
|
&mut self.basic[palette_color]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Palette {
|
|
|
|
/// Returns a custom color from this palette.
|
|
|
|
///
|
|
|
|
/// Returns `None` if the given key was not found.
|
|
|
|
pub fn custom<'a>(&'a self, key: &str) -> Option<&'a Color> {
|
|
|
|
self.custom.get(key).and_then(|node| {
|
|
|
|
if let &PaletteNode::Color(ref color) = node {
|
|
|
|
Some(color)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a new palette where the given namespace has been merged.
|
|
|
|
///
|
|
|
|
/// All values in the namespace will override previous values.
|
|
|
|
pub fn merge(&self, namespace: &str) -> Palette {
|
|
|
|
let mut result = self.clone();
|
|
|
|
|
|
|
|
if let Some(&PaletteNode::Namespace(ref palette)) =
|
|
|
|
self.custom.get(namespace)
|
|
|
|
{
|
|
|
|
// Merge `result` and `palette`
|
|
|
|
for (key, value) in palette.iter() {
|
|
|
|
match *value {
|
|
|
|
PaletteNode::Color(color) => result.set_color(key, color),
|
2018-06-11 06:29:10 +00:00
|
|
|
PaletteNode::Namespace(ref map) => {
|
|
|
|
result.add_namespace(key, map.clone())
|
|
|
|
}
|
2018-06-11 06:24:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the color for the given key.
|
|
|
|
///
|
|
|
|
/// This will update either the basic palette or the custom values.
|
|
|
|
pub fn set_color(&mut self, key: &str, color: Color) {
|
|
|
|
use theme::PaletteColor::*;
|
|
|
|
|
|
|
|
match key {
|
|
|
|
"background" => self.basic[Background] = color,
|
|
|
|
"shadow" => self.basic[Shadow] = color,
|
|
|
|
"view" => self.basic[View] = color,
|
|
|
|
"primary" => self.basic[Primary] = color,
|
|
|
|
"secondary" => self.basic[Secondary] = color,
|
|
|
|
"tertiary" => self.basic[Tertiary] = color,
|
|
|
|
"title_primary" => self.basic[TitlePrimary] = color,
|
|
|
|
"title_secondary" => self.basic[TitleSecondary] = color,
|
|
|
|
"highlight" => self.basic[Highlight] = color,
|
|
|
|
"highlight_inactive" => self.basic[HighlightInactive] = color,
|
|
|
|
other => {
|
|
|
|
self.custom
|
|
|
|
.insert(other.to_string(), PaletteNode::Color(color));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds a color namespace to this palette.
|
2018-06-11 06:35:31 +00:00
|
|
|
pub fn add_namespace(
|
2018-06-11 06:29:10 +00:00
|
|
|
&mut self, key: &str, namespace: HashMap<String, PaletteNode>,
|
|
|
|
) {
|
|
|
|
self.custom
|
|
|
|
.insert(key.to_string(), PaletteNode::Namespace(namespace));
|
2018-06-11 06:24:58 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-17 17:35:57 +00:00
|
|
|
|
2018-01-21 19:06:33 +00:00
|
|
|
/// Returns the default palette for a cursive application.
|
|
|
|
///
|
|
|
|
/// * `Background` => `Dark(Blue)`
|
|
|
|
/// * `Shadow` => `Dark(Black)`
|
|
|
|
/// * `View` => `Dark(White)`
|
|
|
|
/// * `Primary` => `Dark(Black)`
|
|
|
|
/// * `Secondary` => `Dark(Blue)`
|
|
|
|
/// * `Tertiary` => `Dark(White)`
|
|
|
|
/// * `TitlePrimary` => `Dark(Red)`
|
|
|
|
/// * `TitleSecondary` => `Dark(Yellow)`
|
|
|
|
/// * `Highlight` => `Dark(Red)`
|
|
|
|
/// * `HighlightInactive` => `Dark(Blue)`
|
2018-06-11 06:24:58 +00:00
|
|
|
impl Default for Palette {
|
|
|
|
fn default() -> Palette {
|
|
|
|
use self::PaletteColor::*;
|
|
|
|
use theme::BaseColor::*;
|
|
|
|
use theme::Color::*;
|
|
|
|
|
|
|
|
Palette {
|
|
|
|
basic: enum_map!{
|
|
|
|
Background => Dark(Blue),
|
|
|
|
Shadow => Dark(Black),
|
|
|
|
View => Dark(White),
|
|
|
|
Primary => Dark(Black),
|
|
|
|
Secondary => Dark(Blue),
|
|
|
|
Tertiary => Dark(White),
|
|
|
|
TitlePrimary => Dark(Red),
|
|
|
|
TitleSecondary => Dark(Yellow),
|
|
|
|
Highlight => Dark(Red),
|
|
|
|
HighlightInactive => Dark(Blue),
|
|
|
|
},
|
|
|
|
custom: HashMap::new(),
|
|
|
|
}
|
2018-01-17 17:35:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-11 06:24:58 +00:00
|
|
|
// Iterate over a toml
|
2018-06-11 06:29:10 +00:00
|
|
|
fn iterate_toml<'a>(
|
|
|
|
table: &'a toml::value::Table,
|
|
|
|
) -> impl Iterator<Item = (&'a str, PaletteNode)> + 'a {
|
2018-06-11 06:24:58 +00:00
|
|
|
table.iter().flat_map(|(key, value)| {
|
|
|
|
let node = match value {
|
|
|
|
toml::Value::Table(table) => {
|
|
|
|
// This should define a new namespace
|
|
|
|
// Treat basic colors as simple string.
|
|
|
|
// We'll convert them back in the merge method.
|
2018-06-11 06:29:10 +00:00
|
|
|
let map = iterate_toml(table)
|
|
|
|
.map(|(key, value)| (key.to_string(), value))
|
|
|
|
.collect();
|
2018-06-11 06:24:58 +00:00
|
|
|
// Should we only return something if it's non-empty?
|
|
|
|
Some(PaletteNode::Namespace(map))
|
|
|
|
}
|
|
|
|
toml::Value::Array(colors) => {
|
|
|
|
// This should be a list of colors - just pick the first valid one.
|
|
|
|
colors
|
|
|
|
.iter()
|
|
|
|
.flat_map(toml::Value::as_str)
|
|
|
|
.flat_map(Color::parse)
|
|
|
|
.map(PaletteNode::Color)
|
|
|
|
.next()
|
|
|
|
}
|
|
|
|
toml::Value::String(color) => {
|
|
|
|
// This describe a new color - easy!
|
|
|
|
Color::parse(color).map(PaletteNode::Color)
|
|
|
|
}
|
|
|
|
other => {
|
|
|
|
// Other - error?
|
2018-06-11 06:29:10 +00:00
|
|
|
debug!(
|
|
|
|
"Found unexpected value in theme: {} = {:?}",
|
|
|
|
key, other
|
|
|
|
);
|
2018-06-11 06:24:58 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
node.map(|node| (key.as_str(), node))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-01-21 19:06:33 +00:00
|
|
|
/// Fills `palette` with the colors from the given `table`.
|
2018-06-11 06:24:58 +00:00
|
|
|
pub(crate) fn load_toml(palette: &mut Palette, table: &toml::value::Table) {
|
2018-01-21 19:06:33 +00:00
|
|
|
// TODO: use serde for that?
|
|
|
|
// Problem: toml-rs doesn't do well with Enums...
|
2018-06-11 06:24:58 +00:00
|
|
|
|
|
|
|
for (key, value) in iterate_toml(table) {
|
|
|
|
match value {
|
|
|
|
PaletteNode::Color(color) => palette.set_color(key, color),
|
|
|
|
PaletteNode::Namespace(map) => palette.add_namespace(key, map),
|
|
|
|
}
|
|
|
|
}
|
2018-01-17 17:35:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Color entry in a palette.
|
|
|
|
///
|
2018-01-22 22:47:56 +00:00
|
|
|
/// Each `ColorRole` is used for a specific role in a default application.
|
2018-06-19 06:13:02 +00:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Enum)]
|
2018-01-17 17:35:57 +00:00
|
|
|
pub enum PaletteColor {
|
2018-01-08 15:11:12 +00:00
|
|
|
/// Color used for the application background.
|
2018-01-17 17:35:57 +00:00
|
|
|
Background,
|
2018-01-08 15:11:12 +00:00
|
|
|
/// Color used for View shadows.
|
2018-01-17 17:35:57 +00:00
|
|
|
Shadow,
|
2018-01-08 15:11:12 +00:00
|
|
|
/// Color used for View backgrounds.
|
2018-01-17 17:35:57 +00:00
|
|
|
View,
|
2018-01-08 15:11:12 +00:00
|
|
|
/// Primary color used for the text.
|
2018-01-17 17:35:57 +00:00
|
|
|
Primary,
|
2018-01-08 15:11:12 +00:00
|
|
|
/// Secondary color used for the text.
|
2018-01-17 17:35:57 +00:00
|
|
|
Secondary,
|
2018-01-08 15:11:12 +00:00
|
|
|
/// Tertiary color used for the text.
|
2018-01-17 17:35:57 +00:00
|
|
|
Tertiary,
|
2018-01-08 15:11:12 +00:00
|
|
|
/// Primary color used for title text.
|
2018-01-17 17:35:57 +00:00
|
|
|
TitlePrimary,
|
2018-01-08 15:11:12 +00:00
|
|
|
/// Secondary color used for title text.
|
2018-01-17 17:35:57 +00:00
|
|
|
TitleSecondary,
|
2018-01-08 15:11:12 +00:00
|
|
|
/// Color used for highlighting text.
|
2018-01-17 17:35:57 +00:00
|
|
|
Highlight,
|
2018-01-08 15:11:12 +00:00
|
|
|
/// Color used for highlighting inactive text.
|
2018-01-17 17:35:57 +00:00
|
|
|
HighlightInactive,
|
2018-01-08 15:11:12 +00:00
|
|
|
}
|
|
|
|
|
2018-01-17 17:35:57 +00:00
|
|
|
impl PaletteColor {
|
|
|
|
/// Given a palette, resolve `self` to a concrete color.
|
|
|
|
pub fn resolve(self, palette: &Palette) -> Color {
|
2018-01-21 19:06:33 +00:00
|
|
|
palette[self]
|
2018-01-08 15:11:12 +00:00
|
|
|
}
|
|
|
|
}
|