Kael’s context system is the backbone of its reactive model. Rather than passingDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/Augani/kael/llms.txt
Use this file to discover all available pages before exploring further.
App everywhere, different parts of the framework receive purpose-built context objects — Context<T>, async contexts, and window contexts — all sharing a common set of traits. This page documents the three core traits (AppContext, VisualContext, BorrowAppContext), the EventEmitter<E> contract, and the Reservation<T> primitive that lets you obtain an entity ID before the entity is constructed.
AppContext trait
AppContext is the shared interface that App, Context<T>, AsyncApp, and AsyncWindowContext all implement. Any function that accepts impl AppContext or C: AppContext works with all of them.
Entity lifecycle
Creates a new entity and stores it in the app. The closure receives a
Context<T> and must return a value of type T. The returned Entity<T> is a lightweight, reference-counted handle.Reserves a slot for an entity without constructing it yet. The returned
Reservation<T> exposes the future EntityId so you can reference the entity (e.g. in a parent entity) before the entity itself exists.Completes the reservation by constructing the entity. Must be called with the
Reservation returned by the same context’s reserve_entity. Fails to compile if reservation was obtained from a different context instance.Runs the closure with mutable access to the entity’s data and a
Context<T>. Call cx.notify() inside the closure to mark the entity dirty and trigger re-renders.Provides read-only access to the entity’s data alongside a shared
&App.Window access
cx.update_window(handle, f)
(AnyWindowHandle, FnOnce(AnyView, &mut Window, &mut App) -> T) -> Result<T>
Runs
f with mutable access to the window’s root view, its Window, and the App. Returns Err if the window has been closed.Provides read-only access to the window’s root entity. Returns
Err if the window has been closed.Background tasks
Send future on the background thread pool. The returned Task<R> must be held or .detach()ed; dropping it cancels the task.
Reading globals
App, Context<T>), the Result type is the return value directly. On async contexts, it is Result<R>.
Reservation<T>
Reservation<T> is returned by reserve_entity and passed to insert_entity. Its sole purpose is letting you obtain an EntityId before the entity is built.
Reservation<T> is not Clone. You can only complete it once via insert_entity.VisualContext trait
VisualContext extends AppContext with methods that require a window to be present. It is implemented by window contexts such as the callback arguments inside WindowHandle::update and open_window.
Returns the type-erased handle to the window associated with this context.
cx.update_window_entity(entity, update)
(&Entity<T>, FnOnce(&mut T, &mut Window, &mut Context<T>) -> R) -> R
Like
update_entity, but also provides mutable access to the Window. Use this when your entity update needs to interact with window-level APIs — inserting hitboxes, dispatching actions, or reading layout.Creates a new entity with access to both
Window and Context<T>. Use this when construction requires window-level information such as the current scale factor or focus state.Replaces the window’s root view with a new view of type
V. The old root view and all its descendants are dropped. Requires V: Render.Moves keyboard focus to the element associated with
entity. The entity must implement Focusable, which means it must expose a FocusHandle via fn focus_handle(&self, cx: &App) -> FocusHandle.invalidate_cache
cx.notify().
BorrowAppContext trait
BorrowAppContext is auto-implemented for any type that implements BorrowMut<App>, including App, Context<T>, and async contexts. It provides three global-mutation helpers as a convenience over the lower-level App methods.
Stores
value as the app-wide global for type G. Replaces any existing value and notifies all observe_global::<G>() subscribers.Temporarily removes the global from the app, calls
f with a mutable reference to it and &mut Self, then restores it. The closure can call other context methods (e.g., open a window) while mutating the global.Like
update_global, but inserts a G::default() value first if the global is not yet present. Safe to call unconditionally.EventEmitter<E> trait
EventEmitter<E> is a marker trait that declares which event type E a given entity can emit. There is no method to implement — the trait body is empty.
Context::subscribe. The event is dispatched by calling cx.emit(event) inside the entity’s Context<T>.
Combining traits in practice
The following example shows howAppContext, VisualContext, and BorrowAppContext interact in a typical window open/update flow: