Merge pull request #3259 from batistalucasv/fix/sync-egui-text-color-transfer

ui-contract: sync egui font color transfer function with resolved theme mode
This commit is contained in:
Martin Ball
2026-11-24 15:05:24 +00:00
committed by GitHub
4 changed files with 94 additions and 2 deletions
+20 -2
View File
@@ -1,6 +1,8 @@
//! The theme styles' `right_to_left`.
//! The theme styles' `right_to_left` and font coverage transfer function.
use egui::{Context, Ui};
use craft_theme::Mode;
use egui::epaint::FontColorTransferFunction;
use egui::{Context, Theme, Ui};
/// Turns off egui's own caret blink in `ui`, and hides the caret stroke unless `visible`: the runtime blinks it.
pub(crate) fn set_text_cursor(ui: &mut Ui, visible: bool) {
@@ -63,3 +65,19 @@ pub(crate) fn set_theme_direction(ctx: &Context, root: &mut Ui, right_to_left: b
pub(crate) fn set_item_spacing(ui: &mut Ui, gap: egui::Vec2) {
ui.spacing_mut().item_spacing = gap;
}
/// Matches egui's font coverage transfer function to `mode` across both egui theme styles, so dark-on-light text
/// never renders with the dark-mode curve (or vice versa) when the app's mode differs from the OS's.
pub(crate) fn set_text_color_transfer(ctx: &Context, mode: Mode) {
let transfer = match mode {
Mode::Light => FontColorTransferFunction::LIGHT_MODE_DEFAULT,
Mode::Dark => FontColorTransferFunction::DARK_MODE_DEFAULT,
};
let matches = ctx.style_of(Theme::Dark).visuals.text_options.color_transfer_function == transfer
&& ctx.style_of(Theme::Light).visuals.text_options.color_transfer_function == transfer;
if !matches {
ctx.all_styles_mut(|style| {
style.visuals.text_options.color_transfer_function = transfer;
});
}
}
+15
View File
@@ -179,6 +179,21 @@ impl RootEnv {
self.resolved_for(super::system_appearance(ctx).appearance)
}
/// The mode the root's theme actually shows on `ctx`: the resolved mode clamped to the theme's supported
/// variants (for example, `Mode::Light` for a light-only theme even when dark mode is requested).
pub(crate) fn effective_mode(&self, ctx: &Context) -> Mode {
let wanted = self.resolved(ctx).mode;
if let Some(def) = &self.theme_def {
return def.variants.mode_for(wanted);
}
let themes = craft_theme::builtins();
let theme = [self.theme.as_str(), craft_theme::SUITE_DEFAULT]
.into_iter()
.find_map(|id| themes.iter().find(|theme| theme.id.as_str() == id))
.unwrap_or(&themes[0]);
theme.variants.mode_for(wanted)
}
/// What the choices resolve to when the OS reports `system`.
pub fn resolved_for(&self, system: SystemAppearance) -> ResolvedRoot {
let mode = match self.mode {
+14
View File
@@ -664,6 +664,15 @@ pub(crate) fn held_for(ctx: &Context, viewport: ViewportId, frame: u64) -> Vec<e
PerViewport::<ViewportRecord>::with(ctx, viewport, |record| record.held.delivered_in(frame))
}
/// The mode the current frame's root theme resolves to on `ctx`.
fn effective_mode(ctx: &Context) -> Mode {
let root = pass_state::with::<FrameState, _>(ctx, |state| state.input.as_ref().map(|input| input.root.clone()));
match root {
Some(root) => root.effective_mode(ctx),
None => RootEnv::fallback(ctx).effective_mode(ctx),
}
}
/// The input hook: egui runs it before it pushes the viewport, so every step reads only `RawInput` and
/// records keyed by `input.viewport_id`.
pub(crate) fn input_hook(ctx: &Context, input: &mut RawInput) {
@@ -675,6 +684,11 @@ pub(crate) fn input_hook(ctx: &Context, input: &mut RawInput) {
raw_input::prepare(ctx, input);
// H0b: the OS colour scheme egui's integration reports reaches the system reader.
crate::runtime::env::system::input_hook(ctx, &input.events);
// H0d: match egui's font coverage transfer function to the pass's resolved theme mode before egui runs
// `Fonts::begin_pass`, so dark-on-light and light-on-dark text use the right curve even when the app's mode
// differs from the OS's.
let mode = effective_mode(ctx);
style::set_text_color_transfer(ctx, mode);
// From H0 to H3 the events go with the input time each arrived at (`Arrived`): held events keep theirs, and the key
// stage gives each shortcut its own. egui's events are what these stages leave.
let mut events = Arrived::take(input);
@@ -96,3 +96,48 @@ fn a_reported_text_scale_reaches_the_systems_text_scale_and_raises_the_generatio
report_os_text_scale(&frames.ctx, None);
assert_eq!(system_text_scale(&frames.ctx), None);
}
/// egui's font coverage transfer function follows the pass's resolved theme mode before `Fonts::begin_pass`, so
/// dark-on-light text never renders with the dark-mode curve (or vice versa) when the app's mode differs from the
/// OS's, and single-mode themes keep their own variant's curve.
#[trial]
fn egui_font_transfer_function_follows_the_resolved_theme_mode() {
use craft_theme::ThemeId;
use egui::epaint::FontColorTransferFunction;
let mut frames = Frames::new();
let mut transfer = None;
// Light app on a dark OS must use the light-mode transfer function on the same pass.
frames.root(RootEnv::new().mode(ModeChoice::Light));
frames.frame(vec![egui::Event::SystemTheme(egui::Theme::Dark)], |ui| {
transfer = Some(ui.visuals().text_options.color_transfer_function);
});
assert_eq!(transfer, Some(FontColorTransferFunction::LIGHT_MODE_DEFAULT));
// Dark app on a light OS must use the dark-mode transfer function on the same pass.
frames.root(RootEnv::new().mode(ModeChoice::Dark));
frames.frame(vec![egui::Event::SystemTheme(egui::Theme::Light)], |ui| {
transfer = Some(ui.visuals().text_options.color_transfer_function);
});
assert_eq!(transfer, Some(FontColorTransferFunction::DARK_MODE_DEFAULT));
// System mode tracks OS theme changes immediately.
frames.root(RootEnv::new().mode(ModeChoice::System));
frames.frame(vec![egui::Event::SystemTheme(egui::Theme::Dark)], |ui| {
transfer = Some(ui.visuals().text_options.color_transfer_function);
});
assert_eq!(transfer, Some(FontColorTransferFunction::DARK_MODE_DEFAULT));
frames.frame(vec![egui::Event::SystemTheme(egui::Theme::Light)], |ui| {
transfer = Some(ui.visuals().text_options.color_transfer_function);
});
assert_eq!(transfer, Some(FontColorTransferFunction::LIGHT_MODE_DEFAULT));
// A light-only theme keeps the light-mode transfer function even when dark mode is requested.
let classic = ThemeId::parse("craft.classic").expect("classic theme id");
frames.root(RootEnv::new().theme(classic).mode(ModeChoice::Dark));
frames.frame(vec![egui::Event::SystemTheme(egui::Theme::Dark)], |ui| {
transfer = Some(ui.visuals().text_options.color_transfer_function);
});
assert_eq!(transfer, Some(FontColorTransferFunction::LIGHT_MODE_DEFAULT));
}