Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Underline support for rich_text #2526

Merged
merged 3 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions core/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ pub struct Span<'a, Link = (), Font = crate::Font> {
///
/// Currently, it only affects the bounds of the [`Highlight`].
pub padding: Padding,
/// Whether the [`Span`] should be underlined or not.
pub underline: bool,
}

/// A text highlight.
Expand All @@ -268,6 +270,7 @@ impl<'a, Link, Font> Span<'a, Link, Font> {
highlight: None,
link: None,
padding: Padding::ZERO,
underline: false,
}
}

Expand Down Expand Up @@ -386,6 +389,12 @@ impl<'a, Link, Font> Span<'a, Link, Font> {
self
}

/// Sets whether the [`Span`] shoud be underlined or not.
pub fn underline(mut self, underline: bool) -> Self {
self.underline = underline;
self
}

/// Turns the [`Span`] into a static one.
pub fn to_static(self) -> Span<'static, Link, Font> {
Span {
Expand All @@ -397,6 +406,7 @@ impl<'a, Link, Font> Span<'a, Link, Font> {
link: self.link,
highlight: self.highlight,
padding: self.padding,
underline: self.underline,
}
}
}
Expand Down
90 changes: 69 additions & 21 deletions widget/src/text/rich.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ where
theme: &Theme,
defaults: &renderer::Style,
layout: Layout<'_>,
_cursor_position: mouse::Cursor,
cursor: mouse::Cursor,
viewport: &Rectangle,
) {
let state = tree
Expand All @@ -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 &regions {
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),
);
}
}
}
}
Expand Down
Loading