Add padding to text::Span
This commit is contained in:
parent
f7fe1edcbb
commit
2796a6bc97
4 changed files with 57 additions and 12 deletions
|
|
@ -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>
|
impl<T> std::ops::Sub for Size<T>
|
||||||
where
|
where
|
||||||
T: std::ops::Sub<Output = T>,
|
T: std::ops::Sub<Output = T>,
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,9 @@ pub use highlighter::Highlighter;
|
||||||
pub use paragraph::Paragraph;
|
pub use paragraph::Paragraph;
|
||||||
|
|
||||||
use crate::alignment;
|
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::borrow::Cow;
|
||||||
use std::hash::{Hash, Hasher};
|
use std::hash::{Hash, Hasher};
|
||||||
|
|
@ -239,6 +241,10 @@ pub struct Span<'a, Link = (), Font = crate::Font> {
|
||||||
pub link: Option<Link>,
|
pub link: Option<Link>,
|
||||||
/// The [`Highlight`] of the [`Span`].
|
/// The [`Highlight`] of the [`Span`].
|
||||||
pub highlight: Option<Highlight>,
|
pub highlight: Option<Highlight>,
|
||||||
|
/// The [`Padding`] of the [`Span`].
|
||||||
|
///
|
||||||
|
/// Currently, it only affects the bounds of the [`Highlight`].
|
||||||
|
pub padding: Padding,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A text highlight.
|
/// A text highlight.
|
||||||
|
|
@ -261,6 +267,7 @@ impl<'a, Link, Font> Span<'a, Link, Font> {
|
||||||
color: None,
|
color: None,
|
||||||
highlight: None,
|
highlight: None,
|
||||||
link: None,
|
link: None,
|
||||||
|
padding: Padding::ZERO,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -300,6 +307,18 @@ impl<'a, Link, Font> Span<'a, Link, Font> {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Sets the link of the [`Span`].
|
||||||
|
pub fn link(mut self, link: impl Into<Link>) -> Self {
|
||||||
|
self.link = Some(link.into());
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets the link of the [`Span`], if any.
|
||||||
|
pub fn link_maybe(mut self, link: Option<impl Into<Link>>) -> Self {
|
||||||
|
self.link = link.map(Into::into);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Sets the [`Background`] of the [`Span`].
|
/// Sets the [`Background`] of the [`Span`].
|
||||||
pub fn background(self, background: impl Into<Background>) -> Self {
|
pub fn background(self, background: impl Into<Background>) -> Self {
|
||||||
self.background_maybe(Some(background))
|
self.background_maybe(Some(background))
|
||||||
|
|
@ -355,15 +374,15 @@ impl<'a, Link, Font> Span<'a, Link, Font> {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the link of the [`Span`].
|
/// Sets the [`Padding`] of the [`Span`].
|
||||||
pub fn link(mut self, link: impl Into<Link>) -> Self {
|
///
|
||||||
self.link = Some(link.into());
|
/// It only affects the [`background`] and [`border`] of the
|
||||||
self
|
/// [`Span`], currently.
|
||||||
}
|
///
|
||||||
|
/// [`background`]: Self::background
|
||||||
/// Sets the link of the [`Span`], if any.
|
/// [`border`]: Self::border
|
||||||
pub fn link_maybe(mut self, link: Option<impl Into<Link>>) -> Self {
|
pub fn padding(mut self, padding: impl Into<Padding>) -> Self {
|
||||||
self.link = link.map(Into::into);
|
self.padding = padding.into();
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -377,6 +396,7 @@ impl<'a, Link, Font> Span<'a, Link, Font> {
|
||||||
color: self.color,
|
color: self.color,
|
||||||
link: self.link,
|
link: self.link,
|
||||||
highlight: self.highlight,
|
highlight: self.highlight,
|
||||||
|
padding: self.padding,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -262,7 +262,8 @@ pub fn parse<'a>(
|
||||||
.font(Font::MONOSPACE)
|
.font(Font::MONOSPACE)
|
||||||
.color(Color::WHITE)
|
.color(Color::WHITE)
|
||||||
.background(color!(0x111111))
|
.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() {
|
let span = if let Some(link) = link.as_ref() {
|
||||||
span.color(palette.primary).link(link.clone())
|
span.color(palette.primary).link(link.clone())
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ use crate::core::widget::text::{
|
||||||
use crate::core::widget::tree::{self, Tree};
|
use crate::core::widget::tree::{self, Tree};
|
||||||
use crate::core::{
|
use crate::core::{
|
||||||
self, Clipboard, Color, Element, Event, Layout, Length, Pixels, Point,
|
self, Clipboard, Color, Element, Event, Layout, Length, Pixels, Point,
|
||||||
Rectangle, Shell, Size, Widget,
|
Rectangle, Shell, Size, Vector, Widget,
|
||||||
};
|
};
|
||||||
|
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
|
@ -252,6 +252,16 @@ where
|
||||||
let translation = layout.position() - Point::ORIGIN;
|
let translation = layout.position() - Point::ORIGIN;
|
||||||
|
|
||||||
for bounds in state.paragraph.span_bounds(index) {
|
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.fill_quad(
|
||||||
renderer::Quad {
|
renderer::Quad {
|
||||||
bounds: bounds + translation,
|
bounds: bounds + translation,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue