summaryrefslogtreecommitdiffstats
path: root/core/src/text.rs
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-07-21 12:45:05 +0200
committerLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-07-21 12:45:05 +0200
commit9bfaf2840cffe35d689bd115a308d21961ab082a (patch)
treec9a7b4fc04607c3315dabe3ad2527251b0a3dff8 /core/src/text.rs
parent4b44079f34aa9e01977a7974e5f49ae79ff6cd90 (diff)
downloadiced-9bfaf2840cffe35d689bd115a308d21961ab082a.tar.gz
iced-9bfaf2840cffe35d689bd115a308d21961ab082a.tar.bz2
iced-9bfaf2840cffe35d689bd115a308d21961ab082a.zip
Add `Link` support to `rich_text` widget
Diffstat (limited to 'core/src/text.rs')
-rw-r--r--core/src/text.rs34
1 files changed, 30 insertions, 4 deletions
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