diff options
author | 2024-07-28 17:22:32 +0200 | |
---|---|---|
committer | 2024-07-28 17:22:32 +0200 | |
commit | ebc6c0eba86608dbd9912ab180f48ca603f20c19 (patch) | |
tree | 8a3482278ee2ad2257c024f5b579925d279e3bf4 /widget/src/text/rich.rs | |
parent | c47a6ed7b639cf76086554fe2b65a8acecb61ea2 (diff) | |
parent | 9ce55eb51113b79d57b981ccd971242528a36395 (diff) | |
download | iced-ebc6c0eba86608dbd9912ab180f48ca603f20c19.tar.gz iced-ebc6c0eba86608dbd9912ab180f48ca603f20c19.tar.bz2 iced-ebc6c0eba86608dbd9912ab180f48ca603f20c19.zip |
Merge pull request #2526 from iced-rs/feature/rich-text-underline
Underline support for `rich_text`
Diffstat (limited to 'widget/src/text/rich.rs')
-rw-r--r-- | widget/src/text/rich.rs | 90 |
1 files changed, 69 insertions, 21 deletions
diff --git a/widget/src/text/rich.rs b/widget/src/text/rich.rs index 9935e6c5..096056d4 100644 --- a/widget/src/text/rich.rs +++ b/widget/src/text/rich.rs @@ -237,7 +237,7 @@ where theme: &Theme, defaults: &renderer::Style, layout: Layout<'_>, - _cursor_position: mouse::Cursor, + cursor: mouse::Cursor, viewport: &Rectangle, ) { let state = tree @@ -246,29 +246,77 @@ where let style = theme.style(&self.class); + let hovered_span = cursor + .position_in(layout.bounds()) + .and_then(|position| state.paragraph.hit_span(position)); + for (index, span) in self.spans.iter().enumerate() { - if let Some(highlight) = span.highlight { + let is_hovered_link = + span.link.is_some() && Some(index) == hovered_span; + + if span.highlight.is_some() || span.underline || is_hovered_link { let translation = layout.position() - Point::ORIGIN; + let regions = state.paragraph.span_bounds(index); + + if let Some(highlight) = span.highlight { + for bounds in ®ions { + let bounds = Rectangle::new( + bounds.position() + - Vector::new( + span.padding.left, + span.padding.top, + ), + bounds.size() + + Size::new( + span.padding.horizontal(), + span.padding.vertical(), + ), + ); + + renderer.fill_quad( + renderer::Quad { + bounds: bounds + translation, + border: highlight.border, + ..Default::default() + }, + highlight.background, + ); + } + } - for bounds in state.paragraph.span_bounds(index) { - let bounds = Rectangle::new( - bounds.position() - - Vector::new(span.padding.left, span.padding.top), - bounds.size() - + Size::new( - span.padding.horizontal(), - span.padding.vertical(), - ), - ); - - renderer.fill_quad( - renderer::Quad { - bounds: bounds + translation, - border: highlight.border, - ..Default::default() - }, - highlight.background, - ); + if span.underline || is_hovered_link { + let size = span + .size + .or(self.size) + .unwrap_or(renderer.default_size()); + + let line_height = span + .line_height + .unwrap_or(self.line_height) + .to_absolute(size); + + for bounds in regions { + renderer.fill_quad( + renderer::Quad { + bounds: Rectangle::new( + bounds.position() + + translation + + Vector::new( + 0.0, + size.0 + + (line_height.0 - size.0) + / 2.0 + - size.0 * 0.08, + ), + Size::new(bounds.width, 1.0), + ), + ..Default::default() + }, + span.color + .or(style.color) + .unwrap_or(defaults.text_color), + ); + } } } } |