Windows¶
Every PRISM app starts with one window. This page shows how to open it, how to size and decorate it, and how to open more windows while the app runs.
The main window¶
app::run opens the main window, shows your model in it, and returns when the user closes it:
#include <prism/prism.hpp>
using namespace prism;
struct Hello {
core::Field<std::string> greeting{"Hello, ada"};
};
int main() {
Hello model;
return app::run(model, {.title = "Hello", .width = 400, .height = 120});
}
The second argument is an app::AppConfig. Every member has a default, so name only the ones
you need:
Member |
Default |
Meaning |
|---|---|---|
|
|
The window’s title. |
|
|
The window’s size, in pixels. |
|
|
Who draws the title bar (see below). |
|
none |
A struct of fields saved between runs (Settings). |
|
empty |
The settings folder’s parent. |
|
the title, dashed |
The settings folder’s own name ( |
app::run takes an optional third argument: a function run once on the logic thread, before
the first frame. It receives the app’s AppContext, which opens windows, changes the theme and
more (The app context):
app::run(model, {.title = "Hello"}, [](app::AppContext& ctx) {
ctx.follow_system_theme();
});
app::run always returns 0, so return app::run(...) ends main cleanly.
Title bar and borders¶
decoration picks who draws the window’s frame:
|
What you get |
|---|---|
|
PRISM draws the title bar, its buttons and the resize edges. It looks the same on every system. |
|
The system’s window manager draws them. |
|
No frame at all: a borderless window. |
app::run(model, {.title = "Kiosk", .width = 1024, .height = 768,
.decoration = app::DecorationMode::None});
Warning
On Wayland, many desktops draw no frame for an app. There, Native gives a window with no
title bar and no close button. That is why Custom is the default.
More windows¶
A second window shows a model of its own. Open it from the logic thread, with
ctx.open_window(model, config):
struct Editor {
core::Field<ui::TextField<>> text{{.value = "Some text"}};
void view(app::ViewBuilder& vb) { vb.widget(text); }
};
struct Stats {
Editor* editor;
core::Derived<std::string> summary{[this] {
return std::to_string(editor->text.get().value.size()) + " characters";
}};
void view(app::ViewBuilder& vb) { vb.widget(summary); }
};
int main() {
Editor editor;
Stats stats{&editor};
return app::run(editor, {.title = "Editor"}, [&](app::AppContext& ctx) {
ctx.open_window(stats, {.title = "Statistics", .width = 320, .height = 80});
});
}
The second model reads the first one’s field, so its summary follows every edit. Both windows share the logic thread, so they never see each other half-changed.
The second argument is an app::WindowConfig:
Member |
Default |
Meaning |
|---|---|---|
|
|
The title, as a C string. |
|
|
The size, in pixels. |
|
|
Whether the user may resize it. |
|
|
Open in full screen. |
|
|
As above. |
|
none |
Where on the desktop. None lets the system choose. Wayland always chooses. |
|
|
Open maximized. |
The window’s lifetime:
Call |
What it does |
|---|---|
|
Opens the window. Returns its id, or 0 if no window could open. |
|
Whether it is still open. |
|
Closes it from code. Not the main window: closing that ends the app. |
When the user closes a second window, it goes and the app goes on. The model must outlive the
window: keep it in main or in the main model.
Note
In the browser there is one canvas, so open_window returns 0 there. A dock’s floating panel
becomes a card over the dock instead (Docking).
Choosing the renderer¶
By default PRISM draws with SDL’s renderer. A build with -Dwebgpu=enabled adds an experimental
WebGPU renderer. The environment variable PRISM_RENDERER picks one for any app, with no code
change:
PRISM_RENDERER=webgpu ./builddir/examples/model_notes/model_notes
In code, make the backend yourself and hand it to app::model_app, the call app::run is built
on:
auto backend = app::Backend::software({.renderer = app::Renderer::WebGpu});
auto& window = backend.create_window({.title = "Notes", .width = 900, .height = 600});
app::model_app(backend, window, model);
app::RenderConfig also sets the fonts and the frame rate:
Member |
Default |
Meaning |
|---|---|---|
|
|
|
|
bundled Noto Sans |
The UI font file. |
|
bundled JetBrains Mono |
The font for code and text areas. |
|
|
0 follows the display’s refresh rate. A number caps the frames per second. |
|
|
Present on frame boundaries, not after every batch of events. |
Pitfalls¶
open_windowandclose_windowbelong to the logic thread. From a worker, hand the call over withctx.poster()(The app context).The model shown in a window must outlive it. A model built on the stack of a callback is gone when the callback returns.
WindowConfig::titleis a C string: it must stay valid until the window is open.