feat(ui): wire setup_egui_style one-shot into EguiPrimaryContextPass

Local<bool> guard retries until ctx_mut() first succeeds, applies the
sci-fi theme once, then never runs again. .chain()'d before ui_system so
the very first successful frame is already themed. Per-frame set_style
would dirty layout caches; this avoids that.
This commit is contained in:
xfy 2026-07-17 15:37:34 +08:00
parent 3a8a1ee1c3
commit 77b1817f58
2 changed files with 23 additions and 1 deletions

View File

@ -131,7 +131,12 @@ impl Plugin for BlackHolePlugin {
.add_systems(Update, rebuild_bloom)
// bevy_egui 0.41 requires UI systems to run inside the egui context
// pass (fonts/ctx are initialized there); placing them in Update panics.
.add_systems(bevy_egui::EguiPrimaryContextPass, crate::ui::ui_system);
// setup_egui_style is a one-shot (Local<bool> guard): retries until the
// context exists, applies the theme once, never runs again.
.add_systems(
bevy_egui::EguiPrimaryContextPass,
(crate::ui::setup_egui_style, crate::ui::ui_system).chain(),
);
}
}

View File

@ -4,6 +4,23 @@ pub mod preset;
use bevy::prelude::*;
use bevy_egui::egui;
/// One-shot egui styling. Registered in `EguiPrimaryContextPass` with a
/// `Local<bool>` guard so it retries until `ctx_mut()` first succeeds, then
/// runs exactly once. Per-frame set_style/set_visuals would dirty layout
/// caches every frame; this avoids that.
pub fn setup_egui_style(
mut contexts: bevy_egui::EguiContexts,
mut done: Local<bool>,
) {
if *done {
return;
}
if let Ok(ctx) = contexts.ctx_mut() {
crate::ui::style::setup(&ctx);
*done = true;
}
}
pub fn ui_system(
mut contexts: bevy_egui::EguiContexts,
mut params: ResMut<crate::params::BlackHoleParams>,