From 2539f4d24f99deb64e118eb6b62f4ac2f3d70d52 Mon Sep 17 00:00:00 2001 From: ed Date: Mon, 29 Apr 2019 12:38:26 +0100 Subject: [PATCH] DRY-ed some code by adding a static Component::getApproximateScaleFactorForComponent() method --- .../components/juce_Component.cpp | 15 +++++++++++++++ .../components/juce_Component.h | 5 +++++ .../juce_gui_basics/menus/juce_PopupMenu.cpp | 18 +----------------- .../juce_gui_basics/widgets/juce_Slider.cpp | 17 +---------------- 4 files changed, 22 insertions(+), 33 deletions(-) diff --git a/modules/juce_gui_basics/components/juce_Component.cpp b/modules/juce_gui_basics/components/juce_Component.cpp index 0b537d5a28..1a2106a4c3 100644 --- a/modules/juce_gui_basics/components/juce_Component.cpp +++ b/modules/juce_gui_basics/components/juce_Component.cpp @@ -1288,6 +1288,21 @@ AffineTransform Component::getTransform() const return affineTransform != nullptr ? *affineTransform : AffineTransform(); } +float Component::getApproximateScaleFactorForComponent (Component* targetComponent) +{ + AffineTransform transform; + + for (auto* target = targetComponent; target != nullptr; target = target->getParentComponent()) + { + transform = transform.followedBy (target->getTransform()); + + if (target->isOnDesktop()) + transform = transform.scaled (target->getDesktopScaleFactor()); + } + + return (transform.getScaleFactor() / Desktop::getInstance().getGlobalScaleFactor()); +} + //============================================================================== bool Component::hitTest (int x, int y) { diff --git a/modules/juce_gui_basics/components/juce_Component.h b/modules/juce_gui_basics/components/juce_Component.h index 1dcbf881b7..1a818bb3b8 100644 --- a/modules/juce_gui_basics/components/juce_Component.h +++ b/modules/juce_gui_basics/components/juce_Component.h @@ -590,6 +590,11 @@ public: */ bool isTransformed() const noexcept; + /** Returns the approximate scale factor for a given component by traversing its parent hierarchy + and applying each transform and finally scaling this by the global scale factor. + */ + static float JUCE_CALLTYPE getApproximateScaleFactorForComponent (Component* targetComponent); + //============================================================================== /** Returns a proportion of the component's width. This is a handy equivalent of (getWidth() * proportion). diff --git a/modules/juce_gui_basics/menus/juce_PopupMenu.cpp b/modules/juce_gui_basics/menus/juce_PopupMenu.cpp index 465cbd7c3e..cb10aad047 100644 --- a/modules/juce_gui_basics/menus/juce_PopupMenu.cpp +++ b/modules/juce_gui_basics/menus/juce_PopupMenu.cpp @@ -218,7 +218,7 @@ struct MenuWindow : public Component if (parentComponent == nullptr && parentWindow == nullptr && lf.shouldPopupMenuScaleWithTargetComponent (options)) if (auto* targetComponent = options.getTargetComponent()) - scaleFactor = getApproximateScaleFactorForTargetComponent (targetComponent); + scaleFactor = Component::getApproximateScaleFactorForComponent (targetComponent); setOpaque (lf.findColour (PopupMenu::backgroundColourId).isOpaque() || ! Desktop::canUseSemiTransparentWindows()); @@ -969,22 +969,6 @@ struct MenuWindow : public Component bool isTopScrollZoneActive() const noexcept { return canScroll() && childYOffset > 0; } bool isBottomScrollZoneActive() const noexcept { return canScroll() && childYOffset < contentHeight - windowPos.getHeight(); } - //============================================================================== - static float getApproximateScaleFactorForTargetComponent (Component* targetComponent) - { - AffineTransform transform; - - for (auto* target = targetComponent; target != nullptr; target = target->getParentComponent()) - { - transform = transform.followedBy (target->getTransform()); - - if (target->isOnDesktop()) - transform = transform.scaled (target->getDesktopScaleFactor()); - } - - return (transform.getScaleFactor() / Desktop::getInstance().getGlobalScaleFactor()); - } - //============================================================================== MenuWindow* parent; const Options options; diff --git a/modules/juce_gui_basics/widgets/juce_Slider.cpp b/modules/juce_gui_basics/widgets/juce_Slider.cpp index 783dc6b8e5..51b70dd4ce 100644 --- a/modules/juce_gui_basics/widgets/juce_Slider.cpp +++ b/modules/juce_gui_basics/widgets/juce_Slider.cpp @@ -1298,7 +1298,7 @@ public: font (s.getLookAndFeel().getSliderPopupFont (s)) { if (isOnDesktop) - setTransform (AffineTransform::scale (getApproximateScaleFactor (&s))); + setTransform (AffineTransform::scale (Component::getApproximateScaleFactorForComponent (&s))); setAlwaysOnTop (true); setAllowedPlacement (owner.getLookAndFeel().getSliderPopupPlacement (s)); @@ -1338,21 +1338,6 @@ public: } private: - static float getApproximateScaleFactor (Component* targetComponent) - { - AffineTransform transform; - - for (Component* target = targetComponent; target != nullptr; target = target->getParentComponent()) - { - transform = transform.followedBy (target->getTransform()); - - if (target->isOnDesktop()) - transform = transform.scaled (target->getDesktopScaleFactor()); - } - - return (transform.getScaleFactor() / Desktop::getInstance().getGlobalScaleFactor()); - } - //============================================================================== Slider& owner; Font font;