Architecture
PAM Desktop is two processes and a contract. A PHP worker describes the interface; a Rust host lays it out, keeps a retained scene and drives the views of the platform. Knowing how they talk explains most of what you see while building an application: why a slow handler never freezes a scroll, why module calls take callbacks, and why an idle application costs nothing.
┌──────────────────────┐ stdin: events, module results ┌────────────────────────────────────────┐│ PHP worker process │ ◀──────────────────────────────── │ pam-desktop host process ││ │ │ ││ components, state │ stdout: frames, module calls │ engine ─▶ render core ─▶ Backend ││ render() ─▶ encoder │ ────────────────────────────────▶ │ (layout, (scene, (GTK 4 / │└──────────────────────┘ │ diff) controllers) AppKit/Win32)│ └────────────────────────────────────────┘The two processes
Section titled “The two processes”The host is the pam-desktop binary. It opens the first window, then starts the worker:
<php> vendor/pushinbr/pam-desktop/packages/desktop/bin/pam-desktop-worker <entry script>The worker runs your entry script, which calls App::run() and produces the first frame. It then reports that it is ready and loops: it blocks reading its standard input and handles one batch of host messages per turn. While the interface is idle, PHP is asleep in that read.
- The worker’s standard input and output are the channel. Output from
echo,printandvar_dumpis captured and redirected to standard error, so it cannot corrupt a frame. - The worker’s standard error is inherited from the host: PHP warnings and your own
error_log()lines appear in the terminal where you startedpam desktop. - When the worker exits, a production host quits; a development host keeps the last frame on screen and starts a new worker at the next source change.
The channel
Section titled “The channel”There is no HTTP server and no JavaScript bridge. Both directions carry the same framing:
u32 length (little-endian) · u8 type · payload| Worker to host | Host to worker |
|---|---|
| Commit: a tree frame or a patch frame | Event: an element or application event |
| Call: a module call by name | Module result: success or failure for a request id |
| Call typed: a module call by operation number | Flush: ends a batch |
| Error: an error report with phase, source and stack | Resync: the host rejected a frame, send a full tree |
| Ready: the entry script finished booting | Shutdown: leave the loop and exit |
| Idle: everything up to the last flush was handled | Checkpoint: save the hot reload checkpoint |
A message longer than 64 MiB is malformed and ends the channel.
A turn
Section titled “A turn”The host does not send one event and wait. It queues everything that happened during one drain of its loop (several events, several module results), writes them, and ends with one flush marker. The worker dispatches every message of the batch, renders once for all of them, commits a frame if anything changed and answers Idle.
Commits are asynchronous: the worker does not wait for the host to accept a frame. A frame the host cannot apply comes back as a Resync, and the worker sends a complete tree.
Module calls
Section titled “Module calls”Storage, files, HTTP, SQLite and the other modules are host services. A call carries a request id and returns at once; the host answers each request exactly once, at any later time and from any thread. That is why every module API takes a callback:
use Pam\Desktop\Storage\Storage;
Storage::get('launches', function (?string $value): void { $this->launches = (int) $value + 1; // runs in a later turn; then the component renders});Results are tagged with the generation of the worker that asked, so a request made before a hot reload is answered to nobody.
Frames and the render loop
Section titled “Frames and the render loop”A render produces a tree of nodes. Each node has a kind (Column, Text, Input, Scroll, Modal, VirtualList, Media, WebView…), a 64-bit id and a bag of properties keyed by 16-bit identifiers. Event handlers are properties too: the presence of OnPress tells the host that PHP wants presses of that node.
PHP sends either a full tree frame (PNT1) or a patch frame (PNP1) with only what changed. The engine keeps the last tree, applies the patch, runs layout (flexbox, grids, text measured by the platform) and emits a mutation batch of six operations: create, remove, update a property, move, set a layout frame and set the root. The scene applies the batch and pushes the minimal set of commands to the platform views.
The wire format has hard limits: 16 MiB per frame, 100,000 nodes, a depth of 512, 128 properties per node and 1 MiB per value. A frame the engine rejects does not crash anything: the host clears the scene, recreates the engine and asks PHP for a complete tree.
The property and event tables are shared with PAM Native, so both products speak the same protocol. Desktop-only identifiers start at 2001. Platforms & Compatibility explains how the package and the host negotiate them.
What runs without PHP
Section titled “What runs without PHP”PHP is never in the path of a frame. The host handles these by itself and tells PHP only the outcome:
| Feature | In the host | PHP hears |
|---|---|---|
| Animations | Transitions, presets, programs and shimmer are sampled on the frame clock. | Completion only. |
| Scrolling | The native scroll view; list windowing, sticky headers and programmatic scrolls. | Scroll and end-reached events; requests for more rows. |
| Press feedback | Dim, scale and fill animations, hover and state styles. | The press. |
| Gestures | Recognition, drags that follow the pointer, snap animations, pan and zoom. | Begin, end, cancel and settle. |
| Media | Play and pause from intent and visibility, clock labels and progress bars. | Ready, end and error. |
| Text input | Typing, selection, input methods, masks. | Edits, according to the input’s sync mode. |
| Navigation and modals | Transitions, backdrops, closing on Escape or a backdrop click. | The request to close; the end of a transition. |
A slow PHP handler delays the next frame of content. It does not stall an animation, a scroll or the caret.
Backends
Section titled “Backends”Navigation, tabs, modals, sheets, hit testing, gestures and drag thresholds are implemented once in the platform-neutral render core, on top of plain scene nodes. That is why they behave identically everywhere. Each backend only implements a retained-mode Backend contract with seven view classes (Box, Scroll, Input, TextArea, Surface, Media, WebView) and reports raw input back:
| System | Backend |
|---|---|
| Linux | GTK 4: Pango for text, gdk-pixbuf for images, GStreamer for media, WebKitGTK for web views. |
| macOS | AppKit: layer-backed NSViews, TextKit, AVPlayer, WKWebView, NSStatusItem. |
| Windows | One Direct2D-rendered window with DirectWrite text; child windows only for text inputs and WebView2. |
Threading
Section titled “Threading”| Thread | Work |
|---|---|
| UI thread | The platform main loop, the engine, the scene, every controller and every platform call. |
pam-worker-read, pam-worker-write |
Reading and writing the worker channel. |
pam-storage, pam-files, pam-sqlite, pam-secure-storage |
One serial thread each, so results arrive in call order. |
pam-http, pam-image, pam-media-cache and similar |
Bounded pools for network work. |
pam-timers |
Started with the first timer. |
pam-watch |
Development only: watches the project for changes. |
Blocking work (disk, network, SQLite, the keychain, image decoding) never runs on the UI thread. Pool threads start on demand and exit after 30 seconds without work, so an idle pool holds no thread.
Idle and memory rules
Section titled “Idle and memory rules”An application that is doing nothing costs nothing:
- No frame clock while nothing animates. The host asks for frames only while an animation runs.
- One wake-up timer serves every deadline of the core: long press, debounce, media progress.
- PHP sleeps in a blocking read between turns.
- The file watcher exists only in development.
Memory is bounded by design:
- An image is decoded no larger than the first of 128, 256, 512, 1024 or 2048 pixels that covers its box.
- Decoded images no element shows stay in a least-recently-used set of at most 48 images and 96 MiB.
- A media player that is not visible pauses at once and releases its decoder after 5 seconds.
- A list mounts only the rows near its viewport.
- Frames, messages, module payloads and query results have explicit size limits; a value over a limit fails instead of growing a buffer.
Errors
Section titled “Errors”An uncaught PHP error is sent to the host with its phase (boot, render, event, module callback, promise, hot reload), the source around the failing line and the call stack. The host draws the report itself, so it works even when PHP never produced a first frame. What the user sees depends on the mode; see Debugging.
Next steps
Section titled “Next steps”- State & Lifecycle for what triggers a render.
- Performance for measurements and the defaults that keep costs low.
- Security for the trust boundaries this design implies.