From 362844f704ae7dc9dd04da92ee82d615599861c5 Mon Sep 17 00:00:00 2001 From: batistalucasv Date: Mon, 23 Nov 2026 19:57:10 -0300 Subject: [PATCH] ui-contract: sync egui font color transfer function with resolved theme mode --- crates/ui-contract/src/runtime/egui/style.rs | 22 +++++++++- crates/ui-contract/src/runtime/env/root.rs | 15 +++++++ crates/ui-contract/src/runtime/frame.rs | 14 ++++++ crates/ui-contract/tests/it/style/cache.rs | 45 ++++++++++++++++++++ 4 files changed, 94 insertions(+), 2 deletions(-) diff --git a/crates/ui-contract/src/runtime/egui/style.rs b/crates/ui-contract/src/runtime/egui/style.rs index ff7b38fd3..538165f0f 100644 --- a/crates/ui-contract/src/runtime/egui/style.rs +++ b/crates/ui-contract/src/runtime/egui/style.rs @@ -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; + }); + } +} diff --git a/crates/ui-contract/src/runtime/env/root.rs b/crates/ui-contract/src/runtime/env/root.rs index 23fc27c0b..dc93b733d 100644 --- a/crates/ui-contract/src/runtime/env/root.rs +++ b/crates/ui-contract/src/runtime/env/root.rs @@ -154,6 +154,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 { diff --git a/crates/ui-contract/src/runtime/frame.rs b/crates/ui-contract/src/runtime/frame.rs index bf59ec405..cc6af720b 100644 --- a/crates/ui-contract/src/runtime/frame.rs +++ b/crates/ui-contract/src/runtime/frame.rs @@ -642,6 +642,15 @@ pub(crate) fn holds_input(ctx: &Context, viewport: ViewportId) -> bool { PerViewport::::with(ctx, viewport, |record| record.held.holds()) } +/// The mode the current frame's root theme resolves to on `ctx`. +fn effective_mode(ctx: &Context) -> Mode { + let root = pass_state::with::(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) { @@ -653,6 +662,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); diff --git a/crates/ui-contract/tests/it/style/cache.rs b/crates/ui-contract/tests/it/style/cache.rs index 00909fbc5..e2d86f03e 100644 --- a/crates/ui-contract/tests/it/style/cache.rs +++ b/crates/ui-contract/tests/it/style/cache.rs @@ -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)); +}