Expand description
Long-lived, host-registered resources: fonts, images, sounds and fragment shaders, behind typed handles.
A handle (FontId, ImageId, SoundId, FragmentId) is a
slotmap key with generational use-after-free protection. It converts
to and from u64 (to_ffi / from_ffi) so it crosses a scripting
boundary as a plain integer with the generation check intact. The
registry is the session’s, so a resource registered through one window
draws and plays in every window of the session.
Registering through a Core:
use kui_core::{Core, NodeSpec, Size, TextStyle};
let mut core = Core::new();
// A font: raw TTF/OTF bytes; `None` when the data holds no usable face.
let font = core.add_font_data(std::fs::read("Inter.ttf").unwrap()).unwrap();
// An image: RGBA, `width * height * 4` bytes.
let logo = core.resources.add_image(2, 2, vec![255; 16]);
// A sound: encoded file bytes the runner's audio backend decodes.
let ding = core.add_sound(std::fs::read("ding.ogg").unwrap());
let mut ui = core.frame(Size::new(400.0, 300.0), 1.0);
ui.text("Hello", TextStyle::new(14.0).font(font));
ui.image(logo, NodeSpec::row().size(64.0, 64.0));
ui.finish();
core.play(ding, Default::default());Removing a resource (remove_image, remove_font, remove_sound,
remove_fragment on Core) makes its handle a miss: the image draws
nothing, the font shapes as sans, the sound is silent.
A handle is unique to the process, not to its session. Every
Session has its own registry, but the keys come from one process-wide
mint per kind, which also records the session that owns each. So an
ImageId from one session, looked up in another, is a detectable miss
rather than an alias for that session’s first image: it behaves as a
removed handle does, plus a foreign-resource warning the next
Core::take_warnings reports. The mint is touched on registration,
removal and the miss path only; a live lookup never locks it.
Structs§
- Font
Entry - A registered font: the family name shaping resolves it by, the faces it loaded into the font database (empty for installed fonts), and the weights the family is asked at for regular and bold.
- FontId
- A registered font (
Core::add_font_data/add_system_font), used throughTextStyle::font. - Foreign
- A handle from another session that this registry was asked to resolve:
what
diag::foreign_resourceturns into a warning. - Fragment
Entry - A registered fragment: the app’s WGSL as it was given, which is what a
backend compiles (around the core’s prelude and epilogue — see
crate::fragment) and what a C host reads back to compile itself. - Fragment
Id - A registered WGSL fragment function (
Core::add_fragment), drawn by afragmentnode. - Image
Entry - An RGBA image registered by the host (rendering lands in a later pass).
- ImageId
- Image
Opts - How an
imagenode meets the pixels it shows: its two per-node rows. Carried on the node’s content rather than onNodeSpec, so a box pays nothing for a row only an image reads. - Painter
Id - Resources
- One session’s registry. The maps are secondary to the process-wide the process-wide mint (see the module doc), so a lookup that misses is a handle this session never registered or has since removed — never somebody else’s entry.
- Session
Id - Which
Sessiona registry — and so every handle it minted — belongs to. Process-wide unique, from a counter; never serialized, so the number means nothing across runs and is only ever compared or printed. - Sound
Entry - A registered sound: the encoded file (wav/ogg/mp3/flac, whatever the driver’s backend decodes), shared so the backend can hold it without a copy. The core never decodes — headless drivers have no use for PCM.
- SoundId
- A registered sound (
Core::add_sound): encoded file bytes the driver’s audio backend decodes. Played throughCore::play, anaudionode, orNodeSpec::click_sound/hover_sound. - System
Font - One family of the font database — installed or loaded — as its faces
describe it, from what the database read off each face’s tables when
it was scanned: nothing is loaded or shaped to answer.
What
Core::system_fontslists, one per family.
Enums§
- Image
Backing - Where a registered image’s pixels are kept for drawing. The core decides on the two facts that matter — whether the image fits an atlas page, and whether its pixels were ever replaced — and the app never chooses.
- Image
Fit - The
fitrow: how the pixels meet the node’s box. The box itself — its layout, its hit region, its access rect — is the same in every mode; only what is painted inside it moves. - Resource
Kind - The kind of resource a handle names, for the warning that reports a foreign one.
- Sampling
- The
samplingrow: how a backend reads texels between pixel centres.
Constants§
- SPARE_
FRAMES - How many frames an image’s spare buffer outlives its last update. Long enough for a stream slower than the display — a 30 fps video beside a 120 Hz animation updates every fourth frame — and short enough that one that stopped gives its buffer back.