Navigation
Navigation in PAM Desktop is a stack of screens drawn and animated by the host. Routes have names (usually an enum), screens receive typed parameters through their constructor, and the host provides the header, the back behavior, form sheets and shared element flights. Navigation, tabs and modals are implemented in the render core rather than with each platform’s navigation controllers, so they behave the same on Linux, macOS and Windows.
Declaring routes
Section titled “Declaring routes”The skeleton declares routes as an enum and builds a stack from them:
<?php
declare(strict_types=1);
namespace App\Navigation;
enum AppRoute: int{ case Inbox = 1; case Message = 2; case Settings = 3;}<?php
declare(strict_types=1);
namespace App\Navigation;
use App\Screens\Inbox\InboxScreen;use App\Screens\Message\MessageScreen;use App\Screens\Settings\SettingsScreen;use Pam\Desktop\Navigation\NavigationPresentation;use Pam\Desktop\Navigation\Navigator;use Pam\Desktop\Navigation\ScreenOptions;use Pam\Desktop\Routing\Route;
final class AppRouter{ public static function make(): Navigator { return Route::stack( name: 'main', initial: AppRoute::Inbox, routes: static function (): void { Route::screen(AppRoute::Inbox, InboxScreen::class) ->options(new ScreenOptions(title: 'Inbox', headerShown: true)); Route::screen(AppRoute::Message, MessageScreen::class); Route::screen(AppRoute::Settings, SettingsScreen::class) ->presentation(NavigationPresentation::FormSheet); }, restoreState: false, ); }}App::components(__DIR__.'/src', __DIR__.'/.pam-desktop/components');App::run(AppRouter::make());Route::stack() takes a name, the initial route, a closure that registers the screens, and optional transition, durationMs (240 by default), restoreState and default options. With restoreState: true (the default) the stack is stored in the state directory and restored at the next launch.
Moving between screens
Section titled “Moving between screens”From a component, use the navigation helpers:
use App\Navigation\AppRoute;
public function open(int $id): void{ $this->pushRoute(AppRoute::Message, id: $id);}
public function settings(): void{ $this->navigateRoute(AppRoute::Settings);}| Method | Effect |
|---|---|
pushRoute($route, ...$params) |
Pushes a new entry. |
navigateRoute($route, ...$params) |
Goes back to the entry when it is already in the stack, otherwise pushes. |
replaceRoute($route, ...$params) |
Replaces the top entry. |
popRoute() |
Goes back one entry. |
The user goes back with Alt+Left on Linux and Windows, Cmd+[ on macOS, or the back button of the mouse.
Screen parameters
Section titled “Screen parameters”Parameters reach the screen through its constructor, by name, and are checked against the declared types. A missing required parameter or an unknown one is an error that names the route:
final class MessageScreen extends Component{ #[State] public ?array $message = null;
public function __construct(public int $id) { }
public function mount(): void { SQLite::query('mail.db', 'SELECT * FROM messages WHERE id = ?', [$this->id], function (array $rows): void { $this->message = $rows[0] ?? null; }); }}Parameters are scalars or null, so pass identifiers and load the data in the screen.
The header
Section titled “The header”With headerShown: true the host draws a bar over the top of the route: the title, a back button when there is a route below, and optionally a search field. The route content is laid out under the bar, so leave room for it at the top of the screen (the examples use padding-top: 72px).
new ScreenOptions( title: 'Search', headerShown: true, headerBackgroundColor: 0xFF2F6BFF, headerTintColor: 0xFFFFFFFF, headerSearchEnabled: true, headerSearchPlaceholder: 'Search messages', onHeaderSearchChange: function (string $query): void { $this->query = $query; },);Form sheets and modal routes
Section titled “Form sheets and modal routes”A route with presentation: NavigationPresentation::FormSheet covers part of the route below, which stays visible. sheetAllowedDetents lists the heights it may rest at, as fractions of the window:
new ScreenOptions( presentation: NavigationPresentation::FormSheet, animation: NavigationTransition::SlideFromBottom, sheetAllowedDetents: [0.45, 1.0],);Route::modal($name, $screen) registers a route presented full screen above the stack. Escape and a click on the backdrop close sheets; the host animates the exit at once.
A navigator built by hand
Section titled “A navigator built by hand”For a small application or a nested flow, build a Navigator directly with closures as routes:
use Pam\Desktop\Navigation\Navigator;use Pam\Desktop\Navigation\ScreenOptions;
final class Mail extends Component{ private Navigator $navigator;
public function mount(): void { $this->navigator = new Navigator( initialRoute: 'inbox', routes: [ 'inbox' => fn (): Renderable => Screen::make(Column::make( Button::make('Open a message')->onPress(fn () => $this->navigator->push('message')), )->style(new Style(flexGrow: 1, paddingTop: 72, paddingHorizontal: 20, gap: 12))), 'message' => fn (): Renderable => Screen::make( Text::make('Invoice 204')->style(new Style(paddingTop: 72, paddingHorizontal: 20)), ), ], restorePersistedState: false, screenOptions: [ 'inbox' => new ScreenOptions(title: 'Inbox', headerShown: true), 'message' => new ScreenOptions(title: 'Invoice 204', headerShown: true), ], ); }
public function render(): Renderable { return $this->navigator->render(); }}push(), pop(), replace() and popToTop() drive it.
A TabNavigator shows one scene per tab and a bar drawn by the host. Only the selected scene is visible; the others stay mounted once opened.
use Pam\Desktop\Navigation\NavigationTab;use Pam\Desktop\Navigation\TabNavigator;
App::run(new TabNavigator('inbox', [ new NavigationTab('inbox', 'Inbox', fn (): Renderable => Screen::make(Text::make('Inbox'))), new NavigationTab('sent', 'Sent', fn (): Renderable => Screen::make(Text::make('Sent')), badge: '3'), new NavigationTab('settings', 'Settings', new SettingsScreen()),]));A tab’s content is an element, a component or a closure. select('sent') changes the tab from code and selectedTab() reads it. With the default TabPresentation::Adaptive the bar is a side rail when the window is at least 840 units wide and a bottom bar otherwise. The tabs take keyboard focus like the rest of the interface.
Shared element transitions
Section titled “Shared element transitions”An element with the same sharedTransition() tag on two routes flies from one to the other during navigation, entirely in the host:
// On the list routeColumn::make(Text::make('Invoice 204')) ->sharedTransition('invoice') ->style(new Style(width: 220, padding: 14, borderRadius: 14, backgroundColor: 0xFF2F6BFF));
// On the detail routeColumn::make(Text::make('Invoice 204')->style(new Style(fontSize: 28))) ->sharedTransition('invoice', SharedTransitionStyle::spring()) ->style(new Style(height: 180, padding: 24, backgroundColor: 0xFF2F6BFF));Between two images, the picture flies and is resized with its corner radius. Between other pairs the host copies both elements, stretches them to the frame in flight and cross-fades them. An element only flies when it is mostly visible and has at most 96 elements inside.
Laravel-style routes
Section titled “Laravel-style routes”The package also accepts a routes.php file with path-based routes, middleware and modals, booted with App::run(require __DIR__.'/routes.php'):
<?php
use App\Screens\ChatScreen;use App\Screens\ChatsScreen;use App\Screens\LoginScreen;use App\Screens\SettingsScreen;use Pam\Desktop\Route;
Route::screen('/login', LoginScreen::class)->name('login');
Route::middleware('auth')->group(function () { Route::screen('/chats', ChatsScreen::class)->name('chats'); Route::screen('/chat/{id}', ChatScreen::class)->name('chat'); Route::modal('/settings', SettingsScreen::class)->name('settings');});With these routes, Pam\Desktop\navigate('chat', ['id' => $id]) and Pam\Desktop\back() move between screens, and pam desktop check reports unknown routes and missing parameters. The examples and the skeleton use Route::stack(); this API is shared with PAM Native.
Next steps
Section titled “Next steps”- Lists for the screens most routes show.
- Windows, Menus & Tray for separate native windows.