Skip to content

Commit

Permalink
plugin: treat all modals the same way
Browse files Browse the repository at this point in the history
  • Loading branch information
JoepVanlier committed Dec 14, 2024
1 parent af451d5 commit 603c228
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 62 deletions.
12 changes: 10 additions & 2 deletions plugin/components/modal_textinputbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ExclusionFilter : public juce::TextEditor::InputFilter
};


static juce::AlertWindow* show_async_text_input(juce::String title, juce::String message, std::function<void(juce::String, bool)> callback, std::optional<std::function<juce::String(juce::String)>> validator=std::nullopt)
static juce::AlertWindow* show_async_text_input(juce::String title, juce::String message, std::function<void(juce::String, bool)> callback, std::optional<std::function<juce::String(juce::String)>> validator=std::nullopt, juce::Component* parent=nullptr)
{
auto* window = new juce::AlertWindow(title, message, juce::AlertWindow::NoIcon);
window->addTextEditor("textField", "", "");
Expand Down Expand Up @@ -61,11 +61,15 @@ static juce::AlertWindow* show_async_text_input(juce::String title, juce::String
textEditor->setWantsKeyboardFocus(true);
textEditor->grabKeyboardFocus();

if (parent) {
window->setCentrePosition(parent->getScreenPosition() + juce::Point<int>{parent->getScreenBounds().getWidth() / 2, parent->getScreenBounds().getHeight() / 2});
}

return window;
}


static juce::AlertWindow* show_overwrite_window(juce::String title, juce::String message, std::vector<juce::String> buttons, std::function<void(int result)> callback)
static juce::AlertWindow* show_option_window(juce::String title, juce::String message, std::vector<juce::String> buttons, std::function<void(int result)> callback, juce::Component* parent=nullptr)
{
auto* window = new juce::AlertWindow(title, message, juce::AlertWindow::NoIcon);
window->setMessage(message);
Expand All @@ -89,5 +93,9 @@ static juce::AlertWindow* show_overwrite_window(juce::String title, juce::String
window->grabKeyboardFocus();
window->setEscapeKeyCancels(true);

if (parent) {
window->setCentrePosition(parent->getScreenPosition() + juce::Point<int>{parent->getScreenBounds().getWidth() / 2, parent->getScreenBounds().getHeight() / 2});
}

return window;
}
42 changes: 19 additions & 23 deletions plugin/components/rpl_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,31 +254,27 @@ class LoadedBank : public juce::Component, public juce::DragAndDropContainer {

if (names.empty()) return;

juce::AlertWindow::showAsync
(
juce::MessageBoxOptions()
.withTitle("Are you certain?")
.withMessage(
m_confirmDialog.reset(
show_option_window(
TRANS("Are you certain?"),
TRANS("Are you certain you want to delete ") +
((names.size() > 1) ? TRANS("several presets") : names[0])
+ "\n" + TRANS("This operation cannot be undone!")
)
.withButton("Yes")
.withButton("No")
.withParentComponent(this)
.withIconType(juce::MessageBoxIconType::NoIcon),
[this, names](int result){
if (result == 1) {
for (auto name : names)
{
m_bank.reset(ysfx_delete_preset_from_bank(m_bank.get(), name.c_str()));
}
+ "?\n" + TRANS("This operation cannot be undone!"),
std::vector<juce::String>{"Yes", "No"},
[this, names](int result){
if (result == 1) {
for (auto name : names)
{
m_bank.reset(ysfx_delete_preset_from_bank(m_bank.get(), name.c_str()));
}

this->m_listBox->deselectAllRows();
save_bank(m_file.getFullPathName().toStdString().c_str(), m_bank.get());
if (m_bankUpdatedCallback) m_bankUpdatedCallback();
}
}
this->m_listBox->deselectAllRows();
save_bank(m_file.getFullPathName().toStdString().c_str(), m_bank.get());
if (m_bankUpdatedCallback) m_bankUpdatedCallback();
}
},
this
)
);
}

Expand Down Expand Up @@ -413,7 +409,7 @@ class LoadedBank : public juce::Component, public juce::DragAndDropContainer {
if (ysfx_preset_exists(m_bank.get(), src_bank->presets[idx].name) && !force_accept) {
// Ask for overwrite
m_confirmDialog.reset(
show_overwrite_window(
show_option_window(
TRANS("Are you certain?"),
TRANS("Are you certain you want to overwrite the preset named ") + juce::String(src_bank->presets[idx].name) + "?",
std::vector<juce::String>{"Yes", "No", "Yes to all", "Cancel"},
Expand Down
76 changes: 39 additions & 37 deletions plugin/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ struct YsfxEditor::Impl {
YsfxCurrentPresetInfo::Ptr m_currentPresetInfo;
ysfx_bank_shared m_bank;
std::unique_ptr<juce::AlertWindow> m_editDialog;
std::unique_ptr<juce::AlertWindow> m_modalAlert;
std::unique_ptr<juce::Timer> m_infoTimer;
std::unique_ptr<juce::Timer> m_relayoutTimer;
std::unique_ptr<juce::FileChooser> m_fileChooser;
Expand Down Expand Up @@ -70,6 +71,7 @@ struct YsfxEditor::Impl {
void switchEditor(bool showGfx);
void openCodeEditor();
void openPresetWindow();
void quickAlertBox(bool confirmationRequired, std::function<void()> callbackOnSuccess, juce::Component* parent);
static juce::File getAppDataDirectory();
static juce::File getDefaultEffectsDirectory();
juce::RecentlyOpenedFilesList loadRecentFiles();
Expand Down Expand Up @@ -433,20 +435,19 @@ void YsfxEditor::Impl::updateInfo()
relayoutUILater();
}

void _quickAlertBox(bool confirmationRequired, std::function<void()> callbackOnSuccess, juce::Component* parent)
void YsfxEditor::Impl::quickAlertBox(bool confirmationRequired, std::function<void()> callbackOnSuccess, juce::Component* parent)
{
if (confirmationRequired) {
juce::AlertWindow::showAsync(
juce::MessageBoxOptions()
.withTitle("Are you certain?")
.withMessage("Are you certain you want to (re)load the plugin?\n\nNote that you will lose your current preset.")
.withButton("Yes")
.withButton("No")
.withParentComponent(parent)
.withIconType(juce::MessageBoxIconType::NoIcon),
[callbackOnSuccess](int result){
if (result == 1) callbackOnSuccess();
}
m_modalAlert.reset(
show_option_window(
"Are you certain?",
"Are you certain you want to (re)load the plugin?\n\nNote that you will lose your current preset.",
std::vector<juce::String>{"Yes", "No"},
[callbackOnSuccess](int result){
if (result == 1) callbackOnSuccess();
},
parent
)
);
} else {
callbackOnSuccess();
Expand Down Expand Up @@ -490,7 +491,7 @@ void YsfxEditor::Impl::chooseFileAndLoad()
[this, normalLoad, mustAskConfirmation](const juce::FileChooser &chooser) {
juce::File result = chooser.getResult();
if (result != juce::File()) {
_quickAlertBox(
quickAlertBox(
mustAskConfirmation,
[this, normalLoad, result]() {
if (normalLoad) saveScaling();
Expand Down Expand Up @@ -598,7 +599,7 @@ void YsfxEditor::Impl::popupRecentFiles()
m_recentFilesPopup->showMenuAsync(popupOptions, [this, recent](int index) {
if (index != 0) {
juce::File selectedFile = recent.getFile(index - 100);
_quickAlertBox(
quickAlertBox(
ysfx_is_compiled(m_info->effect.get()),
[this, selectedFile]() {
saveScaling();
Expand Down Expand Up @@ -675,23 +676,24 @@ void YsfxEditor::Impl::popupPresetOptions()
std::string preset = presetName.toStdString();
if (wantSave) {
if (m_proc->presetExists(preset.c_str())) {
juce::AlertWindow::showAsync(
juce::MessageBoxOptions()
.withTitle("Overwrite?")
.withMessage("Preset with that name already exists.\nAre you sure you want to overwrite the preset?")
.withButton("Yes")
.withButton("No")
.withParentComponent(this->m_self)
.withIconType(juce::MessageBoxIconType::NoIcon),
[this, preset](int result){
if (result == 1) m_proc->saveCurrentPreset(preset.c_str());
}
this->m_modalAlert.reset(
show_option_window(
"Overwrite?",
"Preset with that name already exists.\nAre you sure you want to overwrite the preset?",
std::vector<juce::String>{"Yes", "No"},
[this, preset](int result){
if (result == 1) m_proc->saveCurrentPreset(preset.c_str());
},
this->m_self
)
);
} else {
m_proc->saveCurrentPreset(preset.c_str());
}
}
}
},
std::nullopt,
this->m_self
)
);
return;
Expand All @@ -713,7 +715,8 @@ void YsfxEditor::Impl::popupPresetOptions()
} else {
return juce::String("");
}
}
},
this->m_self
)
);
break;
Expand All @@ -725,17 +728,16 @@ void YsfxEditor::Impl::popupPresetOptions()
break;
case 5:
// Delete
juce::AlertWindow::showAsync(
juce::MessageBoxOptions()
.withTitle("Delete?")
.withMessage("Are you sure you want to delete the preset named " + m_currentPresetInfo->m_lastChosenPreset + "?")
.withButton("Yes")
.withButton("No")
.withParentComponent(this->m_self)
.withIconType(juce::MessageBoxIconType::NoIcon),
this->m_modalAlert.reset(
show_option_window(
"Delete?",
"Are you sure you want to delete the preset named " + m_currentPresetInfo->m_lastChosenPreset + "?",
std::vector<juce::String>{"Yes", "No"},
[this](int result) {
if (result == 1) m_proc->deleteCurrentPreset();
}
},
this->m_self
)
);
break;
case 6:
Expand Down Expand Up @@ -958,7 +960,7 @@ void YsfxEditor::Impl::connectUI()
YsfxInfo::Ptr info = m_info;
ysfx_t *fx = info->effect.get();
juce::File file{juce::CharPointer_UTF8{ysfx_get_file_path(fx)}};
_quickAlertBox(
quickAlertBox(
ysfx_is_compiled(fx),
[this, file]() {
resetScaling(file);
Expand Down

0 comments on commit 603c228

Please sign in to comment.