From aab6af1601fa4cdb9b351987220f32de062d4b2c Mon Sep 17 00:00:00 2001 From: Johannes Hofmann Date: Wed, 28 Oct 2026 08:42:30 +0100 Subject: web: follow system light/dark preference All colors moved into CSS custom properties on :root (dark default) with a prefers-color-scheme: light override block. The two canvas- drawn surfaces follow too: the viewport now clears transparent so the CSS canvas background shows through, and the compass strip reads its colors from the same variables via a new bridge::css_var helper. theme-color metas (dark + light variants) tint mobile browser chrome to match. --- crates/wipfel/src/bridge.rs | 12 ++++++++++++ crates/wipfel/src/canvas_view.rs | 7 +++---- crates/wipfel/src/compass.rs | 16 +++++++++------- 3 files changed, 24 insertions(+), 11 deletions(-) (limited to 'crates/wipfel/src') diff --git a/crates/wipfel/src/bridge.rs b/crates/wipfel/src/bridge.rs index 6dbba93..0f252c1 100644 --- a/crates/wipfel/src/bridge.rs +++ b/crates/wipfel/src/bridge.rs @@ -67,3 +67,15 @@ pub fn ctx2d(canvas: &HtmlCanvasElement) -> Option { .flatten() .and_then(|c| c.dyn_into::().ok()) } + +/// Read a CSS custom property from :root (follows the active +/// prefers-color-scheme), with a fallback when unavailable. +pub fn css_var(name: &str, fallback: &str) -> String { + window() + .document() + .and_then(|d| window().get_computed_style(&d.document_element()?).ok().flatten()) + .and_then(|s| s.get_property_value(name).ok()) + .map(|v| v.trim().to_string()) + .filter(|v| !v.is_empty()) + .unwrap_or_else(|| fallback.to_string()) +} diff --git a/crates/wipfel/src/canvas_view.rs b/crates/wipfel/src/canvas_view.rs index c5777e3..df46b5c 100644 --- a/crates/wipfel/src/canvas_view.rs +++ b/crates/wipfel/src/canvas_view.rs @@ -156,10 +156,9 @@ pub fn canvas_view(props: &Props) -> Html { canvas.set_height(bh); } let _ = ctx.set_transform(1.0, 0.0, 0.0, 1.0, 0.0, 0.0); - // web-sys 0.3: set_fill_style takes a JsValue (deprecated alias) - #[allow(deprecated)] - ctx.set_fill_style(&wasm_bindgen::JsValue::from_str("#101014")); - ctx.fill_rect(0.0, 0.0, canvas.width() as f64, canvas.height() as f64); + // transparent clear: the CSS background of .canvaswrap + // (a theme variable) shows through + ctx.clear_rect(0.0, 0.0, canvas.width() as f64, canvas.height() as f64); let Some(bitmap) = bitmap else { return }; let _ = ctx.set_transform( dpr * view.scale, diff --git a/crates/wipfel/src/compass.rs b/crates/wipfel/src/compass.rs index b539e07..5e91c9a 100644 --- a/crates/wipfel/src/compass.rs +++ b/crates/wipfel/src/compass.rs @@ -127,9 +127,11 @@ pub fn compass(props: &Props) -> Html { } let _ = ctx.set_transform(dpr, 0.0, 0.0, dpr, 0.0, 0.0); - let on = "rgb(217, 217, 217)"; - let bg = "rgb(41, 41, 41)"; - let divider = "rgb(82, 82, 82)"; + // theme colors come from the CSS variables, so the strip + // follows light/dark preference like the rest of the UI + let on = bridge::css_var("--compass-fg", "rgb(217, 217, 217)"); + let bg = bridge::css_var("--compass-bg", "rgb(41, 41, 41)"); + let divider = bridge::css_var("--compass-divider", "rgb(82, 82, 82)"); let red = "rgb(255, 102, 64)"; let fill = |c: &str| { @@ -142,9 +144,9 @@ pub fn compass(props: &Props) -> Html { ctx.set_line_width(width); }; - fill(bg); + fill(&bg); ctx.fill_rect(0.0, 0.0, w, STRIP_H); - stroke(divider, 1.0); + stroke(÷r, 1.0); ctx.begin_path(); ctx.move_to(0.0, 0.5); ctx.line_to(w, 0.5); @@ -188,7 +190,7 @@ pub fn compass(props: &Props) -> Html { let a_hi = a_hi.min(c + fold); // baseline - stroke(on, 1.0); + stroke(&on, 1.0); ctx.begin_path(); ctx.move_to(0.0, y0 + 0.5); ctx.line_to(w, y0 + 0.5); @@ -225,7 +227,7 @@ pub fn compass(props: &Props) -> Html { if !x.is_finite() || x < -20.0 || x > w + 20.0 { continue; } - fill(if deg == 0.0 { red } else { on }); + fill(if deg == 0.0 { red } else { &on }); ctx.fill_text(name, x, label_y).ok(); } ctx.set_text_align("start"); -- cgit v1.2.3