How a frontend shows a session on screen¶
A frontend is what shows your session to a person: a
desktop window today, and perhaps a web page in the future. redsun ships one
frontend, for Qt.
The core of redsun knows nothing about windows. A frontend adds a session
class to subclass, such as QtSession, which starts
the toolkit and shows the views, and a
Frontend listing the placements
it can show. QtSession lives in redsun.qt because importing it imports the
toolkit, which a session without a window doesn't install.
Placements¶
A view says where it wants to be shown, and the frontend decides whether it
can. The core defines only the Placement base class; the Qt frontend defines
docks, the centre, menus, toolbars and the status bar, and
How to place a view shows where each one puts a
view.
In Frontend.requires, a frontend lists what each placement must hold: Qt asks
for a QWidget in a dock, in the centre or in the status bar, and a
QAction in a menu or toolbar.
A session can also declare the whole first layout of its window: which views share an edge, side by side, stacked or as tabs, and how much of the window each edge takes. The layout places the views it names, and every other view goes where its placement asks, so placements still work in a session that declares a layout. ADR 28 says why the session decides and not the view.
Keyboard shortcuts work the other way round: each component marks the methods its keys run, and the session collects them, settles two components asking for one key, and lets the session file change them, then the keys the user saved from the window. The frontend binds the result, anywhere in its window or only while a view has focus. ADR 29 says why.
Checks before a build¶
The session checks every view before it builds anything; point at a check to read what it asks:
A view that fails a check is left out before anything is built, with a message naming what the frontend shows instead:
Failed to build view 'stray': MyApp.stray asks to be attached as 'Route', which
Qt does not attach. It attaches: Central, Dock, MenuItem, StatusBarItem, ToolBarItem.
The session reads the placement from the class attribute or the declaration,
never from a property, since the view does not exist yet. A session file's
placement words, such as left or central, are read by the frontend's
read_placement; the core knows none of them.
The Qt frontend¶
A Qt view's constructor starts with exactly (name: str, parent: QWidget),
and QtSession passes its main window as the parent
(How to place a view). QtSession also:
- runs a widget's slots on the main thread unless a slot names another
- builds the main window from an
app-modelApplicationholding the menus and commands - restores the docks where the user left them, until a placement changes, and has a Window menu to show a closed dock or reset the layout
- asks before closing when a component has unsaved changes
- logs an exception no slot caught and keeps the window open, where the Qt binding would end the process silently
- closes and deletes every view at shutdown, after delivering waiting
signals, so a third-party widget can clean up in its
closeEvent
The Qt binding is chosen with QT_API, read by
qtpy, never by a session file.
Hook points¶
A hook acts at a fixed moment of a Qt session's life
without changing what the session builds. A Qt session has five
hook points, from making the QApplication to
answering whether the window may close.
Install hooks lists them, with
what each receives and when it runs. A session with no frontend calls no hook
points, so it refuses a hook declared on it.
Choosing the frontend from a file¶
A session file names its frontend by the name the frontend is registered under:
Session.from_config builds on the class registered under that name, or,
without a frontend key, on the class you called it on. Frontends are
registered as entry points, the packaging feature through which an installed
package announces what it offers, in the redsun.frontends group:
Another package registers its session class the same way, with no change to
redsun. A component asking for SessionConfig
finds the registered name in its frontend field, or None.
Writing a frontend¶
A new frontend defines its placements, its Frontend and a session class
that shows the views (How to write a frontend).
It may override Frontend.check_view, to refuse a view class it can't build,
and Session.view_arguments, to add arguments to every view's constructor.
What a frontend provides¶
| what | where | the Qt frontend |
|---|---|---|
| the placements it shows | Frontend.requires |
Central, Dock, MenuItem, StatusBarItem, ToolBarItem |
| the regions a window layout fills | Frontend.regions, Frontend.region_of |
center, left, right, top, bottom |
| whether a layout may start a view hidden | Frontend.hides |
yes |
| which keys it can read, and how it writes them | Frontend.key_problems, Frontend.canonical_key |
app-model's spelling, such as Ctrl+R |
| whether a key can run a view's coroutine method | Frontend.view_coroutines |
no |
| the thread its views' slots run on | Frontend.thread_of |
the main thread, for a QWidget |
| the delivery of the calls held for that thread | the session's run |
psygnal.qt.start_emitting_from_queue |
The thread and the delivery matter because presenters on other threads call a view's slots, while most toolkits allow one thread only. Step through such a call:
A presenter working on a worker thread emits a signal connected to a view's slot.
The slot names no thread and neither does its class, so the session asks Frontend.thread_of, which answers the main thread for a QWidget. The call waits in a queue.
The session calls psygnal.emit_queued from the toolkit's event loop, as often as the views should follow the presenters.
The queued call runs, and the view's slot runs on the main thread, where Qt allows it.
Coroutine slots need nothing from the frontend, because every session sets the
backend that runs them when it starts its runtime. That's why a frontend's
start_runtime must call the start_runtime it overrides.