From 2796a6bc974d847580ab23c6a5f58db994883ec5 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 28 Jul 2024 13:56:39 +0200 Subject: Add `padding` to `text::Span` --- core/src/size.rs | 14 ++++++++++++++ core/src/text.rs | 40 ++++++++++++++++++++++++++++++---------- widget/src/markdown.rs | 3 ++- widget/src/text/rich.rs | 12 +++++++++++- 4 files changed, 57 insertions(+), 12 deletions(-) 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 From> for Vector { } } +impl std::ops::Add for Size +where + T: std::ops::Add, +{ + type Output = Size; + + fn add(self, rhs: Self) -> Self::Output { + Size { + width: self.width + rhs.width, + height: self.height + rhs.height, + } + } +} + impl std::ops::Sub for Size where T: std::ops::Sub, diff --git a/core/src/text.rs b/core/src/text.rs index d54c8e6c..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::{Background, Border, Color, Pixels, Point, Rectangle, Size}; +use crate::{ + Background, Border, Color, Padding, Pixels, Point, Rectangle, Size, +}; use std::borrow::Cow; use std::hash::{Hash, Hasher}; @@ -239,6 +241,10 @@ pub struct Span<'a, Link = (), Font = crate::Font> { pub link: Option, /// The [`Highlight`] of the [`Span`]. pub highlight: Option, + /// The [`Padding`] of the [`Span`]. + /// + /// Currently, it only affects the bounds of the [`Highlight`]. + pub padding: Padding, } /// A text highlight. @@ -261,6 +267,7 @@ impl<'a, Link, Font> Span<'a, Link, Font> { color: None, highlight: None, link: None, + padding: Padding::ZERO, } } @@ -300,6 +307,18 @@ impl<'a, Link, Font> Span<'a, Link, Font> { self } + /// Sets the link of the [`Span`]. + pub fn link(mut self, link: impl Into) -> Self { + self.link = Some(link.into()); + self + } + + /// Sets the link of the [`Span`], if any. + pub fn link_maybe(mut self, link: Option>) -> Self { + self.link = link.map(Into::into); + self + } + /// Sets the [`Background`] of the [`Span`]. pub fn background(self, background: impl Into) -> Self { self.background_maybe(Some(background)) @@ -355,15 +374,15 @@ impl<'a, Link, Font> Span<'a, Link, Font> { self } - /// Sets the link of the [`Span`]. - pub fn link(mut self, link: impl Into) -> Self { - self.link = Some(link.into()); - self - } - - /// Sets the link of the [`Span`], if any. - pub fn link_maybe(mut self, link: Option>) -> Self { - self.link = link.map(Into::into); + /// 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) -> Self { + self.padding = padding.into(); self } @@ -377,6 +396,7 @@ impl<'a, Link, Font> Span<'a, Link, Font> { color: self.color, link: self.link, highlight: self.highlight, + padding: self.padding, } } } diff --git a/widget/src/markdown.rs b/widget/src/markdown.rs index cb3e9cfc..c5eeaea9 100644 --- a/widget/src/markdown.rs +++ b/widget/src/markdown.rs @@ -262,7 +262,8 @@ pub fn parse<'a>( .font(Font::MONOSPACE) .color(Color::WHITE) .background(color!(0x111111)) - .border(border::rounded(2)); + .border(border::rounded(2)) + .padding(padding::left(2).right(2)); let span = if let Some(link) = link.as_ref() { span.color(palette.primary).link(link.clone()) diff --git a/widget/src/text/rich.rs b/widget/src/text/rich.rs index f636b219..67db31db 100644 --- a/widget/src/text/rich.rs +++ b/widget/src/text/rich.rs @@ -10,7 +10,7 @@ use crate::core::widget::text::{ use crate::core::widget::tree::{self, Tree}; use crate::core::{ self, Clipboard, Color, Element, Event, Layout, Length, Pixels, Point, - Rectangle, Shell, Size, Widget, + Rectangle, Shell, Size, Vector, Widget, }; use std::borrow::Cow; @@ -252,6 +252,16 @@ where let translation = layout.position() - Point::ORIGIN; 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, -- cgit