diff options
| author | 2024-07-28 14:40:58 +0200 | |
|---|---|---|
| committer | 2024-07-28 14:40:58 +0200 | |
| commit | c47a6ed7b639cf76086554fe2b65a8acecb61ea2 (patch) | |
| tree | abb30c0620ccab372ed6058f1fd5a2b04e1e4726 /core | |
| parent | 23a7e9f981728e8a95039db8eb8e9f3d8c6ba3d7 (diff) | |
| parent | 41a7318e5df3a49bf6e7fc2110155f2f22ff7e60 (diff) | |
| download | iced-c47a6ed7b639cf76086554fe2b65a8acecb61ea2.tar.gz iced-c47a6ed7b639cf76086554fe2b65a8acecb61ea2.tar.bz2 iced-c47a6ed7b639cf76086554fe2b65a8acecb61ea2.zip | |
Merge pull request #2516 from tarkah/feat/span-background
Add background styling to span / rich text
Diffstat (limited to 'core')
| -rw-r--r-- | core/src/renderer/null.rs | 4 | ||||
| -rw-r--r-- | core/src/size.rs | 14 | ||||
| -rw-r--r-- | core/src/text.rs | 90 | ||||
| -rw-r--r-- | core/src/text/paragraph.rs | 6 | 
4 files changed, 112 insertions, 2 deletions
| 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<usize> {          None      } + +    fn span_bounds(&self, _index: usize) -> Vec<Rectangle> { +        vec![] +    }  }  impl text::Editor for () { diff --git a/core/src/size.rs b/core/src/size.rs index d7459355..95089236 100644 --- a/core/src/size.rs +++ b/core/src/size.rs @@ -99,6 +99,20 @@ impl<T> From<Size<T>> for Vector<T> {      }  } +impl<T> std::ops::Add for Size<T> +where +    T: std::ops::Add<Output = T>, +{ +    type Output = Size<T>; + +    fn add(self, rhs: Self) -> Self::Output { +        Size { +            width: self.width + rhs.width, +            height: self.height + rhs.height, +        } +    } +} +  impl<T> std::ops::Sub for Size<T>  where      T: std::ops::Sub<Output = T>, diff --git a/core/src/text.rs b/core/src/text.rs index aa24d85f..2f085bd8 100644 --- a/core/src/text.rs +++ b/core/src/text.rs @@ -8,7 +8,9 @@ pub use highlighter::Highlighter;  pub use paragraph::Paragraph;  use crate::alignment; -use crate::{Color, Pixels, Point, Rectangle, Size}; +use crate::{ +    Background, Border, Color, Padding, Pixels, Point, Rectangle, Size, +};  use std::borrow::Cow;  use std::hash::{Hash, Hasher}; @@ -237,6 +239,21 @@ pub struct Span<'a, Link = (), Font = crate::Font> {      pub color: Option<Color>,      /// The link of the [`Span`].      pub link: Option<Link>, +    /// The [`Highlight`] of the [`Span`]. +    pub highlight: Option<Highlight>, +    /// The [`Padding`] of the [`Span`]. +    /// +    /// Currently, it only affects the bounds of the [`Highlight`]. +    pub padding: Padding, +} + +/// A text highlight. +#[derive(Debug, Clone, Copy)] +pub struct Highlight { +    /// The [`Background`] of the highlight. +    pub background: Background, +    /// The [`Border`] of the highlight. +    pub border: Border,  }  impl<'a, Link, Font> Span<'a, Link, Font> { @@ -248,7 +265,9 @@ impl<'a, Link, Font> Span<'a, Link, Font> {              line_height: None,              font: None,              color: None, +            highlight: None,              link: None, +            padding: Padding::ZERO,          }      } @@ -300,6 +319,73 @@ impl<'a, Link, Font> Span<'a, Link, Font> {          self      } +    /// Sets the [`Background`] of the [`Span`]. +    pub fn background(self, background: impl Into<Background>) -> Self { +        self.background_maybe(Some(background)) +    } + +    /// Sets the [`Background`] of the [`Span`], if any. +    pub fn background_maybe( +        mut self, +        background: Option<impl Into<Background>>, +    ) -> Self { +        let Some(background) = background else { +            return self; +        }; + +        match &mut self.highlight { +            Some(highlight) => { +                highlight.background = background.into(); +            } +            None => { +                self.highlight = Some(Highlight { +                    background: background.into(), +                    border: Border::default(), +                }); +            } +        } + +        self +    } + +    /// Sets the [`Border`] of the [`Span`]. +    pub fn border(self, border: impl Into<Border>) -> Self { +        self.border_maybe(Some(border)) +    } + +    /// Sets the [`Border`] of the [`Span`], if any. +    pub fn border_maybe(mut self, border: Option<impl Into<Border>>) -> Self { +        let Some(border) = border else { +            return self; +        }; + +        match &mut self.highlight { +            Some(highlight) => { +                highlight.border = border.into(); +            } +            None => { +                self.highlight = Some(Highlight { +                    border: border.into(), +                    background: Background::Color(Color::TRANSPARENT), +                }); +            } +        } + +        self +    } + +    /// Sets the [`Padding`] of the [`Span`]. +    /// +    /// It only affects the [`background`] and [`border`] of the +    /// [`Span`], currently. +    /// +    /// [`background`]: Self::background +    /// [`border`]: Self::border +    pub fn padding(mut self, padding: impl Into<Padding>) -> Self { +        self.padding = padding.into(); +        self +    } +      /// Turns the [`Span`] into a static one.      pub fn to_static(self) -> Span<'static, Link, Font> {          Span { @@ -309,6 +395,8 @@ impl<'a, Link, Font> Span<'a, Link, Font> {              font: self.font,              color: self.color,              link: self.link, +            highlight: self.highlight, +            padding: self.padding,          }      }  } 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<usize>; +    /// 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<Rectangle>; +      /// Returns the distance to the given grapheme index in the [`Paragraph`].      fn grapheme_position(&self, line: usize, index: usize) -> Option<Point>; | 
