Skip to content

Commit 7f1924d

Browse files
committed
Refactor PluginInstaller to use smart pointers for UI components
1 parent 8200bc1 commit 7f1924d

2 files changed

Lines changed: 116 additions & 103 deletions

File tree

Source/UI/PluginInstaller.cpp

Lines changed: 106 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -739,38 +739,43 @@ PluginInstallerComponent::PluginInstallerComponent()
739739
font = FontOptions ("Inter", "Regular", 17.0f);
740740
setSize (getWidth() - 10, getHeight() - 10);
741741

742-
addAndMakeVisible (searchLabel);
743-
searchLabel.setFont (font);
744-
searchLabel.setText ("Search:", dontSendNotification);
745-
746-
addAndMakeVisible (searchEditor);
747-
searchEditor.setJustification (Justification::centredLeft);
748-
searchEditor.setTextToShowWhenEmpty ("Search by display name...", Colours::grey);
749-
searchEditor.setFont (FontOptions ("Inter", "Regular", 15.0f));
750-
searchEditor.setPopupMenuEnabled (false);
751-
searchEditor.onTextChange = [this]
742+
searchLabel = std::make_unique<Label>();
743+
searchLabel->setFont (font);
744+
searchLabel->setText ("Search:", dontSendNotification);
745+
addAndMakeVisible (searchLabel.get());
746+
747+
searchEditor = std::make_unique<TextEditor>();
748+
searchEditor->setJustification (Justification::centredLeft);
749+
searchEditor->setTextToShowWhenEmpty ("Search by display name...", Colours::grey);
750+
searchEditor->setFont (FontOptions ("Inter", "Regular", 15.0f));
751+
searchEditor->setPopupMenuEnabled (false);
752+
searchEditor->onTextChange = [this]
752753
{ applyTableFilters(); };
753-
searchEditor.onEscapeKey = [this]
754+
searchEditor->onEscapeKey = [this]
754755
{
755-
searchEditor.clear();
756+
searchEditor->clear();
756757
applyTableFilters();
757-
searchEditor.giveAwayKeyboardFocus();
758+
searchEditor->giveAwayKeyboardFocus();
758759
};
759-
760-
addAndMakeVisible (viewLabel);
761-
viewLabel.setFont (font);
762-
viewLabel.setText ("View:", dontSendNotification);
763-
764-
addAndMakeVisible (allButton);
765-
allButton.setButtonText ("All");
766-
allButton.setRadioGroupId (101, dontSendNotification);
767-
allButton.setToggleState (true, dontSendNotification);
768-
allButton.addListener (this);
769-
770-
addAndMakeVisible (installedButton);
771-
installedButton.setButtonText ("Installed");
772-
installedButton.setRadioGroupId (101, dontSendNotification);
773-
installedButton.addListener (this);
760+
addAndMakeVisible (searchEditor.get());
761+
762+
viewLabel = std::make_unique<Label>();
763+
viewLabel->setFont (font);
764+
viewLabel->setText ("View:", dontSendNotification);
765+
addAndMakeVisible (viewLabel.get());
766+
767+
allButton = std::make_unique<ToggleButton>();
768+
allButton->setButtonText ("All");
769+
allButton->setRadioGroupId (101, dontSendNotification);
770+
allButton->setToggleState (true, dontSendNotification);
771+
allButton->addListener (this);
772+
addAndMakeVisible (allButton.get());
773+
774+
installedButton = std::make_unique<ToggleButton>();
775+
installedButton->setButtonText ("Installed");
776+
installedButton->setRadioGroupId (101, dontSendNotification);
777+
installedButton->addListener (this);
778+
addAndMakeVisible (installedButton.get());
774779

