From 0f6f2728f238bf0041626765d7429cb71ad739bb Mon Sep 17 00:00:00 2001 From: reuk Date: Fri, 19 Mar 2021 19:57:09 +0000 Subject: [PATCH] AlertWindow: Scale window according to scale of associatedComponent For DialogWindow, uses the scale of componentToCentreAround. This allows drawing dialog windows at the expected scale in plugins. An associated component must be supplied in order for this to work. --- .../juce_gui_basics/windows/juce_AlertWindow.cpp | 7 ++++--- modules/juce_gui_basics/windows/juce_AlertWindow.h | 3 +++ .../juce_gui_basics/windows/juce_DialogWindow.cpp | 13 ++++++++----- modules/juce_gui_basics/windows/juce_DialogWindow.h | 11 ++++++++++- .../juce_gui_basics/windows/juce_TopLevelWindow.cpp | 2 +- 5 files changed, 26 insertions(+), 10 deletions(-) diff --git a/modules/juce_gui_basics/windows/juce_AlertWindow.cpp b/modules/juce_gui_basics/windows/juce_AlertWindow.cpp index f45630f0cc..06e66686b3 100644 --- a/modules/juce_gui_basics/windows/juce_AlertWindow.cpp +++ b/modules/juce_gui_basics/windows/juce_AlertWindow.cpp @@ -42,7 +42,8 @@ AlertWindow::AlertWindow (const String& title, Component* comp) : TopLevelWindow (title, true), alertIconType (iconType), - associatedComponent (comp) + associatedComponent (comp), + desktopScale (comp != nullptr ? Component::getApproximateScaleFactorForComponent (comp) : 1.0f) { setAlwaysOnTop (juce_areThereAnyAlwaysOnTopWindows()); @@ -583,8 +584,8 @@ private: auto& lf = associatedComponent != nullptr ? associatedComponent->getLookAndFeel() : LookAndFeel::getDefaultLookAndFeel(); - std::unique_ptr alertBox (lf.createAlertWindow (title, message, button1, button2, button3, - iconType, numButtons, associatedComponent)); + std::unique_ptr alertBox (lf.createAlertWindow (title, message, button1, button2, button3, + iconType, numButtons, associatedComponent)); jassert (alertBox != nullptr); // you have to return one of these! diff --git a/modules/juce_gui_basics/windows/juce_AlertWindow.h b/modules/juce_gui_basics/windows/juce_AlertWindow.h index d73eb07b45..7008b279e9 100644 --- a/modules/juce_gui_basics/windows/juce_AlertWindow.h +++ b/modules/juce_gui_basics/windows/juce_AlertWindow.h @@ -460,6 +460,8 @@ protected: void userTriedToCloseWindow() override; /** @internal */ int getDesktopWindowStyleFlags() const override; + /** @internal */ + float getDesktopScaleFactor() const override { return desktopScale; } private: //============================================================================== @@ -479,6 +481,7 @@ private: StringArray textboxNames, comboBoxNames; Component* const associatedComponent; bool escapeKeyCancels = true; + float desktopScale = 1.0f; void exitAlert (Button* button); void updateLayout (bool onlyIncreaseSize); diff --git a/modules/juce_gui_basics/windows/juce_DialogWindow.cpp b/modules/juce_gui_basics/windows/juce_DialogWindow.cpp index cc52070199..037c986c12 100644 --- a/modules/juce_gui_basics/windows/juce_DialogWindow.cpp +++ b/modules/juce_gui_basics/windows/juce_DialogWindow.cpp @@ -27,15 +27,15 @@ namespace juce { DialogWindow::DialogWindow (const String& name, Colour colour, - const bool escapeCloses, const bool onDesktop) + const bool escapeCloses, const bool onDesktop, + const float scale) : DocumentWindow (name, colour, DocumentWindow::closeButton, onDesktop), + desktopScale (scale), escapeKeyTriggersCloseButton (escapeCloses) { } -DialogWindow::~DialogWindow() -{ -} +DialogWindow::~DialogWindow() = default; bool DialogWindow::escapeKeyPressed() { @@ -78,7 +78,10 @@ class DefaultDialogWindow : public DialogWindow public: DefaultDialogWindow (LaunchOptions& options) : DialogWindow (options.dialogTitle, options.dialogBackgroundColour, - options.escapeKeyTriggersCloseButton, true) + options.escapeKeyTriggersCloseButton, true, + options.componentToCentreAround != nullptr + ? Component::getApproximateScaleFactorForComponent (options.componentToCentreAround) + : 1.0f) { setUsingNativeTitleBar (options.useNativeTitleBar); setAlwaysOnTop (juce_areThereAnyAlwaysOnTopWindows()); diff --git a/modules/juce_gui_basics/windows/juce_DialogWindow.h b/modules/juce_gui_basics/windows/juce_DialogWindow.h index a102115257..70b98b3f94 100644 --- a/modules/juce_gui_basics/windows/juce_DialogWindow.h +++ b/modules/juce_gui_basics/windows/juce_DialogWindow.h @@ -62,11 +62,17 @@ public: close button to be triggered @param addToDesktop if true, the window will be automatically added to the desktop; if false, you can use it as a child component + @param desktopScale specifies the scale to use when drawing the window. In a plugin, + the host controls the scale used to render the plugin editor. + You should query the editor scale with + Component::getApproximateScaleFactorForComponent() and pass the + result here. You can ignore this parameter in a standalone app */ DialogWindow (const String& name, Colour backgroundColour, bool escapeKeyTriggersCloseButton, - bool addToDesktop = true); + bool addToDesktop = true, + float desktopScale = 1.0f); /** Destructor. If a content component has been set with setContentOwned(), it will be deleted. @@ -255,8 +261,11 @@ protected: void resized() override; /** @internal */ bool keyPressed (const KeyPress&) override; + /** @internal */ + float getDesktopScaleFactor() const override { return desktopScale; } private: + float desktopScale = 1.0f; bool escapeKeyTriggersCloseButton; JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DialogWindow) diff --git a/modules/juce_gui_basics/windows/juce_TopLevelWindow.cpp b/modules/juce_gui_basics/windows/juce_TopLevelWindow.cpp index 75fe2f91f3..d454803e96 100644 --- a/modules/juce_gui_basics/windows/juce_TopLevelWindow.cpp +++ b/modules/juce_gui_basics/windows/juce_TopLevelWindow.cpp @@ -291,7 +291,7 @@ void TopLevelWindow::centreAroundComponent (Component* c, const int width, const } else { - auto targetCentre = c->localPointToGlobal (c->getLocalBounds().getCentre()); + auto targetCentre = c->localPointToGlobal (c->getLocalBounds().getCentre()) / getDesktopScaleFactor(); auto parentArea = c->getParentMonitorArea(); if (auto* parent = getParentComponent())