Skip to content

Commit

Permalink
fix: alert styling for built in themes (#433)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfontanini authored Feb 1, 2025
2 parents 96b020c + 622453b commit 68b10c1
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 98 deletions.
13 changes: 5 additions & 8 deletions src/presentation/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,25 +733,22 @@ impl<'a> PresentationBuilder<'a> {
}

fn push_alert(&mut self, alert_type: AlertType, title: Option<String>, mut lines: Vec<Line>) -> BuildResult {
let (prefix_color, default_title, symbol) = match alert_type {
let (title_color, default_title, symbol) = match alert_type {
AlertType::Note => self.theme.alert.styles.note.as_parts(),
AlertType::Tip => self.theme.alert.styles.tip.as_parts(),
AlertType::Important => self.theme.alert.styles.important.as_parts(),
AlertType::Warning => self.theme.alert.styles.warning.as_parts(),
AlertType::Caution => self.theme.alert.styles.caution.as_parts(),
};
let prefix_color = prefix_color.or(self.theme.alert.base_colors.foreground);
let title_color = Some(title_color);
let title = title.unwrap_or_else(|| default_title.to_string());
let title = match symbol {
Some(symbol) => format!("{symbol} {title}"),
None => title,
};
let title_colors = Colors { foreground: prefix_color, background: self.theme.alert.base_colors.background };
let title = format!("{symbol} {title}");
let title_colors = Colors { foreground: title_color, background: self.theme.alert.base_colors.background };
lines.insert(0, Line::from(Text::from("")));
lines.insert(0, Line::from(Text::new(title, TextStyle::default().colors(title_colors))));

let prefix = self.theme.block_quote.prefix.clone().unwrap_or_default();
self.push_quoted_text(lines, prefix, self.theme.alert.base_colors, prefix_color)
self.push_quoted_text(lines, prefix, self.theme.alert.base_colors, title_color)
}

fn push_quoted_text(
Expand Down
98 changes: 64 additions & 34 deletions src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,25 +499,40 @@ pub(crate) struct AlertTypeStyle<S: AlertTypeProperties> {
pub(crate) color: Option<Color>,

/// The title to be used.
#[serde(default = "S::default_title")]
pub(crate) title: String,
#[serde(default)]
pub(crate) title: Option<String>,

/// The symbol to be used.
#[serde(default = "S::default_symbol")]
#[serde(default)]
pub(crate) symbol: Option<String>,

#[serde(skip)]
_unused: PhantomData<S>,
}

impl<S: AlertTypeProperties> Default for AlertTypeStyle<S> {
fn default() -> Self {
Self {
color: Default::default(),
title: Default::default(),
symbol: Default::default(),
_unused: Default::default(),
}
}
}

impl<S: AlertTypeProperties> AlertTypeStyle<S> {
pub(crate) fn as_parts(&self) -> (&Option<Color>, &str, Option<&str>) {
(&self.color, &self.title, self.symbol.as_deref())
pub(crate) fn as_parts(&self) -> (Color, &str, &str) {
(
self.color.unwrap_or(S::default_color()),
self.title.as_deref().unwrap_or(S::default_title()),
self.symbol.as_deref().unwrap_or(S::default_symbol()),
)
}

fn resolve_palette_colors(&mut self, palette: &ColorPalette) -> Result<(), UndefinedPaletteColorError> {
let Self { color, title: _, symbol: _, _unused: _ } = self;
if let Some(color) = color.as_mut() {
if let Some(color) = color {
*color = color.resolve(palette)?;
}
Ok(())
Expand All @@ -541,15 +556,10 @@ impl<S: AlertTypeProperties> Clone for AlertTypeStyle<S> {
}
}

impl<S: AlertTypeProperties> Default for AlertTypeStyle<S> {
fn default() -> Self {
Self { color: None, title: S::default_title(), symbol: S::default_symbol(), _unused: PhantomData }
}
}

pub(crate) trait AlertTypeProperties {
fn default_title() -> String;
fn default_symbol() -> Option<String>;
fn default_title() -> &'static str;
fn default_symbol() -> &'static str;
fn default_color() -> Color;
}

pub(crate) struct NoteAlertType;
Expand All @@ -559,52 +569,72 @@ pub(crate) struct WarningAlertType;
pub(crate) struct CautionAlertType;

impl AlertTypeProperties for NoteAlertType {
fn default_title() -> String {
"Note".into()
fn default_title() -> &'static str {
"Note"
}

fn default_symbol() -> &'static str {
"󰋽"
}

fn default_symbol() -> Option<String> {
Some("󰋽".into())
fn default_color() -> Color {
Color::Blue
}
}

impl AlertTypeProperties for TipAlertType {
fn default_title() -> String {
"Tip".into()
fn default_title() -> &'static str {
"Tip"
}

fn default_symbol() -> Option<String> {
Some("".into())
fn default_symbol() -> &'static str {
""
}

fn default_color() -> Color {
Color::Green
}
}

impl AlertTypeProperties for ImportantAlertType {
fn default_title() -> String {
"Important".into()
fn default_title() -> &'static str {
"Important"
}

fn default_symbol() -> Option<String> {
Some("".into())
fn default_symbol() -> &'static str {
""
}

fn default_color() -> Color {
Color::Cyan
}
}

impl AlertTypeProperties for WarningAlertType {
fn default_title() -> String {
"Warning".into()
fn default_title() -> &'static str {
"Warning"
}

fn default_symbol() -> &'static str {
""
}

fn default_symbol() -> Option<String> {
Some("".into())
fn default_color() -> Color {
Color::Yellow
}
}

impl AlertTypeProperties for CautionAlertType {
fn default_title() -> String {
"Caution".into()
fn default_title() -> &'static str {
"Caution"
}

fn default_symbol() -> &'static str {
"󰳦"
}

fn default_symbol() -> Option<String> {
Some("󰳦".into())
fn default_color() -> Color {
Color::Red
}
}

Expand Down
19 changes: 12 additions & 7 deletions themes/catppuccin-frappe.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,20 @@ block_quote:

alert:
prefix: ""
colors:
base_colors:
foreground: "c6d0f5"
background: "414559"
types:
note: "8caaee"
tip: "a6d189"
important: "ca9ee6"
warning: "e5c890"
caution: "e78284"
styles:
note:
color: "8caaee"
tip:
color: "a6d189"
important:
color: "ca9ee6"
warning:
color: "e5c890"
caution:
color: "e78284"

typst:
colors:
Expand Down
19 changes: 12 additions & 7 deletions themes/catppuccin-latte.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,20 @@ block_quote:

alert:
prefix: ""
colors:
base_colors:
foreground: "4c4f69"
background: "ccd0da"
types:
note: "1e66f5"
tip: "40a02b"
important: "8839ef"
warning: "df8e1d"
caution: "d20f39"
styles:
note:
color: "1e66f5"
tip:
color: "40a02b"
important:
color: "8839ef"
warning:
color: "df8e1d"
caution:
color: "d20f39"

typst:
colors:
Expand Down
19 changes: 12 additions & 7 deletions themes/catppuccin-macchiato.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,20 @@ block_quote:

alert:
prefix: ""
colors:
base_colors:
foreground: "cad3f5"
background: "363a4f"
types:
note: "8aadf4"
tip: "a6da95"
important: "c6a0f6"
warning: "f5a97f"
caution: "ed8796"
styles:
note:
color: "8aadf4"
tip:
color: "a6da95"
important:
color: "c6a0f6"
warning:
color: "f5a97f"
caution:
color: "ed8796"

typst:
colors:
Expand Down
19 changes: 12 additions & 7 deletions themes/catppuccin-mocha.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,20 @@ block_quote:

alert:
prefix: ""
colors:
base_colors:
foreground: "cdd6f4"
background: "313244"
types:
note: "89b4fa"
tip: "a6e3a1"
important: "cba6f7"
warning: "fab387"
caution: "f38ba8"
styles:
note:
color: "89b4fa"
tip:
color: "a6e3a1"
important:
color: "cba6f7"
warning:
color: "fab387"
caution:
color: "f38ba8"

typst:
colors:
Expand Down
19 changes: 12 additions & 7 deletions themes/light.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,20 @@ block_quote:

alert:
prefix: ""
colors:
base_colors:
foreground: "212529"
background: "e9ecef"
types:
note: "1e66f5"
tip: "40a02b"
important: "8839ef"
warning: "df8e1d"
caution: "d20f39"
styles:
note:
color: "1e66f5"
tip:
color: "40a02b"
important:
color: "8839ef"
warning:
color: "df8e1d"
caution:
color: "d20f39"

typst:
colors:
Expand Down
19 changes: 12 additions & 7 deletions themes/terminal-dark.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,20 @@ block_quote:

alert:
prefix: ""
colors:
base_colors:
foreground: white
background: black
types:
note: blue
tip: green
important: magenta
warning: yellow
caution: red
styles:
note:
color: blue
tip:
color: green
important:
color: magenta
warning:
color: yellow
caution:
color: red

typst:
colors:
Expand Down
19 changes: 12 additions & 7 deletions themes/terminal-light.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,20 @@ block_quote:

alert:
prefix: ""
colors:
base_colors:
foreground: black
background: grey
types:
note: dark_blue
tip: dark_green
important: dark_magenta
warning: dark_yellow
caution: dark_red
styles:
note:
color: dark_blue
tip:
color: dark_green
important:
color: dark_magenta
warning:
color: dark_yellow
caution:
color: dark_red

typst:
colors:
Expand Down
Loading

0 comments on commit 68b10c1

Please sign in to comment.