diff --git a/crates/ui-contract/src/runtime/egui/style.rs b/crates/ui-contract/src/runtime/egui/style.rs index 9c28942e75..4960c63513 100644 --- a/crates/ui-contract/src/runtime/egui/style.rs +++ b/crates/ui-contract/src/runtime/egui/style.rs @@ -1,5 +1,6 @@ //! The theme styles' `right_to_left` and font coverage transfer function. +use super::memory; use craft_theme::Mode; use egui::epaint::FontColorTransferFunction; use egui::{Context, Theme, Ui}; @@ -78,18 +79,36 @@ pub(crate) fn set_style(ui: &mut Ui, style: &Arc) { ui.set_style(Arc::clone(style)); } +/// Each egui theme style's own font coverage transfer function, and the one [`set_text_color_transfer`] last wrote. +#[derive(Clone, Copy)] +struct TextCurves { + light: FontColorTransferFunction, + dark: FontColorTransferFunction, + written: FontColorTransferFunction, +} + /// 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. +/// +/// The curve for `mode` is the one the app set on that mode's egui style (egui's default until it sets one): a +/// style whose curve is not the one written here last has been set by the app since, and that curve becomes the +/// style's own. 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 id = memory::unique("craft-ui-contract text color transfer"); + let held = |theme: Theme| ctx.style_of(theme).visuals.text_options.color_transfer_function; + let (light, dark) = (held(Theme::Light), held(Theme::Dark)); + let (own_light, own_dark) = match memory::peek_copy::(ctx, id) { + Some(last) => (if light == last.written { last.light } else { light }, if dark == last.written { last.dark } else { dark }), + None => (light, dark), }; - 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 { + let transfer = match mode { + Mode::Light => own_light, + Mode::Dark => own_dark, + }; + if light != transfer || dark != transfer { ctx.all_styles_mut(|style| { style.visuals.text_options.color_transfer_function = transfer; }); } + memory::put(ctx, id, TextCurves { light: own_light, dark: own_dark, written: transfer }); } diff --git a/crates/ui-contract/tests/it/style/cache.rs b/crates/ui-contract/tests/it/style/cache.rs index e2d86f03e3..e7f8129b8f 100644 --- a/crates/ui-contract/tests/it/style/cache.rs +++ b/crates/ui-contract/tests/it/style/cache.rs @@ -141,3 +141,33 @@ fn egui_font_transfer_function_follows_the_resolved_theme_mode() { }); assert_eq!(transfer, Some(FontColorTransferFunction::LIGHT_MODE_DEFAULT)); } + +/// An app's own curve for a mode (#3516): the dark curve it sets on egui's dark style is the one dark passes use, +/// while light passes still get the light curve, and the app's dark curve comes back on the next dark pass. +#[trial] +fn an_app_s_own_dark_text_curve_survives_the_mode_sync() { + use egui::epaint::FontColorTransferFunction; + + let mut frames = Frames::new(); + let own = FontColorTransferFunction::Gamma(0.8); + frames.ctx.style_mut_of(egui::Theme::Dark, |style| style.visuals.text_options.color_transfer_function = own); + let mut transfer = None; + + 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(own), "a dark pass keeps the app's dark curve"); + + 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), "a light pass gets the light curve"); + + frames.root(RootEnv::new().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(own), "the app's dark curve comes back with dark mode"); +}