775780
updatesButton = std::make_unique<ShapeButton> ("Refresh Plugins",
776781
Colours::transparentBlack,
@@ -784,31 +789,37 @@ PluginInstallerComponent::PluginInstallerComponent()
784789
updatesButton->addListener (this);
785790
addAndMakeVisible (updatesButton.get());
786791

787-
addAndMakeVisible (typeLabel);
788-
typeLabel.setFont (font);
789-
typeLabel.setText ("Type:", dontSendNotification);
790-
791-
addAndMakeVisible (sourceType);
792-
sourceType.setButtonText ("Source");
793-
sourceType.setToggleState (true, dontSendNotification);
794-
sourceType.addListener (this);
795-
796-
addAndMakeVisible (filterType);
797-
filterType.setButtonText ("Filter");
798-
filterType.setToggleState (true, dontSendNotification);
799-
filterType.addListener (this);
800-
801-
addAndMakeVisible (sinkType);
802-
sinkType.setButtonText ("Sink");
803-
sinkType.setToggleState (true, dontSendNotification);
804-
sinkType.addListener (this);
805-
806-
addAndMakeVisible (otherType);
807-
otherType.setButtonText ("Other");
808-
otherType.setToggleState (true, dontSendNotification);
809-
otherType.addListener (this);
810-
811-
addAndMakeVisible (pluginListAndInfo);
792+
typeLabel = std::make_unique<Label>();
793+
typeLabel->setFont (font);
794+
typeLabel->setText ("Type:", dontSendNotification);
795+
addAndMakeVisible (typeLabel.get());
796+
797+
sourceType = std::make_unique<ToggleButton>();
798+
sourceType->setButtonText ("Source");
799+
sourceType->setToggleState (true, dontSendNotification);
800+
sourceType->addListener (this);
801+
addAndMakeVisible (sourceType.get());
802+
803+
filterType = std::make_unique<ToggleButton>();
804+
filterType->setButtonText ("Filter");
805+
filterType->setToggleState (true, dontSendNotification);
806+
filterType->addListener (this);
807+
addAndMakeVisible (filterType.get());
808+
809+
sinkType = std::make_unique<ToggleButton>();
810+
sinkType->setButtonText ("Sink");
811+
sinkType->setToggleState (true, dontSendNotification);
812+
sinkType->addListener (this);
813+
addAndMakeVisible (sinkType.get());
814+
815+
otherType = std::make_unique<ToggleButton>();
816+
otherType->setButtonText ("Other");
817+
otherType->setToggleState (true, dontSendNotification);
818+
otherType->addListener (this);
819+
addAndMakeVisible (otherType.get());
820+
821+
pluginListAndInfo = std::make_unique<PluginListBoxComponent>();
822+
addAndMakeVisible (pluginListAndInfo.get());
812823
applyTableFilters();
813824
}
814825

@@ -821,30 +832,30 @@ void PluginInstallerComponent::paint (Graphics& g)
821832

822833
void PluginInstallerComponent::resized()
823834
{
824-
searchLabel.setBounds (20, 10, 60, 28);
825-
searchEditor.setBounds (80, 10, 250, 28);
835+
searchLabel->setBounds (20, 10, 60, 28);
836+
searchEditor->setBounds (80, 10, 250, 28);
826837

827-
viewLabel.setBounds (350, 10, 50, 28);
828-
allButton.setBounds (400, 10, 55, 28);
829-
installedButton.setBounds (460, 10, 95, 28);
838+
viewLabel->setBounds (350, 10, 50, 28);
839+
allButton->setBounds (400, 10, 55, 28);
840+
installedButton->setBounds (460, 10, 95, 28);
830841

831-
typeLabel.setBounds (570, 10, 50, 28);
832-
sourceType.setBounds (625, 10, 80, 28);
833-
filterType.setBounds (710, 10, 70, 28);
834-
sinkType.setBounds (785, 10, 65, 28);
835-
otherType.setBounds (855, 10, 75, 28);
842+
typeLabel->setBounds (570, 10, 50, 28);
843+
sourceType->setBounds (625, 10, 80, 28);
844+
filterType->setBounds (710, 10, 70, 28);
845+
sinkType->setBounds (785, 10, 65, 28);
846+
otherType->setBounds (855, 10, 75, 28);
836847

837848
updatesButton->setBounds (getWidth() - 44, 10, 20, 20);
838849

839-
pluginListAndInfo.setBounds (10, 64, getWidth() - 20, getHeight() - 94);
850+
pluginListAndInfo->setBounds (10, 64, getWidth() - 20, getHeight() - 94);
840851
}
841852

