C++: rename PlatformEvent to Platform::Task

This commit is contained in:
Olivier Goffart 2023-07-25 16:15:35 +02:00 committed by GitHub
parent 3137bfe775
commit 2a56e25788
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 72 deletions

View file

@ -10,6 +10,7 @@
#else
# include <utility>
# include <cassert>
struct xcb_connection_t;
struct wl_surface;
@ -157,46 +158,6 @@ public:
}
};
/// An event that is passed to the Platform::invoke_from_event_loop function and needs to be invoked
/// in the event loop.
class PlatformEvent
{
cbindgen_private::PlatformEventOpaque inner { nullptr, nullptr };
friend class Platform;
explicit PlatformEvent(cbindgen_private::PlatformEventOpaque inner) : inner(inner) { }
public:
~PlatformEvent()
{
if (inner._0) {
cbindgen_private::slint_platform_event_drop(std::exchange(inner, { nullptr, nullptr }));
}
}
PlatformEvent(const PlatformEvent &) = delete;
PlatformEvent &operator=(const PlatformEvent &) = delete;
PlatformEvent(PlatformEvent &&other) : inner(other.inner)
{
other.inner = { nullptr, nullptr };
}
PlatformEvent &operator=(PlatformEvent &&other)
{
std::swap(other.inner, inner);
return *this;
}
/// Invoke the event.
///
/// Can only be invoked once and should only be called from the event loop.
void invoke() &&
{
if (inner._0) {
cbindgen_private::slint_platform_event_invoke(
std::exchange(inner, { nullptr, nullptr }));
}
};
};
/// The platform is acting like a factory to create a WindowAdapter
///
/// Platform::register_platform() need to be called before any other Slint handle
@ -227,14 +188,54 @@ public:
/// or re-enter from the event loop
virtual void quit_event_loop() { }
/// Invokes the event from the event loop.
/// An task that is passed to the Platform::run_in_event_loop function and needs to be
/// run in the event loop and not in any other thread.
class Task
{
cbindgen_private::PlatformTaskOpaque inner { nullptr, nullptr };
friend class Platform;
explicit Task(cbindgen_private::PlatformTaskOpaque inner) : inner(inner) { }
public:
~Task()
{
if (inner._0) {
cbindgen_private::slint_platform_task_drop(
std::exchange(inner, { nullptr, nullptr }));
}
}
Task(const Task &) = delete;
Task &operator=(const Task &) = delete;
Task(Task &&other) : inner(other.inner) { other.inner = { nullptr, nullptr }; }
Task &operator=(Task &&other)
{
std::swap(other.inner, inner);
return *this;
}
/// Run the task.
///
/// Can only be invoked once and should only be called from the event loop.
void run() &&
{
private_api::assert_main_thread();
assert(inner._0 && "calling invoke form a moved-from Task");
if (inner._0) {
cbindgen_private::slint_platform_task_run(
std::exchange(inner, { nullptr, nullptr }));
}
};
};
/// Run a task from the event loop.
///
/// This function is called by slint::invoke_from_event_loop().
/// It can be called from any thread, but the passed function must only be called
/// from the event loop.
/// Reimplements this function and move the event to the event loop before calling
/// PlatformEvent::invoke()
virtual void invoke_from_event_loop(PlatformEvent) { }
/// Task::run()
virtual void run_in_event_loop(Task) { }
/// Registers the platform with Slint. Must be called before Slint windows are created.
/// Can only be called once in an application.
@ -252,9 +253,8 @@ public:
},
[](void *p) { return reinterpret_cast<Platform *>(p)->run_event_loop(); },
[](void *p) { return reinterpret_cast<Platform *>(p)->quit_event_loop(); },
[](void *p, cbindgen_private::PlatformEventOpaque event) {
return reinterpret_cast<Platform *>(p)->invoke_from_event_loop(
PlatformEvent(event));
[](void *p, cbindgen_private::PlatformTaskOpaque event) {
return reinterpret_cast<Platform *>(p)->run_in_event_loop(Task(event));
});
}
};