Skip to main content

Module state

Module state 

Source
Expand description

The state egui-pop keeps in egui’s memory, through typed slots.

A Slot names a value of one type kept in a context’s temporary memory (egui::Memory::data): per context (Slot::new), or per instance (slot.keyed(widget_id), one value per key). Every write registers the slot in the context, so the debug bridge’s state collector lists what egui-pop holds: each slot’s name, type, scope, how many entries it has and a one-line summary of its values.

use egui_pop_core::state::Slot;

// Declared once, beside the code that uses it; the name is unique.
const CLICKS: Slot<u32> = Slot::new("example.clicks");

let ctx = egui::Context::default();
CLICKS.update(&ctx, |n| *n += 1);
assert_eq!(CLICKS.get(&ctx), Some(1));
// Per instance: one value per key.
let widget = egui::Id::new("a button");
CLICKS.keyed(widget).set(&ctx, 7);
assert_eq!(CLICKS.keyed(widget).get(&ctx), Some(7));
assert_eq!(CLICKS.get(&ctx), Some(1));

egui’s context is locked while its memory is read or written, and taking the lock again from inside (ctx.viewport_id(), ctx.input(..), another slot) deadlocks. A slot takes the lock itself and only hands its value to the closure of Slot::update; build every key and read every other state before calling it. A slot touched from inside another slot’s update panics with a message instead of deadlocking.

Structs§

Slot
A value of type T kept in a context’s temporary memory, by name.
SlotReport
A slot as the debug bridge reports it.

Enums§

Scope
Where a slot’s value lives.

Constants§

SUMMARY_MAX
The longest summary of a value, in characters.

Functions§

lock
Locks a mutex, ignoring poisoning (a panic caught by a fault boundary must not make the framework’s own state unusable).
report
Every slot written in a context, by name, with its entries still held (keys removed since are forgotten). At most samples summaries per slot.