From 1937cc4d612cad9b47e36d2377adab5702a65cce Mon Sep 17 00:00:00 2001 From: YgorSouza <43298013+YgorSouza@users.noreply.github.com> Date: Thu, 14 Aug 2025 10:40:04 +0200 Subject: [PATCH] Fix `override_text_color` priority (#7439) The override_text_color is now used when rendering text from a String or &str. This is consistent with the RichText variant and makes the option behave as advertised, taking precedence over WidgetVisuals and overriding the color for all text unless explicitly changed for a single widget (via RichText or LayoutJob). * Closes * [x] I have followed the instructions in the PR template --------- Co-authored-by: Emil Ernerfeldt --- crates/egui/src/widget_text.rs | 6 +++- crates/egui_kittest/tests/regression_tests.rs | 30 +++++++++++++++++++ .../override_text_color_interactive.png | 3 ++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 crates/egui_kittest/tests/snapshots/override_text_color_interactive.png diff --git a/crates/egui/src/widget_text.rs b/crates/egui/src/widget_text.rs index f2870784..eb0111a7 100644 --- a/crates/egui/src/widget_text.rs +++ b/crates/egui/src/widget_text.rs @@ -746,13 +746,17 @@ impl WidgetText { ) -> Arc { match self { Self::Text(text) => { + let color = style + .visuals + .override_text_color + .unwrap_or(crate::Color32::PLACEHOLDER); let mut layout_job = LayoutJob::simple_format( text, TextFormat { // We want the style overrides to take precedence over the fallback font font_id: FontSelection::default() .resolve_with_fallback(style, fallback_font), - color: crate::Color32::PLACEHOLDER, + color, valign: default_valign, ..Default::default() }, diff --git a/crates/egui_kittest/tests/regression_tests.rs b/crates/egui_kittest/tests/regression_tests.rs index 0cae152b..a3b7f6d9 100644 --- a/crates/egui_kittest/tests/regression_tests.rs +++ b/crates/egui_kittest/tests/regression_tests.rs @@ -157,3 +157,33 @@ pub fn slider_should_move_with_fixed_decimals() { let actual_slider = harness.get_by_role(accesskit::Role::SpinButton); assert_eq!(actual_slider.value(), Some("1.00".to_owned())); } + +#[test] +pub fn override_text_color_affects_interactive_widgets() { + use egui::{Color32, RichText}; + + let mut harness = Harness::new_ui(|ui| { + _ = ui.button("normal"); + _ = ui.checkbox(&mut true, "normal"); + _ = ui.radio(true, "normal"); + ui.visuals_mut().widgets.inactive.fg_stroke.color = Color32::RED; + _ = ui.button("red"); + _ = ui.checkbox(&mut true, "red"); + _ = ui.radio(true, "red"); + // override_text_color takes precedence over `WidgetVisuals`, as it docstring claims + ui.visuals_mut().override_text_color = Some(Color32::GREEN); + _ = ui.button("green"); + _ = ui.checkbox(&mut true, "green"); + _ = ui.radio(true, "green"); + // Setting the color explicitly with `RichText` overrides style + _ = ui.button(RichText::new("blue").color(Color32::BLUE)); + _ = ui.checkbox(&mut true, RichText::new("blue").color(Color32::BLUE)); + _ = ui.radio(true, RichText::new("blue").color(Color32::BLUE)); + }); + + #[cfg(all(feature = "wgpu", feature = "snapshot"))] + let mut results = SnapshotResults::new(); + + #[cfg(all(feature = "wgpu", feature = "snapshot"))] + results.add(harness.try_snapshot("override_text_color_interactive")); +} diff --git a/crates/egui_kittest/tests/snapshots/override_text_color_interactive.png b/crates/egui_kittest/tests/snapshots/override_text_color_interactive.png new file mode 100644 index 00000000..0db4034a --- /dev/null +++ b/crates/egui_kittest/tests/snapshots/override_text_color_interactive.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:60540cb1b5b71f100b2ea367a939cb9d93a91e56ff1f14ebfc988bbe79d69ac7 +size 19719