Skip to content

Media & Web

Images, media, web pages and drawings are elements like any other, backed by the platform: images are decoded by gdk-pixbuf, ImageIO or WIC off the UI thread; media plays through GStreamer, AVFoundation or Media Foundation; web pages use WebKitGTK, WKWebView or WebView2. The host does the heavy lifting (downloads, caching, decoding, playback, visibility) so PHP only declares what should be shown.

use Pam\Desktop\ImageErrorEvent;
use Pam\Desktop\ImageFit;
use Pam\Desktop\ImageLoadEvent;
use Pam\Desktop\UI\Image;
Image::make('https://example.com/photos/1015.jpg')
->fit(ImageFit::Cover)
->defaultSource('asset://assets/placeholder.png')
->onLoad(fn (ImageLoadEvent $event) => $this->status = "Loaded {$event->width}×{$event->height}")
->onError(fn (ImageErrorEvent $event) => $this->status = 'Error: '.$event->message)
->style(new Style(width: 200, height: 130, borderRadius: 14, backgroundColor: 0xFFE8ECF3));
Source Resolves to
https://… Downloaded into the image cache. Plain http:// only in pam desktop dev.
asset://path or a relative path A file of the project.
pam-file:///path A file of the application’s sandbox; FileReference::uri() builds it.
An absolute path or file:// URL That file, as handed out by file dialogs and drops.
data:image/… Inline bytes, up to 8 MiB.

PNG, JPEG and GIF decode everywhere, WebP where the system has its codec. SVG decodes on Linux with librsvg2-common installed and on macOS 10.15 or newer, but not on Windows.

fit() takes ImageFit::Cover (default), Contain, Fill, Center or Repeat. defaultSource() shows while loading and on failure, loadingIndicatorSource() only while loading, fadeDuration() controls the fade-in, tint() recolors every opaque pixel (icons), and overlayColor() paints over the image. When the source changes, the previous picture stays on screen until the new one is decoded.

Remote images are cached in pam-images-v1/ of the cache directory, one file per URL. cache() takes an ImageCachePolicy:

Policy Behavior
Default, ForceCache Use the cached file when there is one; otherwise download and keep it.
Reload Download again and replace the cached file.
OnlyIfCached Never touch the network.
None Download every time; keep nothing on disk.

headers([...]) adds request headers. Image::prefetch($urls) warms the cache before a screen shows. Decoded images are shared between elements that show the same source at a similar size, and up to 48 images or 96 MiB no element shows are kept for quick scroll-back.

ImageBackground::make($source, ...$children) lays children over a picture and takes the same methods.

use Pam\Desktop\MediaReadyEvent;
use Pam\Desktop\UI\MediaPlayer;
MediaPlayer::make('asset://assets/clip.mp4')
->thumbnail('asset://assets/poster.jpg')
->autoPlay($this->playing)
->muted($this->muted)
->loop($this->loop)
->fit(ImageFit::Contain)
->nativeRef('clip')
->onReady(fn (MediaReadyEvent $event) => $this->duration = $event->duration)
->onEnd(fn () => $this->playing = false)
->onError(fn (string $message) => $this->status = 'Error: '.$message)
->style(new Style(height: 380, borderRadius: 18, backgroundColor: 0xFF12161C));

MediaPlayer::make($source, MediaType::Audio) plays audio. autoPlay($bool) is the play intent, not a command: keep it in state and flip it to play or pause. If the user pauses with the native controls, an unrelated render does not restart playback.

Method Meaning
controls(bool) The platform’s transport controls; on by default.
loop, muted, volume(0..1), playbackRate(0.25..4) Applied live.
currentTime($seconds) Seeks when the value changes.
thumbnail($source) A poster until the first frame.
onLoadStart, onReady, onBuffering, onProgress, onEnd, onError Events; onProgress fires about four times a second while playing.

A player that is not visible (a covered route, an inactive tab, a hidden box) pauses at once and releases its decoder after five seconds; it resumes at the same position when shown again. keepAliveWhenCovered() opts out, for audio that must continue under another screen. autoPlayWhenVisible($fraction, $group) lets the host play a player while at least $fraction of it is on screen, one player per group at a time, with no PHP involved.

