From 268ac3d5c8a6622e339344898a5694784e9b3ffb Mon Sep 17 00:00:00 2001 From: ed Date: Mon, 24 Aug 2020 17:07:01 +0100 Subject: [PATCH] Modernised some code in CallOutBox and added some logic to scale the window with its content component's transform --- examples/GUI/DialogsDemo.h | 4 +-- examples/GUI/WidgetsDemo.h | 12 ++++---- .../jucer_ColourPropertyComponent.h | 5 ++-- .../Project/UI/jucer_ContentViewComponents.h | 4 +-- .../jucer_ColourPropertyComponent.h | 9 +++--- .../windows/juce_CallOutBox.cpp | 28 +++++++++---------- .../juce_gui_basics/windows/juce_CallOutBox.h | 14 ++++------ .../misc/juce_LiveConstantEditor.cpp | 4 +-- 8 files changed, 39 insertions(+), 41 deletions(-) diff --git a/examples/GUI/DialogsDemo.h b/examples/GUI/DialogsDemo.h index 9ac2e0e33f..1ac6f4cc66 100644 --- a/examples/GUI/DialogsDemo.h +++ b/examples/GUI/DialogsDemo.h @@ -236,14 +236,14 @@ private: } else if (type == calloutBoxWindow) { - auto* colourSelector = new ColourSelector(); + auto colourSelector = std::make_unique(); colourSelector->setName ("background"); colourSelector->setCurrentColour (findColour (TextButton::buttonColourId)); colourSelector->setColour (ColourSelector::backgroundColourId, Colours::transparentBlack); colourSelector->setSize (300, 400); - CallOutBox::launchAsynchronously (colourSelector, button.getScreenBounds(), nullptr); + CallOutBox::launchAsynchronously (std::move (colourSelector), button.getScreenBounds(), nullptr); } else if (type == extraComponentsAlertWindow) { diff --git a/examples/GUI/WidgetsDemo.h b/examples/GUI/WidgetsDemo.h index 9c8bca98b9..d388433287 100644 --- a/examples/GUI/WidgetsDemo.h +++ b/examples/GUI/WidgetsDemo.h @@ -87,11 +87,11 @@ public: void clicked() override { - auto* colourSelector = new ColourSelector (ColourSelector::showAlphaChannel - | ColourSelector::showColourAtTop - | ColourSelector::editableColour - | ColourSelector::showSliders - | ColourSelector::showColourspace); + auto colourSelector = std::make_unique (ColourSelector::showAlphaChannel + | ColourSelector::showColourAtTop + | ColourSelector::editableColour + | ColourSelector::showSliders + | ColourSelector::showColourspace); colourSelector->setName ("background"); colourSelector->setCurrentColour (findColour (TextButton::buttonColourId)); @@ -99,7 +99,7 @@ public: colourSelector->setColour (ColourSelector::backgroundColourId, Colours::transparentBlack); colourSelector->setSize (300, 400); - CallOutBox::launchAsynchronously (colourSelector, getScreenBounds(), nullptr); + CallOutBox::launchAsynchronously (std::move (colourSelector), getScreenBounds(), nullptr); } using TextButton::clicked; diff --git a/extras/Projucer/Source/ComponentEditor/Properties/jucer_ColourPropertyComponent.h b/extras/Projucer/Source/ComponentEditor/Properties/jucer_ColourPropertyComponent.h index 0b236a0653..2ba8f534dd 100644 --- a/extras/Projucer/Source/ComponentEditor/Properties/jucer_ColourPropertyComponent.h +++ b/extras/Projucer/Source/ComponentEditor/Properties/jucer_ColourPropertyComponent.h @@ -89,8 +89,9 @@ public: void mouseDown (const MouseEvent&) override { - CallOutBox::launchAsynchronously (new ColourSelectorComp (this, canResetToDefault), - getScreenBounds(), nullptr); + CallOutBox::launchAsynchronously (std::make_unique (this, canResetToDefault), + getScreenBounds(), + nullptr); } class ColourSelectorComp : public Component diff --git a/extras/Projucer/Source/Project/UI/jucer_ContentViewComponents.h b/extras/Projucer/Source/Project/UI/jucer_ContentViewComponents.h index 7bc29f5429..cad7258539 100644 --- a/extras/Projucer/Source/Project/UI/jucer_ContentViewComponents.h +++ b/extras/Projucer/Source/Project/UI/jucer_ContentViewComponents.h @@ -178,10 +178,10 @@ public: void clicked() override { - auto* w = new InfoWindow (info); + auto w = std::make_unique (info); w->setSize (width, w->getHeight() * numLines + 10); - CallOutBox::launchAsynchronously (w, getScreenBounds(), nullptr); + CallOutBox::launchAsynchronously (std::move (w), getScreenBounds(), nullptr); } using Button::clicked; diff --git a/extras/Projucer/Source/Utility/UI/PropertyComponents/jucer_ColourPropertyComponent.h b/extras/Projucer/Source/Utility/UI/PropertyComponents/jucer_ColourPropertyComponent.h index 0636ae159c..c853747ddf 100644 --- a/extras/Projucer/Source/Utility/UI/PropertyComponents/jucer_ColourPropertyComponent.h +++ b/extras/Projucer/Source/Utility/UI/PropertyComponents/jucer_ColourPropertyComponent.h @@ -116,10 +116,11 @@ private: if (undoManager != nullptr) undoManager->beginNewTransaction(); - CallOutBox::launchAsynchronously (new PopupColourSelector (colourValue, - defaultColour, - canResetToDefault), - getScreenBounds(), nullptr); + CallOutBox::launchAsynchronously (std::make_unique (colourValue, + defaultColour, + canResetToDefault), + getScreenBounds(), + nullptr); } private: diff --git a/modules/juce_gui_basics/windows/juce_CallOutBox.cpp b/modules/juce_gui_basics/windows/juce_CallOutBox.cpp index 5bb8169387..9cbd6035ea 100644 --- a/modules/juce_gui_basics/windows/juce_CallOutBox.cpp +++ b/modules/juce_gui_basics/windows/juce_CallOutBox.cpp @@ -40,9 +40,7 @@ CallOutBox::CallOutBox (Component& c, Rectangle area, Component* const pare else { setAlwaysOnTop (juce_areThereAnyAlwaysOnTopWindows()); - updatePosition (area, Desktop::getInstance().getDisplays().findDisplayForRect (area).userArea); - addToDesktop (ComponentPeer::windowIsTemporary); startTimer (100); @@ -51,15 +49,14 @@ CallOutBox::CallOutBox (Component& c, Rectangle area, Component* const pare creationTime = Time::getCurrentTime(); } -CallOutBox::~CallOutBox() = default; - //============================================================================== class CallOutBoxCallback : public ModalComponentManager::Callback, private Timer { public: - CallOutBoxCallback (Component* c, const Rectangle& area, Component* parent) - : content (c), callout (*c, area, parent) + CallOutBoxCallback (std::unique_ptr c, const Rectangle& area, Component* parent) + : content (std::move (c)), + callout (*content, area, parent) { callout.setVisible (true); callout.enterModalState (true, this); @@ -80,11 +77,11 @@ public: JUCE_DECLARE_NON_COPYABLE (CallOutBoxCallback) }; -CallOutBox& CallOutBox::launchAsynchronously (Component* content, Rectangle area, Component* parent) +CallOutBox& CallOutBox::launchAsynchronously (std::unique_ptr content, Rectangle area, Component* parent) { jassert (content != nullptr); // must be a valid content component! - return (new CallOutBoxCallback (content, area, parent))->callout; + return (new CallOutBoxCallback (std::move (content), area, parent))->callout; } //============================================================================== @@ -99,7 +96,11 @@ int CallOutBox::getBorderSize() const noexcept return jmax (getLookAndFeel().getCallOutBoxBorderSize (*this), (int) arrowSize); } -void CallOutBox::lookAndFeelChanged() { resized(); repaint(); } +void CallOutBox::lookAndFeelChanged() +{ + resized(); + repaint(); +} void CallOutBox::paint (Graphics& g) { @@ -158,7 +159,7 @@ void CallOutBox::setDismissalMouseClicksAreAlwaysConsumed (bool b) noexcept dismissalMouseClicksAreAlwaysConsumed = b; } -enum { callOutBoxDismissCommandId = 0x4f83a04b }; +static constexpr int callOutBoxDismissCommandId = 0x4f83a04b; void CallOutBox::handleCommandMessage (int commandId) { @@ -193,9 +194,8 @@ void CallOutBox::updatePosition (const Rectangle& newAreaToPointTo, const R availableArea = newAreaToFitIn; auto borderSpace = getBorderSize(); - - Rectangle newBounds (content.getWidth() + borderSpace * 2, - content.getHeight() + borderSpace * 2); + auto newBounds = getLocalArea (&content, Rectangle (content.getWidth() + borderSpace * 2, + content.getHeight() + borderSpace * 2)); auto hw = newBounds.getWidth() / 2; auto hh = newBounds.getHeight() / 2; @@ -250,7 +250,7 @@ void CallOutBox::refreshPath() const float gap = 4.5f; - outline.addBubble (content.getBounds().toFloat().expanded (gap, gap), + outline.addBubble (getLocalArea (&content, content.getLocalBounds().toFloat()).expanded (gap, gap), getLocalBounds().toFloat(), targetPoint - getPosition().toFloat(), getLookAndFeel().getCallOutBoxCornerSize (*this), arrowSize * 0.7f); diff --git a/modules/juce_gui_basics/windows/juce_CallOutBox.h b/modules/juce_gui_basics/windows/juce_CallOutBox.h index 4857772dde..eaa469928c 100644 --- a/modules/juce_gui_basics/windows/juce_CallOutBox.h +++ b/modules/juce_gui_basics/windows/juce_CallOutBox.h @@ -43,11 +43,12 @@ namespace juce @code void mouseUp (const MouseEvent&) { - FoobarContentComp* content = new FoobarContentComp(); + auto content = std::make_unique(); content->setSize (300, 300); - CallOutBox& myBox - = CallOutBox::launchAsynchronously (content, getScreenBounds(), nullptr); + auto& myBox = CallOutBox::launchAsynchronously (std::move (content), + getScreenBounds(), + nullptr); } @endcode @@ -77,9 +78,6 @@ public: Rectangle areaToPointTo, Component* parentComponent); - /** Destructor. */ - ~CallOutBox() override; - //============================================================================== /** Changes the base width of the arrow. */ void setArrowSize (float newSize); @@ -109,15 +107,13 @@ public: @param contentComponent the component to display inside the call-out. This should already have a size set (although the call-out will also update itself when the component's size is changed later). - This component will be owned by the callout box and deleted - later when the box is dismissed. @param areaToPointTo the area that the call-out's arrow should point towards. If a parentComponent is supplied, then this is relative to that parent; otherwise, it's a global screen coord. @param parentComponent if not a nullptr, this is the component to add the call-out to. If this is a nullptr, the call-out will be added to the desktop. */ - static CallOutBox& launchAsynchronously (Component* contentComponent, + static CallOutBox& launchAsynchronously (std::unique_ptr contentComponent, Rectangle areaToPointTo, Component* parentComponent); diff --git a/modules/juce_gui_extra/misc/juce_LiveConstantEditor.cpp b/modules/juce_gui_extra/misc/juce_LiveConstantEditor.cpp index 2631869a3d..ce6ea14ffc 100644 --- a/modules/juce_gui_extra/misc/juce_LiveConstantEditor.cpp +++ b/modules/juce_gui_extra/misc/juce_LiveConstantEditor.cpp @@ -406,14 +406,14 @@ struct ColourEditorComp : public Component, void mouseDown (const MouseEvent&) override { - auto* colourSelector = new ColourSelector(); + auto colourSelector = std::make_unique(); colourSelector->setName ("Colour"); colourSelector->setCurrentColour (getColour()); colourSelector->addChangeListener (this); colourSelector->setColour (ColourSelector::backgroundColourId, Colours::transparentBlack); colourSelector->setSize (300, 400); - CallOutBox::launchAsynchronously (colourSelector, getScreenBounds(), nullptr); + CallOutBox::launchAsynchronously (std::move (colourSelector), getScreenBounds(), nullptr); } void changeListenerCallback (ChangeBroadcaster* source) override