diff options
author | 2024-07-21 17:18:53 +0200 | |
---|---|---|
committer | 2024-07-21 17:18:53 +0200 | |
commit | 5443e4d8289873895587d856dbcf46f980bda6ab (patch) | |
tree | 4f0037c07349ba0f1ea3e3ace596d988d9c3d952 /core | |
parent | 4b44079f34aa9e01977a7974e5f49ae79ff6cd90 (diff) | |
parent | a2943798a3cf79e15344063fbf4ea8c84d261d6f (diff) | |
download | iced-5443e4d8289873895587d856dbcf46f980bda6ab.tar.gz iced-5443e4d8289873895587d856dbcf46f980bda6ab.tar.bz2 iced-5443e4d8289873895587d856dbcf46f980bda6ab.zip |
Merge pull request #2512 from iced-rs/feature/rich-text-links
Add `Link` support to `rich_text` widget
Diffstat (limited to 'core')
-rw-r--r-- | core/src/renderer/null.rs | 8 | ||||
-rw-r--r-- | core/src/text.rs | 34 | ||||
-rw-r--r-- | core/src/text/paragraph.rs | 9 |
3 files changed, 44 insertions, 7 deletions
diff --git a/core/src/renderer/null.rs b/core/src/renderer/null.rs index f9d1a5b0..7aa3aafb 100644 --- a/core/src/renderer/null.rs +++ b/core/src/renderer/null.rs @@ -77,8 +77,8 @@ impl text::Paragraph for () { fn with_text(_text: Text<&str>) -> Self {} - fn with_spans( - _text: Text<&[text::Span<'_, Self::Font>], Self::Font>, + fn with_spans<Link>( + _text: Text<&[text::Span<'_, Link, Self::Font>], Self::Font>, ) -> Self { } @@ -107,6 +107,10 @@ impl text::Paragraph for () { fn hit_test(&self, _point: Point) -> Option<text::Hit> { None } + + fn hit_span(&self, _point: Point) -> Option<usize> { + None + } } impl text::Editor for () { diff --git a/core/src/text.rs b/core/src/text.rs index 22cfce13..c22734f8 100644 --- a/core/src/text.rs +++ b/core/src/text.rs @@ -223,8 +223,8 @@ pub trait Renderer: crate::Renderer { } /// A span of text. -#[derive(Debug, Clone, PartialEq)] -pub struct Span<'a, Font = crate::Font> { +#[derive(Debug, Clone)] +pub struct Span<'a, Link = (), Font = crate::Font> { /// The [`Fragment`] of text. pub text: Fragment<'a>, /// The size of the [`Span`] in [`Pixels`]. @@ -235,9 +235,11 @@ pub struct Span<'a, Font = crate::Font> { pub font: Option<Font>, /// The [`Color`] of the [`Span`]. pub color: Option<Color>, + /// The link of the [`Span`]. + pub link: Option<Link>, } -impl<'a, Font> Span<'a, Font> { +impl<'a, Link, Font> Span<'a, Link, Font> { /// Creates a new [`Span`] of text with the given text fragment. pub fn new(fragment: impl IntoFragment<'a>) -> Self { Self { @@ -246,6 +248,7 @@ impl<'a, Font> Span<'a, Font> { line_height: None, font: None, color: None, + link: None, } } @@ -285,14 +288,27 @@ impl<'a, Font> Span<'a, Font> { self } + /// Sets the link of the [`Span`]. + pub fn link(mut self, link: impl Into<Link>) -> Self { + self.link = Some(link.into()); + self + } + + /// Sets the link of the [`Span`], if any. + pub fn link_maybe(mut self, link: Option<impl Into<Link>>) -> Self { + self.link = link.map(Into::into); + self + } + /// Turns the [`Span`] into a static one. - pub fn to_static(self) -> Span<'static, Font> { + pub fn to_static(self) -> Span<'static, Link, Font> { Span { text: Cow::Owned(self.text.into_owned()), size: self.size, line_height: self.line_height, font: self.font, color: self.color, + link: self.link, } } } @@ -303,6 +319,16 @@ impl<'a, Font> From<&'a str> for Span<'a, Font> { } } +impl<'a, Link, Font: PartialEq> PartialEq for Span<'a, Link, Font> { + fn eq(&self, other: &Self) -> bool { + self.text == other.text + && self.size == other.size + && self.line_height == other.line_height + && self.font == other.font + && self.color == other.color + } +} + /// A fragment of [`Text`]. /// /// This is just an alias to a string that may be either diff --git a/core/src/text/paragraph.rs b/core/src/text/paragraph.rs index 4ee83798..26650793 100644 --- a/core/src/text/paragraph.rs +++ b/core/src/text/paragraph.rs @@ -12,7 +12,9 @@ pub trait Paragraph: Sized + Default { fn with_text(text: Text<&str, Self::Font>) -> Self; /// Creates a new [`Paragraph`] laid out with the given [`Text`]. - fn with_spans(text: Text<&[Span<'_, Self::Font>], Self::Font>) -> Self; + fn with_spans<Link>( + text: Text<&[Span<'_, Link, Self::Font>], Self::Font>, + ) -> Self; /// Lays out the [`Paragraph`] with some new boundaries. fn resize(&mut self, new_bounds: Size); @@ -35,6 +37,11 @@ pub trait Paragraph: Sized + Default { /// [`Paragraph`], returning information about the nearest character. fn hit_test(&self, point: Point) -> Option<Hit>; + /// Tests whether the provided point is within the boundaries of a + /// [`Span`] in the [`Paragraph`], returning the index of the [`Span`] + /// that was hit. + fn hit_span(&self, point: Point) -> Option<usize>; + /// Returns the distance to the given grapheme index in the [`Paragraph`]. fn grapheme_position(&self, line: usize, index: usize) -> Option<Point>; |