From ddcf02f9d0377afe6a35dbbb09a29b4bd52efe2e Mon Sep 17 00:00:00 2001 From: Cory Forsstrom Date: Tue, 23 Jul 2024 13:36:40 -0700 Subject: Add background styling to span / rich text --- core/src/renderer/null.rs | 4 ++++ core/src/text.rs | 39 ++++++++++++++++++++++++++++++++++++++- core/src/text/paragraph.rs | 6 +++++- 3 files changed, 47 insertions(+), 2 deletions(-) (limited to 'core/src') diff --git a/core/src/renderer/null.rs b/core/src/renderer/null.rs index d8d3c50a..5c7513c6 100644 --- a/core/src/renderer/null.rs +++ b/core/src/renderer/null.rs @@ -111,6 +111,10 @@ impl text::Paragraph for () { fn hit_span(&self, _point: Point) -> Option { None } + + fn span_bounds(&self, _index: usize) -> Vec { + vec![] + } } impl text::Editor for () { diff --git a/core/src/text.rs b/core/src/text.rs index aa24d85f..0bdc6851 100644 --- a/core/src/text.rs +++ b/core/src/text.rs @@ -8,7 +8,7 @@ pub use highlighter::Highlighter; pub use paragraph::Paragraph; use crate::alignment; -use crate::{Color, Pixels, Point, Rectangle, Size}; +use crate::{Border, Color, Pixels, Point, Rectangle, Size}; use std::borrow::Cow; use std::hash::{Hash, Hasher}; @@ -235,6 +235,8 @@ pub struct Span<'a, Link = (), Font = crate::Font> { pub font: Option, /// The [`Color`] of the [`Span`]. pub color: Option, + /// The [`Background`] of the [`Span`]. + pub background: Option, /// The link of the [`Span`]. pub link: Option, } @@ -248,6 +250,7 @@ impl<'a, Link, Font> Span<'a, Link, Font> { line_height: None, font: None, color: None, + background: None, link: None, } } @@ -288,6 +291,21 @@ impl<'a, Link, Font> Span<'a, Link, Font> { self } + /// Sets the [`Background`] of the [`Span`]. + pub fn background(mut self, background: impl Into) -> Self { + self.background = Some(background.into()); + self + } + + /// Sets the [`Background`] of the [`Span`], if any. + pub fn background_maybe( + mut self, + background: Option>, + ) -> Self { + self.background = background.map(Into::into); + self + } + /// Sets the link of the [`Span`]. pub fn link(mut self, link: impl Into) -> Self { self.link = Some(link.into()); @@ -308,6 +326,7 @@ impl<'a, Link, Font> Span<'a, Link, Font> { line_height: self.line_height, font: self.font, color: self.color, + background: self.background, link: self.link, } } @@ -406,3 +425,21 @@ into_fragment!(isize); into_fragment!(f32); into_fragment!(f64); + +/// The background style of text +#[derive(Debug, Clone, Copy)] +pub struct Background { + /// The background [`Color`] + pub color: Color, + /// The background [`Border`] + pub border: Border, +} + +impl From for Background { + fn from(color: Color) -> Self { + Background { + color, + border: Border::default(), + } + } +} diff --git a/core/src/text/paragraph.rs b/core/src/text/paragraph.rs index 26650793..04a97f35 100644 --- a/core/src/text/paragraph.rs +++ b/core/src/text/paragraph.rs @@ -1,7 +1,7 @@ //! Draw paragraphs. use crate::alignment; use crate::text::{Difference, Hit, Span, Text}; -use crate::{Point, Size}; +use crate::{Point, Rectangle, Size}; /// A text paragraph. pub trait Paragraph: Sized + Default { @@ -42,6 +42,10 @@ pub trait Paragraph: Sized + Default { /// that was hit. fn hit_span(&self, point: Point) -> Option; + /// Returns all bounds for the provided [`Span`] index of the [`Paragraph`]. + /// A [`Span`] can have multiple bounds for each line it's on. + fn span_bounds(&self, index: usize) -> Vec; + /// Returns the distance to the given grapheme index in the [`Paragraph`]. fn grapheme_position(&self, line: usize, index: usize) -> Option; -- cgit