842853
void PluginInstallerComponent::buttonClicked (Button* button)
843854
{
844855
if (button == updatesButton.get())
845856
{
846857
MouseCursor::showWaitCursor();
847-
pluginListAndInfo.refreshCatalog();
858+
pluginListAndInfo->refreshCatalog();
848859
MouseCursor::hideWaitCursor();
849860
}
850861

@@ -858,12 +869,12 @@ void PluginInstallerComponent::colourChanged()
858869

859870
void PluginInstallerComponent::applyTableFilters()
860871
{
861-
pluginListAndInfo.setSearchText (searchEditor.getText());
862-
pluginListAndInfo.setShowInstalledOnly (installedButton.getToggleState());
863-
pluginListAndInfo.setTypeFilters (sourceType.getToggleState(),
864-
filterType.getToggleState(),
865-
sinkType.getToggleState(),
866-
otherType.getToggleState());
872+
pluginListAndInfo->setSearchText (searchEditor->getText());
873+
pluginListAndInfo->setShowInstalledOnly (installedButton->getToggleState());
874+
pluginListAndInfo->setTypeFilters (sourceType->getToggleState(),
875+
filterType->getToggleState(),
876+
sinkType->getToggleState(),
877+
otherType->getToggleState());
867878
}
868879

869880
/* ================================== Plugin Table Component ================================== */
@@ -873,14 +884,14 @@ PluginListBoxComponent::PluginListBoxComponent()
873884
tableFont = FontOptions ("Inter", "Regular", 14.0f);
874885
headerFont = FontOptions ("Inter", "Semi Bold", 15.0f);
875886

876-
addAndMakeVisible (pluginTable);
877-
pluginTable.setModel (this);
878-
pluginTable.setRowHeight (38);
879-
pluginTable.setHeaderHeight (30);
880-
pluginTable.getViewport()->setScrollBarThickness (12);
881-
pluginTable.grabKeyboardFocus();
887+
pluginTable = std::make_unique<TableListBox>();
888+
pluginTable->setModel (this);
889+
pluginTable->setRowHeight (38);
890+
pluginTable->setHeaderHeight (30);
891+
pluginTable->getViewport()->setScrollBarThickness (12);
892+
addAndMakeVisible (pluginTable.get());
882893

883-
auto& header = pluginTable.getHeader();
894+
auto& header = pluginTable->getHeader();
884895
constexpr int sortableColumnFlags = TableHeaderComponent::visible | TableHeaderComponent::resizable | TableHeaderComponent::sortable;
885896
constexpr int regularColumnFlags = TableHeaderComponent::visible | TableHeaderComponent::resizable;
886897

@@ -897,10 +908,12 @@ PluginListBoxComponent::PluginListBoxComponent()
897908
header.addColumn ("Remove", uninstallColumn, 60, 60, 60, TableHeaderComponent::visible);
898909
header.setSortColumnId (displayNameColumn, true);
899910

900-
tableDropShadower.setOwner (&pluginTable);
911+
tableDropShadower = std::make_unique<DropShadower> (DropShadow (Colours::black.withAlpha (0.5f), 6, { 2, 2 }));
912+
tableDropShadower->setOwner (pluginTable.get());
901913

902-
actionRunner.setOperationCompleteHandler ([this] (const SelectedPluginInfo& pluginInfo, bool isInstalled)
903-
{ updatePluginState (pluginInfo, isInstalled); });
914+
actionRunner = std::make_unique<PluginInstallActionRunner>();
915+
actionRunner->setOperationCompleteHandler ([this] (const SelectedPluginInfo& pluginInfo, bool isInstalled)
916+
{ updatePluginState (pluginInfo, isInstalled); });
904917

905918
refreshCatalog();
906919
}
@@ -1108,7 +1121,7 @@ int PluginListBoxComponent::getColumnAutoSizeWidth (int columnId)
11081121
if (columnId != displayNameColumn)
11091122
return 0;
11101123

1111-
auto maxWidth = GlyphArrangement::getStringWidthInt (Font (headerFont), pluginTable.getHeader().getColumnName (displayNameColumn));
1124+
auto maxWidth = GlyphArrangement::getStringWidthInt (Font (headerFont), pluginTable->getHeader().getColumnName (displayNameColumn));
11121125

11131126
for (const auto& pluginInfo : allPlugins)
11141127
maxWidth = jmax (maxWidth, GlyphArrangement::getStringWidthInt (Font (headerFont), pluginInfo.displayName));
@@ -1118,7 +1131,7 @@ int PluginListBoxComponent::getColumnAutoSizeWidth (int columnId)
11181131

11191132
void PluginListBoxComponent::resized()
11201133
{
1121-
pluginTable.setBounds (getLocalBounds().reduced (6));
1134+
pluginTable->setBounds (getLocalBounds().reduced (6));
11221135
}
11231136

11241137
void PluginListBoxComponent::setSearchText (const String& text)
@@ -1189,8 +1202,8 @@ bool PluginListBoxComponent::refreshCatalog()
11891202
}
11901203

11911204
allPlugins = std::move (loadedPlugins);
1192-
actionRunner.setDownloadURL (catalog.downloadUrl);
1193-
pluginTable.autoSizeColumn (displayNameColumn);
1205+
actionRunner->setDownloadURL (catalog.downloadUrl);
1206+
pluginTable->autoSizeColumn (displayNameColumn);
11941207

