morph 0.1.0
Asynchronous bridge between GUI thread and business models
Loading...
Searching...
No Matches
backend.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2
3#pragma once
4#include <functional>
5#include <memory>
6#include <string>
7#include <string_view>
8#include <vector>
9
10#include "../attributes.hpp"
11#include "../ui/backend.hpp"
12
13class QQmlEngine;
14class QQuickItem;
15
16/// @file
17/// @brief `morph::qt_quick::Backend`: the view tree's widgets as Qt Quick items.
18///
19/// Specified in `docs/spec/qt_quick/backend.md`.
20
21namespace morph::qt_quick {
22
23class Components;
24
25namespace detail {
26class Context;
27} // namespace detail
28
29/// @brief The window width class `{"env": "widthClass"}` reads (spec 3 §8).
30/// @param logicalWidth A width in logical pixels.
31/// @return `"compact"` below 600, `"medium"` below 1200, else `"expanded"`.
32[[nodiscard]] std::string widthClassOf(double logicalWidth);
33
34/// @brief What a `Backend` is made with beside its engine and host item.
36 /// @brief The restyle map and the custom components; null renders every kind with its `MorphUi` component and
37 /// loads no custom component. Borrowed: it outlives the backend.
39 /// @brief Called with the host item's width class (`widthClassOf`) once at construction and again each time
40 /// the class changes, not on every resize; empty calls nothing.
41 std::function<void(std::string const& widthClass)> onWidthClass;
42};
43
44// The view tree's custom component and request, which `Backend::createCustom` makes and takes.
47
48/// @brief `ui::IViewBackend` over Qt Quick: each widget is a Qt Quick item made from its kind's `MorphUi`
49/// component, or from the component the restyle map names for that kind.
50///
51/// Every root a mount makes is appended to a column the backend adds to the host item, so several roots share it
52/// top to bottom. Neither copyable nor movable. It belongs to the thread of its engine, and every widget it makes
53/// is destroyed before it.
54class Backend final : public ui::IViewBackend {
55public:
56 /// @brief A backend whose roots fill @p host.
57 /// @param engine The engine its components are compiled in. Borrowed.
58 /// @param host The item its roots are shown in. Borrowed.
59 /// @param options The components and the width class callback.
60 Backend(QQmlEngine& engine MORPH_LIFETIMEBOUND, QQuickItem& host MORPH_LIFETIMEBOUND, BackendOptions options = {});
61 /// @brief Removes the roots' column from the host.
62 ~Backend() override;
63 Backend(Backend const&) = delete;
64 Backend& operator=(Backend const&) = delete;
65 Backend(Backend&&) = delete;
66 Backend& operator=(Backend&&) = delete;
67
68 /// @brief The Qt Quick item a widget of this backend shows.
69 /// @param widget The widget.
70 /// @return Its item; null for a widget this backend did not make.
71 [[nodiscard]] static QQuickItem* itemOf(ui::Widget const& widget) noexcept;
72
73 /// @brief Loads a custom component by name.
74 ///
75 /// A host component loads into this backend's engine; a bundle component must match its digest and compiles in
76 /// the components' contained engine. A component that cannot be loaded — not registered, a host component below
77 /// `minVersion`, a digest that does not match, a compile error, a declared prop it lacks — is reported once
78 /// through the components' failure callback, and the caller shows the fallback.
79 /// @param parent The container to append it to; null makes a root.
80 /// @param request The component and the props the document declares.
81 /// @return The widget, or null when the fallback is shown instead.
82 [[nodiscard]] std::unique_ptr<CustomWidget> createCustom(ui::ContainerWidget* parent,
83 CustomRequest const& request) override;
84
85 /// @copydoc ui::IViewBackend::createText
86 [[nodiscard]] std::unique_ptr<ui::TextWidget> createText(ui::ContainerWidget* parent) override;
87 /// @copydoc ui::IViewBackend::createButton
88 [[nodiscard]] std::unique_ptr<ui::ButtonWidget> createButton(ui::ContainerWidget* parent) override;
89 /// @copydoc ui::IViewBackend::createTextInput
90 [[nodiscard]] std::unique_ptr<ui::TextInputWidget> createTextInput(ui::ContainerWidget* parent,
91 ui::TextInputMode mode) override;
92 /// @copydoc ui::IViewBackend::createCheckbox
93 [[nodiscard]] std::unique_ptr<ui::CheckboxWidget> createCheckbox(ui::ContainerWidget* parent) override;
94 /// @copydoc ui::IViewBackend::createSelect
95 [[nodiscard]] std::unique_ptr<ui::SelectWidget> createSelect(ui::ContainerWidget* parent,
96 ui::SelectStyle style) override;
97 /// @copydoc ui::IViewBackend::createMenu
98 [[nodiscard]] std::unique_ptr<ui::MenuWidget> createMenu(ui::ContainerWidget* parent) override;
99 /// @copydoc ui::IViewBackend::createStack
100 [[nodiscard]] std::unique_ptr<ui::StackWidget> createStack(ui::ContainerWidget* parent, ui::Axis axis) override;
101 /// @copydoc ui::IViewBackend::createGrid
102 [[nodiscard]] std::unique_ptr<ui::GridWidget> createGrid(ui::ContainerWidget* parent) override;
103 /// @copydoc ui::IViewBackend::createSpacer
104 [[nodiscard]] std::unique_ptr<ui::SpacerWidget> createSpacer(ui::ContainerWidget* parent) override;
105 /// @copydoc ui::IViewBackend::createPanel
106 [[nodiscard]] std::unique_ptr<ui::PanelWidget> createPanel(ui::ContainerWidget* parent) override;
107 /// @copydoc ui::IViewBackend::createScroll
108 [[nodiscard]] std::unique_ptr<ui::ScrollWidget> createScroll(ui::ContainerWidget* parent, ui::Axis axis) override;
109 /// @copydoc ui::IViewBackend::createSlot
110 [[nodiscard]] std::unique_ptr<ui::SlotWidget> createSlot(ui::ContainerWidget* parent) override;
111 /// @copydoc ui::IViewBackend::createTabs
112 [[nodiscard]] std::unique_ptr<ui::TabsWidget> createTabs(ui::ContainerWidget* parent) override;
113 /// @copydoc ui::IViewBackend::createDialog
114 [[nodiscard]] std::unique_ptr<ui::DialogWidget> createDialog(ui::ContainerWidget* parent) override;
115 /// @copydoc ui::IViewBackend::createToast
116 [[nodiscard]] std::unique_ptr<ui::ToastWidget> createToast(ui::ContainerWidget* parent) override;
117 /// @copydoc ui::IViewBackend::createBusy
118 [[nodiscard]] std::unique_ptr<ui::BusyWidget> createBusy(ui::ContainerWidget* parent) override;
119 /// @copydoc ui::IViewBackend::createTable
120 [[nodiscard]] std::unique_ptr<ui::TableWidget> createTable(ui::ContainerWidget* parent) override;
121 /// @copydoc ui::IViewBackend::createDateTimeInput
122 [[nodiscard]] std::unique_ptr<ui::DateTimeInputWidget> createDateTimeInput(ui::ContainerWidget* parent,
123 ui::DateMode mode,
124 int offsetMinutes) override;
125 /// @copydoc ui::IViewBackend::createSlider
126 [[nodiscard]] std::unique_ptr<ui::SliderWidget> createSlider(ui::ContainerWidget* parent) override;
127 /// @copydoc ui::IViewBackend::createFilePicker
128 [[nodiscard]] std::unique_ptr<ui::FilePickerWidget> createFilePicker(ui::ContainerWidget* parent,
129 ui::FilePickerMode mode) override;
130 /// @copydoc ui::IViewBackend::createBanner
131 [[nodiscard]] std::unique_ptr<ui::BannerWidget> createBanner(ui::ContainerWidget* parent) override;
132 /// @copydoc ui::IViewBackend::createBadge
133 [[nodiscard]] std::unique_ptr<ui::BadgeWidget> createBadge(ui::ContainerWidget* parent) override;
134 /// @copydoc ui::IViewBackend::createProgress
135 [[nodiscard]] std::unique_ptr<ui::ProgressWidget> createProgress(ui::ContainerWidget* parent) override;
136 /// @copydoc ui::IViewBackend::createSteps
137 [[nodiscard]] std::unique_ptr<ui::StepsWidget> createSteps(ui::ContainerWidget* parent) override;
138 /// @copydoc ui::IViewBackend::createKeyValue
139 [[nodiscard]] std::unique_ptr<ui::KeyValueWidget> createKeyValue(ui::ContainerWidget* parent) override;
140 /// @copydoc ui::IViewBackend::createEmptyState
141 [[nodiscard]] std::unique_ptr<ui::EmptyStateWidget> createEmptyState(ui::ContainerWidget* parent) override;
142 /// @copydoc ui::IViewBackend::createDrawer
143 [[nodiscard]] std::unique_ptr<ui::DrawerWidget> createDrawer(ui::ContainerWidget* parent, ui::Side side) override;
144 /// @copydoc ui::IViewBackend::createSplitter
145 [[nodiscard]] std::unique_ptr<ui::SplitterWidget> createSplitter(ui::ContainerWidget* parent,
146 ui::Axis axis) override;
147 /// @copydoc ui::IViewBackend::createCollapsible
148 [[nodiscard]] std::unique_ptr<ui::CollapsibleWidget> createCollapsible(ui::ContainerWidget* parent) override;
149 /// @copydoc ui::IViewBackend::createDropZone
150 [[nodiscard]] std::unique_ptr<ui::DropZoneWidget> createDropZone(ui::ContainerWidget* parent) override;
151
152private:
153 std::unique_ptr<detail::Context> _context;
154};
155
156} // namespace morph::qt_quick
std::unique_ptr< ui::SelectWidget > createSelect(ui::ContainerWidget *parent, ui::SelectStyle style) override
Makes a choice of one option.
static QQuickItem * itemOf(ui::Widget const &widget) noexcept
The Qt Quick item a widget of this backend shows.
std::unique_ptr< ui::FilePickerWidget > createFilePicker(ui::ContainerWidget *parent, ui::FilePickerMode mode) override
Makes a file path field.
std::unique_ptr< ui::ProgressWidget > createProgress(ui::ContainerWidget *parent) override
Makes a progress bar.
std::unique_ptr< ui::KeyValueWidget > createKeyValue(ui::ContainerWidget *parent) override
Makes a key-value list.
std::unique_ptr< ui::MenuWidget > createMenu(ui::ContainerWidget *parent) override
Makes a command list.
std::unique_ptr< ui::ScrollWidget > createScroll(ui::ContainerWidget *parent, ui::Axis axis) override
Makes a scroll area.
std::unique_ptr< ui::GridWidget > createGrid(ui::ContainerWidget *parent) override
Makes a grid.
std::unique_ptr< CustomWidget > createCustom(ui::ContainerWidget *parent, CustomRequest const &request) override
Loads a custom component by name.
std::unique_ptr< ui::TextInputWidget > createTextInput(ui::ContainerWidget *parent, ui::TextInputMode mode) override
Makes a text field.
std::unique_ptr< ui::EmptyStateWidget > createEmptyState(ui::ContainerWidget *parent) override
Makes an empty state.
std::unique_ptr< ui::SplitterWidget > createSplitter(ui::ContainerWidget *parent, ui::Axis axis) override
Makes a splitter.
std::unique_ptr< ui::PanelWidget > createPanel(ui::ContainerWidget *parent) override
Makes a titled frame.
std::unique_ptr< ui::DropZoneWidget > createDropZone(ui::ContainerWidget *parent) override
Makes a drop zone.
std::unique_ptr< ui::CollapsibleWidget > createCollapsible(ui::ContainerWidget *parent) override
Makes a collapsible section.
std::unique_ptr< ui::DateTimeInputWidget > createDateTimeInput(ui::ContainerWidget *parent, ui::DateMode mode, int offsetMinutes) override
Makes a date or date-and-time field.
std::unique_ptr< ui::SlotWidget > createSlot(ui::ContainerWidget *parent) override
Makes a slot.
Backend(QQmlEngine &engine, QQuickItem &host, BackendOptions options={})
A backend whose roots fill host.
std::unique_ptr< ui::BadgeWidget > createBadge(ui::ContainerWidget *parent) override
Makes a badge.
~Backend() override
Removes the roots' column from the host.
std::unique_ptr< ui::SpacerWidget > createSpacer(ui::ContainerWidget *parent) override
Makes empty space.
std::unique_ptr< ui::CheckboxWidget > createCheckbox(ui::ContainerWidget *parent) override
Makes a check box.
std::unique_ptr< ui::ToastWidget > createToast(ui::ContainerWidget *parent) override
Makes a transient message.
std::unique_ptr< ui::BusyWidget > createBusy(ui::ContainerWidget *parent) override
Makes a progress indicator.
std::unique_ptr< ui::BannerWidget > createBanner(ui::ContainerWidget *parent) override
Makes a banner.
std::unique_ptr< ui::DialogWidget > createDialog(ui::ContainerWidget *parent) override
Makes a modal overlay.
std::unique_ptr< ui::StepsWidget > createSteps(ui::ContainerWidget *parent) override
Makes a steps indicator.
std::unique_ptr< ui::SliderWidget > createSlider(ui::ContainerWidget *parent) override
Makes a slider.
std::unique_ptr< ui::StackWidget > createStack(ui::ContainerWidget *parent, ui::Axis axis) override
Makes a stack.
std::unique_ptr< ui::TabsWidget > createTabs(ui::ContainerWidget *parent) override
Makes a tab bar.
std::unique_ptr< ui::ButtonWidget > createButton(ui::ContainerWidget *parent) override
Makes a push button.
std::unique_ptr< ui::DrawerWidget > createDrawer(ui::ContainerWidget *parent, ui::Side side) override
Makes a drawer.
std::unique_ptr< ui::TableWidget > createTable(ui::ContainerWidget *parent) override
Makes a table.
std::unique_ptr< ui::TextWidget > createText(ui::ContainerWidget *parent) override
Makes read-only text.
The QML components a client renders beside the built-in kinds (spec 5 §10).
A widget with children. A factory given this container appends the new widget as its last child.
Definition backend.hpp:103
A component a renderer loaded in a custom node's place (spec 5 §10): a bundle's verified QML or a hos...
Definition backend.hpp:773
Makes widgets. Each factory appends the new widget to parent, or makes a root when it is null,...
Definition backend.hpp:810
A retained widget. A new widget is visible, enabled, content-sized, not draggable and not a drop targ...
Definition backend.hpp:34
std::string widthClassOf(double logicalWidth)
The window width class {"env": "widthClass"} reads (spec 3 §8).
What a Backend is made with beside its engine and host item.
Definition backend.hpp:35
Components * components
The restyle map and the custom components; null renders every kind with its MorphUi component and loa...
Definition backend.hpp:38
std::function< void(std::string const &widthClass)> onWidthClass
Called with the host item's width class (widthClassOf) once at construction and again each time the c...
Definition backend.hpp:41
What IViewBackend::createCustom asks for.
Definition backend.hpp:799
The backend contract: the retained widgets a mount drives, and the factory that makes them.
TextInputMode
How a TextInput edits.
Definition view.hpp:209
DateMode
What a DateTimeInput edits.
Definition view.hpp:228
Side
The edge a Drawer slides in from, in reading direction.
Definition view.hpp:249
FilePickerMode
Whether a FilePicker chooses an existing file or a file to write.
Definition view.hpp:234
SelectStyle
How a Select presents its options.
Definition view.hpp:216
Axis
The direction a stack arranges, or a scroll area scrolls, its content.
Definition view.hpp:222