Skip to content

Windows, Menus & Tray

The shell is what makes a window a desktop application: window control, more windows, the menu bar, context menus, the tray, shortcuts, file dialogs, notifications, drag and drop and the clipboard. All of it is native to each platform and driven from PHP. The classes live in Pam\Desktop unless noted, and calls are usually made in a component’s mount().

Pam\Desktop\Window is a facade for the main window. Its initial title and size come from pam-desktop.json.

use Pam\Desktop\Window;
use Pam\Desktop\WindowState;
public function mount(): void
{
Window::title('Invoices — March');
Window::minimumSize(640, 480);
Window::onStateChange(function (WindowState $state): void {
$this->maximized = $state->maximized;
});
Window::onCloseRequested(function (): void {
if ($this->unsaved) {
$this->confirmClose = true; // show your own dialog
return;
}
Window::close();
});
}
Method Effect
title(string), resize(w, h), minimumSize(w, h), resizable(bool) Title and size. minimumSize(null) removes the limit.
minimize(), maximize(), restore(), fullScreen(bool) Window state.
focus(), show(), hide() hide() keeps the application running; pair it with a tray icon.
center(), alwaysOnTop(bool) Placement and stacking. Work on X11; do nothing on Wayland.
progress(?float), badge(?int) Progress and a count on the taskbar, dock or launcher icon.
close() Closes the window and ends the application, without asking.
onCloseRequested(?Closure) Decide what the close button does; call Window::close() to really close.
onStateChange(Closure), state(Closure) Maximized, minimized, focused, full screen and size.
onFilesDrop(Closure) Files dropped on the window outside any element that handles them.

On Linux, progress() and badge() reach docks that listen to the Unity launcher API (Ubuntu Dock, Dash to Dock, Plasma) for an installed application. On macOS they are the Dock badge, and on Windows the taskbar button.

A secondary window is an element. Pam\Desktop\UI\Window opens a native window while it is in the tree and closes it when it leaves. Its children belong to the component that renders them, so they share its state:

use Pam\Desktop\UI\Window as WindowElement;
Column::make(
Button::make('Inspector')->onPress(fn () => $this->inspector = !$this->inspector),
$this->inspector
? WindowElement::make(
Column::make(
Text::make("The counter is {$this->count}."),
Button::make('Add one')->onPress(fn () => $this->count++),
)->style(new Style(flexGrow: 1, padding: 24, gap: 12)),
)
->title('Inspector')
->size(360, 260)
->onClose(fn () => $this->inspector = false)
: null,
);

title(), size(), minimumSize(), resizable(), modal() (blocks the main window, like a dialog) and open(false) (hide without unmounting) configure it. onClose only reports the request: clear the state that renders the window.

The element and the main window facade share the short name Window; import one under an alias.

App::menu() sets the application menu. On macOS it is the system menu bar; on Windows and Linux it sits at the top of the main window.

use Pam\Desktop\App;
use Pam\Desktop\Menu;
use Pam\Desktop\MenuItem;
use Pam\Desktop\MenuRole;
private function installMenu(): void
{
App::menu(
Menu::make(
'File',
MenuItem::make('New document', $this->create(...), 'CmdOrCtrl+N'),
MenuItem::make('Open…', $this->open(...), 'CmdOrCtrl+O'),
MenuItem::separator(),
MenuItem::role(MenuRole::Quit),
),
Menu::edit(),
Menu::make(
'View',
MenuItem::checkbox('Sidebar', $this->sidebar, function (bool $visible): void {
$this->sidebar = $visible;
$this->installMenu(); // menus are values: install again to update the check mark
}, 'CmdOrCtrl+B'),
MenuItem::role(MenuRole::ToggleFullScreen),
),
Menu::window(),
);
}
Item Built with
Action MenuItem::make($label, $handler, $shortcut, enabled: true, destructive: false)
Check item MenuItem::checkbox($label, $checked, $handler, $shortcut)
Platform item MenuItem::role(MenuRole::Copy)
Separator MenuItem::separator()
Submenu MenuItem::submenu($label, ...$items)

Menu::edit() and Menu::window() are the standard Edit and Window menus. Roles (Quit, About, Preferences, Undo, Redo, Cut, Copy, Paste, SelectAll, Minimize, Zoom, ToggleFullScreen, CloseWindow, Hide, HideOthers, ShowAll) are implemented by the system with conventional labels and shortcuts, which you may override: MenuItem::role(MenuRole::Quit, 'Exit').

contextMenu() exists on every element. The menu opens on a right click, or with the menu key and Shift+F10 on the focused element:

Pressable::make(Text::make($name))
->contextMenu(
MenuItem::make('Open', fn () => $this->select($index)),
MenuItem::separator(),
MenuItem::make('Delete', fn () => $this->remove($index), destructive: true),
);

In a template, :contextMenu takes a list of MenuItem, usually from a method:

/** @return list<MenuItem> */
public function menu(Message $message): array
{
return [
MenuItem::make('Edit message', fn () => $this->edit($message), shortcut: 'E'),
MenuItem::separator(),
MenuItem::make('Delete message', fn () => $this->delete($message), destructive: true),
];
}
<Pressable p-for="$message in $messages" p-key="$message->id" :contextMenu="menu($message)">…</Pressable>

A shortcut on a context menu item only labels it; register the key itself with Shortcut::register() or the menu bar.

use Pam\Desktop\Tray;
Tray::show(
Menu::make(
'Tray',
MenuItem::make('Show window', fn () => Window::focus()),
MenuItem::separator(),
MenuItem::role(MenuRole::Quit),
),
'Acme Sync',
onActivate: fn () => Window::focus(),
);

Tray::show($menu, $tooltip, $icon, $onActivate) shows or updates the icon ($icon is a PNG path; the application icon by default), and Tray::hide() removes it.

A StatusNotifierItem on the session bus. The desktop must host the protocol: KDE, Cinnamon and XFCE do; GNOME needs the AppIndicator extension. Without a host, Tray::show() still succeeds and the icon appears if a host starts later.

use Pam\Desktop\Shortcut;
$id = Shortcut::register('CmdOrCtrl+K', fn () => $this->searching = true);
Shortcut::register('Ctrl+Alt+P', $this->toggle(...), global: true);
Shortcut::unregister($id);

A local shortcut fires while the application has the focus, on keys no text control consumed. A global shortcut (global: true) also fires while another application has the focus. Combinations join modifiers and one key with +: CmdOrCtrl (Command on macOS, Control elsewhere), Ctrl, Alt, Shift, Cmd, and a key such as K, F5, Enter, Escape, ArrowUp or Comma.

On Linux, global shortcuts are X11 key grabs (verified) or go through the GlobalShortcuts desktop portal on Wayland, with the user’s consent (not verified).

Dialog opens the system’s file dialogs and hands back absolute paths; a cancelled dialog reports null or an empty list:

use Pam\Desktop\Dialog;
use Pam\Desktop\FileFilter;
Dialog::openFiles(function (array $paths): void {
foreach ($paths as $path) {
$this->documents[] = basename($path);
}
}, 'Open documents', [new FileFilter('Documents', 'md', 'txt'), FileFilter::images(), FileFilter::all()]);
Dialog::saveFile(function (?string $path): void {
if ($path !== null) {
file_put_contents($path, $this->export());
}
}, 'report.md', 'Export document');

openFile(), openFiles(), saveFile() and selectFolder() are available. These give your PHP real paths to read and write with its own functions. To copy a user’s file into the application’s private sandbox instead, use Files::pick(); to write a file the user chooses without handling paths, use Files::save() (see Storage & Files).

use Pam\Desktop\Notification;
Notification::show('Export finished', 'report.pdf is ready', fn () => Window::focus(), [
'Open folder' => fn () => $this->reveal(),
]);

The third argument runs when the user clicks the notification, and the fourth adds up to three buttons as label => handler. show() returns the notification id. On Linux, buttons appear where the notification server supports actions (GNOME, KDE and most others).

onFilesDrop() exists on every element and receives the absolute paths of files dropped from the file manager:

Column::make(/* … */)->onFilesDrop(fn (array $paths) => $this->import($paths));

DragSource and DropTarget move a typed payload between elements, such as a card between columns of a board:

use Pam\Desktop\DragPayload;
use Pam\Desktop\UI\DragSource;
use Pam\Desktop\UI\DropTarget;
enum Drag: int { case Card = 1; }
DropTarget::make(...$cards)
->accepts(Drag::Card)
->onDragEnter(fn (DragPayload $drag) => $this->over = 'done')
->onDragLeave(fn (DragPayload $drag) => $this->over = null)
->onDrop(fn (DragPayload $drag) => $this->move($drag->data, 'done'));
DragSource::make(Text::make($card->title))
->payload(Drag::Card, $card->id)
->onDragStart(fn (DragPayload $drag) => $this->dragging = $drag->data)
->onDragEnd(fn () => $this->dragging = null);
<DropTarget dropType="1" on:drop="drop" on:dragEnter="enter" on:dragLeave="leave">
<DragSource dragType="1" :dragData="$card->id">…</DragSource>
</DropTarget>

The type is an integer from 1 to 65535 or an int-backed enum; the data is a string of at most 512 bytes. Holding a drag near the edge of a scroll view scrolls it. Verified on Linux. For plain text that may also cross into other applications, use InteractionRegion with draggable($text) and acceptsDrop().

windowDrag() makes an element act as a title bar; controls inside keep working:

Row::make(Text::make('Acme'))->windowDrag()->style(new Style(height: 40, paddingHorizontal: 16, alignItems: Align::Center));
use Pam\Desktop\System\Clipboard;
Clipboard::setText('Copied text', fn (bool $copied) => $this->copied = $copied);
Clipboard::getText(fn (?string $text) => $this->pasted = $text ?? '');

setText() accepts up to one megabyte. getText() reports null when the clipboard holds no text; hasText() reports a boolean.

use Pam\Desktop\Platform;
$modifier = Platform::select(['macos' => 'Cmd', 'default' => 'Ctrl']);
if (Platform::isLinux()) {
// …
}

Platform::os() returns linux, macos or windows.