Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Themes

How to pick a theme, adjust it, and read it when painting. How tokens are written, resolved and checked is in Design tokens.

A theme

use egui_pop::egui;
use egui_pop::theme::{self, Density, ThemeSpec, Tokens};

fn setup(ctx: &egui::Context) {
    let tokens = theme::install(
        ctx,
        &ThemeSpec::new("pop-dark").density(Density::Compact).accent("blue"),
    );
    for problem in tokens.resolved().problems() {
        eprintln!("theme: {problem}");
    }
}

fn draw(ui: &mut egui::Ui) {
    // egui's widgets follow the theme; custom painting reads the tokens.
    let tokens = Tokens::of(ui.ctx());
    ui.colored_label(tokens.status.success, "Saved");
}

setup(&egui::Context::default());

In an application, give the spec to AppBuilder::theme; the theme service (AppHandle::theme) switches it while running, installed before the next frame:

use egui_pop::egui;
use egui_pop::prelude::*;

let app = App::builder()
    .theme(ThemeSpec::new("pop-light").reduced_motion(true))
    .build(&egui::Context::default())
    .unwrap();
app.handle().theme().set(ThemeSpec::new("gruvbox-dark").accent("amber"));

Light and dark together

A theme may name its counterpart, the other variant of it, light or dark: its token file says so at the root ($extensions: {"org.egui-pop.variant-of": "pop-dark"}, on one side or both), or the spec does (ThemeSpec::pair(dark, light), ThemeSpec::counterpart). The spec’s density, accent, reduced motion and overrides apply to both, unless the counterpart has overrides of its own (ThemeSpec::counterpart_overrides, set one by one with ThemeSpec::counterpart_override): a pair edited variant by variant, as the theme editor does. Every built-in theme with a light and a dark version is paired with it; pop-high-contrast is alone.

install then installs both: each in its own egui style (dark or light), with its own kit, and Tokens::of and Kit::of give the ones of egui’s theme of the moment, read at draw time. The theme named first is shown, as for a theme alone; theme::set_preference (or egui’s own ctx.set_theme) switches to the other, or follows the system’s light or dark mode, without installing anything again:

use egui_pop::egui;
use egui_pop::theme::{self, ThemeSpec, Tokens};

let ctx = egui::Context::default();
let dark = theme::install(&ctx, &ThemeSpec::new("gruvbox-dark"));
assert!(theme::is_paired(&ctx));
theme::set_preference(&ctx, egui::ThemePreference::Light);
assert_eq!(Tokens::of(&ctx).resolved().spec().base, "gruvbox-light");
assert_ne!(Tokens::of(&ctx).surface.panel, dark.surface.panel);
// Follows the system; the theme named first when it says nothing.
theme::set_preference(&ctx, egui::ThemePreference::System);

// Any two themes, or one alone (its mode is then forced).
theme::install(&ctx, &ThemeSpec::pair("pop-high-contrast", "github-light"));
theme::install(&ctx, &ThemeSpec::new("pop-dark").alone());
assert!(!theme::is_paired(&ctx));

A theme alone keeps today’s behaviour: both of egui’s styles are its, and its mode is forced whatever is asked. A widget reading Tokens::of or Kit::of while drawing follows a switch; anything keeping what it drew from them across frames keys on the kit’s address (Arc::as_ptr(&Kit::of(ctx))) or on the colours themselves. Fonts and icon sizes come from the theme named first.

In an application, the theme service keeps the preference, the appearance.theme_preference setting (system, light, dark; in the settings’ Appearance section): ThemeService::preference, set_preference, shown (the theme drawn) and toggle, the view.theme.toggle_light_dark command (View menu). Picking a theme (ThemeService::pick) shows it: when the preference would show its counterpart, the preference becomes the picked theme’s mode. Without a counterpart, the preference changes nothing.

A theme in one part of a window

theme::ScopedTheme draws a theme in a child Ui without installing it: egui’s style for it, and Tokens::of and Kit::of giving its tokens and kit while it draws, so egui-pop’s widgets and custom painting follow it too. The rest of the window keeps the installed theme. The theme editor’s preview uses it; so can a themed island, or a theme picker’s thumbnails.

use egui_pop::egui;
use egui_pop::theme::{ScopedTheme, ThemeSpec, Themes, Tokens};

// Built once and kept: compiling the kit has a cost.
fn island(ctx: &egui::Context) -> ScopedTheme {
    ScopedTheme::new(ctx, Tokens::new(Themes::default().resolve(&ThemeSpec::new("pop-light"))))
}

fn draw(ui: &mut egui::Ui, light: &ScopedTheme) {
    light.show(ui, |ui| {
        ui.label("Drawn in pop-light");
        let _ = Tokens::of(ui.ctx()); // pop-light's
    });
}
let _ = (island, draw);

Scopes nest, and the previous one comes back when show returns, even after a panic.

Popups follow the scope. egui builds the Ui of a popup, a tooltip, a menu or a window from the context’s style, not from the Ui that opened it. So while a scope draws, the context’s style is the scoped theme’s too, and it is put back when the scope ends. A combo box’s list, a tooltip, a context menu, a menu, a popover, a hover card or a kit picker opened inside the scope is drawn in the scoped theme, whether it comes from egui or from egui-pop. A popup that is drawn later, outside the scope, records the scope it was opened in (menus::popup_at does) and is drawn in ScopedTheme::within, which applies the theme to the context without a Ui:

use egui_pop::egui;
use egui_pop::theme::ScopedTheme;

// Opened inside a scope, drawn at the end of the frame.
struct Later {
    scope: Option<ScopedTheme>,
}

fn open(ctx: &egui::Context) -> Later {
    Later { scope: ScopedTheme::current(ctx) }
}

fn draw(ctx: &egui::Context, later: &Later, show: fn()) {
    match &later.scope {
        Some(scope) => scope.within(ctx, show),
        None => show(),
    }
}
let _ = (open, draw);

What stays the installed theme’s: fonts (egui’s font definitions are global and rebuilding them rebuilds the atlas), egui’s light or dark mode (ctx.theme()), and what is drawn outside the scope. Install themes between frames: a change to the context’s style made while a scope draws is undone when the scope ends.

What can be chosen

  • Themes: pop-dark, pop-light and pop-high-contrast, and twelve editor-inspired ones (GitHub, Catppuccin, One, Solarized and Gruvbox families, light and dark, and GitHub’s high-contrast pair; sources and licences in NOTICE).
  • Densities: comfortable and compact.
  • Accents: gold, blue, green, amber and red, a theme’s own scales, or any colour: ThemeSpec::accent_color(seed) generates the accent’s scale from it, for light and dark (Design tokens). In an application, the appearance.accent_color setting does the same (ThemeService::set_accent_color), for both variants of a pair.
  • Reduced motion.
  • Any token, overridden by its path (override_token).

See Design tokens for the order a theme is resolved in and how to write a theme of your own, and the theme editor to edit one live and export it.

Every built-in theme is shown at full size below.

The built-in themes

Each one as the gallery’s overview page shows it, at full size (a 1280x800 window, one pixel per point); click an image to open it alone.

pop-dark

The gallery's overview in pop-dark

pop-light

The gallery's overview in pop-light

pop-high-contrast

The gallery's overview in pop-high-contrast

github-light

The gallery's overview in github-light

github-dark

The gallery's overview in github-dark

github-light-high-contrast

The gallery's overview in github-light-high-contrast

github-dark-high-contrast

The gallery's overview in github-dark-high-contrast

catppuccin-latte

The gallery's overview in catppuccin-latte

catppuccin-mocha

The gallery's overview in catppuccin-mocha

one-light

The gallery's overview in one-light

one-dark-pro

The gallery's overview in one-dark-pro

solarized-light

The gallery's overview in solarized-light

solarized-dark

The gallery's overview in solarized-dark

gruvbox-light

The gallery's overview in gruvbox-light

gruvbox-dark

The gallery's overview in gruvbox-dark

The images are refreshed with the others (cargo xtask screenshots, see the README).

Example kits

Not built in, and not in any published crate: kit folders an application adds with AppBuilder::kit (kits as packages).

  • examples/kits/rerun-like, “Rerun-like Dark” and its counterpart “Rerun-like Light” in one kit: the Rerun viewer’s look, from the token values of its re_ui crate (Rerun Technologies AB, MIT OR Apache-2.0; not affiliated with or endorsed by Rerun; see NOTICE). The Rerun-like viewer uses it, and lists which tokens came from where.