mirror of
https://github.com/slint-ui/slint.git
synced 2025-11-01 20:31:27 +00:00
C++: rename PlatformEvent to Platform::Task
This commit is contained in:
parent
3137bfe775
commit
2a56e25788
3 changed files with 59 additions and 72 deletions
|
|
@ -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));
|
||||
});
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -108,7 +108,7 @@ struct CppPlatform {
|
|||
duration_since_start: unsafe extern "C" fn(PlatformUserData) -> u64,
|
||||
run_event_loop: unsafe extern "C" fn(PlatformUserData),
|
||||
quit_event_loop: unsafe extern "C" fn(PlatformUserData),
|
||||
invoke_from_event_loop: unsafe extern "C" fn(PlatformUserData, PlatformEventOpaque),
|
||||
invoke_from_event_loop: unsafe extern "C" fn(PlatformUserData, PlatformTaskOpaque),
|
||||
}
|
||||
|
||||
impl Drop for CppPlatform {
|
||||
|
|
@ -151,7 +151,7 @@ impl Platform for CppPlatform {
|
|||
struct CppEventLoopProxy {
|
||||
user_data: PlatformUserData,
|
||||
quit_event_loop: unsafe extern "C" fn(PlatformUserData),
|
||||
invoke_from_event_loop: unsafe extern "C" fn(PlatformUserData, PlatformEventOpaque),
|
||||
invoke_from_event_loop: unsafe extern "C" fn(PlatformUserData, PlatformTaskOpaque),
|
||||
}
|
||||
|
||||
impl i_slint_core::platform::EventLoopProxy for CppEventLoopProxy {
|
||||
|
|
@ -167,9 +167,7 @@ impl i_slint_core::platform::EventLoopProxy for CppEventLoopProxy {
|
|||
unsafe {
|
||||
(self.invoke_from_event_loop)(
|
||||
self.user_data,
|
||||
core::mem::transmute::<*mut dyn FnOnce(), PlatformEventOpaque>(Box::into_raw(
|
||||
event,
|
||||
)),
|
||||
core::mem::transmute::<*mut dyn FnOnce(), PlatformTaskOpaque>(Box::into_raw(event)),
|
||||
)
|
||||
};
|
||||
Ok(())
|
||||
|
|
@ -187,7 +185,7 @@ pub unsafe extern "C" fn slint_platform_register(
|
|||
#[allow(unused)] duration_since_start: unsafe extern "C" fn(PlatformUserData) -> u64,
|
||||
run_event_loop: unsafe extern "C" fn(PlatformUserData),
|
||||
quit_event_loop: unsafe extern "C" fn(PlatformUserData),
|
||||
invoke_from_event_loop: unsafe extern "C" fn(PlatformUserData, PlatformEventOpaque),
|
||||
invoke_from_event_loop: unsafe extern "C" fn(PlatformUserData, PlatformTaskOpaque),
|
||||
) {
|
||||
let p = CppPlatform {
|
||||
user_data,
|
||||
|
|
@ -248,16 +246,16 @@ pub extern "C" fn slint_platform_duration_until_next_timer_update() -> u64 {
|
|||
}
|
||||
|
||||
#[repr(C)]
|
||||
pub struct PlatformEventOpaque(*const c_void, *const c_void);
|
||||
pub struct PlatformTaskOpaque(*const c_void, *const c_void);
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn slint_platform_event_drop(event: PlatformEventOpaque) {
|
||||
drop(Box::from_raw(core::mem::transmute::<PlatformEventOpaque, *mut dyn FnOnce()>(event)));
|
||||
pub unsafe extern "C" fn slint_platform_task_drop(event: PlatformTaskOpaque) {
|
||||
drop(Box::from_raw(core::mem::transmute::<PlatformTaskOpaque, *mut dyn FnOnce()>(event)));
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn slint_platform_event_invoke(event: PlatformEventOpaque) {
|
||||
let f = Box::from_raw(core::mem::transmute::<PlatformEventOpaque, *mut dyn FnOnce()>(event));
|
||||
pub unsafe extern "C" fn slint_platform_task_run(event: PlatformTaskOpaque) {
|
||||
let f = Box::from_raw(core::mem::transmute::<PlatformTaskOpaque, *mut dyn FnOnce()>(event));
|
||||
f();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ namespace slint_platform = slint::experimental::platform;
|
|||
struct TestPlatform : slint_platform::Platform
|
||||
{
|
||||
std::mutex the_mutex;
|
||||
std::deque<slint_platform::PlatformEvent> queue;
|
||||
std::deque<slint_platform::Platform::Task> queue;
|
||||
bool quit = false;
|
||||
std::condition_variable cv;
|
||||
std::chrono::time_point<std::chrono::steady_clock> start = std::chrono::steady_clock::now();
|
||||
|
|
@ -34,7 +34,7 @@ struct TestPlatform : slint_platform::Platform
|
|||
{
|
||||
while (true) {
|
||||
slint_platform::update_timers_and_animations();
|
||||
std::optional<slint_platform::PlatformEvent> event;
|
||||
std::optional<slint_platform::Platform::Task> event;
|
||||
{
|
||||
std::unique_lock lock(the_mutex);
|
||||
if (queue.empty()) {
|
||||
|
|
@ -54,16 +54,12 @@ struct TestPlatform : slint_platform::Platform
|
|||
}
|
||||
}
|
||||
if (event) {
|
||||
std::move(*event).invoke();
|
||||
std::move(*event).run();
|
||||
event.reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Exits the event loop.
|
||||
///
|
||||
/// This is what is called by slint::quit_event_loop() and can be called from a different thread
|
||||
/// or re-enter from the event loop
|
||||
virtual void quit_event_loop() override
|
||||
{
|
||||
const std::unique_lock lock(the_mutex);
|
||||
|
|
@ -71,14 +67,7 @@ struct TestPlatform : slint_platform::Platform
|
|||
cv.notify_all();
|
||||
}
|
||||
|
||||
/// Invokes the event 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(slint_platform::PlatformEvent event) override
|
||||
virtual void run_in_event_loop(slint_platform::Platform::Task event) override
|
||||
{
|
||||
const std::unique_lock lock(the_mutex);
|
||||
queue.push_back(std::move(event));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue