Flesh out the markdown example a bit more

This commit is contained in:
Héctor Ramón Jiménez 2024-07-18 13:14:56 +02:00
parent 910eb72a06
commit 904704d7c1
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
10 changed files with 435 additions and 141 deletions

View file

@ -11,6 +11,8 @@ use crate::core::{
self, Color, Element, Length, Pixels, Rectangle, Size, Widget,
};
use std::borrow::Cow;
/// A bunch of [`Rich`] text.
#[derive(Debug)]
pub struct Rich<'a, Theme = crate::Theme, Renderer = crate::Renderer>
@ -18,7 +20,7 @@ where
Theme: Catalog,
Renderer: core::text::Renderer,
{
spans: Vec<Span<'a, Renderer::Font>>,
spans: Cow<'a, [Span<'a, Renderer::Font>]>,
size: Option<Pixels>,
line_height: LineHeight,
width: Length,
@ -37,7 +39,7 @@ where
/// Creates a new empty [`Rich`] text.
pub fn new() -> Self {
Self {
spans: Vec::new(),
spans: Cow::default(),
size: None,
line_height: LineHeight::default(),
width: Length::Shrink,
@ -51,10 +53,10 @@ where
/// Creates a new [`Rich`] text with the given text spans.
pub fn with_spans(
spans: impl IntoIterator<Item = Span<'a, Renderer::Font>>,
spans: impl Into<Cow<'a, [Span<'a, Renderer::Font>]>>,
) -> Self {
Self {
spans: spans.into_iter().collect(),
spans: spans.into(),
..Self::new()
}
}
@ -151,7 +153,7 @@ where
/// Adds a new text [`Span`] to the [`Rich`] text.
pub fn push(mut self, span: impl Into<Span<'a, Renderer::Font>>) -> Self {
self.spans.push(span.into());
self.spans.to_mut().push(span.into());
self
}
}
@ -207,7 +209,7 @@ where
limits,
self.width,
self.height,
self.spans.as_slice(),
self.spans.as_ref(),
self.line_height,
self.size,
self.font,
@ -303,6 +305,22 @@ where
})
}
impl<'a, Theme, Renderer> FromIterator<Span<'a, Renderer::Font>>
for Rich<'a, Theme, Renderer>
where
Theme: Catalog,
Renderer: core::text::Renderer,
{
fn from_iter<T: IntoIterator<Item = Span<'a, Renderer::Font>>>(
spans: T,
) -> Self {
Self {
spans: spans.into_iter().collect(),
..Self::new()
}
}
}
impl<'a, Message, Theme, Renderer> From<Rich<'a, Theme, Renderer>>
for Element<'a, Message, Theme, Renderer>
where