Skip to main content

Module contexts

Module contexts 

Source
Expand description

The context stack (ADR 0012): places (drill-downs, shown as a breadcrumb) and modes (global states, shown as chips), owned by the application’s kernel and projected by the interface.

Experimental: may change in any release (docs/stability.md).
  • The owner implements Contexts: the frames, the state, the transitions (through the pure egui_pop_model::context::apply), the holds. The host gives it (Host::contexts); without one, ContextsPlugin keeps a LocalContexts, which writes the stack to the context document inside each transition. The owner publishes context.changed (with the whole new state and its revision), then context.left and context.entered per frame, or context.refused; context.restored at start, context.held and context.released.
  • The interface asks, never writes. Every transition is the step context.request (ADR 0011): a before-hook vetoes it (unsaved typed input) or redirects it, its default asks the owner. Nothing is shown until the owner’s state has a new revision, so a refusal leaves the display as it was.
  • The projection follows the owner’s state: per projection key (the place path and the active modes), the main window’s workbench layout (a place’s arrangement the first time, a mode’s panels shown or hidden; a change made in a mode belongs to it and goes when it is left; panels docked in other application windows stay there), the focused panel and the values components keep (view_value, scroll_area); the context keys place, placeDepth, modePushed, mode.<group> and the frames’ own keys, read by when clauses (a frame’s own key bindings use FrameSpec::clause: Ctx::frame_keybinding). It is written to the view document about a second after it changes.
  • Widgets: breadcrumb, mode_chips, mode_switch, mode_toggle. Commands: nav.back (Alt+Left; Escape while a mode is pushed), nav.up (Alt+Shift+Up), nav.enter, and for agents context.read, context.request (with if_rev and dry_run), context.go, context.set_mode, context.hold and context.release. The contexts collector shows it all.
  • Undo out of sight: the value store’s new steps carry the place shown (Store::set_place); an undo, a redo or a jump that walks a step made in another place (its StepInfo::place) says so in a toast, with “Go there”.
use egui_pop_api::contexts::{self, ContextsPlugin, FrameSpec};
use egui_pop_api::{App, Ctx, Plugin, PluginError};
use egui_pop_model::ids::{FrameId, GroupId, PluginId};

const ALBUM: FrameId = FrameId::from_static("demo.album");
const CULLING: FrameId = FrameId::from_static("demo.culling");

struct Demo;
impl Plugin for Demo {
    fn id(&self) -> PluginId { PluginId::from_static("demo") }
    fn requires(&self) -> Vec<PluginId> { vec![contexts::PLUGIN] }
    fn connect(&mut self, ctx: &mut Ctx<'_>) -> Result<(), PluginError> {
        ctx.frame(FrameSpec::place(ALBUM, "Album"))?
            .frame(FrameSpec::mode(CULLING, GroupId::from_static("orientation"), "Culling"))?;
        Ok(())
    }
}

let app = App::builder().plugin(ContextsPlugin::default()).plugin(Demo)
    .build(&egui::Context::default()).unwrap();
let h = app.handle();
assert!(contexts::enter(h, ALBUM, serde_json::Value::Null).ok());
assert!(contexts::set_mode(h, CULLING).ok());
assert_eq!(h.context().get("place"), "demo.album");
assert_eq!(h.context().get("mode.orientation"), "demo.culling");
assert!(contexts::back(h).vetoed().is_none());
assert_eq!(h.context().get("place"), "root");

Structs§

Chip
An active mode, as a chip shows it.
ContextService
The context stack in the application: the default owner, the projection, the requests waiting for a remote owner.
ContextsPlugin
The context stack as a plugin: the default owner (a LocalContexts, unless the host owns the stack), the projection, the context.request step, the commands and keys, the contexts collector, the undo toasts.
Dropped
A frame dropped on restore, and why.
Entry
A place on the path, with its payload (the application’s own: a sub-workspace’s id).
Envelope
A request with its conditions.
FrameClash
Two frames that can be on together (a place and a mode, two modes of different groups) binding the same keys to different commands (Ctx::frame_keybinding): while both are on, one wins (the later declared, between equals) and the other never fires.
FrameId
A frame of the context stack (ADR 0012): a place (demo.album) or a mode (demo.orientation.culling).
FrameRegistry
The frames that exist.
FrameSpec
A frame as declared.
GroupId
A group of modes, one active at a time (orientation): the context key mode.<group>, so letters, digits, _ and ..
HandWrite
One of the stack’s own context keys, set by hand (AppHandle::set_context) instead of by the projection: two truths, and the projection’s overwrites the plugin’s at the next transition.
Hold
A short lock on groups of the stack: a drag in progress, a batch writing, an agent holding the image. Requests touching a held group are refused with its reason.
LabelCall
What a place’s crumb label is asked about.
LocalContexts
The default owner, in process: frames declared by plugins, the stack written to a document inside each transition when given one.
ModeEntry
An active mode.
Refusal
A refusal, with its reason and who refused.
RootLabelCall
What the root crumb’s label is asked about (ContextsPlugin::root_label).
State
The stack: the place path, the active modes, a revision.
Transition
A transition applied (or, for a dry run, that would be).

Enums§

