Skip to content

Commit

Permalink
plugin: fix crash issue
Browse files Browse the repository at this point in the history
ensure that the plugin doesn't crash when an edit modal is left open, the plugin is closed and then the edit modal is completed
  • Loading branch information
JoepVanlier committed Nov 28, 2024
1 parent 6e1575b commit 3d4f9bc
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 41 deletions.
16 changes: 12 additions & 4 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 void 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)
{
auto* window = new juce::AlertWindow(title, message, juce::AlertWindow::NoIcon);
window->addTextEditor("textField", "", "");
Expand All @@ -39,10 +39,16 @@ static void show_async_text_input(juce::String title, juce::String message, std:
}
}

callback(textEditor->getText(), true); window->exitModalState(0);
callback(textEditor->getText(), true);
window->exitModalState(0);
window->setVisible(false);
};
};
auto finalize_cancel = [window, textEditor, callback]() { callback(textEditor->getText(), false); window->exitModalState(0); };
auto finalize_cancel = [window, textEditor, callback]() {
callback(textEditor->getText(), false);
window->exitModalState(0);
window->setVisible(false);
};

textEditor->onReturnKey = finalize_success;
window->addButton("Ok", 1);
Expand All @@ -51,7 +57,9 @@ static void show_async_text_input(juce::String title, juce::String message, std:
window->getButton("Cancel")->onClick = finalize_cancel;

window->setAlwaysOnTop(true);
window->enterModalState(true, nullptr, true);
window->enterModalState(true, nullptr, false);
textEditor->setWantsKeyboardFocus(true);
textEditor->grabKeyboardFocus();

return window;
}
79 changes: 42 additions & 37 deletions plugin/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ struct YsfxEditor::Impl {
YsfxInfo::Ptr m_info;
YsfxCurrentPresetInfo::Ptr m_currentPresetInfo;
ysfx_bank_shared m_bank;
std::unique_ptr<juce::AlertWindow> m_editDialog;
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 @@ -656,50 +657,54 @@ void YsfxEditor::Impl::popupPresetOptions()
switch (index) {
case 1:
// Save
show_async_text_input(
"Enter preset name",
"",
[this](juce::String presetName, bool wantSave) {
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());
}
);
} else {
m_proc->saveCurrentPreset(preset.c_str());
m_editDialog.reset(
show_async_text_input(
"Enter preset name",
"",
[this](juce::String presetName, bool wantSave) {
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());
}
);
} else {
m_proc->saveCurrentPreset(preset.c_str());
}
}
}
}
)
);
return;
case 2:
// Rename
show_async_text_input(
"Enter new name",
"",
[this](juce::String presetName, bool wantRename) {
std::string preset = presetName.toStdString();
if (wantRename) {
m_proc->renameCurrentPreset(preset.c_str());
}
},
[this](juce::String presetName) {
if (m_proc->presetExists(presetName.toStdString().c_str())) {
return juce::String("Preset with that name already exists.\nChoose a different name or click cancel.");
} else {
return juce::String("");
m_editDialog.reset(
show_async_text_input(
"Enter new name",
"",
[this](juce::String presetName, bool wantRename) {
std::string preset = presetName.toStdString();
if (wantRename) {
m_proc->renameCurrentPreset(preset.c_str());
}
},
[this](juce::String presetName) {
if (m_proc->presetExists(presetName.toStdString().c_str())) {
return juce::String("Preset with that name already exists.\nChoose a different name or click cancel.");
} else {
return juce::String("");
}
}
}
)
);
break;
case 3:
Expand Down

0 comments on commit 3d4f9bc

Please sign in to comment.