Skip to content

Commit

Permalink
Handle bound components in menus (#5402)
Browse files Browse the repository at this point in the history
I was super careful to make sure my menus lived less long than
the thises they bound, but in a close editor case I am out of
luck. Tnanks to the TAP folks, especially daniel, I learned
to use juce::Component::SafePointer<> which does what I need.
See discussion in #5401 and the forum post linked there for more

Closes #5401
  • Loading branch information
baconpaul authored Nov 14, 2021
1 parent fc8e653 commit 57d267a
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 53 deletions.
38 changes: 7 additions & 31 deletions src/surge-xt/gui/SurgeGUIEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1969,7 +1969,7 @@ void SurgeGUIEditor::effectSettingsBackgroundClick(int whichScene, Surge::Widget

if (!juce::PopupMenu::dismissAllActiveMenus())
{
fxGridMenu.showMenuAsync(juce::PopupMenu::Options(), [c](int i) { c->endHover(); });
fxGridMenu.showMenuAsync(juce::PopupMenu::Options(), Surge::GUI::makeEndHoverCallback(c));
}
}

Expand Down Expand Up @@ -2046,12 +2046,7 @@ void SurgeGUIEditor::showZoomMenu(const juce::Point<int> &where,
if (!juce::PopupMenu::dismissAllActiveMenus())
{
auto m = makeZoomMenu(where, true);
m.showMenuAsync(optionsForPosition(where), [launchFrom](int i) {
if (launchFrom)
{
launchFrom->endHover();
}
});
m.showMenuAsync(optionsForPosition(where), Surge::GUI::makeEndHoverCallback(launchFrom));
}
}

Expand All @@ -2061,12 +2056,7 @@ void SurgeGUIEditor::showMPEMenu(const juce::Point<int> &where,
if (!juce::PopupMenu::dismissAllActiveMenus())
{
auto m = makeMpeMenu(where, true);
m.showMenuAsync(optionsForPosition(where), [launchFrom](int i) {
if (launchFrom)
{
launchFrom->endHover();
}
});
m.showMenuAsync(optionsForPosition(where), Surge::GUI::makeEndHoverCallback(launchFrom));
}
}
void SurgeGUIEditor::showLfoMenu(const juce::Point<int> &where,
Expand All @@ -2075,12 +2065,7 @@ void SurgeGUIEditor::showLfoMenu(const juce::Point<int> &where,
if (!juce::PopupMenu::dismissAllActiveMenus())
{
auto m = makeLfoMenu(where);
m.showMenuAsync(optionsForPosition(where), [launchFrom](int i) {
if (launchFrom)
{
launchFrom->endHover();
}
});
m.showMenuAsync(optionsForPosition(where), Surge::GUI::makeEndHoverCallback(launchFrom));
}
}

Expand All @@ -2103,12 +2088,7 @@ void SurgeGUIEditor::showTuningMenu(const juce::Point<int> &where,
{
auto m = makeTuningMenu(where, true);

m.showMenuAsync(optionsForPosition(where), [launchFrom](int i) {
if (launchFrom)
{
launchFrom->endHover();
}
});
m.showMenuAsync(optionsForPosition(where), Surge::GUI::makeEndHoverCallback(launchFrom));
}
}

Expand Down Expand Up @@ -2338,12 +2318,8 @@ void SurgeGUIEditor::showSettingsMenu(const juce::Point<int> &where,

if (!juce::PopupMenu::dismissAllActiveMenus())
{
settingsMenu.showMenuAsync(optionsForPosition(where), [launchFrom](int i) {
if (launchFrom != nullptr)
{
launchFrom->endHover();
}
});
settingsMenu.showMenuAsync(optionsForPosition(where),
Surge::GUI::makeEndHoverCallback(launchFrom));
}
}

Expand Down
16 changes: 6 additions & 10 deletions src/surge-xt/gui/SurgeGUIEditorValueCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ int32_t SurgeGUIEditor::controlModifierClicked(Surge::GUI::IComponentTagValue *c
if (!juce::PopupMenu::dismissAllActiveMenus())
{
contextMenu.showMenuAsync(optionsForPosition(cwhere),
[control](int i) { control->endHover(); });
Surge::GUI::makeEndHoverCallback(control));
}
return 1;
}
Expand Down Expand Up @@ -537,7 +537,7 @@ int32_t SurgeGUIEditor::controlModifierClicked(Surge::GUI::IComponentTagValue *c
if (!juce::PopupMenu::dismissAllActiveMenus())
{
contextMenu.showMenuAsync(optionsForPosition(cwhere),
[control](int i) { control->endHover(); });
Surge::GUI::makeEndHoverCallback(control));
}
return 1;
}
Expand Down Expand Up @@ -1028,7 +1028,7 @@ int32_t SurgeGUIEditor::controlModifierClicked(Surge::GUI::IComponentTagValue *c
if (!juce::PopupMenu::dismissAllActiveMenus())
{
contextMenu.showMenuAsync(juce::PopupMenu::Options(),
[control](int opt) { control->endHover(); });
Surge::GUI::makeEndHoverCallback(control));
}
return 1;
}
Expand Down Expand Up @@ -2234,7 +2234,7 @@ int32_t SurgeGUIEditor::controlModifierClicked(Surge::GUI::IComponentTagValue *c
if (!juce::PopupMenu::dismissAllActiveMenus())
{
contextMenu.showMenuAsync(juce::PopupMenu::Options(),
[control](int i) { control->endHover(); });
Surge::GUI::makeEndHoverCallback(control));
}
return 1;
}
Expand Down Expand Up @@ -2448,12 +2448,8 @@ void SurgeGUIEditor::valueChanged(Surge::GUI::IComponentTagValue *control)
auto launchFrom = control;
if (!juce::PopupMenu::dismissAllActiveMenus())
{
m.showMenuAsync(juce::PopupMenu::Options(), [launchFrom](int i) {
if (launchFrom)
{
launchFrom->endHover();
}
});
m.showMenuAsync(juce::PopupMenu::Options(),
Surge::GUI::makeEndHoverCallback(launchFrom));
}
}

