|
Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
|
Русская версия · User guides · Glossary
This tutorial develops a small note form in four steps. The finished application has an editor, Save and Clear draft buttons, keyboard shortcuts and a theme. Save copies the current editor text to a line below it; Clear draft clears the editor while leaving that saved line unchanged. The note lasts until the application exits, because this example does not write a file.
The animation shows the finished application: F2 saves the initial text, F3 clears the editor, a new note is entered, and F2 saves it. We will start with a form containing an editor, then add button actions, shortcuts and styling.
You need Git, CMake 3.24+, Ninja and a toolchain supporting C99 and C++23, including <expected> and <format>. On Windows, an x64 Developer PowerShell for Visual Studio is required. Integration details are in the short guide.
For the first version, you can create a quick-note directory and save this code as main.cpp:
Here, ui::vbox describes a vertical arrangement of controls. It is a recipe, which ui::scene stores in a scene. ui::Ref<LineEdit> draft provides access to the editor created from that recipe. When the program runs, you can edit Remember the milk; F10 exits the application.
The second file, CMakeLists.txt, connects this application to Portals:
From quick-note, these commands configure, build and run the application:
On Windows, the executable is build/quick_note.exe; it can be run in a terminal. During the first configuration, CMake downloads the pinned Portals revision. An existing local checkout can be used by adding -DFETCHCONTENT_SOURCE_DIR_PORTALS="C:/path/to/portals" to the configure command, substituting your path.
The first version has an editor, but no place to see a saved copy. After exiting the application, continue in the same main.cpp from step 1. The label and button need references of their own, placed after ui::Ref<LineEdit> draft;:
Inside ui::vbox(...), the label and Save button follow the editor, with the hint label at the end:
The saveNote action copies the editor text into the label. It belongs between the scene declaration and return scene; the ui::onClick stage connects it to the button before the existing F10 handler:
When Save is activated, saveNote reads the editor and assigns its current text to the label. The | operator attaches a UI stage to the scene, and ui::onClick connects the button to an action. The handler captures references by value; they observe the scene's controls and do not extend their lifetime. See control references for details.
The build command from step 1 prepares this version. You can then run it with the same ./build/quick_note command. While the application is running, the text can be edited; Tab moves focus to Save, and Enter activates the focused button. The saved copy appears below the editor. Further edits leave it unchanged until Save is activated again.
The form can now save a copy. The next version adds a separate way to clear the editor and shortcuts for both actions. After exiting the application, the next changes go into main.cpp from step 2. A reference for the new button follows ui::Ref<Button> save;:
Inside ui::vbox(...), the Clear draft button follows Save. The final hint label also lists the new shortcuts; the scene declaration becomes:
The clearDraft action clears the editor. After the existing saveNote action, it can be connected to the new button and F3; F2 can call the existing save action. The code from clearDraft through the end of the handler chain is:
clearDraft clears only the editor; the saved line stays on screen. The Clear draft button and F3 invoke the same action. The Save button and F2 invoke the other action, saveNote, created in the previous step.
After rebuilding and running, F2 saves the current text and F3 clears the editor. The saved line remains visible when the draft is cleared. New text can replace that line with another F2 press. Tab moves focus between the editor and buttons; Enter activates the focused button. The input guide describes other shortcut forms.
The final version adds a palette and a rule that changes button appearance. After exiting the application, the styling header can follow #include <portals/ui.hpp> in main.cpp from step 3:
The theme and style stages can precede the existing event handlers in the chain:
ui::theme(nordTheme()) selects a palette. The PSS rule makes button text bold and sets a minimum width of 16 terminal cells. The references, form and handlers from step 3 remain part of the completed program. Styles and themes describe the available options.
After building with the same command, ./build/quick_note runs this version with its theme and button styling.
The image shows output from the final program in a 64 × 10 in-memory terminal after Save, using the Nord theme. It is displayed through xterm.js; your terminal's font and colors may differ.
If startup fails, ui::runApp() reports the error to stderr after attempting terminal cleanup and returns a nonzero exit code. Description construction before runApp() follows ordinary C++ exception rules; use the factory overload to include that step in the error boundary. Diagnostics covers default and custom reports.
Further topics include shared state and layout. Other practical tasks are covered in the user guides.