Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rendering of the Qt platform example with high-dpi #2771

Merged
merged 1 commit into from May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions api/cpp/include/slint_platform.h
Expand Up @@ -140,6 +140,15 @@ class WindowAdapter
}
}

/// Notifies the platform about a change in the device pixel ratio.
void dispatch_scale_factor_change_event(float factor)
{
private_api::assert_main_thread();
if (was_initialized) {
cbindgen_private::slint_windowrc_dispatch_scale_factor_change_event(&self, factor);
}
}

/// Returns true if the window is currently animating
bool has_active_animations() const
{
Expand Down
14 changes: 13 additions & 1 deletion api/cpp/platform.rs
Expand Up @@ -139,7 +139,7 @@ pub unsafe extern "C" fn slint_windowrc_has_active_animations(
window_adapter.window().has_active_animations()
}

/// Dispatch a key pressed or release event
/// Dispatch resize event
#[no_mangle]
pub unsafe extern "C" fn slint_windowrc_dispatch_resize_event(
handle: *const WindowAdapterRcOpaque,
Expand All @@ -152,6 +152,18 @@ pub unsafe extern "C" fn slint_windowrc_dispatch_resize_event(
});
}

/// Dispatch scale factor change event
#[no_mangle]
pub unsafe extern "C" fn slint_windowrc_dispatch_scale_factor_change_event(
handle: *const WindowAdapterRcOpaque,
scale_factor: f32,
) {
let window_adapter = &*(handle as *const Rc<dyn WindowAdapter>);
window_adapter
.window()
.dispatch_event(i_slint_core::platform::WindowEvent::ScaleFactorChanged { scale_factor });
}

#[no_mangle]
pub extern "C" fn slint_platform_update_timers_and_animations() {
i_slint_core::platform::update_timers_and_animations()
Expand Down
23 changes: 12 additions & 11 deletions api/cpp/tests/manual/platform_qt/main.cpp
Expand Up @@ -56,8 +56,7 @@ class MyWindow : public QWindow, public slint_platform::WindowAdapter
public:
MyWindow(QWindow *parentWindow = nullptr) : QWindow(parentWindow)
{
m_renderer.emplace(window_handle_for_qt_window(this),
slint::PhysicalSize({ uint32_t(width()), uint32_t(height()) }));
m_renderer.emplace(window_handle_for_qt_window(this), physical_size());
}

slint_platform::AbstractRenderer &renderer() override { return m_renderer.value(); }
Expand All @@ -71,8 +70,7 @@ class MyWindow : public QWindow, public slint_platform::WindowAdapter
{
slint_platform::update_timers_and_animations();

auto windowSize = slint::PhysicalSize({ uint32_t(width()), uint32_t(height()) });
m_renderer->render(window(), windowSize);
m_renderer->render(window(), physical_size());

if (has_active_animations()) {
requestUpdate();
Expand All @@ -91,6 +89,8 @@ class MyWindow : public QWindow, public slint_platform::WindowAdapter

void show() const override
{
const_cast<WindowAdapter *>(static_cast<const WindowAdapter *>(this))
->dispatch_scale_factor_change_event(devicePixelRatio());
auto window = const_cast<QWindow *>(static_cast<const QWindow *>(this));
window->QWindow::show();
m_renderer->show();
Expand All @@ -102,21 +102,22 @@ class MyWindow : public QWindow, public slint_platform::WindowAdapter
}
slint::PhysicalSize physical_size() const override
{
auto s = size();
return slint::PhysicalSize({ uint32_t(s.width()), uint32_t(s.height()) });
auto windowSize = slint::LogicalSize({ float(width()), float(height()) });
float scale_factor = devicePixelRatio();
return slint::PhysicalSize({ uint32_t(windowSize.width * scale_factor),
uint32_t(windowSize.height * scale_factor) });
}

void request_redraw() const override { const_cast<MyWindow *>(this)->requestUpdate(); }

void resizeEvent(QResizeEvent *ev) override
{
auto windowSize = slint::PhysicalSize(
{ uint32_t(ev->size().width()), uint32_t(ev->size().height()) });
m_renderer->resize(windowSize);
auto logicalSize = ev->size();
float scale_factor = devicePixelRatio();
m_renderer->resize(slint::PhysicalSize({ uint32_t(logicalSize.width() * scale_factor),
uint32_t(logicalSize.height() * scale_factor) }));
WindowAdapter::dispatch_resize_event(
slint::LogicalSize({ float(windowSize.width) / scale_factor,
float(windowSize.height) / scale_factor }));
slint::LogicalSize({ float(logicalSize.width()), float(logicalSize.height()) }));
}

void mousePressEvent(QMouseEvent *event) override
Expand Down