1 #ifndef RADSLINTBACKEND_H
2 #define RADSLINTBACKEND_H
11 #include <RADUi/RADUi.h>
12 #include <RADUi/RADTheme.h>
17 #include <type_traits>
33 void postToUi(std::function<
void()> task)
override;
38 std::thread::id uiThreadId_;
45 template<
typename Component,
typename F>
46 void postToComponent(
const slint::ComponentHandle<Component>& component, F&& action) {
47 slint::ComponentWeakHandle<Component> weakComponent(component);
48 RADUi::postToUi([weakComponent, action = std::forward<F>(action)]()
mutable {
49 if (
auto componentHandle = weakComponent.lock()) {
50 action(*componentHandle);
57 template<
typename Component,
typename Callback,
typename... Args>
63 const slint::ComponentHandle<Component>& component,
65 : weakComponent_(component), callback_(std::move(callback)) {
71 using StoredArgs = std::tuple<std::decay_t<Args>...>;
72 StoredArgs storedArgs(std::forward<Args>(args)...);
73 auto weakComponent = weakComponent_;
74 auto callback = callback_;
76 RADUi::postToUi([weakComponent, callback, storedArgs = std::move(storedArgs)]()
mutable {
77 if (
auto componentHandle = weakComponent.lock()) {
78 std::apply([&](auto&&... unpacked) {
79 callback(*componentHandle, std::forward<decltype(unpacked)>(unpacked)...);
80 }, std::move(storedArgs));
86 slint::ComponentWeakHandle<Component> weakComponent_;
92 template<
typename Component,
typename Callback>
98 const slint::ComponentHandle<Component>& component,
100 : manager_(manager), weakComponent_(component), callback_(std::move(callback)) {
101 connection_ = manager_.changed.connect(
this, &ThemeComponentBridge::handle);
107 auto weakComponent = weakComponent_;
108 auto callback = callback_;
109 auto theme = manager_.theme();
110 RADUi::postToUi([weakComponent, callback, theme = std::move(theme)]()
mutable {
111 if (
auto componentHandle = weakComponent.lock()) {
112 callback(*componentHandle, theme);
119 slint::ComponentWeakHandle<Component> weakComponent_;
126 template<
typename Component,
typename... Args,
typename F>
129 const slint::ComponentHandle<Component>& component,
132 return RADUi::Binding(std::make_shared<Bridge>(event, component, std::forward<F>(callback)));
136 template<
typename Component,
typename... Args,
typename F>
139 const slint::ComponentHandle<Component>& component,
145 template<
typename Component,
typename Value,
typename Setter>
148 const slint::ComponentHandle<Component>& component,
151 [setter](
auto componentHandle, Value value)
mutable {
152 ((*componentHandle).*setter)(std::move(value));
157 template<
typename Component,
typename Value,
typename Setter,
typename Transform>
160 const slint::ComponentHandle<Component>& component,
162 Transform transform) {
164 [setter, transform = std::move(transform)](
auto componentHandle, Value value)
mutable {
165 ((*componentHandle).*setter)(transform(std::move(value)));
170 template<
typename Component,
typename F>
173 const slint::ComponentHandle<Component>& component,
176 return RADUi::Binding(std::make_shared<Bridge>(manager, component, std::forward<F>(callback)));
Explicit event connection handle.
Definition: RADCore.h:935
Typed event template; use RAD_EVENT to declare instances.
Definition: RADCore.h:906
Base class for event receivers, senders, and thread-affinity aware objects.
Definition: RADCore.h:963
Abstract UI backend used to post tasks to a toolkit UI thread.
Definition: RADUi.h:47
RAII-style binding handle that keeps a bridge object alive until disconnected.
Definition: RADUi.h:30
Bridge object that forwards RADCore events to a Slint component callback.
Definition: RADSlintBackend.h:58
EventComponentBridge(RADCore::Event< void(Args...)> &event, const slint::ComponentHandle< Component > &component, Callback callback)
Connects event and stores a weak component reference.
Definition: RADSlintBackend.h:61
void handle(Args... args)
Handles the RAD event and posts callback invocation to the UI thread.
Definition: RADSlintBackend.h:70
Bridge object that forwards ThemeManager changes to a Slint component callback.
Definition: RADSlintBackend.h:93
ThemeComponentBridge(RADUi::ThemeManager &manager, const slint::ComponentHandle< Component > &component, Callback callback)
Connects manager and stores a weak component reference.
Definition: RADSlintBackend.h:96
void handle()
Posts current theme tokens to the component.
Definition: RADSlintBackend.h:106
RADUi backend implementation that uses Slint's event loop.
Definition: RADSlintBackend.h:23
SlintBackend()
Captures the creating thread as the UI thread.
void postToUi(std::function< void()> task) override
Posts task to Slint's UI queue.
bool isUiThread() const override
Returns true when called from the captured UI thread.
void run() override
Runs Slint's main event loop.
void quit() override
Requests Slint's event loop to quit.
Process/application theme manager with RADCore events.
Definition: RADTheme.h:103
Definition: RADSlintBackend.h:20
std::shared_ptr< SlintBackend > installBackend()
Installs and returns a shared Slint backend for RADUi.
RADUi::Binding bindTheme(RADUi::ThemeManager &manager, const slint::ComponentHandle< Component > &component, F &&callback)
Binds ThemeManager changes to a generated Slint component callback.
Definition: RADSlintBackend.h:171
RADUi::Binding bindEventToComponent(RADCore::Event< void(Args...)> &event, const slint::ComponentHandle< Component > &component, F &&callback)
Alias for connectEventToComponent used by property-oriented bridge code.
Definition: RADSlintBackend.h:137
RADUi::Binding connectEventToComponent(RADCore::Event< void(Args...)> &event, const slint::ComponentHandle< Component > &component, F &&callback)
Connects a RADCore event to a Slint component callback and returns a binding handle.
Definition: RADSlintBackend.h:127
RADUi::Binding bindProperty(RADCore::Event< void(Value)> &event, const slint::ComponentHandle< Component > &component, Setter setter, Transform transform)
Binds an event value to a generated Slint property setter after transform.
Definition: RADSlintBackend.h:158
void postToComponent(const slint::ComponentHandle< Component > &component, F &&action)
Posts action to the UI thread and invokes it only if component is still alive.
Definition: RADSlintBackend.h:46
void postToUi(std::function< void()> task)
Posts task to the installed UI backend.