Skip to main content

Module host

Module host 

Source
Expand description

The host: the application’s kernel, which egui-pop wraps (ADR 0010).

Experimental: may change in any release (docs/stability.md).

egui-pop keeps the interface’s part: command declarations (name, keys, when, menus) feeding the menus, the palette, the keymap and the traces; focus, overlays, layout, theme and interface settings. The application’s part goes through a Host:

  • Host::dispatch runs a command, now or later (Reply), so a long or remote command never holds a frame;
  • an event journal (Host::publish, Host::since, Host::last_seq): each event has a sequence number, a source and a cause, and tests and the debug bridge read it since a sequence number instead of sleeping. A host keeps it in a Journal it hands out (Host::journal), or on its own bus (it answers publish, since and last_seq itself), or not at all: egui-pop then keeps one in memory, as LocalHost does. When the application’s bus offers the contract’s journal and the host keeps none of its own (LocalHost keeps none then), the journal is the bus’s: one per process (crate::bus::HOST_JOURNAL);
  • the value store holding settings (Host::store);
  • the owner of the application’s histories (Host::history, ADR 0014): its scopes, their steps and labels (“Undo exposure change”), undo, redo and jump. egui-pop owns the keys, the commands and the steps around them (edit.undo, edit.redo, edit.jump, ADR 0011) and routes Ctrl+Z by focus; the owner says what an undo is. The default undoes the store’s application history;
  • a Waker the host calls from any thread when something changed (Host::set_waker);
  • where the workbench’s layout is kept (Host::layout_document): one document by default, a workspace’s own, or none (the host keeps it);
  • the owner of the context stack (Host::contexts): the kernel’s, or egui-pop’s LocalContexts by default;
  • the owner of the clipboard (Host::clipboard): the kernel’s, or egui-pop’s LocalClipboard by default; and the history groups a paste is made in (Host::begin_group, Host::end_group): the store’s by default.

LocalHost is the default: commands run in-process on the interface thread (their handlers given to Ctx::command), the journal is kept in memory. An application with its own kernel implements Host, declares its commands with Ctx::declare and gives the host to AppBuilder::host.

use egui_pop_api::host::{self, Dispatch, Host, Journal, Reply, Waker};
use egui_pop_api::{App, Ctx, Plugin, PluginError};
use egui_pop_model::commands::{CommandSpec, Origin};
use egui_pop_model::ids::{CommandId, PluginId};

/// A kernel elsewhere: answers later, from another thread.
#[derive(Default)]
struct Remote {
    journal: Journal,
    store: egui_pop_core::binding::SharedStore,
    waker: Waker,
}

impl Host for Remote {
    fn dispatch(&mut self, d: &Dispatch<'_>) -> Reply {
        let (pending, resolver) = host::pending();
        let waker = self.waker.clone();
        let n = d.args.as_i64().unwrap_or(0);
        std::thread::spawn(move || {
            resolver.resolve(Ok(serde_json::json!(n * 2)));
            waker.wake();
        })
        .join()
        .unwrap();
        Reply::Later(pending)
    }
    fn journal(&self) -> Option<&Journal> { Some(&self.journal) }
    fn journal_mut(&mut self) -> Option<&mut Journal> { Some(&mut self.journal) }
    fn store(&self) -> egui_pop_core::binding::SharedStore { self.store.clone() }
    fn set_waker(&mut self, waker: Waker) { self.waker = waker; }
}

struct Doubler;
impl Plugin for Doubler {
    fn id(&self) -> PluginId { PluginId::from_static("doubler") }
    fn connect(&mut self, ctx: &mut Ctx<'_>) -> Result<(), PluginError> {
        ctx.declare(CommandSpec::new(CommandId::from_static("doubler.go"), "Double", "Demo"))?;
        Ok(())
    }
}

let mut app = App::builder().host(Remote::default()).plugin(Doubler)
    .build(&egui::Context::default()).unwrap();
let go = CommandId::from_static("doubler.go");
assert!(matches!(app.run(&go, serde_json::json!(21), Origin::User), Reply::Later(_)));
assert!(app.handle().running(&go));
app.settle();
assert_eq!(app.handle().history().last().unwrap().result, Ok(serde_json::json!(42)));

Structs§

Dispatch
A command handed to the host.
Event
Something that happened, as the journal keeps it.
Journal
Events in order, the oldest dropped past a capacity.
LocalHost
The default host: commands run in-process with their handlers, the journal is in memory.
Pending
A result to come, held by the interface.
Resolver
Gives a Pending its result, from any thread.
Waker
Wakes the interface (asks for a frame) from any thread.

Enums§

Reply
What a dispatch gives back.

Constants§

JOURNAL
How many events a Journal keeps by default.
LAYOUT_DOCUMENT
The layout’s document when the host names none of its own.
REDACTED
What stands for a private command’s arguments, result or error when its reader does not read private data (redact_private).

Traits§

Groups
Opens and ends a host’s history groups without the host itself: while the host dispatches a command it is out of reach, and a command’s AppHandle::group (Call::group) goes through this. A host keeping its own history hands one out from Host::groups; the default Host::begin_group and Host::end_group use it too.
Host
The application’s kernel, as the interface sees it.

Functions§

pending
A result to come and the way to give it.
redact_private
Hides what a private command (CommandSpec::private) carries from a reader that does not read private data, wherever it sits in v: a debug-bridge answer, a snapshot, a wait’s event.
source_of
The journal’s name for an origin: user, agent:<session>, script:<name>… (the origin’s Display).

Type Aliases§

Seq
A position in the journal: events are numbered from 1, in order.