A time label and a progress bar need no onProgress handler. Name the player with nativeRef() and bind other elements to it; the host updates them while PHP sleeps:

Row::make(
Text::make('0:00')
->mediaClock('clip', MediaClockFormat::ElapsedAndDuration)
->style(new Style(fontSize: 13, width: 96)),
Column::make(
Column::make()->mediaProgress('clip')->style(new Style(height: 6, borderRadius: 3, backgroundColor: 0xFF2F6BFF)),
)->style(new Style(flexGrow: 1, height: 6, borderRadius: 3, backgroundColor: 0xFFE4E7EC, overflow: Overflow::Hidden)),
)->style(new Style(gap: 12, alignItems: Align::Center));

Remote media streams by default. cache(MediaCachePolicy::Disk) (also MemoryAndDisk, CacheFirst, NetworkFirst, CacheOnly, StaleWhileRevalidate) stores files in pam-media-v1/. streamingCache() plays while downloading, maxCacheSize($bytes) bounds the cache, pinOffline() keeps a file when the cache is trimmed, and onCacheHit, onCacheMiss, onCacheProgress and onCacheReady report what happened.

On Linux, playing common formats needs gstreamer1.0-plugins-good.

WebView embeds a page rendered by the system’s web engine. It is an element like any other; it is not how PAM Desktop renders your application.

use Pam\Desktop\UI\WebView;
WebView::make('https://docs.example.com/')
->allowedHosts('docs.example.com')
->onLoad(fn () => $this->status = 'Page loaded')
->onError(fn (string $message) => $this->status = 'Error: '.$message)
->style(new Style(flexGrow: 1, borderRadius: 16));

The source is an https, http or file URL, or inline HTML (a string that starts with <) up to one mebibyte, loaded with an about:blank origin. On Linux, WebKitGTK 6 is loaded the first time a WebView is shown; a missing library is reported through onError.

A page sends text to PHP with window.PamNative.postMessage(text), received by onMessage with the page URL. PHP talks to the page with injectedJavaScript(), evaluated after every load and again whenever its text changes:

WebView::make(<<<'HTML'
<!doctype html>
<button onclick="PamNative.postMessage('clicked')">Send a message to PHP</button>
<script>window.setCount = function (value) { document.title = value; };</script>
HTML)
->injectedJavaScript("window.setCount({$this->count});")
->onMessage(function (string $message, string $url): void {
$this->status = "Message: {$message}";
});

The bridge exists in the main frame only, and a message holds up to 1,048,576 characters.

javaScriptEnabled(), domStorageEnabled(), userAgent(), allowsInlineMedia() and scrollEnabled() configure the page. A page that neither finishes nor fails within 30 seconds is stopped and reported.

Canvas draws vector primitives from PHP, in the element’s logical units:

use Pam\Desktop\UI\Canvas;
$chart = Canvas::make()->roundedRectangle(0, 0, 380, 200, 16, 0xFF12161C);
foreach ($this->bars as $index => $value) {
$x = 32 + $index * 48;
$chart = $chart->roundedRectangle($x, 180 - $value * 1.6, 28, $value * 1.6, 8, 0xFF2F6BFF);
}
$chart->line(20, 180, 360, 180, 2, 0xFF8A93A3)->style(new Style(width: 380, height: 200));

rectangle(), roundedRectangle(), circle() and line() are available, up to 10,000 commands. Paths, gradients and text are drawn through the bundled Pam\Desktop\Canvas library, which the charts use.

DrawingCanvas is a freehand surface over a picture. Strokes are a JSON document kept in PHP state:

use Pam\Desktop\DrawingMode;
use Pam\Desktop\UI\DrawingCanvas;
DrawingCanvas::make('asset://assets/photo.jpg', $this->drawing)
->brush($this->color, 6, $this->eraser ? DrawingMode::Eraser : DrawingMode::Brush)
->undoRequest($this->undo)
->clearRequest($this->clear)
->onChange(fn (string $drawing) => $this->drawing = $drawing)
->cursor('crosshair')
->style(new Style(width: 400, height: 300, borderRadius: 16));

Strokes are stored relative to the picture, so a drawing shows correctly at any size. The document keeps the last 256 strokes.