Expand description
Light/dark switching: what the user asked for, what the OS reports, and the plumbing that turns a change in either into a repaint.
Three pieces, following the pattern zed uses (crates/theme/src/theme.rs
SystemAppearance + reload_theme + cx.refresh_windows):
AppearanceMode— the persisted user choice: follow the OS, or pin one.AppearanceState— a gpui global holding that choice alongside the last appearance the OS reported, soresolvecan combine them.observe_window— subscribes to the platform’s appearance notification (macOSviewDidChangeEffectiveAppearance) and re-applies.
§Why refresh_windows and not notify
Colors are read imperatively (Theme::of(cx).text) at paint time, not
through a reactive binding, so no view knows its colors went stale — a
notify() on some entity would repaint that entity and nothing else.
App::refresh_windows marks every window dirty and disables gpui’s
per-view prepaint cache for the frame, which is the only thing that forces
already-laid-out elements to re-run their paint with the new palette.
Structs§
- Appearance
State - Global state behind the current theme: what the user chose, and what the OS
last said. Kept separate from
Themeitself so that flipping the OS appearance while the user has pinned Light still records the new system value (and takes effect the moment they switch back toSystem).
Enums§
- Appearance
Mode - The user’s appearance preference. Serde-serializable so callers can persist it wherever their settings live; this crate never touches disk.
Functions§
- apply
- Re-resolve the palette and, if it moved, swap the theme and force a full repaint. A no-op when the resolved appearance is unchanged — the OS fires the notification for vibrancy and accent-color changes too, and repainting every window for those would be a visible hitch for nothing.
- init
- Install the appearance globals and the matching theme. Call once at boot, before any window opens, so the first frame is already the right palette (installing later produces a visible dark-to-light flash).
- keep_
background - Leave this window’s background where it is, whatever the palette says.
- mode
- The mode currently in effect (defaults to
Systembeforeinit). - observe_
window - Subscribe a window to OS appearance changes. The returned
Subscriptionmust outlive the window — callers typically.detach()it. - reapply_
window_ background - Push the theme’s window background appearance onto every open window, bar the ones that asked to keep their own.
- reports_
the_ os - Whether what a window reports is the OS’s own answer.
- resolve
- Combine the user’s choice with the OS state.
- set_
mode - Change the user’s preference and repaint if that changed the palette. Persisting the choice is the caller’s job.