Browse Source

DRY-ed some code by adding a static Component::getApproximateScaleFactorForComponent() method

tags/2021-05-28
ed 6 years ago
parent
commit
2539f4d24f
4 changed files with 22 additions and 33 deletions
  1. +15
    -0
      modules/juce_gui_basics/components/juce_Component.cpp
  2. +5
    -0
      modules/juce_gui_basics/components/juce_Component.h
  3. +1
    -17
      modules/juce_gui_basics/menus/juce_PopupMenu.cpp
  4. +1
    -16
      modules/juce_gui_basics/widgets/juce_Slider.cpp

+ 15
- 0
modules/juce_gui_basics/components/juce_Component.cpp View File

@@ -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)
{


+ 5
- 0
modules/juce_gui_basics/components/juce_Component.h View File

@@ -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).


+ 1
- 17
modules/juce_gui_basics/menus/juce_PopupMenu.cpp View File

@@ -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;


+ 1
- 16
modules/juce_gui_basics/widgets/juce_Slider.cpp View File

@@ -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;


Loading…
Cancel
Save