mirror of
https://github.com/storytold/gridcraft.git
synced 2026-10-31 06:06:48 -06:00
Web & UI: resolve system theme preference and default web to system theme
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -45,3 +45,7 @@ sync_github_name = false
|
||||
|
||||
[people.Losminek]
|
||||
sync_github_name = true
|
||||
|
||||
[people.GhostMitra]
|
||||
sync_github_name = true
|
||||
|
||||
|
||||
@@ -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";
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user