From 565599876172b3f56d86b119ae453b5bcd8949e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Tue, 4 Feb 2025 07:53:56 +0100 Subject: [PATCH] Draft `Viewer` trait for `markdown` --- Cargo.lock | 3 + core/src/padding.rs | 6 + examples/changelog/src/main.rs | 26 +- examples/markdown/Cargo.toml | 8 +- examples/markdown/src/main.rs | 139 ++++++++- widget/src/helpers.rs | 6 +- widget/src/markdown.rs | 553 +++++++++++++++++++++------------ widget/src/pop.rs | 24 +- widget/src/text/rich.rs | 57 +++- 9 files changed, 588 insertions(+), 234 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c9c7ddc5f3..4175a1880a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3299,7 +3299,10 @@ name = "markdown" version = "0.1.0" dependencies = [ "iced", + "image", "open", + "reqwest", + "tokio", ] [[package]] diff --git a/core/src/padding.rs b/core/src/padding.rs index e26cdd9bde..9ec02e6d93 100644 --- a/core/src/padding.rs +++ b/core/src/padding.rs @@ -202,3 +202,9 @@ impl From for Size { Self::new(padding.horizontal(), padding.vertical()) } } + +impl From for Padding { + fn from(pixels: Pixels) -> Self { + Self::from(pixels.0) + } +} diff --git a/examples/changelog/src/main.rs b/examples/changelog/src/main.rs index f889e757eb..a6528ce937 100644 --- a/examples/changelog/src/main.rs +++ b/examples/changelog/src/main.rs @@ -267,25 +267,21 @@ impl Generator { } => { let details = { let title = rich_text![ - span(&pull_request.title).size(24).link( - Message::OpenPullRequest(pull_request.id) - ), + span(&pull_request.title) + .size(24) + .link(pull_request.id), span(format!(" by {}", pull_request.author)) .font(Font { style: font::Style::Italic, ..Font::default() }), ] + .on_link_clicked(Message::OpenPullRequest) .font(Font::MONOSPACE); - let description = markdown::view( - description, - markdown::Settings::default(), - markdown::Style::from_palette( - self.theme().palette(), - ), - ) - .map(Message::UrlClicked); + let description = + markdown::view(&self.theme(), description) + .map(Message::UrlClicked); let labels = row(pull_request.labels.iter().map(|label| { @@ -349,11 +345,11 @@ impl Generator { container( scrollable( markdown::view( - preview, - markdown::Settings::with_text_size(12), - markdown::Style::from_palette( - self.theme().palette(), + markdown::Settings::with_text_size( + 12, + &self.theme(), ), + preview, ) .map(Message::UrlClicked), ) diff --git a/examples/markdown/Cargo.toml b/examples/markdown/Cargo.toml index fa6ced741e..4711b1c483 100644 --- a/examples/markdown/Cargo.toml +++ b/examples/markdown/Cargo.toml @@ -7,6 +7,12 @@ publish = false [dependencies] iced.workspace = true -iced.features = ["markdown", "highlighter", "tokio", "debug"] +iced.features = ["markdown", "highlighter", "image", "tokio", "debug"] + +reqwest.version = "0.12" +reqwest.features = ["json"] + +image.workspace = true +tokio.workspace = true open = "5.3" diff --git a/examples/markdown/src/main.rs b/examples/markdown/src/main.rs index ba93ee1885..29625d794e 100644 --- a/examples/markdown/src/main.rs +++ b/examples/markdown/src/main.rs @@ -1,10 +1,17 @@ use iced::highlighter; use iced::time::{self, milliseconds}; use iced::widget::{ - self, hover, markdown, right, row, scrollable, text_editor, toggler, + self, center_x, horizontal_space, hover, image, markdown, pop, right, row, + scrollable, text_editor, toggler, }; use iced::{Element, Fill, Font, Subscription, Task, Theme}; +use tokio::task; + +use std::collections::HashMap; +use std::io; +use std::sync::Arc; + pub fn main() -> iced::Result { iced::application("Markdown - Iced", Markdown::update, Markdown::view) .subscription(Markdown::subscription) @@ -14,6 +21,7 @@ pub fn main() -> iced::Result { struct Markdown { content: text_editor::Content, + images: HashMap, mode: Mode, theme: Theme, } @@ -26,10 +34,19 @@ enum Mode { }, } +enum Image { + Loading, + Ready(image::Handle), + #[allow(dead_code)] + Errored(Error), +} + #[derive(Debug, Clone)] enum Message { Edit(text_editor::Action), LinkClicked(markdown::Url), + ImageShown(markdown::Url), + ImageDownloaded(markdown::Url, Result), ToggleStream(bool), NextToken, } @@ -43,6 +60,7 @@ impl Markdown { ( Self { content: text_editor::Content::with_text(INITIAL_CONTENT), + images: HashMap::new(), mode: Mode::Preview(markdown::parse(INITIAL_CONTENT).collect()), theme, }, @@ -70,6 +88,25 @@ impl Markdown { Task::none() } + Message::ImageShown(url) => { + if self.images.contains_key(&url) { + return Task::none(); + } + + let _ = self.images.insert(url.clone(), Image::Loading); + + Task::perform(download_image(url.clone()), move |result| { + Message::ImageDownloaded(url.clone(), result) + }) + } + Message::ImageDownloaded(url, result) => { + let _ = self.images.insert( + url, + result.map(Image::Ready).unwrap_or_else(Image::Errored), + ); + + Task::none() + } Message::ToggleStream(enable_stream) => { if enable_stream { self.mode = Mode::Stream { @@ -126,12 +163,13 @@ impl Markdown { Mode::Stream { parsed, .. } => parsed.items(), }; - let preview = markdown( + let preview = markdown::view_with( + &MarkdownViewer { + images: &self.images, + }, + &self.theme, items, - markdown::Settings::default(), - markdown::Style::from_palette(self.theme.palette()), - ) - .map(Message::LinkClicked); + ); row![ editor, @@ -167,3 +205,92 @@ impl Markdown { } } } + +struct MarkdownViewer<'a> { + images: &'a HashMap, +} + +impl<'a> markdown::Viewer<'a, Message> for MarkdownViewer<'a> { + fn on_link_clicked(url: markdown::Url) -> Message { + Message::LinkClicked(url) + } + + fn image( + &self, + _settings: markdown::Settings, + _title: &markdown::Text, + url: &'a markdown::Url, + ) -> Element<'a, Message> { + if let Some(Image::Ready(handle)) = self.images.get(url) { + center_x(image(handle)).into() + } else { + pop(horizontal_space().width(0)) + .key(url.as_str()) + .on_show(|_size| Message::ImageShown(url.clone())) + .into() + } + } +} + +async fn download_image(url: markdown::Url) -> Result { + use std::io; + use tokio::task; + + let client = reqwest::Client::new(); + + let bytes = client + .get(url) + .send() + .await? + .error_for_status()? + .bytes() + .await?; + + let image = task::spawn_blocking(move || { + Ok::<_, Error>( + ::image::ImageReader::new(io::Cursor::new(bytes)) + .with_guessed_format()? + .decode()? + .to_rgba8(), + ) + }) + .await??; + + Ok(image::Handle::from_rgba( + image.width(), + image.height(), + image.into_raw(), + )) +} + +#[derive(Debug, Clone)] +pub enum Error { + RequestFailed(Arc), + IOFailed(Arc), + JoinFailed(Arc), + ImageDecodingFailed(Arc<::image::ImageError>), +} + +impl From for Error { + fn from(error: reqwest::Error) -> Self { + Self::RequestFailed(Arc::new(error)) + } +} + +impl From for Error { + fn from(error: io::Error) -> Self { + Self::IOFailed(Arc::new(error)) + } +} + +impl From for Error { + fn from(error: task::JoinError) -> Self { + Self::JoinFailed(Arc::new(error)) + } +} + +impl From<::image::ImageError> for Error { + fn from(error: ::image::ImageError) -> Self { + Self::ImageDecodingFailed(Arc::new(error)) + } +} diff --git a/widget/src/helpers.rs b/widget/src/helpers.rs index 4cba197d06..2716d4c6a0 100644 --- a/widget/src/helpers.rs +++ b/widget/src/helpers.rs @@ -187,7 +187,7 @@ macro_rules! text { #[macro_export] macro_rules! rich_text { () => ( - $crate::Column::new() + $crate::text::Rich::new() ); ($($x:expr),+ $(,)?) => ( $crate::text::Rich::from_iter([$($crate::text::Span::from($x)),+]) @@ -1155,9 +1155,9 @@ where /// .into() /// } /// ``` -pub fn rich_text<'a, Link, Theme, Renderer>( +pub fn rich_text<'a, Link, Message, Theme, Renderer>( spans: impl AsRef<[text::Span<'a, Link, Renderer::Font>]> + 'a, -) -> text::Rich<'a, Link, Theme, Renderer> +) -> text::Rich<'a, Link, Message, Theme, Renderer> where Link: Clone + 'static, Theme: text::Catalog + 'a, diff --git a/widget/src/markdown.rs b/widget/src/markdown.rs index 628a10c6ee..d8d33763c2 100644 --- a/widget/src/markdown.rs +++ b/widget/src/markdown.rs @@ -47,6 +47,7 @@ //! } //! } //! ``` +#![allow(missing_docs)] use crate::core::border; use crate::core::font::{self, Font}; use crate::core::padding; @@ -144,6 +145,7 @@ impl Content { let mut state = State { leftover: String::new(), references: self.state.references.clone(), + images: HashSet::new(), highlighter: None, }; @@ -153,6 +155,7 @@ impl Content { self.items[*index] = item; } + self.state.images.extend(state.images.drain()); drop(state); } @@ -167,6 +170,11 @@ impl Content { pub fn items(&self) -> &[Item] { &self.items } + + /// Returns the URLs of the Markdown images present in the [`Content`]. + pub fn images(&self) -> impl Iterator { + self.state.images.iter() + } } /// A Markdown item. @@ -187,130 +195,13 @@ pub enum Item { /// The items of the list. items: Vec>, }, -} - -impl Item { - /// Displays a Markdown [`Item`] using the default, built-in look for its children. - pub fn view<'a, 'b, Theme, Renderer>( - &'b self, - settings: Settings, - style: Style, - index: usize, - ) -> Element<'a, Url, Theme, Renderer> - where - Theme: Catalog + 'a, - Renderer: core::text::Renderer + 'a, - { - self.view_with(index, settings, style, &DefaultView) - } - - /// Displays a Markdown [`Item`] using the given [`View`] for its children. - pub fn view_with<'a, 'b, Theme, Renderer>( - &'b self, - index: usize, - settings: Settings, - style: Style, - view: &dyn View<'a, 'b, Url, Theme, Renderer>, - ) -> Element<'a, Url, Theme, Renderer> - where - Theme: Catalog + 'a, - Renderer: core::text::Renderer + 'a, - { - let Settings { - text_size, - h1_size, - h2_size, - h3_size, - h4_size, - h5_size, - h6_size, - code_size, - spacing, - } = settings; - - match self { - Item::Heading(level, heading) => { - container(rich_text(heading.spans(style)).size(match level { - pulldown_cmark::HeadingLevel::H1 => h1_size, - pulldown_cmark::HeadingLevel::H2 => h2_size, - pulldown_cmark::HeadingLevel::H3 => h3_size, - pulldown_cmark::HeadingLevel::H4 => h4_size, - pulldown_cmark::HeadingLevel::H5 => h5_size, - pulldown_cmark::HeadingLevel::H6 => h6_size, - })) - .padding(padding::top(if index > 0 { - text_size / 2.0 - } else { - Pixels::ZERO - })) - .into() - } - Item::Paragraph(paragraph) => { - rich_text(paragraph.spans(style)).size(text_size).into() - } - Item::List { start: None, items } => { - column(items.iter().map(|items| { - row![ - text("•").size(text_size), - view_with( - items, - Settings { - spacing: settings.spacing * 0.6, - ..settings - }, - style, - view - ) - ] - .spacing(spacing) - .into() - })) - .spacing(spacing * 0.75) - .into() - } - Item::List { - start: Some(start), - items, - } => column(items.iter().enumerate().map(|(i, items)| { - row![ - text!("{}.", i as u64 + *start).size(text_size), - view_with( - items, - Settings { - spacing: settings.spacing * 0.6, - ..settings - }, - style, - view - ) - ] - .spacing(spacing) - .into() - })) - .spacing(spacing * 0.75) - .into(), - Item::CodeBlock(lines) => container( - scrollable( - container(column(lines.iter().map(|line| { - rich_text(line.spans(style)) - .font(Font::MONOSPACE) - .size(code_size) - .into() - }))) - .padding(spacing.0 / 2.0), - ) - .direction(scrollable::Direction::Horizontal( - scrollable::Scrollbar::default() - .width(spacing.0 / 2.0) - .scroller_width(spacing.0 / 2.0), - )), - ) - .width(Length::Fill) - .padding(spacing.0 / 2.0) - .class(Theme::code_block()) - .into(), - } - } + /// An image. + Image { + /// The destination URL of the image. + url: Url, + /// The title of the image. + title: Text, + }, } /// A bunch of parsed Markdown text. @@ -470,6 +361,7 @@ pub fn parse(markdown: &str) -> impl Iterator + '_ { struct State { leftover: String, references: HashMap, + images: HashSet, #[cfg(feature = "highlighter")] highlighter: Option, } @@ -560,6 +452,10 @@ fn parse_with<'a>( mut state: impl BorrowMut + 'a, markdown: &'a str, ) -> impl Iterator)> + 'a { + enum Scope { + List(List), + } + struct List { start: Option, items: Vec>, @@ -575,7 +471,8 @@ fn parse_with<'a>( let mut metadata = false; let mut table = false; let mut link = None; - let mut lists = Vec::new(); + let mut image = None; + let mut stack = Vec::new(); #[cfg(feature = "highlighter")] let mut highlighter = None; @@ -616,10 +513,18 @@ fn parse_with<'a>( } let produce = move |state: &mut State, - lists: &mut Vec, + stack: &mut Vec, item, source: Range| { - if lists.is_empty() { + if let Some(scope) = stack.last_mut() { + match scope { + Scope::List(list) => { + list.items.last_mut().expect("item context").push(item); + } + } + + None + } else { state.leftover = markdown[source.start..].to_owned(); Some(( @@ -627,16 +532,6 @@ fn parse_with<'a>( &markdown[source.start..source.end], broken_links.take(), )) - } else { - lists - .last_mut() - .expect("list context") - .items - .last_mut() - .expect("item context") - .push(item); - - None } }; @@ -673,31 +568,36 @@ fn parse_with<'a>( None } + pulldown_cmark::Tag::Image { dest_url, .. } + if !metadata && !table => + { + image = Url::parse(&dest_url).ok(); + None + } pulldown_cmark::Tag::List(first_item) if !metadata && !table => { let prev = if spans.is_empty() { None } else { produce( state.borrow_mut(), - &mut lists, + &mut stack, Item::Paragraph(Text::new(spans.drain(..).collect())), source, ) }; - lists.push(List { + stack.push(Scope::List(List { start: first_item, items: Vec::new(), - }); + })); prev } pulldown_cmark::Tag::Item => { - lists - .last_mut() - .expect("list context") - .items - .push(Vec::new()); + if let Some(Scope::List(list)) = stack.last_mut() { + list.items.push(Vec::new()); + } + None } pulldown_cmark::Tag::CodeBlock( @@ -726,7 +626,7 @@ fn parse_with<'a>( } else { produce( state.borrow_mut(), - &mut lists, + &mut stack, Item::Paragraph(Text::new(spans.drain(..).collect())), source, ) @@ -748,7 +648,7 @@ fn parse_with<'a>( pulldown_cmark::TagEnd::Heading(level) if !metadata && !table => { produce( state.borrow_mut(), - &mut lists, + &mut stack, Item::Heading(level, Text::new(spans.drain(..).collect())), source, ) @@ -770,12 +670,16 @@ fn parse_with<'a>( None } pulldown_cmark::TagEnd::Paragraph if !metadata && !table => { - produce( - state.borrow_mut(), - &mut lists, - Item::Paragraph(Text::new(spans.drain(..).collect())), - source, - ) + if spans.is_empty() { + None + } else { + produce( + state.borrow_mut(), + &mut stack, + Item::Paragraph(Text::new(spans.drain(..).collect())), + source, + ) + } } pulldown_cmark::TagEnd::Item if !metadata && !table => { if spans.is_empty() { @@ -783,18 +687,20 @@ fn parse_with<'a>( } else { produce( state.borrow_mut(), - &mut lists, + &mut stack, Item::Paragraph(Text::new(spans.drain(..).collect())), source, ) } } pulldown_cmark::TagEnd::List(_) if !metadata && !table => { - let list = lists.pop().expect("list context"); + let scope = stack.pop()?; + + let Scope::List(list) = scope; produce( state.borrow_mut(), - &mut lists, + &mut stack, Item::List { start: list.start, items: list.items, @@ -802,6 +708,15 @@ fn parse_with<'a>( source, ) } + pulldown_cmark::TagEnd::Image if !metadata && !table => { + let url = image.take()?; + let title = Text::new(spans.drain(..).collect()); + + let state = state.borrow_mut(); + let _ = state.images.insert(url.clone()); + + produce(state, &mut stack, Item::Image { url, title }, source) + } pulldown_cmark::TagEnd::CodeBlock if !metadata && !table => { #[cfg(feature = "highlighter")] { @@ -810,7 +725,7 @@ fn parse_with<'a>( produce( state.borrow_mut(), - &mut lists, + &mut stack, Item::CodeBlock(code.drain(..).collect()), source, ) @@ -910,15 +825,25 @@ pub struct Settings { pub code_size: Pixels, /// The spacing to be used between elements. pub spacing: Pixels, + /// The styling of the Markdown. + pub style: Style, } impl Settings { + /// Creates new [`Settings`] with default text size and the given [`Style`]. + pub fn with_style(style: impl Into