use std::{collections::HashMap, sync::Arc};
use super::{PaletteColor, RoleName, Theme};
use crate::{app::window::Appearance, layout::text_style::FontWeight, node::TextColor};
#[derive(Clone, Debug, Default, PartialEq)]
pub struct CodeStyle {
pub color: Option<TextColor>,
pub weight: Option<FontWeight>,
pub italic: Option<bool>,
}
impl CodeStyle {
pub fn new() -> Self {
Self::default()
}
pub fn foreground(mut self, color: impl Into<TextColor>) -> Self {
self.color = Some(color.into());
self
}
pub fn weight(mut self, weight: FontWeight) -> Self {
self.weight = Some(weight);
self
}
pub fn bold(self) -> Self {
self.weight(FontWeight::Bold)
}
pub fn italic(mut self) -> Self {
self.italic = Some(true);
self
}
pub fn upright(mut self) -> Self {
self.italic = Some(false);
self
}
pub fn is_empty(&self) -> bool {
*self == Self::default()
}
pub fn or(mut self, under: &CodeStyle) -> Self {
if self.color.is_none() {
self.color.clone_from(&under.color);
}
if self.weight.is_none() {
self.weight = under.weight;
}
if self.italic.is_none() {
self.italic = under.italic;
}
self
}
pub fn is_whole(&self) -> bool {
self.color.is_some() && self.weight.is_some() && self.italic.is_some()
}
}
impl<T: Into<TextColor>> From<T> for CodeStyle {
fn from(color: T) -> Self {
Self::new().foreground(color)
}
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct CodeScheme {
styles: HashMap<RoleName, CodeStyle>,
}
impl CodeScheme {
pub fn new() -> Self {
Self::default()
}
pub fn lurq() -> Self {
use PaletteColor::{Info, MutedForeground, Success, Warning};
let mut scheme = Self::new();
for (name, role) in [
("keyword", Info),
("type", Info),
("namespace", Info),
("tag", Info),
("property", Info),
("variable.builtin", Info),
("constant.language", Info),
("string", Success),
("regex", Success),
("number", Warning),
("constant", Warning),
("attribute", Warning),
("comment", MutedForeground),
("punctuation", MutedForeground),
("operator", MutedForeground),
("markup.code", Success),
("markup.link", Info),
("markup.list", Warning),
] {
scheme.set(name, CodeStyle::new().foreground(role));
}
scheme.set("markup.heading", CodeStyle::new().foreground(Info).bold());
scheme.set("markup.quote", CodeStyle::new().foreground(MutedForeground).italic());
scheme.set("markup.bold", CodeStyle::new().bold());
scheme.set("markup.italic", CodeStyle::new().italic());
scheme
}
pub fn color(mut self, name: impl Into<RoleName>, color: impl Into<TextColor>) -> Self {
self.set_color(name, color);
self
}
pub fn style(mut self, name: impl Into<RoleName>, style: impl Into<CodeStyle>) -> Self {
self.set(name, style);
self
}
pub fn set(&mut self, name: impl Into<RoleName>, style: impl Into<CodeStyle>) {
self.styles.insert(name.into(), style.into());
}
pub fn set_color(&mut self, name: impl Into<RoleName>, color: impl Into<TextColor>) {
self.styles.entry(name.into()).or_default().color = Some(color.into());
}
pub fn remove(&mut self, name: impl Into<RoleName>) {
self.styles.remove(&name.into());
}
pub fn get(&self, name: impl Into<RoleName>) -> Option<&CodeStyle> {
self.styles.get(&name.into())
}
pub fn names(&self) -> impl Iterator<Item = RoleName> + '_ {
self.styles.keys().copied()
}
pub fn resolve(&self, name: &str) -> CodeStyle {
let mut style = CodeStyle::new();
for name in crate::code::lookup_names(name) {
if let Some(own) = self.styles.get(&RoleName::new(name)) {
style = style.or(own);
if style.is_whole() {
break;
}
}
}
style
}
}
#[derive(Clone, Debug, PartialEq)]
pub(super) struct ThemeCode {
pub(super) light: Arc<CodeScheme>,
pub(super) dark: Option<Arc<CodeScheme>>,
}
impl ThemeCode {
pub(super) fn of(&self, appearance: Appearance) -> &Arc<CodeScheme> {
match (appearance, &self.dark) {
(Appearance::Dark, Some(dark)) => dark,
_ => &self.light,
}
}
fn update(&mut self, appearance: Option<Appearance>, change: impl Fn(&mut CodeScheme)) {
match appearance {
None => {
change(Arc::make_mut(&mut self.light));
if let Some(dark) = &mut self.dark {
change(Arc::make_mut(dark));
}
}
Some(Appearance::Light) => {
if self.dark.is_none() {
self.dark = Some(self.light.clone());
}
change(Arc::make_mut(&mut self.light));
}
Some(Appearance::Dark) => {
let dark = self.dark.get_or_insert_with(|| self.light.clone());
change(Arc::make_mut(dark));
}
}
}
}
impl Theme {
pub fn code_scheme(&self) -> Arc<CodeScheme> {
self.track_access();
let inner = self.inner.read().unwrap();
inner.code.of(inner.appearance).clone()
}
pub fn code_scheme_for(&self, appearance: Appearance) -> Arc<CodeScheme> {
self.inner.read().unwrap().code.of(appearance).clone()
}
pub fn code_style(&self, name: &str) -> CodeStyle {
self.code_scheme().resolve(name)
}
pub fn set_code_scheme(&self, scheme: CodeScheme) {
self.mutate_inner(|inner| {
inner.code = ThemeCode {
light: Arc::new(scheme),
dark: None,
};
});
}
pub fn set_code_schemes(&self, light: CodeScheme, dark: CodeScheme) {
self.mutate_inner(|inner| {
inner.code = ThemeCode {
light: Arc::new(light),
dark: Some(Arc::new(dark)),
};
});
}
pub fn set_code_color(&self, name: impl Into<RoleName>, color: impl Into<TextColor>) {
let (name, color) = (name.into(), color.into());
self.mutate_inner(|inner| inner.code.update(None, |scheme| scheme.set_color(name, color.clone())));
}
pub fn set_code_color_for(&self, appearance: Appearance, name: impl Into<RoleName>, color: impl Into<TextColor>) {
let (name, color) = (name.into(), color.into());
self.mutate_inner(|inner| {
inner
.code
.update(Some(appearance), |scheme| scheme.set_color(name, color.clone()))
});
}
pub fn set_code_style(&self, name: impl Into<RoleName>, style: impl Into<CodeStyle>) {
let (name, style) = (name.into(), style.into());
self.mutate_inner(|inner| inner.code.update(None, |scheme| scheme.set(name, style.clone())));
}
}