C++: Platform::register_platform -> platform::set_platform

This commit is contained in:
Olivier Goffart 2023-07-25 16:02:39 +02:00 committed by Olivier Goffart
parent 42bb2bf705
commit 69a11f7dbc
4 changed files with 32 additions and 34 deletions

View file

@ -59,9 +59,6 @@ class WindowAdapter
// Whether this WindowAdapter was already given to the slint runtime
bool was_initialized = false;
private:
friend class Platform;
cbindgen_private::WindowAdapterRcOpaque initialize()
{
cbindgen_private::slint_window_adapter_new(
@ -80,6 +77,8 @@ private:
return self;
}
friend inline void set_platform(std::unique_ptr<class Platform> platform);
public:
/// Construct a WindowAdapter
explicit WindowAdapter() { }
@ -160,7 +159,7 @@ public:
/// The platform is acting like a factory to create a WindowAdapter
///
/// Platform::register_platform() need to be called before any other Slint handle
/// slint::platform::set_platform() need to be called before any other Slint handle
/// are created, and if it is called, it will use the WindowAdapter provided by the
/// create_window_adapter function.
class Platform
@ -195,8 +194,7 @@ public:
class Task
{
cbindgen_private::PlatformTaskOpaque inner { nullptr, nullptr };
friend class Platform;
friend inline void set_platform(std::unique_ptr<Platform> platform);
explicit Task(cbindgen_private::PlatformTaskOpaque inner) : inner(inner) { }
public:
@ -238,33 +236,33 @@ public:
/// Reimplements this function and move the event to the event loop before calling
/// 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.
static void register_platform(std::unique_ptr<Platform> platform)
{
cbindgen_private::slint_platform_register(
platform.release(), [](void *p) { delete reinterpret_cast<const Platform *>(p); },
[](void *p, cbindgen_private::WindowAdapterRcOpaque *out) {
auto w = reinterpret_cast<Platform *>(p)->create_window_adapter();
*out = w->initialize();
(void)w.release();
},
[]([[maybe_unused]] void *p) -> uint64_t {
# ifdef SLINT_FEATURE_STD
return 0;
# else
return reinterpret_cast<const Platform *>(p)->duration_since_start().count();
# endif
},
[](void *p) { return reinterpret_cast<Platform *>(p)->run_event_loop(); },
[](void *p) { return reinterpret_cast<Platform *>(p)->quit_event_loop(); },
[](void *p, cbindgen_private::PlatformTaskOpaque event) {
return reinterpret_cast<Platform *>(p)->run_in_event_loop(Task(event));
});
}
};
/// Registers the platform with Slint. Must be called before Slint windows are created.
/// Can only be called once in an application.
inline void set_platform(std::unique_ptr<Platform> platform)
{
cbindgen_private::slint_platform_register(
platform.release(), [](void *p) { delete reinterpret_cast<const Platform *>(p); },
[](void *p, cbindgen_private::WindowAdapterRcOpaque *out) {
auto w = reinterpret_cast<Platform *>(p)->create_window_adapter();
*out = w->initialize();
(void)w.release();
},
[]([[maybe_unused]] void *p) -> uint64_t {
# ifdef SLINT_FEATURE_STD
return 0;
# else
return reinterpret_cast<const Platform *>(p)->duration_since_start().count();
# endif
},
[](void *p) { return reinterpret_cast<Platform *>(p)->run_event_loop(); },
[](void *p) { return reinterpret_cast<Platform *>(p)->quit_event_loop(); },
[](void *p, cbindgen_private::PlatformTaskOpaque event) {
return reinterpret_cast<Platform *>(p)->run_in_event_loop(Platform::Task(event));
});
}
/// Represents a region on the screen, used for partial rendering.
///
/// The region may be composed of multiple sub-regions.

View file

@ -35,7 +35,7 @@ void AppView::attachToWindow(WINDOW_HANDLE winId)
auto p = std::make_unique<MyPlatform>();
p->the_window = std::make_unique<MyWindowAdapter>(winId);
myWindow = p->the_window.get();
slint_platform::Platform::register_platform(std::move(p));
slint_platform::set_platform(std::move(p));
// AppWindow is the auto-generated slint code
static auto app = AppWindow::create();

View file

@ -168,7 +168,7 @@ int main(int argc, char **argv)
static MyPlatform *plarform = [] {
auto platform = std::make_unique<MyPlatform>();
auto p2 = platform.get();
MyPlatform::register_platform(std::move(platform));
slint_platform::set_platform(std::move(platform));
return p2;
}();

View file

@ -83,7 +83,7 @@ struct TestPlatform : slint_platform::Platform
#endif
};
bool init_platform = (TestPlatform::register_platform(std::make_unique<TestPlatform>()), true);
bool init_platform = (slint_platform::set_platform(std::make_unique<TestPlatform>()), true);
TEST_CASE("C++ Singleshot Timers")
{