Remove the WindowAdapter from the renderer constructor

This allows disentangling the native window creation from the renderer
creation, which is rather ugly and complicated on the C++ side.
This commit is contained in:
Simon Hausmann 2023-05-15 14:47:28 +02:00 committed by Simon Hausmann
parent dd5ef9993f
commit fe4a434ce4
13 changed files with 170 additions and 173 deletions

View file

@ -19,22 +19,46 @@ struct Geometry
uint32_t height = 0;
};
struct NativeWindowHandle
struct MyWindowAdapter : public slint_platform::WindowAdapter<slint_platform::SkiaRenderer>
{
HWND hwnd;
};
struct MyWindowAdapter : NativeWindowHandle,
public slint_platform::WindowAdapter<slint_platform::SkiaRenderer>
{
Geometry geometry = { 0, 0, 600, 300 };
MyWindowAdapter(HWND winId)
: NativeWindowHandle { MyWindowAdapter::create_window(winId) },
slint_platform::WindowAdapter<slint_platform::SkiaRenderer>(
slint_platform::NativeWindowHandle::from_win32(hwnd, GetModuleHandleW(nullptr)),
slint::PhysicalSize({ 600, 300 }))
{
HINSTANCE hInstance = GetModuleHandleW(nullptr);
// Register the window class.
const wchar_t CLASS_NAME[] = L"Sample Window Class";
WNDCLASS wc = {};
wc.lpfnWndProc = MyWindowAdapter::windowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
// Create the window.
hwnd = CreateWindowEx(0, // Optional window styles.
CLASS_NAME, // Window class
L"Learn to Program Windows", // Window text
WS_CHILDWINDOW, // Window style
// Size and position
0, 0, 600, 300,
winId,
NULL, // Menu
hInstance, // Instance handle
NULL // Additional application data
);
set_renderer(std::make_unique<slint_platform::SkiaRenderer>(
slint_platform::NativeWindowHandle::from_win32(hwnd, hInstance),
slint::PhysicalSize({ 600, 300 })));
SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)this);
}
@ -61,7 +85,7 @@ struct MyWindowAdapter : NativeWindowHandle,
void render()
{
renderer().render(physical_size());
renderer().render(window(), physical_size());
if (has_active_animations())
request_redraw();
}
@ -205,37 +229,4 @@ struct MyWindowAdapter : NativeWindowHandle,
}
private:
static HWND create_window(HWND parentWindow)
{
HINSTANCE hInstance = GetModuleHandleW(nullptr);
// Register the window class.
const wchar_t CLASS_NAME[] = L"Sample Window Class";
WNDCLASS wc = {};
wc.lpfnWndProc = MyWindowAdapter::windowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
// Create the window.
HWND hwnd = CreateWindowEx(0, // Optional window styles.
CLASS_NAME, // Window class
L"Learn to Program Windows", // Window text
WS_CHILDWINDOW, // Window style
// Size and position
0, 0, 600, 300,
parentWindow,
NULL, // Menu
hInstance, // Instance handle
NULL // Additional application data
);
return hwnd;
}
};

View file

@ -54,11 +54,11 @@ class MyWindow : public QWindow, public slint_platform::WindowAdapter<slint_plat
public:
MyWindow(QWindow *parentWindow = nullptr)
: QWindow(parentWindow),
slint_platform::WindowAdapter<slint_platform::SkiaRenderer>(
window_handle_for_qt_window(this),
slint::PhysicalSize({ uint32_t(width()), uint32_t(height()) }))
: QWindow(parentWindow), slint_platform::WindowAdapter<slint_platform::SkiaRenderer>()
{
set_renderer(std::make_unique<slint_platform::SkiaRenderer>(
window_handle_for_qt_window(this),
slint::PhysicalSize({ uint32_t(width()), uint32_t(height()) })));
}
/*void keyEvent(QKeyEvent *event) override
@ -71,7 +71,7 @@ public:
slint_platform::update_timers_and_animations();
auto windowSize = slint::PhysicalSize({ uint32_t(width()), uint32_t(height()) });
renderer().render(windowSize);
renderer().render(window(), windowSize);
if (has_active_animations()) {
requestUpdate();