11951208
updatablePlugins.clear();
11961209

@@ -1226,10 +1239,10 @@ void PluginListBoxComponent::applyFilters()
12261239
if (result == 0)
12271240
return allPlugins[static_cast<size_t> (lhs)].pluginName.compareNatural (allPlugins[static_cast<size_t> (rhs)].pluginName) < 0;
12281241

1229-
return pluginTable.getHeader().isSortedForwards() ? result < 0 : result > 0; });
1242+
return pluginTable->getHeader().isSortedForwards() ? result < 0 : result > 0; });
12301243

1231-
pluginTable.updateContent();
1232-
pluginTable.repaint();
1244+
pluginTable->updateContent();
1245+
pluginTable->repaint();
12331246
}
12341247

12351248
bool PluginListBoxComponent::matchesCurrentFilters (const SelectedPluginInfo& pluginInfo) const
@@ -1280,25 +1293,25 @@ void PluginListBoxComponent::setSelectedVersion (int rowNumber, const String& ve
12801293
if (auto* pluginInfo = getPluginForVisibleRow (rowNumber))
12811294
{
12821295
pluginInfo->selectedVersion = version;
1283-
pluginTable.repaintRow (rowNumber);
1296+
pluginTable->repaintRow (rowNumber);
12841297
}
12851298
}
12861299

12871300
void PluginListBoxComponent::installPluginForRow (int rowNumber)
12881301
{
12891302
if (auto* pluginInfo = getPluginForVisibleRow (rowNumber))
12901303
{
1291-
actionRunner.setPluginInfo (*pluginInfo);
1292-
actionRunner.installSelectedPlugin();
1304+
actionRunner->setPluginInfo (*pluginInfo);
1305+
actionRunner->installSelectedPlugin();
12931306
}
12941307
}
12951308

12961309
void PluginListBoxComponent::uninstallPluginForRow (int rowNumber)
12971310
{
12981311
if (auto* pluginInfo = getPluginForVisibleRow (rowNumber))
12991312
{
1300-
actionRunner.setPluginInfo (*pluginInfo);
1301-
actionRunner.uninstallSelectedPlugin();
1313+
actionRunner->setPluginInfo (*pluginInfo);
1314+
actionRunner->uninstallSelectedPlugin();
13021315
}
13031316
}
13041317

Source/UI/PluginInstaller.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,8 @@ class PluginListBoxComponent : public Component,
223223

224224
void updatePluginState (const SelectedPluginInfo& pluginInfo, bool isInstalled);
225225

226-
TableListBox pluginTable;
227-
PluginInstallActionRunner actionRunner;
226+
std::unique_ptr<TableListBox> pluginTable;
227+
std::unique_ptr<PluginInstallActionRunner> actionRunner;
228228
std::vector<SelectedPluginInfo> allPlugins;
229229
std::vector<int> visibleRows;
230230
String searchText;
@@ -235,7 +235,7 @@ class PluginListBoxComponent : public Component,
235235
bool showSinks = true;
236236
bool showOther = true;
237237

238-
DropShadower tableDropShadower { DropShadow (Colours::black.withAlpha (0.5f), 6, { 2, 2 }) };
238+
std::unique_ptr<DropShadower> tableDropShadower;
239239

240240
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PluginListBoxComponent);
241241
};
@@ -258,17 +258,17 @@ class PluginInstallerComponent : public Component,
258258
void colourChanged() override;
259259

260260
private:
261-
PluginListBoxComponent pluginListAndInfo;
261+
std::unique_ptr<PluginListBoxComponent> pluginListAndInfo;
262262

263-
Label searchLabel;
264-
TextEditor searchEditor;
263+
std::unique_ptr<Label> searchLabel;
264+
std::unique_ptr<TextEditor> searchEditor;
265265

266-
Label viewLabel;
267-
ToggleButton allButton, installedButton;
266+
std::unique_ptr<Label> viewLabel;
267+
std::unique_ptr<ToggleButton> allButton, installedButton;
268268
std::unique_ptr<ShapeButton> updatesButton;
269269

270-
Label typeLabel;
271-
ToggleButton filterType, sourceType, sinkType, otherType;
270+
std::unique_ptr<Label> typeLabel;
271+
std::unique_ptr<ToggleButton> filterType, sourceType, sinkType, otherType;
272272

273273
FontOptions font;
274274

0 commit comments

Comments
 (0)