GUI Panel Operations
What you build
A small program that puts a web panel into the shared QAROS room. It subscribes to the panel's updates, then moves, resizes, minimizes and navigates the panel, and prints each distinct update the subscription delivers. It then lists every panel in the session and closes its own.
What you will learn:
- how a panel's common name makes creation idempotent,
- how to subscribe to one panel's updates, and why the callback needs a lock,
- why an update tells you that the panel changed, not what you should apply,
- when it is safe to end a subscription and free the state its callback uses,
- which handles you destroy, and in which order.
Before you start
- Windows, with QAROS installed on the Hub machine. The program runs on the Hub machine. Joining as a local app starts the Hub when it is not running.
- Joining. The program joins the Hub as a local app: click Allow in the QAROS approval prompt on the first run of the executable. Nothing is stored, and later runs join without a prompt while the executable is unchanged.
- The examples built, with the repository's CMake presets (MSVC).
Run it
From the repository root, in a Developer PowerShell for Visual Studio:
The program loads the runtime of the installed QAROS. To load another one, such as a development build, add --qaros-library=PATH with the path of its qar-streaming-c.dll. Any other argument prints the usage and exits with 1.
Expected output after the join messages (<...> stands for values of your own run; the library's own log lines are left out):
In the room you see the panel appear, shrink and minimize, then disappear when the program closes it.
When no update arrives within 10 s of the changes, the line No update of the panel arrived within 10 s of the changes. replaces the Update: lines, the listing is left out, and the program exits with 1.
The Hub delivers updates on its own schedule and reports your own changes back to you, often several times. A few changes can arrive as dozens of updates, most of them the same to the millimeter, and an update can carry an older state than the one you set. That is why the program prints only an update that differs from the last one it printed, and why the Update: lines need not match the calls one to one. When no update repeated another, the Received line ends after printed <m>.
Exit codes: 0 when every step succeeded; 1 for an unknown argument, when a call failed (the line before names it and its status), or when no update of the panel arrived within 10 s of the changes; 2 when no runtime loaded; 3 to 7 for a refused local-app join (the program prints what to do): 3 when it could not start or reach the Hub, 4 when the approval was denied, 5 when nobody answered in time, 6 when the program answering is not the installed QAROS, 7 when the executable no longer matches its approval.
Walkthrough
1. Get a session
A panel is shared session state: every peer sees the same panel at the same place. So everything starts with a session. The program loads the runtime, creates a runtime handle and joins with the helpers of common.h: example_join_session joins the Hub on this machine as a local app with the program's app tag.
The Local App Onboarding tutorial explains the local-app join, its approval prompt and its app tag.
2. Get or create the panel
A panel is identified by its common name, not by a handle. qar_gui_panels_get_or_create() returns the panel with that name and creates it the first time, so the same call is safe on every start: a restarted app reattaches to its panel instead of adding a second one. The id derives from the name. Use a DNS-style, app-scoped name and keep it stable across versions.
The call does not say whether it created the panel or found it. This program closes its panel at the end, so a normal run creates it; after a run that ended early, it finds the one left behind.
The pose is the midpoint of the panel's top edge in room space, not its center; the surface hangs below it (Coordinate Systems). QarGuiPanelSize::content_scale zooms the web content; the default keeps it at 1. With no content extension chained to the init, the panel starts with the default website content until you navigate it.
What can go wrong: the common name must be ASCII, and the runtime rejects any other. Put other text in display_name. When the call fails, the program stops: every later call needs the panel id.
3. Subscribe before you change anything
Other peers, other apps and users grabbing the panel all change it. A subscription tells you when. Subscribe before your own changes so you do not miss any.
The callback runs on a library thread, while your main thread does something else. It receives a QarGuiPanel handle that the library owns and that is valid only during the call: read what you need with the qar_gui_panel_get_* getters, copy it, and do not destroy the handle. Keep the callback short and do not call blocking QAROS functions from it.
The callback and the main thread share the update log, so a lock guards it. The callback skips an update that matches the last one it printed: the Hub repeats updates, and a repeat tells you nothing new.
A repeat need not be equal to the bit. Its pose and size can differ from the earlier report below any precision you print or see, so comparing floats with == takes most repeats for changes. Compare at the precision your app cares about. This program allows a millimeter for pose and size, and compares the state and the address exactly:
The tolerances are in common.h, shared with App Volume Management :
Every qar_*_subscribe_* call hands its subscription to a cancel token. The token is how you end it (step 5).
qar_gui_panels_subscribe_updates() works the same way for every panel in the session; qar_gui_panels_subscribe_panel_updates() filters to one.
4. Change the panel
Every change goes through the session and the panel id. Each call is checked, and the first failure stops the chain.
- qar_gui_panels_update_pose() moves the top-edge midpoint.
- qar_gui_panels_change_size() changes the physical size and the content scale together.
- qar_gui_panels_set_state() switches between QAR_GUI_PANEL_STATE_VISIBLE, _MINIMIZED and _HIDDEN. To remove a panel, close it (step 7).
- qar_gui_panels_navigate_to_uri() points the panel at web content.
What can go wrong: QAR_STATUS_GUI_PANEL_INVALID_ID means no panel has that id, for example because another peer closed it. Call qar_gui_panels_get_or_create() again if your app still needs it. QAR_STATUS_GUI_PANEL_CHANGE_NOT_PERMITTED from the navigate call means the panel shows a stream, not a website.
5. Receive the updates, then end the subscription
The program waits up to 10 s for the first update, then until no update has arrived for half a second, and then ends the subscription. The Hub can take a few seconds to deliver the first update, so a quiet period alone could end before anything arrived. When no update comes within 10 s, the program says so, skips the listing, closes the panel and exits with 1: a program that follows updates and sees none should not report success.
The wait lives in common.h, shared with App Volume Management. The callback counts updates under the lock, so the wait reads the count under the same lock:
A real application does not wait for a quiet period. It reads what the callback stored from its own loop, such as a UI frame, and never blocks a callback waiting for that loop.
Treat an update as news that the panel changed, not as an instruction. Expect your own changes to come back, repeated and sometimes older than what you set: skip an update that matches the last one you handled, and do not take an update for a user's action, since it may be the echo of your own call.
When qar_cancel_token_cancel() returns, a callback that was running on another thread has finished, and no callback starts afterwards. That is what makes it safe to read the counts without the lock, and to free the update log once the function returns. The guarantee comes with one rule: never end a subscription while holding a lock its callback takes. Ending it waits for the running callback; if that callback waits for your lock, neither returns. Ending a subscription from inside its own callback is allowed, and does not wait for that callback. The full rules are under Ending a subscription in the API reference.
6. List the panels
The listing is a snapshot of every panel in the session, read with the count-then-fetch pattern: ask for the count, allocate that many handles, fetch them. You own each handle the fetch returns: read it, then destroy it. A fetch that returns an error may still have written some handles: destroy those too.
The listing can be stale the moment it returns. Use it at startup or for display, and the subscription to follow changes. A panel's display name can come back empty from the Hub, so tell panels apart by id, which derives from the common name, not by display name.
7. Close the panel
Closing removes the panel for every peer, since panels are shared. A later qar_gui_panels_get_or_create() with the same name creates it again.
Ownership and teardown in C
| Value | Who owns it | How it ends |
|---|---|---|
| QarRuntime*, QarSession* | you | qar_session_handle_destroy(), then qar_runtime_handle_destroy(), then example_unload_library (qar_library_destroy() and the unload) |
| QarCancelToken* | you | qar_cancel_token_cancel() ends the subscription; qar_cancel_token_handle_destroy() frees the token and also ends it |
| the update log (user_state) | you | free it only after the subscription has ended; here it lives on the stack of the function that ends it |
| QarGuiPanel* in the callback | the library | valid during the call only; never destroy it |
| QarGuiPanel* from qar_query_gui_panels() | you | qar_gui_panel_handle_destroy() after reading it, then free the array |
| QarGuiPanelId | a plain value | names the panel; not a handle, nothing to free |
| the panel itself | the session, shared by every peer | qar_gui_panels_close_panel(); destroying your session does not close it |
qar_session_handle_destroy() and qar_library_destroy() also end every subscription still running, so a forgotten token does not outlive the session. Do not destroy the session or the library from a callback.
Troubleshooting
| Symptom | Cause | What to do |
|---|---|---|
| Could not start or reach QAROS on this machine. Start QAROS from the Start menu, then start this app again., exit code 3 | The local-app join could neither start the Hub nor reach it | Start QAROS from the Start menu and run again |
| Exit code 4 or 5 after Joining the QAROS Hub on this machine as a local app | The approval prompt was denied, closed or not answered within 120 s | Run again and click Allow |
| qar_gui_panels_get_or_create: ERROR | The common name is not ASCII, or the session is gone | Keep the name ASCII; put other text in display_name |
| ERROR (code=305) from a setter: QAR_STATUS_GUI_PANEL_INVALID_ID | The panel was closed, possibly by another peer | qar_gui_panels_get_or_create() again |
| No update of the panel arrived within 10 s of the changes., exit code 1 | The Hub delivered no update of the panel within 10 s after the changes | Check that the Hub is still running and run again. In your app, read updates as they come instead of waiting for them a fixed time |
| Many more updates received than printed | The Hub reports your own changes back, repeated | Expected; skip repeats as the callback does |
| Update: or listing values differ from what you set | Updates arrive asynchronously and can be older than your last call | Do not apply an update's values blindly; treat it as a sign that the panel changed |
| name='' in the listing | The Hub returned an empty display name | Identify panels by id, not by display name |
| The program hangs while ending the subscription | A callback is waiting for a lock the ending thread holds | Release the lock before qar_cancel_token_cancel() |
| No QAROS runtime loaded | The default lookup found no runtime | Install QAROS, fix or unset QAR_INSTANCE, or pass --qaros-library=PATH |
Next
- App Volume Management : the 3D box your app renders into, with the same get-or-create and update patterns.
- Custom GUI Panels: visibility per peer and integration patterns.
- API Conventions: handles, results, callbacks and count-then-fetch in every language.
- The same program in Rust: GUI Panels.
Generated via doxygen2docusaurus 2.2.1 by Doxygen 1.9.8.