A small, beautiful desktop toolkit. One theme restyles the whole
window, widgets place themselves in a column or a row, and everything is
owner-drawn so it looks the same on every machine. No dependencies:
hphp build links the whole Win32 layer into one exe.
import "ui";
$app = new App("My App");
$app->heading("Hello, world");
$app->sub("This is HolyPHP UI 2.0.");
$app->button("Click me", "primary", function() use ($app) {
$app->msg("It works.");
});
$app->run();
No coordinates. The heading, the subtitle and the button stack themselves,
and every one of them follows the same theme.
Themes
A theme is a universal style: colours, corner radius, type size,
row height. Change it at any time — every widget follows immediately.
void theme(string|map $which, map $over = null)
$app->theme("dark"); // dark (default)
$app->theme("light");
$app->theme("midnight");
$app->theme("ocean");
$app->theme("forest");
$app->theme("rose");
$app->theme("mono");
// override single keys, on top of a preset …
$app->theme("dark", ["accent" => 0xFF5722]);
// … or roll a whole theme from scratch
$app->theme(["bg" => 0x101014, "surface" => 0x171A21,
"text" => 0xE8EAF0, "accent" => 0x7C5CFF,
"dark" => 1]);
Six ready-made button styles. Pass one to a factory, or make it the default
for everything that follows.
$app->button("Save"); // subtle (the default)
$app->button("Save", "primary"); // filled with the accent colour
$app->button("Delete", "danger");
$app->button("Done", "success");
$app->button("More", "ghost"); // no fill until hovered
$app->link("Read the docs"); // a text link
$app->style("primary"); // every button from here on
Layout
The window stacks its children top to bottom. To put things side by
side, put them inside a row() — a row lays its children out
across and wraps when it runs out of room. Every container grows to fit what
you put inside it.
Tip:run() blocks like a normal desktop app
(best with hphp build). tick() returns every ~15 ms so your
script keeps control and console output stays alive (best with hphp run).
Menus
$file = $app->menu("File");
$file->item("Open", function() use ($app) {
$f = $app->openFile("Text|*.txt;HolyPHP|*.hphp");
if ($f != "") { $app->msg("Opened " . $f); }
});
$file->separator();
$file->item("Quit", function() use ($app) { $app->close(); });
Two helpers build them: ui_rgb($r, $g, $b) packs channels and
ui_channel($rgb, $i) reads one back.
How it works
The Win32 layer lives in the runtime (hphp_ui.c): a handle table, a
window proc that routes events to HolyPHP closures, and guarded callback invocation —
an exception inside a handler prints and keeps the app alive instead of unwinding
through C. Every control is owner-drawn from one paint routine, so the theme is the
single source of truth. Callbacks run on the GUI thread; long work belongs in a
timer.
Tip:onClick() / onChange() take a
closure directly — $btn->onClick(function() { ... }) — and capture
anything with use.
Examples
5 complete programs. Every one is type-checked with the compiler before the site is published; copy one into src/ of a project (hphp create) and run it.
01Hello, window
The smallest complete desktop app. new App() opens a themed window, and widgets such as heading(), sub() and button() stack themselves top to bottom - no coordinates needed. A button takes a role (primary, danger, ghost, ...) and a closure to run when it is clicked.
01-hello-window.hphp
<?php
import "ui";
// A 480 x 300 window with the default dark theme.
$app = new App("Hello", 480, 300);
// Text: a big heading and a muted subtitle.
$app->heading("Hello, world");
$app->sub("This window was written in HolyPHP.");
// A button that opens a message box when clicked.
$app->button("Say hello", "primary", function() use ($app) {
$app->msg("Hello from HolyPHP!", "Greetings");
});
// A second button that asks before closing the window.
$app->button("Quit", "ghost", function() use ($app) {
if ($app->ask("Really quit?")) {
$app->close();
}
});
// Hand control to the window until it is closed.
$app->run();
Try it
hphp run 01-hello-window.hphp
02A counter with buttons
State in a GUI program is just a variable that the click handlers share. The handlers capture it by reference (use (&$count)) so every button sees the same number, and text() updates the label. Buttons sit side by side inside a row(), and the minus button is disabled at zero.
02-counter.hphp
<?php
import "ui";
$app = new App("Counter", 360, 260);
$app->theme("ocean");
$count = 0;
// The number itself, shown as a heading.
$app->sub("Clicks so far");
$display = $app->heading("0");
// Three buttons next to each other.
$buttons = $app->row();
$minus = $buttons->button("-1");
$plus = $buttons->button("+1", "primary");
$reset = $buttons->button("Reset", "ghost");
// Redraw the label and enable or disable "-1" to match the count.
$refresh = function() use (&$count, $display, $minus) {
$display->text("$count");
$minus->enable($count > 0);
};
$refresh();
// Each handler changes the shared count, then refreshes the view.
$plus->onClick(function() use (&$count, $refresh) {
$count = $count + 1;
$refresh();
});
$minus->onClick(function() use (&$count, $refresh) {
$count = $count - 1;
$refresh();
});
$reset->onClick(function() use (&$count, $refresh) {
$count = 0;
$refresh();
});
$app->run();
Try it
hphp run 02-counter.hphp
03A sign-up form with validation
A small form inside a card(): text inputs, a combo() drop-down and a checkbox(). Clicking "Create account" reads every field with content(), selectedText() and checked(), validates them, and shows either the first problem in a red label or a summary in a message box.
03-signup-form.hphp
<?php
import "ui";
$app = new App("Create account", 460, 560);
$app->theme("light");
$app->heading("Create your account");
// The form fields, inside a rounded card.
$form = $app->card(400);
$form->label("Name");
$name = $form->input("");
$form->label("Email");
$email = $form->input("");
$form->label("Plan");
$plan = $form->combo(["Free", "Pro", "Team"]);
$plan->select(0);
$terms = $form->checkbox("I accept the terms");
// Where validation errors appear (empty until needed).
$errors = $form->label("")->color(0xD32F2F);
// Read the fields, find the first problem, then report.
$submit = function() use ($app, $name, $email, $plan, $terms, $errors) {
$problem = "";
if (trim($name->content()) == "") {
$problem = "Please enter your name.";
} elseif (!str_contains($email->content(), "@")) {
$problem = "That email address looks wrong.";
} elseif (!$terms->checked()) {
$problem = "You need to accept the terms.";
}
$errors->text($problem);
if ($problem != "") {
return;
}
$app->msg("Welcome, " . trim($name->content()) . "!\n"
. "Plan: " . $plan->selectedText() . "\n"
. "We sent a confirmation to " . $email->content() . ".", "Account created");
};
$form->button("Create account", "primary", $submit);
// Pressing Enter in the email field submits too (13 = the Enter key).
$email->onKey(function($key, $char, $mods) use ($submit) {
if ($key == 13) {
$submit();
}
});
$app->run();
Try it
hphp run 03-signup-form.hphp
04A to-do list with a menu
A list() box holds the tasks. Type a task and press Enter or click "Add"; select one and click "Done" to remove it. A menu bar adds File > Save, which writes the tasks to a file picked with saveFile().
04-todo-list.hphp
<?php
import "ui";
$app = new App("To-do", 420, 480);
$app->heading("Things to do");
// Input and "Add" button side by side.
$entry = $app->row();
$newTask = $entry->input("");
$add = $entry->button("Add", "primary");
// The list, the "Done" button and a status line.
$tasks = $app->list(["Water the plants", "Write the report"]);
$done = $app->button("Done", "success");
$status = $app->sub("");
$updateStatus = function() use ($tasks, $status) {
$status->text($tasks->count() . " task(s) left");
};
$updateStatus();
// Add the typed task, then clear the input for the next one.
$addTask = function() use ($newTask, $tasks, $updateStatus) {
$text = trim($newTask->content());
if ($text == "") {
return;
}
$tasks->add($text);
$newTask->text("");
$updateStatus();
};
$add->onClick($addTask);
$newTask->onKey(function($key, $char, $mods) use ($addTask) {
if ($key == 13) {
$addTask();
}
});
// Remove the selected task (selected() is -1 when nothing is selected).
$done->onClick(function() use ($tasks, $updateStatus) {
$index = $tasks->selected();
if ($index >= 0) {
$tasks->remove($index);
$updateStatus();
}
});
// File menu: save the list to disk, or quit.
$file = $app->menu("File");
$file->item("Save...", function() use ($app, $tasks) {
$path = $app->saveFile("Text files|*.txt");
if ($path != "") {
file_put_contents($path, implode("\n", $tasks->items()) . "\n");
$app->msg("Saved " . $tasks->count() . " tasks to " . $path);
}
});
$file->item("Quit", function() use ($app) {
$app->close();
});
$app->run();
Try it
hphp run 04-todo-list.hphp
05A focus timer with themes
A Pomodoro timer: a slider() sets the minutes, every() ticks once a second, a progress() bar fills up, and a combo() of ui_theme_names() restyles the whole window live with theme().
05-focus-timer.hphp
<?php
import "ui";
$app = new App("Focus timer", 420, 420);
// Settings: session length (1-60 minutes) and theme.
$settings = $app->section("Settings", 360);
$length = $settings->slider(25);
$length->range(1, 60);
$themes = $settings->combo(ui_theme_names());
// The clock: time left, a progress bar and the start button.
$clock = $app->heading("25:00");
$bar = $app->progress(0);
$startStop = $app->button("Start", "primary");
// Timer state, shared by the handlers below.
$total = 25 * 60;
$left = $total;
$running = false;
// Show the remaining time as mm:ss and fill the bar as time passes.
$show = function() use (&$total, &$left, $clock, $bar) {
$clock->text(sprintf("%02d:%02d", intdiv($left, 60), $left % 60));
$bar->progress(intdiv(($total - $left) * 100, $total));
};
// Moving the slider resets the timer to the new length.
$length->onChange(function($minutes) use (&$total, &$left, $show) {
$total = $minutes * 60;
$left = $total;
$show();
});
// Picking a theme restyles every widget at once.
$themes->onChange(function($index, $name) use ($app) {
$app->theme($name);
});
// Start / pause toggles one flag; the timer below checks it.
$startStop->onClick(function() use (&$running, $startStop) {
$running = !$running;
$startStop->text($running ? "Pause" : "Start");
});
// Once a second: count down while running; at zero, announce and reset.
$app->every(1000, function() use ($app, &$running, &$left, &$total, $startStop, $show) {
if (!$running) {
return;
}
$left = $left - 1;
if ($left == 0) {
$running = false;
$startStop->text("Start");
$app->msg("Time is up - take a break!");
$left = $total;
}
$show();
});
$app->run();