Skip to content

Commit

Permalink
Change the look and feel to have shared pointer lifecycle (#7192)
Browse files Browse the repository at this point in the history
Closes #7156
  • Loading branch information
baconpaul authored Aug 21, 2023
1 parent 2976336 commit 5356a65
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 13 deletions.
23 changes: 21 additions & 2 deletions src/surge-xt/SurgeSynthEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,30 @@ struct VKeyboardSus : public juce::Component
}
};

static std::weak_ptr<SurgeJUCELookAndFeel> surgeLookAndFeelWeakPointer;
static std::mutex surgeLookAndFeelSetupMutex;

//==============================================================================
SurgeSynthEditor::SurgeSynthEditor(SurgeSynthProcessor &p)
: juce::AudioProcessorEditor(&p), processor(p)
{
surgeLF = std::make_unique<SurgeJUCELookAndFeel>(&(processor.surge->storage));
{
std::lock_guard<std::mutex> grd(surgeLookAndFeelSetupMutex);
if (auto sp = surgeLookAndFeelWeakPointer.lock())
{
std::cout << "Re-using shared pointer" << std::endl;
surgeLF = sp;
}
else
{
surgeLF = std::make_shared<SurgeJUCELookAndFeel>();
surgeLookAndFeelWeakPointer = surgeLF;

juce::LookAndFeel::setDefaultLookAndFeel(surgeLF.get());
juce::LookAndFeel::setDefaultLookAndFeel(surgeLF.get());
}

surgeLF->addStorage(&(processor.surge->storage));
}

addKeyListener(this);

Expand Down Expand Up @@ -254,6 +271,8 @@ SurgeSynthEditor::~SurgeSynthEditor()
sge->bitmapStore->clearAllLoadedBitmaps();
}

surgeLF->removeStorage(&(processor.surge->storage));

sge.reset(nullptr);
}

Expand Down
5 changes: 3 additions & 2 deletions src/surge-xt/SurgeSynthEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ class SurgeSynthEditor : public juce::AudioProcessorEditor,
// clang-format on
std::string currentVKBLayout;

std::shared_ptr<SurgeJUCELookAndFeel> surgeLF;
SurgeJUCELookAndFeel *getSurgeLookAndFeel() { return surgeLF.get(); }

private:
// This reference is provided as a quick way for your editor to
// access the processor object that created it.
Expand All @@ -135,8 +138,6 @@ class SurgeSynthEditor : public juce::AudioProcessorEditor,
std::unique_ptr<SurgeGUIEditor> sge;
std::unique_ptr<juce::Drawable> logo;

std::unique_ptr<SurgeJUCELookAndFeel> surgeLF;

JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(SurgeSynthEditor)
};

Expand Down
6 changes: 3 additions & 3 deletions src/surge-xt/gui/SurgeGUIEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ void SurgeGUIEditor::idle()
if (slowIdleCounter++ == 600)
{
slowIdleCounter = 0;
juceEditor->surgeLF->updateDarkIfNeeded();
juceEditor->getSurgeLookAndFeel()->updateDarkIfNeeded();
}

if (needsModUpdate)
Expand Down Expand Up @@ -4596,7 +4596,7 @@ juce::PopupMenu SurgeGUIEditor::makeSkinMenu(const juce::Point<int> &where)
Surge::Storage::getUserDefaultValue(&(synth->storage), Surge::Storage::MenuLightness, 2);
auto resetMenuTo = [this](int i) {
Surge::Storage::updateUserDefaultValue(&(synth->storage), Surge::Storage::MenuLightness, i);
juceEditor->surgeLF->onSkinChanged();
juceEditor->getSurgeLookAndFeel()->onSkinChanged();
};

skinSubMenu.addSeparator();
Expand Down Expand Up @@ -5061,7 +5061,7 @@ void SurgeGUIEditor::reloadFromSkin()
return;
}

juceEditor->surgeLF->setSkin(currentSkin, bitmapStore);
juceEditor->getSurgeLookAndFeel()->setSkin(currentSkin, bitmapStore);

float dbs = juce::Desktop::getInstance().getDisplays().getPrimaryDisplay()->scale;

Expand Down
8 changes: 4 additions & 4 deletions src/surge-xt/gui/SurgeJUCELookAndFeel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ void SurgeJUCELookAndFeel::onSkinChanged()

int menuMode = 0;

if (storage)
if (hasStorage())
{
menuMode = Surge::Storage::getUserDefaultValue(storage, Surge::Storage::MenuLightness, 2);
menuMode = Surge::Storage::getUserDefaultValue(storage(), Surge::Storage::MenuLightness, 2);
}

if (menuMode == 1)
Expand Down Expand Up @@ -464,9 +464,9 @@ void SurgeJUCELookAndFeel::updateDarkIfNeeded()
{
int menuMode = 0;

if (storage)
if (hasStorage())
{
menuMode = Surge::Storage::getUserDefaultValue(storage, Surge::Storage::MenuLightness, 2);
menuMode = Surge::Storage::getUserDefaultValue(storage(), Surge::Storage::MenuLightness, 2);
}

if (menuMode == 1)
Expand Down
15 changes: 13 additions & 2 deletions src/surge-xt/gui/SurgeJUCELookAndFeel.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@

#include "juce_gui_basics/juce_gui_basics.h"
#include "SkinSupport.h"
#include <cassert>

class SurgeJUCELookAndFeel : public juce::LookAndFeel_V4, public Surge::GUI::SkinConsumingComponent
{
public:
SurgeJUCELookAndFeel(SurgeStorage *s) : storage(s) {}
SurgeJUCELookAndFeel() {}

void drawLabel(juce::Graphics &graphics, juce::Label &label) override;
void drawTextEditorOutline(juce::Graphics &graphics, int width, int height,
juce::TextEditor &editor) override;
Expand All @@ -44,7 +46,16 @@ class SurgeJUCELookAndFeel : public juce::LookAndFeel_V4, public Surge::GUI::Ski
bool isMouseDown) override;

void onSkinChanged() override;
SurgeStorage *storage{nullptr};
// SurgeStorage *storage{nullptr};
std::set<SurgeStorage *> storagePointers;
void addStorage(SurgeStorage *s) { storagePointers.insert(s); }
void removeStorage(SurgeStorage *s) { storagePointers.erase(s); }
SurgeStorage *storage()
{
assert(!storagePointers.empty());
return *(storagePointers.begin());
}
bool hasStorage() { return !storagePointers.empty(); }

juce::Font getPopupMenuFont() override;
juce::Font getPopupMenuBoldFont();
Expand Down

0 comments on commit 5356a65

Please sign in to comment.