summaryrefslogtreecommitdiffstats
path: root/graphics/src
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón <hector@hecrj.dev>2024-07-21 17:18:53 +0200
committerLibravatar GitHub <noreply@github.com>2024-07-21 17:18:53 +0200
commit5443e4d8289873895587d856dbcf46f980bda6ab (patch)
tree4f0037c07349ba0f1ea3e3ace596d988d9c3d952 /graphics/src
parent4b44079f34aa9e01977a7974e5f49ae79ff6cd90 (diff)
parenta2943798a3cf79e15344063fbf4ea8c84d261d6f (diff)
downloadiced-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 'graphics/src')
-rw-r--r--graphics/src/text/paragraph.rs50
1 files changed, 35 insertions, 15 deletions
diff --git a/graphics/src/text/paragraph.rs b/graphics/src/text/paragraph.rs
index 37fa97f2..da703ceb 100644
--- a/graphics/src/text/paragraph.rs
+++ b/graphics/src/text/paragraph.rs
@@ -100,8 +100,8 @@ impl core::text::Paragraph for Paragraph {
}))
}
- fn with_spans(text: Text<&[Span<'_>]>) -> Self {
- log::trace!("Allocating rich paragraph: {:?}", text.content);
+ fn with_spans<Link>(text: Text<&[Span<'_, Link>]>) -> Self {
+ log::trace!("Allocating rich paragraph: {} spans", text.content.len());
let mut font_system =
text::font_system().write().expect("Write font system");
@@ -122,18 +122,8 @@ impl core::text::Paragraph for Paragraph {
buffer.set_rich_text(
font_system.raw(),
- text.content.iter().map(|span| {
- let attrs = cosmic_text::Attrs::new();
-
- let attrs = if let Some(font) = span.font {
- attrs
- .family(text::to_family(font.family))
- .weight(text::to_weight(font.weight))
- .stretch(text::to_stretch(font.stretch))
- .style(text::to_style(font.style))
- } else {
- text::to_attributes(text.font)
- };
+ text.content.iter().enumerate().map(|(i, span)| {
+ let attrs = text::to_attributes(span.font.unwrap_or(text.font));
let attrs = match (span.size, span.line_height) {
(None, None) => attrs,
@@ -156,7 +146,7 @@ impl core::text::Paragraph for Paragraph {
attrs
};
- (span.text.as_ref(), attrs)
+ (span.text.as_ref(), attrs.metadata(i))
}),
text::to_attributes(text.font),
text::to_shaping(text.shaping),
@@ -231,6 +221,36 @@ impl core::text::Paragraph for Paragraph {
Some(Hit::CharOffset(cursor.index))
}
+ fn hit_span(&self, point: Point) -> Option<usize> {
+ let internal = self.internal();
+
+ let cursor = internal.buffer.hit(point.x, point.y)?;
+ let line = internal.buffer.lines.get(cursor.line)?;
+
+ let mut last_glyph = None;
+ let mut glyphs = line
+ .layout_opt()
+ .as_ref()?
+ .iter()
+ .flat_map(|line| line.glyphs.iter())
+ .peekable();
+
+ while let Some(glyph) = glyphs.peek() {
+ if glyph.start <= cursor.index && cursor.index < glyph.end {
+ break;
+ }
+
+ last_glyph = glyphs.next();
+ }
+
+ let glyph = match cursor.affinity {
+ cosmic_text::Affinity::Before => last_glyph,
+ cosmic_text::Affinity::After => glyphs.next(),
+ }?;
+
+ Some(glyph.metadata)
+ }
+
fn grapheme_position(&self, line: usize, index: usize) -> Option<Point> {
use unicode_segmentation::UnicodeSegmentation;