Skip to main content

Module appearance

Module appearance 

Source
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):

  1. AppearanceMode — the persisted user choice: follow the OS, or pin one.
  2. AppearanceState — a gpui global holding that choice alongside the last appearance the OS reported, so resolve can combine them.
  3. observe_window — subscribes to the platform’s appearance notification (macOS viewDidChangeEffectiveAppearance) 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§

AppearanceState
Global state behind the current theme: what the user chose, and what the OS last said. Kept separate from Theme itself 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 to System).

Enums§

AppearanceMode
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).
mode
The mode currently in effect (defaults to System before init).
observe_window
Subscribe a window to OS appearance changes. The returned Subscription must outlive the window — callers typically .detach() it.
reapply_window_background
Push the theme’s window background appearance onto every open window.
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.