ui-contract: keep an app's own text curve for each mode (#3516) (#3589)

The mode sync from #3259 wrote egui's default curve for the resolved mode into both theme styles every pass, so an app's own curve (WordCraft's dark Gamma(0.8)) was lost. Each style's curve is now remembered as the app's own whenever it differs from the one the sync last wrote, and the resolved mode's own curve is written into both styles.
This commit is contained in:
acebear17
2026-11-26 01:09:34 +00:00
committed by GitHub
parent d701080517
commit 0b8bbaf4c8
2 changed files with 55 additions and 6 deletions
+25 -6
View File
@@ -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<egui::Style>) {
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::<TextCurves>(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 });
}
@@ -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");
}