Web & UI: resolve system theme preference and default web to system theme

This commit is contained in:
debarghaya
2026-10-12 00:51:23 +05:30
parent 336deb87de
commit 1b04d06014
4 changed files with 48 additions and 0 deletions
+1
View File
@@ -38,6 +38,7 @@ pub fn start() {
let _ = session.execute("file.new", serde_json::json!({"sample": "sales"}));
}
let mut app = SheetApp::new(session, services(inbox.clone(), cc.egui_ctx.clone()));
app.ui.system_theme = true;
// Follow the browser's language, except a CJK one: the web build has no system
// CJK fonts (none are bundled), so that interface would render as tofu.
let browser = gridcraft_ui_egui::i18n::Language::system();
+4
View File
@@ -45,3 +45,7 @@ sync_github_name = false
[people.Losminek]
sync_github_name = true
[people.GhostMitra]
sync_github_name = true
+15
View File
@@ -125,6 +125,21 @@ impl Tokens {
}
}
/// Resolves an [`egui::ThemePreference`] against the context's system theme.
pub fn resolve_theme(pref: egui::ThemePreference, ctx: &egui::Context) -> egui::Theme {
match pref {
egui::ThemePreference::System => {
if ctx.input(|i| i.raw.system_theme == Some(egui::Theme::Dark)) {
egui::Theme::Dark
} else {
egui::Theme::Light
}
}
egui::ThemePreference::Light => egui::Theme::Light,
egui::ThemePreference::Dark => egui::Theme::Dark,
}
}
pub const UI: &str = "ui";
pub const UI_BOLD: &str = "ui-bold";
pub const CELL: &str = "cell";
+28
View File
@@ -236,3 +236,31 @@ pub fn toast(app: &mut SheetApp, ctx: &egui::Context) {
);
ctx.request_repaint_after(std::time::Duration::from_millis(200));
}
/// Turn a monochrome image (black on white) into an alpha mask tinted with the active text color.
pub fn raster_to_alpha_mask(pixels: &mut [egui::Color32]) {
for p in pixels.iter_mut() {
// Darkness of source becomes the opacity
let darkness = 255u8.saturating_sub(p.r());
// Pure white glyph with variable alpha
*p = egui::Color32::from_rgba_unmultiplied(255, 255, 255, darkness);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_raster_to_alpha_mask() {
let mut pixels = vec![
egui::Color32::WHITE, // r=255 -> darkness=0 -> alpha=0
egui::Color32::BLACK, // r=0 -> darkness=255 -> alpha=255
egui::Color32::from_rgb(100, 100, 100), // r=100 -> darkness=155 -> alpha=155
];
raster_to_alpha_mask(&mut pixels);
assert_eq!(pixels[0], egui::Color32::from_rgba_unmultiplied(255, 255, 255, 0));
assert_eq!(pixels[1], egui::Color32::from_rgba_unmultiplied(255, 255, 255, 255));
assert_eq!(pixels[2], egui::Color32::from_rgba_unmultiplied(255, 255, 255, 155));
}
}