From 668abc283881a4905de2a5eb8f24ef306f715381 Mon Sep 17 00:00:00 2001
From: GiGaGon <107241144+MeGaGiGaGon@users.noreply.github.com>
Date: Fri, 21 Mar 2025 05:45:34 -0700
Subject: [PATCH] Add `expand_bg` to customize size of text background (#5365)
This removes the `expand(1.0)` on text background colors, since it makes
translucent background colors have bad looking bleeding.
There is probably a smarter solution than disabling the highlighting
entirely, but I don't see a way to do that while keeping the area
consumed consistent between translucent/solid colors, or adding a decent
step up in complexity.
Since this makes it impossible to tell if selected text is highlighted,
this also adds a blanket `0.5` gamma multiply to the text selection
background color. If that is undesirable because it's a bad arbitrary
number choice, or if it's too much of an unexpected change and just the
default values should be changed, please let me know.
These changes cause the tests that use screenshots with highlighted text
to fail, though I am not sure how to update those tests to match the
changes.
Comparison Images
Current:

After changes:

Code used to make comparison images
```rs
fn color_text_format(ui: &Ui, color: Color32) -> TextFormat {
TextFormat { font_id: FontId::monospace(ui.text_style_height(&egui::TextStyle::Monospace)), background: color, ..Default::default() }
}
fn color_sequence_galley(ui: &Ui, text: &str, colors: [Color32; 3]) -> Arc {
let mut layout_job = LayoutJob::default();
for color in colors {
layout_job.append(text, 0.0, color_text_format(ui, color));
}
ui.fonts(|f| f.layout_job(layout_job))
}
fn color_sequence_row(ui: &mut Ui, label_text: &str, text: &str, colors: [Color32; 3]) {
ui.label(label_text);
ui.label(color_sequence_galley(ui, text, colors));
ui.end_row();
}
egui::Grid::new("comparison display").show(ui, |ui| {
ui.ctx().set_pixels_per_point(2.0);
let transparent = Color32::TRANSPARENT;
let solid = Color32::RED;
let solid_2 = Color32::GREEN;
let translucent_1 = Color32::GRAY.gamma_multiply(0.5);
let translucent_2 = Color32::GREEN.gamma_multiply(0.5);
color_sequence_row(ui, "Transparent to Solid:", " ", [transparent, solid, transparent]);
color_sequence_row(ui, "Translucent to Transparent:", " ", [transparent, translucent_1, transparent]);
color_sequence_row(ui, "Solid to Transparent:", " ", [solid, solid_2, solid]);
color_sequence_row(ui, "Solid to Solid:", " ", [solid, transparent, solid]);
color_sequence_row(ui, "Solid to Translucent:", " ", [solid, translucent_1, solid]);
color_sequence_row(ui, "Translucent to Translucent:", " ", [translucent_1, translucent_2, translucent_1]);
color_sequence_row(ui, "Transparent to Solid:", "a", [transparent, solid, transparent]);
color_sequence_row(ui, "Translucent to Transparent:", "a", [transparent, translucent_1, transparent]);
color_sequence_row(ui, "Solid to Transparent:", "a", [solid, solid_2, solid]);
color_sequence_row(ui, "Solid to Solid:", "a", [solid, transparent, solid]);
color_sequence_row(ui, "Solid to Translucent:", "a", [solid, translucent_1, solid]);
color_sequence_row(ui, "Translucent to Translucent:", "a", [translucent_1, translucent_2, translucent_1]);
})
```
* [x] I have followed the instructions in the PR template
---------
Co-authored-by: Emil Ernerfeldt
---
crates/egui/src/widget_text.rs | 28 ++++++++++++++++++++-
crates/epaint/src/text/text_layout.rs | 13 +++++-----
crates/epaint/src/text/text_layout_types.rs | 8 ++++++
3 files changed, 42 insertions(+), 7 deletions(-)
diff --git a/crates/egui/src/widget_text.rs b/crates/egui/src/widget_text.rs
index 1132aa30..5ddafc4b 100644
--- a/crates/egui/src/widget_text.rs
+++ b/crates/egui/src/widget_text.rs
@@ -22,7 +22,7 @@ use crate::{
/// RichText::new("colored").color(Color32::RED);
/// RichText::new("Large and underlined").size(20.0).underline();
/// ```
-#[derive(Debug, Clone, Default, PartialEq)]
+#[derive(Clone, Debug, PartialEq)]
pub struct RichText {
text: String,
size: Option,
@@ -31,6 +31,7 @@ pub struct RichText {
family: Option,
text_style: Option,
background_color: Color32,
+ expand_bg: f32,
text_color: Option,
code: bool,
strong: bool,
@@ -41,6 +42,29 @@ pub struct RichText {
raised: bool,
}
+impl Default for RichText {
+ fn default() -> Self {
+ Self {
+ text: Default::default(),
+ size: Default::default(),
+ extra_letter_spacing: Default::default(),
+ line_height: Default::default(),
+ family: Default::default(),
+ text_style: Default::default(),
+ background_color: Default::default(),
+ expand_bg: 1.0,
+ text_color: Default::default(),
+ code: Default::default(),
+ strong: Default::default(),
+ weak: Default::default(),
+ strikethrough: Default::default(),
+ underline: Default::default(),
+ italics: Default::default(),
+ raised: Default::default(),
+ }
+ }
+}
+
impl From<&str> for RichText {
#[inline]
fn from(text: &str) -> Self {
@@ -364,6 +388,7 @@ impl RichText {
family,
text_style,
background_color,
+ expand_bg,
text_color: _, // already used by `get_text_color`
code,
strong: _, // already used by `get_text_color`
@@ -429,6 +454,7 @@ impl RichText {
underline,
strikethrough,
valign,
+ expand_bg,
},
)
}
diff --git a/crates/epaint/src/text/text_layout.rs b/crates/epaint/src/text/text_layout.rs
index 4eb5965c..ff973ba4 100644
--- a/crates/epaint/src/text/text_layout.rs
+++ b/crates/epaint/src/text/text_layout.rs
@@ -758,10 +758,10 @@ fn add_row_backgrounds(job: &LayoutJob, row: &Row, mesh: &mut Mesh) {
return;
}
- let mut end_run = |start: Option<(Color32, Rect)>, stop_x: f32| {
- if let Some((color, start_rect)) = start {
+ let mut end_run = |start: Option<(Color32, Rect, f32)>, stop_x: f32| {
+ if let Some((color, start_rect, expand)) = start {
let rect = Rect::from_min_max(start_rect.left_top(), pos2(stop_x, start_rect.bottom()));
- let rect = rect.expand(1.0); // looks better
+ let rect = rect.expand(expand);
mesh.add_colored_rect(rect, color);
}
};
@@ -776,18 +776,19 @@ fn add_row_backgrounds(job: &LayoutJob, row: &Row, mesh: &mut Mesh) {
if color == Color32::TRANSPARENT {
end_run(run_start.take(), last_rect.right());
- } else if let Some((existing_color, start)) = run_start {
+ } else if let Some((existing_color, start, expand)) = run_start {
if existing_color == color
&& start.top() == rect.top()
&& start.bottom() == rect.bottom()
+ && format.expand_bg == expand
{
// continue the same background rectangle
} else {
end_run(run_start.take(), last_rect.right());
- run_start = Some((color, rect));
+ run_start = Some((color, rect, format.expand_bg));
}
} else {
- run_start = Some((color, rect));
+ run_start = Some((color, rect, format.expand_bg));
}
last_rect = rect;
diff --git a/crates/epaint/src/text/text_layout_types.rs b/crates/epaint/src/text/text_layout_types.rs
index afe2573d..2b8b3e81 100644
--- a/crates/epaint/src/text/text_layout_types.rs
+++ b/crates/epaint/src/text/text_layout_types.rs
@@ -272,6 +272,11 @@ pub struct TextFormat {
pub background: Color32,
+ /// Amount to expand background fill by.
+ ///
+ /// Default: 1.0
+ pub expand_bg: f32,
+
pub italics: bool,
pub underline: Stroke,
@@ -299,6 +304,7 @@ impl Default for TextFormat {
line_height: None,
color: Color32::GRAY,
background: Color32::TRANSPARENT,
+ expand_bg: 1.0,
italics: false,
underline: Stroke::NONE,
strikethrough: Stroke::NONE,
@@ -316,6 +322,7 @@ impl std::hash::Hash for TextFormat {
line_height,
color,
background,
+ expand_bg,
italics,
underline,
strikethrough,
@@ -328,6 +335,7 @@ impl std::hash::Hash for TextFormat {
}
color.hash(state);
background.hash(state);
+ emath::OrderedFloat(*expand_bg).hash(state);
italics.hash(state);
underline.hash(state);
strikethrough.hash(state);