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 --- widget/src/text/rich.rs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'widget/src/text/rich.rs') diff --git a/widget/src/text/rich.rs b/widget/src/text/rich.rs index 9c0e2fac..832a3ae7 100644 --- a/widget/src/text/rich.rs +++ b/widget/src/text/rich.rs @@ -9,8 +9,8 @@ use crate::core::widget::text::{ }; use crate::core::widget::tree::{self, Tree}; use crate::core::{ - self, Clipboard, Color, Element, Event, Layout, Length, Pixels, Rectangle, - Shell, Size, Widget, + self, Clipboard, Color, Element, Event, Layout, Length, Pixels, Point, + Rectangle, Shell, Size, Widget, }; use std::borrow::Cow; @@ -246,6 +246,24 @@ where let style = theme.style(&self.class); + // Draw backgrounds + for (index, span) in self.spans.iter().enumerate() { + if let Some(background) = span.background { + let translation = layout.position() - Point::ORIGIN; + + for bounds in state.paragraph.span_bounds(index) { + renderer.fill_quad( + renderer::Quad { + bounds: bounds + translation, + border: background.border, + ..Default::default() + }, + background.color, + ); + } + } + } + text::draw( renderer, defaults, -- cgit From f7fe1edcbbfde71d801379805b4605ff36075b11 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 28 Jul 2024 13:44:08 +0200 Subject: Improve ergonomics of `span` background highlighting --- widget/src/text/rich.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'widget/src/text/rich.rs') diff --git a/widget/src/text/rich.rs b/widget/src/text/rich.rs index 832a3ae7..f636b219 100644 --- a/widget/src/text/rich.rs +++ b/widget/src/text/rich.rs @@ -248,17 +248,17 @@ where // Draw backgrounds for (index, span) in self.spans.iter().enumerate() { - if let Some(background) = span.background { + if let Some(highlight) = span.highlight { let translation = layout.position() - Point::ORIGIN; for bounds in state.paragraph.span_bounds(index) { renderer.fill_quad( renderer::Quad { bounds: bounds + translation, - border: background.border, + border: highlight.border, ..Default::default() }, - background.color, + highlight.background, ); } } -- cgit 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` --- widget/src/text/rich.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'widget/src/text/rich.rs') 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 From 41a7318e5df3a49bf6e7fc2110155f2f22ff7e60 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 28 Jul 2024 14:19:24 +0200 Subject: Remove comment in `text::Rich::draw` --- widget/src/text/rich.rs | 1 - 1 file changed, 1 deletion(-) (limited to 'widget/src/text/rich.rs') diff --git a/widget/src/text/rich.rs b/widget/src/text/rich.rs index 67db31db..9935e6c5 100644 --- a/widget/src/text/rich.rs +++ b/widget/src/text/rich.rs @@ -246,7 +246,6 @@ where let style = theme.style(&self.class); - // Draw backgrounds for (index, span) in self.spans.iter().enumerate() { if let Some(highlight) = span.highlight { let translation = layout.position() - Point::ORIGIN; -- cgit