From ca31dcadd52b3be05bcf01aa0426bf4279ac5f13 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 28 Jul 2024 15:10:33 +0200 Subject: Underline `rich_text` links when hovered --- widget/src/markdown.rs | 4 +--- widget/src/text/rich.rs | 13 ++++++++++--- 2 files changed, 11 insertions(+), 6 deletions(-) (limited to 'widget/src') diff --git a/widget/src/markdown.rs b/widget/src/markdown.rs index dbdb6e42..9cd4a62f 100644 --- a/widget/src/markdown.rs +++ b/widget/src/markdown.rs @@ -248,9 +248,7 @@ pub fn parse( }; let span = if let Some(link) = link.as_ref() { - span.color(palette.primary) - .link(link.clone()) - .underline(true) + span.color(palette.primary).link(link.clone()) } else { span }; diff --git a/widget/src/text/rich.rs b/widget/src/text/rich.rs index 8e4b0b7e..d179c2d6 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: mouse::Cursor, + cursor: mouse::Cursor, viewport: &Rectangle, ) { let state = tree @@ -246,8 +246,15 @@ 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 span.highlight.is_some() || span.underline { + 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); @@ -277,7 +284,7 @@ where } } - if span.underline { + if span.underline || is_hovered_link { let line_height = span .line_height .unwrap_or(self.line_height) -- cgit