diff --git a/modules/juce_gui_basics/native/juce_WaylandSurfaceScale_linux.cpp b/modules/juce_gui_basics/native/juce_WaylandSurfaceScale_linux.cpp index 9bfe1a5f75c..4f72395865c 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 f631297eb49..995c0e74565 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 a148d15dfd4..21997379d2a 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());