Skip to content

Commit

Permalink
Make RichText generic over data structure
Browse files Browse the repository at this point in the history
... and decouple `markdown::parse` from theming
  • Loading branch information
hecrj committed Aug 22, 2024
1 parent 55764b9 commit 4c883f1
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 94 deletions.
2 changes: 1 addition & 1 deletion core/src/padding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use crate::{Pixels, Size};
/// let widget = Widget::new().padding(20); // 20px on all sides
/// let widget = Widget::new().padding([10, 20]); // top/bottom, left/right
/// ```
#[derive(Debug, Copy, Clone, Default)]
#[derive(Debug, Copy, Clone, PartialEq, Default)]
pub struct Padding {
/// Top padding
pub top: f32,
Expand Down
2 changes: 1 addition & 1 deletion core/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ pub struct Span<'a, Link = (), Font = crate::Font> {
}

/// A text highlight.
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Highlight {
/// The [`Background`] of the highlight.
pub background: Background,
Expand Down
18 changes: 9 additions & 9 deletions examples/markdown/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ impl Markdown {
(
Self {
content: text_editor::Content::with_text(INITIAL_CONTENT),
items: markdown::parse(INITIAL_CONTENT, theme.palette())
.collect(),
items: markdown::parse(INITIAL_CONTENT).collect(),
theme,
},
widget::focus_next(),
Expand All @@ -45,11 +44,8 @@ impl Markdown {
self.content.perform(action);

if is_edit {
self.items = markdown::parse(
&self.content.text(),
self.theme.palette(),
)
.collect();
self.items =
markdown::parse(&self.content.text()).collect();
}
}
Message::LinkClicked(link) => {
Expand All @@ -67,8 +63,12 @@ impl Markdown {
.font(Font::MONOSPACE)
.highlight("markdown", highlighter::Theme::Base16Ocean);

let preview = markdown(&self.items, markdown::Settings::default())
.map(Message::LinkClicked);
let preview = markdown(
&self.items,
markdown::Settings::default(),
markdown::Style::from_palette(self.theme.palette()),
)
.map(Message::LinkClicked);

row![editor, scrollable(preview).spacing(10).height(Fill)]
.spacing(10)
Expand Down
1 change: 0 additions & 1 deletion widget/src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ where
}

/// Sets the style class of the [`Container`].
#[cfg(feature = "advanced")]
#[must_use]
pub fn class(mut self, class: impl Into<Theme::Class<'a>>) -> Self {
self.class = class.into();
Expand Down
5 changes: 3 additions & 2 deletions widget/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::tooltip::{self, Tooltip};
use crate::vertical_slider::{self, VerticalSlider};
use crate::{Column, MouseArea, Row, Space, Stack, Themer};

use std::borrow::{Borrow, Cow};
use std::borrow::Borrow;
use std::ops::RangeInclusive;

/// Creates a [`Column`] with the given children.
Expand Down Expand Up @@ -707,12 +707,13 @@ where
///
/// [`Rich`]: text::Rich
pub fn rich_text<'a, Link, Theme, Renderer>(
spans: impl Into<Cow<'a, [text::Span<'a, Link, Renderer::Font>]>>,
spans: impl AsRef<[text::Span<'a, Link, Renderer::Font>]> + 'a,
) -> text::Rich<'a, Link, Theme, Renderer>
where
Link: Clone + 'static,
Theme: text::Catalog + 'a,
Renderer: core::text::Renderer,
Renderer::Font: 'a,
{
text::Rich::with_spans(spans)
}
Expand Down
Loading

0 comments on commit 4c883f1

Please sign in to comment.