Skip to content

Commit

Permalink
ZWidget Wayland backend: Actually scale everything with the desired s…
Browse files Browse the repository at this point in the history
…cale factor
  • Loading branch information
LupertEverett authored and dpjudas committed Jul 8, 2024
1 parent c5771a6 commit aea3429
Showing 1 changed file with 40 additions and 40 deletions.
80 changes: 40 additions & 40 deletions Thirdparty/ZWidget/src/window/wayland/wayland_display_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,27 @@ WaylandDisplayWindow::WaylandDisplayWindow(DisplayWindowHost* windowHost, bool p
s_ScreenSize = Size(width, height);
};

if (m_FractionalScaleManager)
{
m_FractionalScale = m_FractionalScaleManager.get_fractional_scale(m_AppSurface);

m_FractionalScale.on_preferred_scale() = [&](uint32_t scale_numerator) {
// parameter is the numerator of a fraction with the denominator of 120
m_ScaleFactor = scale_numerator / 120.0;

m_NeedsUpdate = true;
windowHost->OnWindowDpiScaleChanged();
};
}
else
{
m_waylandOutput.on_scale() = [&] (int32_t scale) {
m_ScaleFactor = scale;
m_NeedsUpdate = true;
windowHost->OnWindowDpiScaleChanged();
};
}

/*
m_DataDevice = m_DataDeviceManager.get_data_device(m_waylandSeat);
Expand Down Expand Up @@ -178,42 +199,18 @@ void WaylandDisplayWindow::InitializeToplevel()
windowHost->OnWindowGeometryChanged();
};

if (m_FractionalScaleManager)
{
m_FractionalScale = m_FractionalScaleManager.get_fractional_scale(m_AppSurface);

m_FractionalScale.on_preferred_scale() = [&](uint32_t scale_numerator) {
// parameter is the numerator of a fraction with the denominator of 120
m_ScaleFactor = scale_numerator / 120.0;

m_NeedsUpdate = true;
windowHost->OnWindowDpiScaleChanged();
};
}
else
{
m_waylandOutput.on_scale() = [&] (int32_t scale) {
m_ScaleFactor = scale;
m_NeedsUpdate = true;
windowHost->OnWindowDpiScaleChanged();
};
}

m_AppSurface.commit();

s_waylandDisplay.roundtrip();

// These have to be added after the roundtrip
if (m_XDGToplevel)
{
m_XDGToplevel.on_configure() = [&] (int32_t width, int32_t height, wayland::array_t states) {
OnXDGToplevelConfigureEvent(width, height);
};
m_XDGToplevel.on_configure() = [&] (int32_t width, int32_t height, wayland::array_t states) {
OnXDGToplevelConfigureEvent(width, height);
};

m_XDGToplevel.on_close() = [&] () {
OnExitEvent();
};
}
m_XDGToplevel.on_close() = [&] () {
OnExitEvent();
};

if (!hasKeyboard)
throw std::runtime_error("No keyboard detected!");
Expand Down Expand Up @@ -527,7 +524,7 @@ Rect WaylandDisplayWindow::GetWindowFrame() const

Size WaylandDisplayWindow::GetClientSize() const
{
return m_WindowSize;
return m_WindowSize / m_ScaleFactor;
}

int WaylandDisplayWindow::GetPixelWidth() const
Expand Down Expand Up @@ -581,12 +578,12 @@ void WaylandDisplayWindow::SetClipboardText(const std::string& text)

Point WaylandDisplayWindow::MapFromGlobal(const Point& pos) const
{
return pos - m_WindowGlobalPos;
return (pos - m_WindowGlobalPos) / m_ScaleFactor;
}

Point WaylandDisplayWindow::MapToGlobal(const Point& pos) const
{
return m_WindowGlobalPos + pos;
return (m_WindowGlobalPos + pos) / m_ScaleFactor;
}

void WaylandDisplayWindow::ProcessEvents()
Expand Down Expand Up @@ -654,8 +651,8 @@ void WaylandDisplayWindow::UpdateTimers()
void WaylandDisplayWindow::OnXDGToplevelConfigureEvent(int32_t width, int32_t height)
{
Rect rect = GetWindowFrame();
rect.width = width;
rect.height = height;
rect.width = width / m_ScaleFactor;
rect.height = height / m_ScaleFactor;
SetWindowFrame(rect);
windowHost->OnWindowGeometryChanged();
}
Expand Down Expand Up @@ -734,7 +731,7 @@ void WaylandDisplayWindow::OnMouseReleaseEvent(InputKey button)

void WaylandDisplayWindow::OnMouseMoveEvent(Point surfacePos)
{
m_SurfaceMousePos = surfacePos;
m_SurfaceMousePos = surfacePos / m_ScaleFactor;
windowHost->OnWindowMouseMove(m_SurfaceMousePos);
}

Expand Down Expand Up @@ -770,12 +767,15 @@ void WaylandDisplayWindow::CreateBuffers(int32_t width, int32_t height)
if (shared_mem)
shared_mem.reset();

shared_mem = std::make_shared<SharedMemHelper>(width * height * 4);
auto pool = m_waylandSHM.create_pool(shared_mem->get_fd(), width * height * 4);
int scaled_width = width * m_ScaleFactor;
int scaled_height = height * m_ScaleFactor;

shared_mem = std::make_shared<SharedMemHelper>(scaled_width * scaled_height * 4);
auto pool = m_waylandSHM.create_pool(shared_mem->get_fd(), scaled_width * scaled_height * 4);

m_AppSurfaceBuffer = pool.create_buffer(0, width, height, width * 4, wayland::shm_format::xrgb8888);
m_AppSurfaceBuffer = pool.create_buffer(0, scaled_width, scaled_height, scaled_width * 4, wayland::shm_format::xrgb8888);

m_WindowSize = Size(width, height);
m_WindowSize = Size(scaled_width, scaled_height);
}

std::string WaylandDisplayWindow::GetWaylandCursorName(StandardCursor cursor)
Expand Down

0 comments on commit aea3429

Please sign in to comment.