Expand Down
34 changes: 34 additions & 0 deletions src/surge-xt/gui/SurgeJUCEHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,45 @@
#define SURGE_XT_JUCEHELPERS_H

#include "juce_gui_basics/juce_gui_basics.h"
#include "SurgeGUICallbackInterfaces.h"

namespace Surge
{
namespace GUI
{

template <typename T>
inline std::function<void(int)> makeAsyncCallback(T *that, std::function<void(T *, int)> cb)
{
return [safethat = juce::Component::SafePointer<T>(that), cb](int x) {
if (safethat)
cb(safethat, x);
};
}

template <typename T> inline std::function<void(int)> makeEndHoverCallback(T *that)
{
return [safethat = juce::Component::SafePointer<T>(that)](int x) {
if (safethat)
{
safethat->endHover();
}
};
}

template <>
inline std::function<void(int)> makeEndHoverCallback(Surge::GUI::IComponentTagValue *that)
{
return
[safethat = juce::Component::SafePointer<juce::Component>(that->asJuceComponent())](int x) {
if (safethat)
{
auto igtv = dynamic_cast<Surge::GUI::IComponentTagValue *>(safethat.getComponent());
if (igtv)
igtv->endHover();
}
};
}
struct WheelAccumulationHelper
{
float accum{0};
Expand Down
6 changes: 4 additions & 2 deletions src/surge-xt/gui/overlays/ModulationEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "widgets/MenuCustomComponents.h"
#include "widgets/ModulatableSlider.h"
#include "widgets/MultiSwitch.h"
#include "SurgeJUCEHelpers.h"

namespace Surge
{
Expand Down Expand Up @@ -697,7 +698,7 @@ void ModulationSideControls::valueChanged(GUI::IComponentTagValue *c)
});
if (!juce::PopupMenu::dismissAllActiveMenus())
{
men.showMenuAsync(juce::PopupMenu::Options(), [this](int) { filterW->endHover(); });
men.showMenuAsync(juce::PopupMenu::Options(), GUI::makeEndHoverCallback(filterW.get()));
}
}
break;
Expand All @@ -712,7 +713,8 @@ void ModulationSideControls::valueChanged(GUI::IComponentTagValue *c)
men.addItem("Coming soon!", [this]() {});
if (!juce::PopupMenu::dismissAllActiveMenus())
{
men.showMenuAsync(juce::PopupMenu::Options(), [this](int) { addSourceW->endHover(); });
men.showMenuAsync(juce::PopupMenu::Options(),
GUI::makeEndHoverCallback(addSourceW.get()));
}
}
break;
Expand Down
3 changes: 2 additions & 1 deletion src/surge-xt/gui/widgets/ModulationSourceButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "SurgeGUIEditor.h"
#include "SurgeImage.h"
#include "SurgeGUIUtils.h"
#include "SurgeJUCEHelpers.h"

