use std::rc::Rc;
use waft_embedder::{Brightness, Color, FontStyle, FontWeight, TextAlign, TextDecoration};
use waft_foundation::App;
use waft_painting::{
Border, BorderRadius, BorderSide, BorderStyle, BoxDecoration, EdgeInsets, K_DEFAULT_FONT_SIZE,
TextStyle,
};
use waft_rendering::{
FlexColumnWidth, TableBorder, TableCellVerticalAlignment, TableColumnWidthRef,
};
use waft_widgets::{
BuildContext, DefaultTextStyle, InheritedWidget, IntoWidget, KeyRef, MediaQuery, WidgetRef,
};
mod system_colors {
use waft_embedder::Color;
pub(super) const LINK: (Color, Color) = (Color::new(0xFF007AFF), Color::new(0xFF0984FF));
pub(super) const BLUE: (Color, Color) = (Color::new(0xFF007AFF), Color::new(0xFF0A84FF));
pub(super) const GREY_6: (Color, Color) = (Color::new(0xFFF2F2F7), Color::new(0xFF1C1C1E));
pub(super) const GREY_4: (Color, Color) = (Color::new(0xFFD1D1D6), Color::new(0xFF3A3A3C));
pub(super) const SEPARATOR: (Color, Color) = (Color::new(0x493C3C43), Color::new(0x99545458));
}
#[derive(Clone, Debug, PartialEq)]
pub struct MarkdownStyleSheet {
pub text: TextStyle,
pub headings: [HeadingStyle; 6],
pub link: TextStyle,
pub inline_code: TextStyle,
pub code_block: BlockStyle,
pub quote: BlockStyle,
pub list: ListStyle,
pub table: TableStyle,
pub rule: BoxDecoration,
pub block_spacing: f64,
}
#[derive(Clone, Debug, PartialEq)]
pub struct HeadingStyle {
pub text: TextStyle,
pub padding: EdgeInsets,
}
#[derive(Clone, Debug, PartialEq)]
pub struct BlockStyle {
pub text: TextStyle,
pub padding: EdgeInsets,
pub decoration: BoxDecoration,
}
#[derive(Clone, Debug, PartialEq)]
pub struct ListStyle {
pub indent: f64,
pub bullet: TextStyle,
pub bullet_padding: EdgeInsets,
pub checkbox: TextStyle,
}
#[derive(Clone, Debug)]
pub struct TableStyle {
pub head: TextStyle,
pub body: TextStyle,
pub head_align: TextAlign,
pub border: TableBorder,
pub cell_padding: EdgeInsets,
pub stripe: Option<BoxDecoration>,
pub column_width: TableColumnWidthRef,
pub vertical_alignment: TableCellVerticalAlignment,
}
impl PartialEq for TableStyle {
fn eq(&self, other: &TableStyle) -> bool {
self.head == other.head
&& self.body == other.body
&& self.head_align == other.head_align
&& self.border == other.border
&& self.cell_padding == other.cell_padding
&& self.stripe == other.stripe
&& Rc::ptr_eq(&self.column_width, &other.column_width)
&& self.vertical_alignment == other.vertical_alignment
}
}
impl MarkdownStyleSheet {
pub fn from_text_style(text: TextStyle, brightness: Brightness) -> MarkdownStyleSheet {
let by_brightness = |(light, dark): (Color, Color)| match brightness {
Brightness::Dark => dark,
Brightness::Light => light,
};
let font_size = text.font_size.unwrap_or(K_DEFAULT_FONT_SIZE);
let heading = |size_increase: Option<f64>| {
let mut style = text.copy_with().font_weight(FontWeight::W500);
if let Some(increase) = size_increase {
style = style.font_size(font_size + increase);
}
HeadingStyle {
text: style,
padding: EdgeInsets::ZERO,
}
};
let code = text
.copy_with()
.font_family("monospace")
.font_size(font_size * 0.85);
let grey_6 = by_brightness(system_colors::GREY_6);
let grey_4 = by_brightness(system_colors::GREY_4);
let side = |width: f64| {
BorderSide::new(
grey_4,
width,
BorderStyle::Solid,
BorderSide::STROKE_ALIGN_INSIDE,
)
};
MarkdownStyleSheet {
text: text.clone(),
headings: [
heading(Some(10.0)),
heading(Some(8.0)),
heading(Some(6.0)),
heading(Some(4.0)),
heading(Some(2.0)),
heading(None),
],
link: TextStyle::new().color(by_brightness(system_colors::LINK)),
inline_code: code.clone(),
code_block: BlockStyle {
text: code,
padding: EdgeInsets::all(8.0),
decoration: BoxDecoration::new().color(grey_6),
},
quote: BlockStyle {
text: text.clone(),
padding: EdgeInsets::all(16.0),
decoration: BoxDecoration::new().color(grey_6).border(Border {
left: side(4.0),
top: BorderSide::NONE,
right: BorderSide::NONE,
bottom: BorderSide::NONE,
}),
},
list: ListStyle {
indent: 24.0,
bullet: text.clone(),
bullet_padding: EdgeInsets::only(0.0, 0.0, 4.0, 0.0),
checkbox: text.copy_with().color(by_brightness(system_colors::BLUE)),
},
table: TableStyle {
head: text.copy_with().font_weight(FontWeight::W600),
body: text.clone(),
head_align: TextAlign::Center,
border: TableBorder::all(
by_brightness(system_colors::SEPARATOR),
0.0,
BorderStyle::Solid,
BorderRadius::ZERO,
),
cell_padding: EdgeInsets::from_ltrb(16.0, 8.0, 16.0, 8.0),
stripe: Some(BoxDecoration::new().color(grey_6)),
column_width: FlexColumnWidth::default_width(),
vertical_alignment: TableCellVerticalAlignment::Middle,
},
rule: BoxDecoration::new().border(Border {
top: side(1.0),
right: BorderSide::NONE,
bottom: BorderSide::NONE,
left: BorderSide::NONE,
}),
block_spacing: 8.0,
}
}
pub(crate) fn emphasis(style: &TextStyle) -> TextStyle {
style.copy_with().font_style(FontStyle::Italic)
}
pub(crate) fn strong(style: &TextStyle) -> TextStyle {
style.copy_with().font_weight(FontWeight::BOLD)
}
pub(crate) fn strikethrough(style: &TextStyle) -> TextStyle {
style.copy_with().decoration(TextDecoration::LINE_THROUGH)
}
}
#[derive(Debug)]
pub struct DefaultMarkdownStyle {
pub key: Option<KeyRef>,
pub style_sheet: MarkdownStyleSheet,
pub child: WidgetRef,
}
impl DefaultMarkdownStyle {
pub fn new<K>(
style_sheet: MarkdownStyleSheet,
child: impl IntoWidget<K>,
) -> DefaultMarkdownStyle {
DefaultMarkdownStyle {
key: None,
style_sheet,
child: child.into_widget(),
}
}
pub fn key(mut self, key: KeyRef) -> DefaultMarkdownStyle {
self.key = Some(key);
self
}
pub fn maybe_of(app: &mut App, context: BuildContext) -> Option<MarkdownStyleSheet> {
context
.depend_on_inherited_widget_of_exact_type::<DefaultMarkdownStyle>(app)
.map(|widget| widget.style_sheet.clone())
}
pub fn of(app: &mut App, context: BuildContext) -> MarkdownStyleSheet {
Self::maybe_of(app, context).unwrap_or_else(|| {
let text = DefaultTextStyle::of(app, context).style;
let brightness = MediaQuery::platform_brightness_of(app, context);
MarkdownStyleSheet::from_text_style(text, brightness)
})
}
}
impl InheritedWidget for DefaultMarkdownStyle {
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn child(&self) -> &WidgetRef {
&self.child
}
fn update_should_notify(&self, old_widget: &DefaultMarkdownStyle) -> bool {
self.style_sheet != old_widget.style_sheet
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn from_text_style_sizes_the_headings_from_the_text() {
let text = TextStyle::new().font_size(15.0);
let sheet = MarkdownStyleSheet::from_text_style(text.clone(), Brightness::Light);
assert_eq!(sheet.text, text);
let sizes: Vec<Option<f64>> = sheet
.headings
.iter()
.map(|heading| heading.text.font_size)
.collect();
assert_eq!(
sizes,
[
Some(25.0),
Some(23.0),
Some(21.0),
Some(19.0),
Some(17.0),
Some(15.0)
]
);
assert_eq!(sheet.code_block.text.font_size, Some(15.0 * 0.85));
let without_size = MarkdownStyleSheet::from_text_style(TextStyle::new(), Brightness::Light);
assert_eq!(
without_size.headings[0].text.font_size,
Some(K_DEFAULT_FONT_SIZE + 10.0)
);
}
#[test]
fn from_text_style_takes_the_colors_of_the_brightness() {
let light = MarkdownStyleSheet::from_text_style(TextStyle::new(), Brightness::Light);
let dark = MarkdownStyleSheet::from_text_style(TextStyle::new(), Brightness::Dark);
assert_eq!(light.link.color.as_deref(), Some(&system_colors::LINK.0));
assert_eq!(dark.link.color.as_deref(), Some(&system_colors::LINK.1));
assert_ne!(light.quote, dark.quote);
assert_ne!(light.table, dark.table);
assert_eq!(light, light.clone());
}
}