Answer
What the owner answers a request.
Kind
What a frame is.
Life
How a mode is on.
RefusalKind
Why a transition did not happen.
Request
A transition asked of the owner.
Verdict
What a guard decides.

Constants§

CMD_BACK
Back: pops the last pushed mode, else leaves the top place.
CMD_ENTER
Enters a place (frame, payload).
CMD_GO
Goes to a place path (places: [{ "frame", "payload" }]).
CMD_HOLD
Holds groups of the stack (groups, reason): requests touching them are refused until released.
CMD_READ
The stack, its holds and its frames.
CMD_RELEASE
Releases a hold (token).
CMD_REQUEST
Any transition, as an envelope (request, if_rev, dry_run).
CMD_SET_MODE
Sets a mode (mode), pushes it (push: true), or turns a group off (group, no mode).
CMD_UP
Up: leaves the top place.
CONTEXTS
The context stack’s service.
CONTEXT_DOCUMENT
The document LocalContexts writes the stack to, beside the layout’s (workspaces/photos/context when the host keeps the layout in workspaces/photos/layout; see documents).
PLACES
The group a hold names to stop place transitions (enter, leave, jump, go).
PLUGIN
The plugin’s id: plugins declaring frames require it.
ROOT
The place at the bottom of every path, as the place context key says it.
STEP_REQUEST
A transition asked of the owner ({ "request": envelope, "origin" }): a before-hook vetoes it (the interface’s own guards) or redirects it; the default asks the owner. Its result: the new revision, the frames entered and left; a refusal is its error.
VIEW_DOCUMENT
The document the projection writes its layouts and view states to, beside the layout’s too.

Traits§

Contexts
The owner of the context stack, as the interface reaches it.

Functions§

back
Back, as the user: pops the last pushed mode, else leaves the top place.
breadcrumb
The breadcrumb: the root, then each place; a crumb jumps back to its level (the last one is where the user is).
chips
The active modes, in the order they came. Empty without the context plugin.
clear_mode
Turns a group’s mode off, as the user.
crumb_icons
The icon each crumb’s place declares (FrameSpec::icon), by name, in crumbs’ order: None for the root and for a place without one.
crumbs
The breadcrumb’s crumbs as the interface says them: the root’s title, then each place’s (its title, and its payload when it is a text or a number). Crumb i is reached with jump(app, i). Empty without the context plugin.
documents
The documents the stack (LocalContexts) and its view are kept in: CONTEXT_DOCUMENT and VIEW_DOCUMENT beside the layout’s document as the host names it (Host::layout_document), so a host keeping a layout per workspace keeps a stack per workspace too; at the top when it names none.
enter
Enters a place, as the user.
frame_clashes
The keymap’s frame clashes (see FrameClash): the key conflicts (AppHandle::key_conflicts) between two frames’ own bindings.
go
Goes to a place path, as the user.
hold
Holds groups of the stack (empty: all of it) for by: requests touching them are refused with reason until release.
is_stack_key
Whether a context key is the stack’s own: place, placeDepth, modePushed, mode.<group>, the top place’s payload and payload.<field>. Only the projection sets them; a plugin declares frames and reads them.
jump
Leaves places until depth are left (0: the root), as the user.
leave
Leaves the top place, as the user.
mode_chips
The active modes as chips, each with a way out (a pushed mode pops, a set one is turned off).
mode_switch
The modes of a group as a row of choices, the one on selected; none adds a choice turning the group off.
mode_toggle
A pushed mode as a toggle: pushes it, or pops it (turns it off when it is not the last pushed).
place_value
A value kept for the place shown (place_value_of).
place_value_of
A value kept for a place (its frame id, ROOT at the root) rather than for a projection key: the same in every album, whatever the modes. Saved with the view state.
pop_mode
Pops the last pushed mode, as the user.
push_mode
Pushes a mode, as the user.
release
Releases a hold; whether it was held.
reload
Reads the stack and its view again from documents, as at start: after the host changed where they are (another workspace opened in the same window). What was shown is forgotten (its view was written already); what the documents hold, checked against today’s frames, is projected.
request
Asks the owner for a transition, through the context.request step; the projection follows (at once, or at the next pass while the areas are drawn). A refusal or a veto asked by the user shows in a toast, unless there was nothing to do.
scroll_area
A scroll area whose offset is kept per projection key: entering a place and coming back finds it where it was.
set_mode
Sets a mode, as the user.
set_place_value
Keeps a value for the place shown (Json::Null forgets it); written to the view document a second later.
set_view_value
Keeps a value for the projection key shown; written to the view document a second later.
state
The state the interface shows, as projected last (None without the plugin).
sync
Projects the owner’s state when its revision moved (each pass does).
view_value
A value a component keeps for the projection key shown (a list’s scroll offset, a selection), saved with the view state.
why_not
Why the owner would refuse a request now (its guards, a hold), asked as a dry run of the stack’s own owner; None when it would go, when there is nothing to do, or when the host owns the stack (its answer may come later).
with_owner
Runs f on the owner: the host’s, else the plugin’s LocalContexts. None without the plugin, or while the host (owning the stack) runs a command.

Type Aliases§

RootLabel
The root crumb’s label (ContextsPlugin::root_label): None keeps the title.
Validator
Decides whether a saved frame may come back (its payload still exists): None when it may, else why not.