namespace Surge
{
Expand Down Expand Up @@ -323,7 +324,7 @@ void ModulationSourceButton::mouseDown(const juce::MouseEvent &event)

if (!juce::PopupMenu::dismissAllActiveMenus())
{
menu.showMenuAsync(juce::PopupMenu::Options(), [this](int) { endHover(); });
menu.showMenuAsync(juce::PopupMenu::Options(), Surge::GUI::makeEndHoverCallback(this));
}
return;
}
Expand Down
3 changes: 2 additions & 1 deletion src/surge-xt/gui/widgets/PatchSelector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "overlays/PatchStoreDialog.h"
#include "PatchDB.h"
#include "fmt/core.h"
#include "SurgeJUCEHelpers.h"

namespace Surge
{
Expand Down Expand Up @@ -266,7 +267,7 @@ void PatchSelector::mouseDown(const juce::MouseEvent &e)
if (!juce::PopupMenu::dismissAllActiveMenus())
{
menu.showMenuAsync(juce::PopupMenu::Options(),
[this](int) { this->endHover(); });
Surge::GUI::makeEndHoverCallback(this));
}
}

Expand Down
20 changes: 12 additions & 8 deletions src/surge-xt/gui/widgets/XMLConfiguredMenus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,8 @@ OscillatorMenu::OscillatorMenu()
#endif
}

OscillatorMenu::~OscillatorMenu() = default;

void OscillatorMenu::paint(juce::Graphics &g)
{
bg->draw(g, 1.0);
Expand Down Expand Up @@ -374,10 +376,11 @@ void OscillatorMenu::mouseDown(const juce::MouseEvent &event)

if (!juce::PopupMenu::dismissAllActiveMenus())
{
menu.showMenuAsync(juce::PopupMenu::Options(), [this](int) {
isHovered = false;
repaint();
});
menu.showMenuAsync(juce::PopupMenu::Options(),
Surge::GUI::makeAsyncCallback<OscillatorMenu>(this, [](auto *that, int) {
that->isHovered = false;
that->repaint();
}));
}
}

Expand Down Expand Up @@ -486,10 +489,11 @@ void FxMenu::mouseDown(const juce::MouseEvent &event)

if (!juce::PopupMenu::dismissAllActiveMenus())
{
menu.showMenuAsync(juce::PopupMenu::Options(), [this](int i) {
isHovered = false;
repaint();
});
menu.showMenuAsync(juce::PopupMenu::Options(),
Surge::GUI::makeAsyncCallback<FxMenu>(this, [](auto *that, int) {
that->isHovered = false;
that->repaint();
}));
}
}

Expand Down
1 change: 1 addition & 0 deletions src/surge-xt/gui/widgets/XMLConfiguredMenus.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ struct OscillatorMenu : public juce::Component,
public WidgetBaseMixin<OscillatorMenu>
{
OscillatorMenu();
~OscillatorMenu();
void loadSnapshot(int type, TiXmlElement *e, int idx) override;

Surge::GUI::IComponentTagValue *asControlValueInterface() override { return this; };
Expand Down

0 comments on commit 57d267a

Please sign in to comment.