use std::any::Any;
use std::fmt;
use std::rc::Rc;
use waft_embedder::valo::{FillRule, Point};
use waft_embedder::{
Canvas, Clip, Color, Paint, PathBuilder, PlaceholderAlignment, RRect, Size, TextAlign,
TextBaseline, rrect_radii_elliptical,
};
use waft_foundation::{App, Handle, Listener, Ref};
use waft_gestures::{RecognizerRef, TapGestureRecognizer};
use waft_painting::{
Axis, EdgeInsets, InlineSpanRef, K_DEFAULT_FONT_SIZE, TextSpan, TextStyle, draw_drrect,
};
use waft_rendering::{
CrossAxisAlignment, CustomPainter, FixedColumnWidth, FlexFit, IntrinsicColumnWidth,
MainAxisSize,
};
use waft_widgets::{
BuildContext, Column, Container, CustomPaint, DecoratedBox, Flexible, GestureDetector,
IntoWidget, Padding, RawScrollbar, Row, ScrollController, ScrollControllerLeaf,
SingleChildScrollView, SizedBox, State, StateData, StatefulWidget, Table, TableCell, TableRow,
Text, WidgetRef, WidgetSpan,
};
use crate::document::{Alignment, Block, Cell, Inline, ListItem};
use crate::style_sheet::MarkdownStyleSheet;
use crate::widget::MarkdownHooks;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Bullet {
pub number: Option<u64>,
pub index: usize,
pub depth: usize,
}
pub struct RenderContext<'a> {
style_sheet: &'a MarkdownStyleSheet,
hooks: &'a MarkdownHooks,
fit_content: bool,
text: TextStyle,
list_depth: usize,
recognizers: &'a mut Vec<RecognizerRef>,
}
impl<'a> RenderContext<'a> {
pub(crate) fn new(
style_sheet: &'a MarkdownStyleSheet,
hooks: &'a MarkdownHooks,
fit_content: bool,
recognizers: &'a mut Vec<RecognizerRef>,
) -> RenderContext<'a> {
RenderContext {
style_sheet,
hooks,
fit_content,
text: style_sheet.text.clone(),
list_depth: 0,
recognizers,
}
}
fn link_tap(&mut self, app: &mut App, url: &str, title: &str) -> Option<LinkTap> {
let on_tap_link = self.hooks.on_tap_link.clone()?;
let (url, title) = (url.to_owned(), title.to_owned());
let on_tap = Listener::new(move |app| on_tap_link(app, &url, &title));
let recognizer = TapGestureRecognizer::new(app);
recognizer.set_on_tap(app, Some(on_tap.clone()));
let recognizer: RecognizerRef = recognizer.into_erased();
self.recognizers.push(recognizer.clone());
Some(LinkTap { recognizer, on_tap })
}
}
pub fn blocks(
cx: &mut RenderContext,
app: &mut App,
blocks: &[Block],
spacing: f64,
) -> Vec<WidgetRef> {
spaced(
blocks.iter().map(|block| self::block(cx, app, block)),
spacing,
)
}
pub fn block(cx: &mut RenderContext, app: &mut App, block: &Block) -> WidgetRef {
match block {
Block::Paragraph(content) => paragraph(cx, app, content),
Block::Heading { level, content } => heading(cx, app, *level, content),
Block::Quote(blocks) => quote(cx, app, blocks),
Block::List { start, items, .. } => list(cx, app, *start, items),
Block::CodeBlock { language, text } => code_block(cx, app, language.as_deref(), text),
Block::Table {
alignments,
head,
rows,
} => table(cx, app, alignments, head, rows),
Block::Rule => rule(cx),
}
}
pub fn paragraph(cx: &mut RenderContext, app: &mut App, content: &[Inline]) -> WidgetRef {
let style = cx.text.clone();
rich_text(spans(cx, app, content, &style), style, TextAlign::Start)
}
pub fn heading(cx: &mut RenderContext, app: &mut App, level: u8, content: &[Inline]) -> WidgetRef {
let heading = &cx.style_sheet.headings[usize::from(level.clamp(1, 6)) - 1];
let style = cx.text.merge(Some(&heading.text));
padded(
heading.padding,
rich_text(spans(cx, app, content, &style), style, TextAlign::Start),
)
}
pub fn quote(cx: &mut RenderContext, app: &mut App, blocks: &[Block]) -> WidgetRef {
let sheet = cx.style_sheet;
let quoted = cx.text.merge(Some(&sheet.quote.text));
let outer = std::mem::replace(&mut cx.text, quoted);
let children = self::blocks(cx, app, blocks, sheet.block_spacing);
cx.text = outer;
DecoratedBox::new(sheet.quote.decoration.clone())
.child(Padding::new(sheet.quote.padding).child(column(children, cx.fit_content)))
.into_widget()
}
pub fn list(
cx: &mut RenderContext,
app: &mut App,
start: Option<u64>,
items: &[ListItem],
) -> WidgetRef {
let (sheet, hooks) = (cx.style_sheet, cx.hooks);
let spacing = sheet.block_spacing;
let depth = cx.list_depth;
cx.list_depth += 1;
let mut rows = Vec::with_capacity(items.len());
for (index, item) in items.iter().enumerate() {
let marker = match item.checked {
Some(checked) => match &hooks.checkbox {
Some(hook) => hook(app, checked),
None => checkbox(cx, checked),
},
None => {
let marker = Bullet {
number: start.map(|start| start + index as u64),
index,
depth,
};
match &hooks.bullet {
Some(hook) => hook(app, &marker),
None => bullet(cx, &marker),
}
}
};
let content = column(
self::blocks(cx, app, &item.content, spacing),
cx.fit_content,
);
rows.push(list_item(cx, marker, content));
}
cx.list_depth = depth;
column(spaced(rows, spacing), cx.fit_content)
}
pub fn bullet(cx: &RenderContext, bullet: &Bullet) -> WidgetRef {
let style = cx.text.merge(Some(&cx.style_sheet.list.bullet));
let (text, text_align) = match bullet.number {
None => ("•".to_owned(), TextAlign::Center),
Some(number) => (format!("{number}."), TextAlign::Right),
};
Text::new(text)
.style(style)
.text_align(text_align)
.into_widget()
}
pub fn checkbox(cx: &RenderContext, checked: bool) -> WidgetRef {
let style = cx.text.merge(Some(&cx.style_sheet.list.checkbox));
let size = style.font_size.unwrap_or(K_DEFAULT_FONT_SIZE);
let color = style
.color
.as_deref()
.copied()
.unwrap_or(Color::new(0xFF000000));
let glyph = CustomPaint::new()
.painter(CheckboxPainter { checked, color })
.size(Size::new(size, size));
let line = TextSpan::new().children(vec![
WidgetSpan::new(glyph)
.alignment(PlaceholderAlignment::Middle)
.into_span(),
]);
Text::rich(line.style(style).into_span()).into_widget()
}
pub fn code_block(
cx: &mut RenderContext,
app: &mut App,
language: Option<&str>,
text: &str,
) -> WidgetRef {
let block = &cx.style_sheet.code_block;
let code = match &cx.hooks.highlight_code {
Some(highlight) => {
TextSpan::new().children(vec![highlight(app, language, text).into_span()])
}
None => TextSpan::new().text(text),
};
let style = cx.text.merge(Some(&block.text));
Container::new()
.clip_behavior(Clip::HardEdge)
.decoration(block.decoration.clone())
.child(SidewaysScroll {
padding: block.padding,
child: Text::rich(code.style(style).into_span()).into_widget(),
})
.into_widget()
}
pub fn table(
cx: &mut RenderContext,
app: &mut App,
alignments: &[Alignment],
head: &[Cell],
rows: &[Vec<Cell>],
) -> WidgetRef {
let table = &cx.style_sheet.table;
let head_style = cx.text.merge(Some(&table.head));
let body_style = cx.text.merge(Some(&table.body));
let mut table_rows = Vec::with_capacity(rows.len() + 1);
table_rows.push(TableRow::new().children(table_cells(
cx,
app,
alignments,
head,
&head_style,
table.head_align,
)));
for (index, row) in rows.iter().enumerate() {
let mut table_row = TableRow::new().children(table_cells(
cx,
app,
alignments,
row,
&body_style,
TextAlign::Start,
));
if index % 2 == 1
&& let Some(stripe) = &table.stripe
{
table_row = table_row.decoration(stripe.clone());
}
table_rows.push(table_row);
}
let widget = Table::new()
.default_column_width(Rc::clone(&table.column_width))
.default_vertical_alignment(table.vertical_alignment)
.border(table.border)
.children(table_rows)
.into_widget();
let column_width = table.column_width.as_any();
if column_width.is::<IntrinsicColumnWidth>() || column_width.is::<FixedColumnWidth>() {
return SidewaysScroll {
padding: EdgeInsets::ZERO,
child: widget,
}
.into_widget();
}
widget
}
pub fn rule(cx: &RenderContext) -> WidgetRef {
Container::new()
.decoration(cx.style_sheet.rule.clone())
.into_widget()
}
pub fn spans(
cx: &mut RenderContext,
app: &mut App,
content: &[Inline],
style: &TextStyle,
) -> Vec<InlineSpanRef> {
let mut spans = Vec::new();
push_spans(cx, app, content, style, None, &mut spans);
spans
}
struct LinkTap {
recognizer: RecognizerRef,
on_tap: Listener,
}
fn push_spans(
cx: &mut RenderContext,
app: &mut App,
content: &[Inline],
style: &TextStyle,
link: Option<&LinkTap>,
spans: &mut Vec<InlineSpanRef>,
) {
let (sheet, hooks) = (cx.style_sheet, cx.hooks);
for inline in content {
match inline {
Inline::Text(text) => spans.push(text_span(text, style, link)),
Inline::Emphasis(content) => {
let style = MarkdownStyleSheet::emphasis(style);
push_spans(cx, app, content, &style, link, spans);
}
Inline::Strong(content) => {
let style = MarkdownStyleSheet::strong(style);
push_spans(cx, app, content, &style, link, spans);
}
Inline::Strikethrough(content) => {
let style = MarkdownStyleSheet::strikethrough(style);
push_spans(cx, app, content, &style, link, spans);
}
Inline::Code(code) => {
spans.push(text_span(
code,
&style.merge(Some(&sheet.inline_code)),
link,
));
}
Inline::Link {
url,
title,
content,
} => {
if content.is_empty() {
continue;
}
let style = style.merge(Some(&sheet.link));
let tap = cx.link_tap(app, url, title);
push_spans(cx, app, content, &style, tap.as_ref(), spans);
}
Inline::Image(image) => match &hooks.image {
Some(hook) => {
let mut child = hook(app, image);
if let Some(link) = link {
child = GestureDetector::new()
.on_tap(link.on_tap.clone())
.child(child)
.into_widget();
}
spans.push(WidgetSpan::new(child).into_span());
}
None if !image.alt.is_empty() => spans.push(text_span(&image.alt, style, link)),
None => {}
},
Inline::LineBreak => spans.push(text_span("\n", style, link)),
}
}
}
fn text_span(text: &str, style: &TextStyle, link: Option<&LinkTap>) -> InlineSpanRef {
let mut span = TextSpan::new().text(text).style(style.clone());
if let Some(link) = link {
span = span.recognizer(link.recognizer.clone());
}
span.into_span()
}
fn rich_text(spans: Vec<InlineSpanRef>, style: TextStyle, text_align: TextAlign) -> WidgetRef {
Text::rich(TextSpan::new().style(style).children(spans).into_span())
.text_align(text_align)
.into_widget()
}
fn table_cells(
cx: &mut RenderContext,
app: &mut App,
alignments: &[Alignment],
cells: &[Cell],
style: &TextStyle,
unaligned: TextAlign,
) -> Vec<WidgetRef> {
let padding = cx.style_sheet.table.cell_padding;
alignments
.iter()
.enumerate()
.map(|(column, alignment)| {
let text_align = match alignment {
Alignment::None => unaligned,
Alignment::Left => TextAlign::Left,
Alignment::Center => TextAlign::Center,
Alignment::Right => TextAlign::Right,
};
let content = cells.get(column).map_or(&[][..], |cell| &cell.content);
let text = rich_text(spans(cx, app, content, style), style.clone(), text_align);
TableCell::new(Padding::new(padding).child(text)).into_widget()
})
.collect()
}
fn list_item(cx: &RenderContext, marker: WidgetRef, content: WidgetRef) -> WidgetRef {
let list = &cx.style_sheet.list;
let padding = list.bullet_padding;
Row::new()
.main_axis_size(if cx.fit_content {
MainAxisSize::Min
} else {
MainAxisSize::Max
})
.cross_axis_alignment(CrossAxisAlignment::Baseline)
.text_baseline(TextBaseline::Alphabetic)
.children([
SizedBox::new()
.width(list.indent + padding.left + padding.right)
.child(Padding::new(padding).child(marker))
.into_widget(),
Flexible::new(content)
.fit(if cx.fit_content {
FlexFit::Loose
} else {
FlexFit::Tight
})
.into_widget(),
])
.into_widget()
}
pub(crate) fn column(children: Vec<WidgetRef>, fit_content: bool) -> WidgetRef {
Column::new()
.main_axis_size(MainAxisSize::Min)
.cross_axis_alignment(if fit_content {
CrossAxisAlignment::Start
} else {
CrossAxisAlignment::Stretch
})
.children(children)
.into_widget()
}
fn spaced(children: impl IntoIterator<Item = WidgetRef>, spacing: f64) -> Vec<WidgetRef> {
let mut spaced = Vec::new();
for child in children {
if !spaced.is_empty() {
spaced.push(SizedBox::new().height(spacing).into_widget());
}
spaced.push(child);
}
spaced
}
fn padded(padding: EdgeInsets, child: WidgetRef) -> WidgetRef {
if padding == EdgeInsets::ZERO {
return child;
}
Padding::new(padding).child(child).into_widget()
}
#[derive(PartialEq)]
struct CheckboxPainter {
checked: bool,
color: Color,
}
impl CustomPainter for CheckboxPainter {
fn paint(&self, _app: &mut App, canvas: &mut Canvas, size: Size) {
let unit = size.width().min(size.height()) / 24.0;
let square = |inset: f64, radius: f64| {
RRect::from_ltrbxy(
inset * unit,
inset * unit,
(24.0 - inset) * unit,
(24.0 - inset) * unit,
radius * unit,
radius * unit,
)
};
let paint = Paint::from_color(self.color.into());
let outer = square(3.0, 2.0);
if !self.checked {
draw_drrect(canvas, outer, square(5.0, 0.0), &paint);
return;
}
let mut path = PathBuilder::new();
path.rrect_radii_elliptical(outer.outer_rect(), rrect_radii_elliptical(outer));
let point = |x: f64, y: f64| Point::new((x * unit) as f32, (y * unit) as f32);
path.move_to(point(10.0, 17.0));
for (x, y) in [
(5.0, 12.0),
(6.41, 10.59),
(10.0, 14.17),
(17.59, 6.58),
(19.0, 8.0),
] {
path.line_to(point(x, y));
}
path.close();
canvas.draw_path(&path.build(), FillRule::EvenOdd, &paint);
}
fn should_repaint(&self, _app: &App, old_delegate: &dyn CustomPainter) -> bool {
old_delegate.as_any().downcast_ref::<CheckboxPainter>() != Some(self)
}
fn as_any(&self) -> &dyn Any {
self
}
}
struct SidewaysScroll {
padding: EdgeInsets,
child: WidgetRef,
}
impl fmt::Debug for SidewaysScroll {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SidewaysScroll")
.field("child", &self.child)
.finish_non_exhaustive()
}
}
impl StatefulWidget for SidewaysScroll {
type State = SidewaysScrollState;
fn create_state(&self) -> SidewaysScrollState {
SidewaysScrollState {
state: StateData::new(),
controller: None,
}
}
}
struct SidewaysScrollState {
state: StateData<SidewaysScroll>,
controller: Option<Ref<ScrollController>>,
}
impl State for SidewaysScrollState {
type Widget = SidewaysScroll;
waft_widgets::state_accessors!();
fn init_state(self: Handle<Self>, app: &mut App) {
let controller = ScrollController::default(app);
app.get_mut(self).controller = Some(controller);
}
fn dispose(self: Handle<Self>, app: &mut App) {
if let Some(controller) = app.get_mut(self).controller.take() {
controller.dispose(app);
}
}
fn build(self: Handle<Self>, app: &mut App, _context: BuildContext) -> WidgetRef {
let controller: waft_widgets::ScrollControllerRef = app
.get(self)
.controller
.as_ref()
.expect("made in init_state")
.erase();
let widget = self.widget(app);
let scroll_view = SingleChildScrollView::new()
.controller(controller.clone())
.scroll_direction(Axis::Horizontal)
.padding(widget.padding)
.child(widget.child.clone());
RawScrollbar::new(scroll_view)
.controller(controller)
.into_widget()
}
}