use std::{sync::Arc, time::Duration};
use super::{PaletteColor, ThemePalette};
use crate::{
layout::text_style::{FontFeatures, FontStyle, FontWeight, TextStrikethrough, TextStyle, TextUnderline},
node::{TextColor, border::BorderRadius, padding::Padding},
ui::bulleted_list::look as list,
};
#[derive(Clone, Default, PartialEq)]
pub struct MarkdownTextStyle {
pub font_family: Option<Arc<str>>,
pub font_size: Option<f32>,
pub font_size_scale: Option<f32>,
pub line_height: Option<f32>,
pub letter_spacing: Option<f32>,
pub font_features: Option<FontFeatures>,
pub weight: Option<FontWeight>,
pub style: Option<FontStyle>,
pub color: Option<TextColor>,
pub underline: Option<TextUnderline>,
pub strikethrough: Option<TextStrikethrough>,
pub baseline_shift: Option<f32>,
}
impl MarkdownTextStyle {
pub fn new() -> Self {
Self::default()
}
pub fn apply(&self, base: &TextStyle) -> TextStyle {
let mut style = base.clone();
if let Some(font_family) = &self.font_family {
style.font_family = font_family.clone();
}
if let Some(font_size) = self.font_size {
style.font_size = font_size;
}
if let Some(font_size_scale) = self.font_size_scale {
style.font_size *= font_size_scale;
}
if let Some(line_height) = self.line_height {
style.line_height = line_height;
}
if let Some(letter_spacing) = self.letter_spacing {
style.letter_spacing = letter_spacing;
}
if let Some(font_features) = &self.font_features {
style.font_features = font_features.clone();
}
if let Some(weight) = self.weight {
style.weight = weight;
}
if let Some(font_style) = self.style {
style.style = font_style;
}
if let Some(TextColor::Color(color)) = &self.color {
style.color = *color;
}
if let Some(underline) = self.underline {
style.underline = Some(underline);
}
if let Some(strikethrough) = self.strikethrough {
style.strikethrough = Some(strikethrough);
}
if let Some(shift) = self.baseline_shift {
style.baseline_shift = shift * base.font_size;
}
style
}
pub fn resolve(&self, palette: &ThemePalette) -> Self {
Self {
color: resolve(&self.color, palette),
..self.clone()
}
}
}
fn resolve(color: &Option<TextColor>, palette: &ThemePalette) -> Option<TextColor> {
match color {
Some(TextColor::Palette(role)) => palette.try_get(role.clone()).map(TextColor::Color),
other => other.clone(),
}
}
#[derive(Clone, Default, PartialEq)]
pub struct MarkdownInlineStyle {
pub text: MarkdownTextStyle,
}
impl MarkdownInlineStyle {
pub fn new() -> Self {
Self::default()
}
pub fn apply(&self, base: &TextStyle) -> TextStyle {
self.text.apply(base)
}
pub fn resolve(&self, palette: &ThemePalette) -> Self {
Self {
text: self.text.resolve(palette),
}
}
}
#[derive(Clone, Default, PartialEq)]
pub struct MarkdownBlockStyle {
pub background: Option<TextColor>,
pub border_color: Option<TextColor>,
pub border_width: Option<f32>,
pub radius: Option<f32>,
pub padding: Option<Padding>,
}
impl MarkdownBlockStyle {
pub fn new() -> Self {
Self::default()
}
pub fn resolve(&self, palette: &ThemePalette) -> Self {
Self {
background: resolve(&self.background, palette),
border_color: resolve(&self.border_color, palette),
..self.clone()
}
}
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct MarkdownLinkState {
pub color: Option<TextColor>,
pub underline_color: Option<TextColor>,
pub background: Option<TextColor>,
pub trailing_color: Option<TextColor>,
}
impl MarkdownLinkState {
pub fn new() -> Self {
Self::default()
}
pub fn color(mut self, color: impl Into<TextColor>) -> Self {
self.color = Some(color.into());
self
}
pub fn underline_color(mut self, color: impl Into<TextColor>) -> Self {
self.underline_color = Some(color.into());
self
}
pub fn background(mut self, color: impl Into<TextColor>) -> Self {
self.background = Some(color.into());
self
}
pub fn trailing_color(mut self, color: impl Into<TextColor>) -> Self {
self.trailing_color = Some(color.into());
self
}
pub fn resolve(&self, palette: &ThemePalette) -> Self {
Self {
color: resolve(&self.color, palette),
underline_color: resolve(&self.underline_color, palette),
background: resolve(&self.background, palette),
trailing_color: resolve(&self.trailing_color, palette),
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct MarkdownFocusRing {
pub width: f32,
pub offset: f32,
pub color: TextColor,
pub radius: f32,
}
impl MarkdownFocusRing {
pub fn new(width: f32, color: impl Into<TextColor>) -> Self {
Self {
width,
offset: 0.0,
color: color.into(),
radius: 0.0,
}
}
pub fn offset(mut self, offset: f32) -> Self {
self.offset = offset;
self
}
pub fn radius(mut self, radius: f32) -> Self {
self.radius = radius;
self
}
pub fn resolve(&self, palette: &ThemePalette) -> Self {
Self {
color: resolve(&Some(self.color.clone()), palette).unwrap_or_else(|| self.color.clone()),
..self.clone()
}
}
}
#[derive(Clone, PartialEq)]
pub struct ThemeMarkdown {
pub document_spacing: f32,
pub list_item_spacing: f32,
pub task_item_spacing: f32,
pub nested_block_spacing: f32,
pub list_marker_gap: f32,
pub bullet_gap: f32,
pub box_gap: f32,
pub bullet_indent: f32,
pub bullet_size: f32,
pub unordered_list_marker_width: f32,
pub ordered_list_marker_width: f32,
pub table_row_spacing: f32,
pub table_column_spacing: f32,
pub table_cell_min_width: f32,
pub table_stripe: Option<TextColor>,
pub heading: MarkdownInlineStyle,
pub heading_1: MarkdownInlineStyle,
pub heading_2: MarkdownInlineStyle,
pub heading_3: MarkdownInlineStyle,
pub heading_4: MarkdownInlineStyle,
pub heading_5: MarkdownInlineStyle,
pub heading_6: MarkdownInlineStyle,
pub heading_2_rule: Option<TextColor>,
pub strong: MarkdownInlineStyle,
pub emphasis: MarkdownInlineStyle,
pub inline_code: MarkdownInlineStyle,
pub kbd: MarkdownInlineStyle,
pub link: MarkdownInlineStyle,
pub link_hovered: MarkdownLinkState,
pub link_focused: MarkdownLinkState,
pub link_focus_ring: Option<MarkdownFocusRing>,
pub link_radius: BorderRadius,
pub link_tooltip: MarkdownInlineStyle,
pub link_tooltip_box: MarkdownBlockStyle,
pub link_tooltip_delay: Duration,
pub strikethrough: MarkdownInlineStyle,
pub list_marker: MarkdownInlineStyle,
pub task_done: MarkdownInlineStyle,
pub table_header: MarkdownInlineStyle,
pub table_cell: MarkdownInlineStyle,
pub table_marker: MarkdownInlineStyle,
pub table_box: MarkdownBlockStyle,
pub table_header_box: MarkdownBlockStyle,
pub table_cell_box: MarkdownBlockStyle,
pub rule: MarkdownBlockStyle,
pub image_box: MarkdownBlockStyle,
pub image_height: f32,
pub image_caption: MarkdownInlineStyle,
pub details_box: MarkdownBlockStyle,
pub details_summary_box: MarkdownBlockStyle,
pub details_summary: MarkdownInlineStyle,
pub details_padding: Padding,
pub footnote_reference: MarkdownInlineStyle,
pub footnote: MarkdownInlineStyle,
pub footnotes_rule_width: f32,
}
impl ThemeMarkdown {
pub fn new() -> Self {
Self::default()
}
pub fn resolve(&self, palette: &ThemePalette) -> Self {
let inline = |style: &MarkdownInlineStyle| style.resolve(palette);
let block = |style: &MarkdownBlockStyle| style.resolve(palette);
Self {
table_stripe: resolve(&self.table_stripe, palette),
heading: inline(&self.heading),
heading_1: inline(&self.heading_1),
heading_2: inline(&self.heading_2),
heading_3: inline(&self.heading_3),
heading_4: inline(&self.heading_4),
heading_5: inline(&self.heading_5),
heading_6: inline(&self.heading_6),
heading_2_rule: resolve(&self.heading_2_rule, palette),
strong: inline(&self.strong),
emphasis: inline(&self.emphasis),
inline_code: inline(&self.inline_code),
kbd: inline(&self.kbd),
link: inline(&self.link),
link_hovered: self.link_hovered.resolve(palette),
link_focused: self.link_focused.resolve(palette),
link_focus_ring: self.link_focus_ring.as_ref().map(|ring| ring.resolve(palette)),
link_tooltip: inline(&self.link_tooltip),
link_tooltip_box: block(&self.link_tooltip_box),
strikethrough: inline(&self.strikethrough),
list_marker: inline(&self.list_marker),
task_done: inline(&self.task_done),
table_header: inline(&self.table_header),
table_cell: inline(&self.table_cell),
table_marker: inline(&self.table_marker),
table_box: block(&self.table_box),
table_header_box: block(&self.table_header_box),
table_cell_box: block(&self.table_cell_box),
rule: block(&self.rule),
image_box: block(&self.image_box),
image_caption: inline(&self.image_caption),
details_box: block(&self.details_box),
details_summary_box: block(&self.details_summary_box),
details_summary: inline(&self.details_summary),
footnote_reference: inline(&self.footnote_reference),
footnote: inline(&self.footnote),
..self.clone()
}
}
}
impl Default for ThemeMarkdown {
fn default() -> Self {
use PaletteColor::{Bar, Border, Foreground, Info, Muted, MutedForeground, Popover, PopoverForeground, Ring};
let color = |role: PaletteColor| Some(TextColor::Palette(role));
let of_15 = |size: f32| Some(size / 15.0);
let mut heading = MarkdownInlineStyle::default();
heading.text.color = color(Foreground);
let heading_1 = MarkdownInlineStyle::default();
let heading_2 = MarkdownInlineStyle::default();
let heading_3 = MarkdownInlineStyle::default();
let heading_4 = MarkdownInlineStyle::default();
let heading_5 = MarkdownInlineStyle::default();
let mut heading_6 = MarkdownInlineStyle::default();
heading_6.text.color = color(MutedForeground);
let mut strong = MarkdownInlineStyle::default();
strong.text.weight = Some(FontWeight::Bold);
let mut emphasis = MarkdownInlineStyle::default();
emphasis.text.style = Some(FontStyle::Italic);
let mut inline_code = MarkdownInlineStyle::default();
inline_code.text.font_family = Some(Arc::from("monospace"));
inline_code.text.font_size_scale = of_15(13.0);
inline_code.text.color = color(Foreground);
let mut kbd = MarkdownInlineStyle::default();
kbd.text.font_family = Some(Arc::from("monospace"));
kbd.text.font_size_scale = of_15(11.0);
kbd.text.color = color(MutedForeground);
let mut link = MarkdownInlineStyle::default();
link.text.color = color(Info);
link.text.weight = Some(FontWeight::Medium);
let link_focus_ring = MarkdownFocusRing::new(2.0, Ring).offset(1.0).radius(3.0);
let mut link_tooltip = MarkdownInlineStyle::default();
link_tooltip.text.font_size_scale = of_15(12.0);
link_tooltip.text.color = color(PopoverForeground);
let link_tooltip_box = MarkdownBlockStyle {
background: color(Popover),
border_color: color(Border),
border_width: Some(1.0),
radius: Some(6.0),
padding: Some(Padding::symmetric(8.0, 4.0)),
};
let mut list_marker = MarkdownInlineStyle::default();
list_marker.text.color = color(MutedForeground);
let mut task_done = MarkdownInlineStyle::default();
task_done.text.color = color(MutedForeground);
let mut table_header = MarkdownInlineStyle::default();
table_header.text.font_size_scale = of_15(14.0);
table_header.text.weight = Some(FontWeight::SemiBold);
table_header.text.color = color(Foreground);
let table_cell = inline_code.clone();
let mut table_marker = MarkdownInlineStyle::default();
table_marker.text.color = color(Border);
let table_box = MarkdownBlockStyle {
background: None,
border_color: color(Border),
border_width: Some(1.0),
radius: Some(6.0),
padding: None,
};
let table_header_box = MarkdownBlockStyle {
background: color(Bar),
border_color: None,
border_width: None,
radius: None,
padding: Some(Padding::symmetric(12.0, 8.0)),
};
let table_cell_box = MarkdownBlockStyle {
background: None,
border_color: None,
border_width: None,
radius: None,
padding: Some(Padding::symmetric(12.0, 8.0)),
};
let rule = MarkdownBlockStyle {
background: color(Border),
..MarkdownBlockStyle::default()
};
let image_box = MarkdownBlockStyle {
background: color(Muted),
border_color: color(Border),
border_width: Some(1.0),
radius: Some(8.0),
padding: None,
};
let mut image_caption = MarkdownInlineStyle::default();
image_caption.text.font_size_scale = of_15(13.0);
image_caption.text.color = color(MutedForeground);
let details_box = MarkdownBlockStyle {
background: None,
border_color: color(Border),
border_width: Some(1.0),
radius: Some(6.0),
padding: None,
};
let details_summary_box = MarkdownBlockStyle {
background: color(Bar),
border_color: color(Border),
border_width: Some(1.0),
radius: None,
padding: Some(Padding::symmetric(12.0, 10.0)),
};
let mut details_summary = MarkdownInlineStyle::default();
details_summary.text.font_size_scale = of_15(14.0);
details_summary.text.weight = Some(FontWeight::Medium);
details_summary.text.color = color(Foreground);
let mut footnote_reference = MarkdownInlineStyle::default();
footnote_reference.text.font_size_scale = of_15(11.0);
footnote_reference.text.weight = Some(FontWeight::SemiBold);
footnote_reference.text.color = color(Info);
footnote_reference.text.baseline_shift = Some(0.45);
let mut footnote = MarkdownInlineStyle::default();
footnote.text.font_size_scale = of_15(13.0);
footnote.text.color = color(MutedForeground);
Self {
document_spacing: 16.0,
list_item_spacing: list::ITEM_GAP,
task_item_spacing: 8.0,
nested_block_spacing: list::ITEM_GAP,
list_marker_gap: list::NUMBER_GAP,
bullet_gap: list::BULLET_GAP,
box_gap: 6.0,
bullet_indent: 6.0,
bullet_size: list::BULLET,
unordered_list_marker_width: 11.0,
ordered_list_marker_width: list::NUMBER_WIDTH,
table_row_spacing: 0.0,
table_column_spacing: 0.0,
table_cell_min_width: 80.0,
table_stripe: color(Muted),
heading,
heading_1,
heading_2,
heading_3,
heading_4,
heading_5,
heading_6,
heading_2_rule: color(Border),
strong,
emphasis,
inline_code,
kbd,
link,
link_hovered: MarkdownLinkState::default(),
link_focused: MarkdownLinkState::default(),
link_focus_ring: Some(link_focus_ring),
link_radius: BorderRadius::all(2.0),
link_tooltip,
link_tooltip_box,
link_tooltip_delay: Duration::from_millis(500),
strikethrough: MarkdownInlineStyle {
text: MarkdownTextStyle {
strikethrough: Some(TextStrikethrough::new()),
color: color(MutedForeground),
..MarkdownTextStyle::default()
},
},
list_marker,
task_done,
table_header,
table_cell,
table_marker,
table_box,
table_header_box,
table_cell_box,
rule,
image_box,
image_height: 150.0,
image_caption,
details_box,
details_summary_box,
details_summary,
details_padding: Padding::all(12.0),
footnote_reference,
footnote,
footnotes_rule_width: 120.0,
}
}
}