Simplify C++ WindowAdapter <> Renderer interface further

Instead of the WindowAdapter being a template for a concept, let's just
use an abstract base class for the renderer. Since we provide the
renderers, this simplifies two things:

- Ownership becomes clear to the user by creating the renderer instance
in a field of theirs.
- All template use goes away on the user side.
This commit is contained in:
Simon Hausmann 2023-05-17 17:27:41 +02:00 committed by Simon Hausmann
parent fabe3ec189
commit baba30370a
4 changed files with 76 additions and 86 deletions

View file

@ -17,7 +17,7 @@ namespace slint_platform = slint::experimental::platform;
struct MyPlatform : public slint_platform::Platform
{
mutable std::unique_ptr<MyWindowAdapter> the_window;
std::unique_ptr<slint_platform::AbstractWindowAdapter> create_window_adapter() const override
std::unique_ptr<slint_platform::WindowAdapter> create_window_adapter() const override
{
return std::move(the_window);
}

View file

@ -19,10 +19,11 @@ struct Geometry
uint32_t height = 0;
};
struct MyWindowAdapter : public slint_platform::WindowAdapter<slint_platform::SkiaRenderer>
struct MyWindowAdapter : public slint_platform::WindowAdapter
{
HWND hwnd;
Geometry geometry = { 0, 0, 600, 300 };
std::unique_ptr<slint_platform::SkiaRenderer> m_renderer;
MyWindowAdapter(HWND winId)
{
@ -55,13 +56,15 @@ struct MyWindowAdapter : public slint_platform::WindowAdapter<slint_platform::Sk
NULL // Additional application data
);
set_renderer(std::make_unique<slint_platform::SkiaRenderer>(
m_renderer = std::make_unique<slint_platform::SkiaRenderer>(
slint_platform::NativeWindowHandle::from_win32(hwnd, hInstance),
slint::PhysicalSize({ 600, 300 })));
slint::PhysicalSize({ 600, 300 }));
SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)this);
}
slint_platform::AbstractRenderer &renderer() const override { return *m_renderer.get(); }
slint::PhysicalSize physical_size() const override
{
RECT r;
@ -72,20 +75,20 @@ struct MyWindowAdapter : public slint_platform::WindowAdapter<slint_platform::Sk
void show() const override
{
ShowWindow(hwnd, SW_SHOWNORMAL);
renderer().show();
m_renderer->show();
}
void hide() const override
{
// TODO: destroy window
renderer().hide();
m_renderer->hide();
}
void request_redraw() const override { InvalidateRect(hwnd, nullptr, false); }
void render()
{
renderer().render(window(), physical_size());
m_renderer->render(window(), physical_size());
if (has_active_animations())
request_redraw();
}
@ -93,7 +96,7 @@ struct MyWindowAdapter : public slint_platform::WindowAdapter<slint_platform::Sk
void resize(uint32_t width, uint32_t height)
{
slint::PhysicalSize windowSize({ width, height });
renderer().resize(windowSize);
m_renderer->resize(windowSize);
dispatch_resize_event(slint::LogicalSize({ (float)width, (float)height }));
}

View file

@ -49,18 +49,20 @@ static slint_platform::NativeWindowHandle window_handle_for_qt_window(QWindow *w
#endif
}
class MyWindow : public QWindow, public slint_platform::WindowAdapter<slint_platform::SkiaRenderer>
class MyWindow : public QWindow, public slint_platform::WindowAdapter
{
std::unique_ptr<slint_platform::SkiaRenderer> m_renderer;
public:
MyWindow(QWindow *parentWindow = nullptr)
: QWindow(parentWindow), slint_platform::WindowAdapter<slint_platform::SkiaRenderer>()
MyWindow(QWindow *parentWindow = nullptr) : QWindow(parentWindow)
{
set_renderer(std::make_unique<slint_platform::SkiaRenderer>(
m_renderer = std::make_unique<slint_platform::SkiaRenderer>(
window_handle_for_qt_window(this),
slint::PhysicalSize({ uint32_t(width()), uint32_t(height()) })));
slint::PhysicalSize({ uint32_t(width()), uint32_t(height()) }));
}
slint_platform::AbstractRenderer &renderer() const override { return *m_renderer.get(); }
/*void keyEvent(QKeyEvent *event) override
{
renderer()->dispatch_key_event(slint::cbingen_private::UglyEnum {... })
@ -71,7 +73,7 @@ public:
slint_platform::update_timers_and_animations();
auto windowSize = slint::PhysicalSize({ uint32_t(width()), uint32_t(height()) });
renderer().render(window(), windowSize);
m_renderer->render(window(), windowSize);
if (has_active_animations()) {
requestUpdate();
@ -92,11 +94,11 @@ public:
{
auto window = const_cast<QWindow *>(static_cast<const QWindow *>(this));
window->QWindow::show();
renderer().show();
m_renderer->show();
}
void hide() const override
{
renderer().hide();
m_renderer->hide();
const_cast<MyWindow *>(this)->QWindow::hide();
}
slint::PhysicalSize physical_size() const override
@ -111,9 +113,9 @@ public:
{
auto windowSize = slint::PhysicalSize(
{ uint32_t(ev->size().width()), uint32_t(ev->size().height()) });
renderer().resize(windowSize);
m_renderer->resize(windowSize);
float scale_factor = devicePixelRatio();
WindowAdapter<slint_platform::SkiaRenderer>::dispatch_resize_event(
WindowAdapter::dispatch_resize_event(
slint::LogicalSize({ float(windowSize.width) / scale_factor,
float(windowSize.height) / scale_factor }));
}
@ -152,7 +154,7 @@ struct MyPlatform : public slint_platform::Platform
std::unique_ptr<QWindow> parentWindow;
std::unique_ptr<slint_platform::AbstractWindowAdapter> create_window_adapter() const override
std::unique_ptr<slint_platform::WindowAdapter> create_window_adapter() const override
{
return std::make_unique<MyWindow>(parentWindow.get());
}