emilk/egui · error

A style should be set for {:?}

Error message

A style should be set for {:?}

What it means

`Themes::get::<S>()` fetches a widget style (`ThemeWrap<S>`) previously stored via the corresponding `set`. It reads from egui's temp memory keyed by the widget-style type; if no style for type `S` was ever set (or memory was cleared/reset between frames), the lookup returns None and it panics with the type name.

Source

Thrown at crates/egui/src/theme/themes.rs:51

        if !force
            && self
                .themes
                .get_temp::<ThemeWrap<S>>(Id::NULL)
                .is_some_and(|t| t.lock().type_id() == theme.type_id())
        {
            return;
        }

        self.themes
            .insert_temp::<ThemeWrap<S>>(Id::NULL, Arc::new(Mutex::new(Box::new(theme))));
    }

    /// Fetch the style of the current theme
    pub fn get<S: WidgetStyle + 'static>(&self) -> ThemeWrap<S> {
        let v = self.themes.get_temp::<ThemeWrap<S>>(Id::NULL);

        v.unwrap_or_else(|| {
            panic!(
                "A style should be set for {:?}",
                core::any::type_name::<S>()
            )
        })
    }
}

View on GitHub (pinned to 441971a776)

Solutions

  1. Call the matching `themes.set::<MyWidgetStyle>(ThemeWrap::new(...))` before any `get`.
  2. Use the non-panicking lookup (`get_temp` / an `opt` variant) and fall back to a default style when None.
  3. Ensure the `set` runs on the same Context/Themes instance and every frame (temp memory is per-frame for non-persisted temps).
  4. In tests, run the same style-initialization code path as the app before exercising widgets that call `get`.

Example fix

// before
let style = themes.get::<ButtonStyle>(); // panics if never set
// after
themes.set::<ButtonStyle>(ThemeWrap::new(ButtonStyle::default()));
let style = themes.get::<ButtonStyle>();
Defensive patterns

Strategy: fallback

Validate before calling

// ensure style exists before get
if !themes.is_set::<ButtonStyle>() {
    themes.set::<ButtonStyle>(ThemeWrap::new(ButtonStyle::default()));
}

Type guard

fn style_is_set<S: WidgetStyle + 'static>(themes: &Themes) -> bool {
    themes.get_temp::<ThemeWrap<S>>(Id::NULL).is_some() // or the crate's opt accessor
}

Prevention

When it happens

Trigger: Calling `themes.get::<MyWidgetStyle>()` before ever calling `themes.set::<MyWidgetStyle>(...)`, or after egui memory was reset (e.g. `ctx.memory` clear / new Context instance), so `get_temp::<ThemeWrap<S>>(Id::NULL)` yields None.

Common situations: Setting theme styles in one place of the app (e.g. on startup with a feature flag) but reading them unconditionally elsewhere; tests creating a fresh Context without running the set-up path; forgetting the set call after refactoring.

Related errors


AI-assisted analysis of emilk/egui@441971a776 (2026-09-12). Data as JSON: /api/errors/81f3c6402a878820. Report an issue: GitHub.