From 7eeea6b6f3c04a961744069b1ea9961be6d613dd Mon Sep 17 00:00:00 2001 From: topemalheiro Date: Sat, 14 Nov 2026 20:08:50 +0000 Subject: [PATCH] Wayland: Start a popup with the scale of the window it belongs to A new surface learns its scale from the compositor only after a roundtrip, and the outputs it is on only after it has been mapped. Until then it reported the largest integer scale of all outputs. PopupMenu reads the scale of its window as soon as the window is created, to compare it with the scale of its target component. With fractional scaling, for example a 1.2 output that the compositor rounds up to 2 for wl_output, a menu opened from a window at 1.2 computed a scale of 1.2 / 2 = 0.6: it came up at 60% of its size and away from its target component. A popup almost always opens on the output of the window it belongs to, so until the compositor reports the popup's own scale, the popup now takes the preferred scales of its owner window, and uses the owner's integer scale while it hasn't entered an output. The owner's override scale is not taken. Co-Authored-By: Claude Opus 5.5 --- .../native/juce_WaylandSurfaceScale_linux.cpp | 34 +++++++++++++++++++ .../native/juce_WaylandSurfaceScale_linux.h | 1 + .../native/juce_Windowing_wayland_linux.cpp | 22 +++++++++++- 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/modules/juce_gui_basics/native/juce_WaylandSurfaceScale_linux.cpp b/modules/juce_gui_basics/native/juce_WaylandSurfaceScale_linux.cpp index 9bfe1a5f75ce..4f72395865c6 100644 --- a/modules/juce_gui_basics/native/juce_WaylandSurfaceScale_linux.cpp +++ b/modules/juce_gui_basics/native/juce_WaylandSurfaceScale_linux.cpp @@ -75,6 +75,20 @@ WaylandSurfaceScale::Update WaylandSurfaceScale::setOverrideScale (std::optional }); } +WaylandSurfaceScale::Update WaylandSurfaceScale::inheritPreferredScales (const WaylandSurfaceScale& other) +{ + return updateScaleState ([&] + { + // Stands in for the compositor's preferences until it sends this surface its own. + // The override belongs to the other surface's component, so it isn't taken. + if (! preferredFractionalScale120.has_value()) + preferredFractionalScale120 = other.preferredFractionalScale120; + + if (! preferredBufferScale.has_value()) + preferredBufferScale = other.preferredBufferScale; + }); +} + std::optional WaylandSurfaceScale::getOverrideScale() const { return overrideScale; @@ -432,6 +446,26 @@ class WaylandSurfaceScaleTests final : public UnitTest expectEquals (state.getScaleFactor(), 2.0); }); + testCase ("A surface uses inherited preferred scales until it receives its own", [&] + { + WaylandSurfaceScale owner; + owner.setOutputScale (2); + owner.setPreferredFractionalScale120 (144); + owner.setOverrideScale (1.5); + + WaylandSurfaceScale state; + state.setOutputScale (2); + state.inheritPreferredScales (owner); + expectEquals (state.getScaleFactor(), 1.2); + expect (! state.getOverrideScale().has_value()); + + state.setPreferredFractionalScale120 (180); + expectEquals (state.getScaleFactor(), 1.5); + + state.inheritPreferredScales (owner); + expectEquals (state.getScaleFactor(), 1.5); + }); + // JUCE scale override. testCase ("Only changes to the JUCE scale factor are reported", [&] { diff --git a/modules/juce_gui_basics/native/juce_WaylandSurfaceScale_linux.h b/modules/juce_gui_basics/native/juce_WaylandSurfaceScale_linux.h index f631297eb492..995c0e74565e 100644 --- a/modules/juce_gui_basics/native/juce_WaylandSurfaceScale_linux.h +++ b/modules/juce_gui_basics/native/juce_WaylandSurfaceScale_linux.h @@ -64,6 +64,7 @@ class WaylandSurfaceScale final Update setPreferredBufferScale (int scale); Update setOutputScale (int scale); Update setOverrideScale (std::optional scale); + Update inheritPreferredScales (const WaylandSurfaceScale& other); std::optional getOverrideScale() const; double getScaleFactor() const; diff --git a/modules/juce_gui_basics/native/juce_Windowing_wayland_linux.cpp b/modules/juce_gui_basics/native/juce_Windowing_wayland_linux.cpp index a148d15dfd46..21997379d2ac 100644 --- a/modules/juce_gui_basics/native/juce_Windowing_wayland_linux.cpp +++ b/modules/juce_gui_basics/native/juce_Windowing_wayland_linux.cpp @@ -300,6 +300,11 @@ class WaylandComponentPeer final : public ComponentPeer, // No repaint or listener notification is needed while the peer is being constructed. surfaceScale.setOutputScale (getScaleForSurfaceOutputs()); + + // The compositor sends a new surface its preferred scale only after a roundtrip, but a popup + // may need its scale straight away: PopupMenu sizes itself by comparing it with its target's. + if (const auto* owner = getPopupOwner()) + surfaceScale.inheritPreferredScales (owner->surfaceScale); createScaleObjects(); updateToplevelSizeConstraints(); WaylandProtocol::wlSurfaceAddListener (surface.get(), &surfaceListener, callbackState.get()); @@ -1744,10 +1749,25 @@ class WaylandComponentPeer final : public ComponentPeer, { auto* windowSystem = WaylandWindowSystem::getInstance(); - return surfaceOutputs.getLargestScale (windowSystem->getLargestOutputScale(), + // Until the surface has entered an output, guess the scale it will get there. + const auto* owner = getPopupOwner(); + const auto scaleWithoutOutputs = owner != nullptr ? owner->surfaceScale.getIntegerCompositorScale() + : windowSystem->getLargestOutputScale(); + + return surfaceOutputs.getLargestScale (scaleWithoutOutputs, [windowSystem] (wl_output* output) { return windowSystem->getScaleForOutput (output); }); } + // A popup almost always opens on the output of the window it belongs to, so until the compositor + // reports the popup's own scale, the popup takes that window's. + const WaylandComponentPeer* getPopupOwner() const + { + if (const auto* popupRole = getPopupRole(); popupRole != nullptr && popupRole->owner.has_value()) + return popupRole->owner->toplevel.get(); + + return nullptr; + } + void updateOutputScale() { const auto update = surfaceScale.setOutputScale (getScaleForSurfaceOutputs());