From 901b73f280888e4ff4f31ed8276aa92e0f51afe1 Mon Sep 17 00:00:00 2001 From: EvilDragon Date: Thu, 3 Mar 2022 20:35:58 +0100 Subject: [PATCH] Add prompt to confirm patch load if current one is dirty (#5927) * Add prompt to confirm patch load if current one is dirty * clangformat --- src/common/SurgeStorage.cpp | 6 +-- src/common/SurgeSynthesizer.h | 7 ++- src/common/SurgeSynthesizerIO.cpp | 26 +++++++--- src/common/UserDefaults.cpp | 3 ++ src/common/UserDefaults.h | 2 + src/surge-xt/gui/SurgeGUIEditor.cpp | 48 +++++++++++++++---- src/surge-xt/gui/SurgeGUIEditor.h | 9 ++++ .../gui/SurgeGUIEditorValueCallbacks.cpp | 32 ++++++------- 8 files changed, 93 insertions(+), 40 deletions(-) diff --git a/src/common/SurgeStorage.cpp b/src/common/SurgeStorage.cpp index 1855bcfee03..d890c94c2da 100644 --- a/src/common/SurgeStorage.cpp +++ b/src/common/SurgeStorage.cpp @@ -1172,7 +1172,7 @@ int SurgeStorage::getAdjacentWaveTable(int id, bool nextPrev) const if (!n) return -1; - // See comment in SurgeSynthesizerIO::incrementPatch and #319 + // See comment in SurgeSynthesizerIO::jogPatch and #319 if (id < 0 || id > n - 1) { return wtOrdering[0]; @@ -1182,9 +1182,7 @@ int SurgeStorage::getAdjacentWaveTable(int id, bool nextPrev) const int order = wt_list[id].order; if (nextPrev) - order = (order >= (n - 1)) - ? 0 - : order + 1; // see comment in incrementPatch for that >= vs == + order = (order >= (n - 1)) ? 0 : order + 1; // see comment in jogPatch for that >= vs == else order = (order <= 0) ? n - 1 : order - 1; diff --git a/src/common/SurgeSynthesizer.h b/src/common/SurgeSynthesizer.h index 8ad87eaba81..75176af7fab 100644 --- a/src/common/SurgeSynthesizer.h +++ b/src/common/SurgeSynthesizer.h @@ -327,10 +327,13 @@ class alignas(16) SurgeSynthesizer void loadRaw(const void *data, int size, bool preset = false); void loadPatch(int id); bool loadPatchByPath(const char *fxpPath, int categoryId, const char *name); - void incrementPatch(bool nextPrev, bool insideCategory = true); - void incrementCategory(bool nextPrev); void selectRandomPatch(); + // if increment is true, we go to next patch, else go to previous patch + void jogCategory(bool increment); + void jogPatch(bool increment, bool insideCategory = true); + void jogPatchOrCategory(bool increment, bool isCategory, bool insideCategory = true); + void swapMetaControllers(int ct1, int ct2); void savePatchToPath(fs::path p); diff --git a/src/common/SurgeSynthesizerIO.cpp b/src/common/SurgeSynthesizerIO.cpp index 32f6e78e5fa..13e89652330 100644 --- a/src/common/SurgeSynthesizerIO.cpp +++ b/src/common/SurgeSynthesizerIO.cpp @@ -48,7 +48,7 @@ struct fxChunkSetCustom // char chunk[8]; // variable }; -void SurgeSynthesizer::incrementPatch(bool nextPrev, bool insideCategory) +void SurgeSynthesizer::jogPatch(bool increment, bool insideCategory) { // Don't increment if we still have an outstanding load if (patchid_queue >= 0) @@ -102,16 +102,16 @@ void SurgeSynthesizer::incrementPatch(bool nextPrev, bool insideCategory) } } - int np = nextPrev == true ? 1 : -1; + int np = increment == true ? 1 : -1; int numPatches = patchesInCategory.size(); do { - if (nextPrev) + if (increment) { if (!insideCategory && order >= p - 1) { - incrementCategory(nextPrev); + jogCategory(increment); category = current_category_id; } order = (order >= p - 1) ? 0 : order + 1; @@ -120,7 +120,7 @@ void SurgeSynthesizer::incrementPatch(bool nextPrev, bool insideCategory) { if (!insideCategory && order <= 0) { - incrementCategory(nextPrev); + jogCategory(increment); category = current_category_id; } order = (order <= 0) ? p - 1 : order - 1; @@ -133,7 +133,7 @@ void SurgeSynthesizer::incrementPatch(bool nextPrev, bool insideCategory) return; } -void SurgeSynthesizer::incrementCategory(bool nextPrev) +void SurgeSynthesizer::jogCategory(bool increment) { int c = storage.patch_category.size(); @@ -151,7 +151,7 @@ void SurgeSynthesizer::incrementCategory(bool nextPrev) int orderOrig = order; do { - if (nextPrev) + if (increment) order = (order >= (c - 1)) ? 0 : order + 1; else order = (order <= 0) ? c - 1 : order - 1; @@ -175,6 +175,18 @@ void SurgeSynthesizer::incrementCategory(bool nextPrev) } } +void SurgeSynthesizer::jogPatchOrCategory(bool increment, bool isCategory, bool insideCategory) +{ + if (isCategory) + { + jogCategory(increment); + } + else + { + jogPatch(increment, insideCategory); + } +} + void SurgeSynthesizer::selectRandomPatch() { if (patchid_queue >= 0) diff --git a/src/common/UserDefaults.cpp b/src/common/UserDefaults.cpp index 51df745d9b4..ff3b9e88602 100644 --- a/src/common/UserDefaults.cpp +++ b/src/common/UserDefaults.cpp @@ -168,6 +168,9 @@ void initMaps() case PromptToActivateCategoryAndPatchOnKeypress: r = "promptToActivateCategoryAndPatchOnKeypress"; break; + case PromptToLoadOverDirtyPatch: + r = "promptToLoadOverDirtyPatch"; + break; case InfoWindowPopupOnIdle: r = "infoWindowPopupOnIdle"; break; diff --git a/src/common/UserDefaults.h b/src/common/UserDefaults.h index da049d9bca5..93840eeed65 100644 --- a/src/common/UserDefaults.h +++ b/src/common/UserDefaults.h @@ -69,8 +69,10 @@ enum DefaultKey // streamed as strings so feel free to change the order to whate UseKeyboardShortcuts_Plugin, UseKeyboardShortcuts_Standalone, + PromptToActivateShortcutsOnAccKeypress, PromptToActivateCategoryAndPatchOnKeypress, + PromptToLoadOverDirtyPatch, TuningOverlayLocation, ModlistOverlayLocation, diff --git a/src/surge-xt/gui/SurgeGUIEditor.cpp b/src/surge-xt/gui/SurgeGUIEditor.cpp index 820cad777fa..a62733ea97c 100644 --- a/src/surge-xt/gui/SurgeGUIEditor.cpp +++ b/src/surge-xt/gui/SurgeGUIEditor.cpp @@ -3462,6 +3462,18 @@ juce::PopupMenu SurgeGUIEditor::makeWorkflowMenu(const juce::Point &where) &(this->synth->storage), Surge::Storage::PatchJogWraparound, !patchJogWrap); }); + int patchDirtyCheck = Surge::Storage::getUserDefaultValue( + &(this->synth->storage), Surge::Storage::PromptToLoadOverDirtyPatch, DUNNO); + + wfMenu.addItem(Surge::GUI::toOSCaseForMenu("Confirm Patch Loading if Unsaved Changes Exist"), + true, (patchDirtyCheck != ALWAYS), [this, patchDirtyCheck]() { + int newVal = (patchDirtyCheck != ALWAYS) ? ALWAYS : DUNNO; + + Surge::Storage::updateUserDefaultValue( + &(this->synth->storage), Surge::Storage::PromptToLoadOverDirtyPatch, + newVal); + }); + wfMenu.addSeparator(); bool tabArm = Surge::Storage::getUserDefaultValue(&(this->synth->storage), @@ -6227,7 +6239,7 @@ bool SurgeGUIEditor::keyPressed(const juce::KeyPress &key, juce::Component *orig Surge::Storage::PromptToActivateCategoryAndPatchOnKeypress, [this, keyCode]() { closeOverlay(SAVE_PATCH); - synth->incrementCategory(keyCode == juce::KeyPress::rightKey); + loadPatchWithDirtyCheck(keyCode == juce::KeyPress::rightKey, true); }); } else @@ -6256,7 +6268,8 @@ bool SurgeGUIEditor::keyPressed(const juce::KeyPress &key, juce::Component *orig auto insideCategory = Surge::Storage::getUserDefaultValue( &(this->synth->storage), Surge::Storage::PatchJogWraparound, 1); - synth->incrementPatch(keyCode == juce::KeyPress::rightKey, insideCategory); + loadPatchWithDirtyCheck(keyCode == juce::KeyPress::rightKey, false, + insideCategory); }); } else @@ -6824,13 +6837,6 @@ bool SurgeGUIEditor::promptForOKCancelWithDontAskAgain(const ::std::string &titl return false; } - enum AgainStates - { - DUNNO = 1, - ALWAYS = 10, - NEVER = 100 - }; - auto bypassed = Surge::Storage::getUserDefaultValue(&(synth->storage), dontAskAgainKey, DUNNO); if (bypassed == NEVER) @@ -6891,4 +6897,28 @@ bool SurgeGUIEditor::promptForOKCancelWithDontAskAgain(const ::std::string &titl false); return false; +} + +void SurgeGUIEditor::loadPatchWithDirtyCheck(bool increment, bool isCategory, bool insideCategory) +{ + if (synth->storage.getPatch().isDirty) + { + promptForOKCancelWithDontAskAgain( + "Confirm Patch Loading", + fmt::format("The currently loaded patch has unsaved changes.\n" + "Loading a new patch will discard any such changes.\n\n" + "Do you want to proceed?"), + Surge::Storage::PromptToLoadOverDirtyPatch, + [this, increment, isCategory, insideCategory]() { + closeOverlay(SAVE_PATCH); + + synth->jogPatchOrCategory(increment, isCategory, insideCategory); + }); + } + else + { + closeOverlay(SAVE_PATCH); + + synth->jogPatchOrCategory(increment, isCategory, insideCategory); + } } \ No newline at end of file diff --git a/src/surge-xt/gui/SurgeGUIEditor.h b/src/surge-xt/gui/SurgeGUIEditor.h index bdfd87c01f1..e3b1b8bf010 100644 --- a/src/surge-xt/gui/SurgeGUIEditor.h +++ b/src/surge-xt/gui/SurgeGUIEditor.h @@ -399,6 +399,8 @@ class SurgeGUIEditor : public Surge::GUI::IComponentTagValue::Listener, synth->processThreadunsafeOperations(); } + void loadPatchWithDirtyCheck(bool increment, bool isCategory, bool insideCategory = false); + void openMacroRenameDialog(const int ccid, const juce::Point where, Surge::Widgets::ModulationSourceButton *msb); void openLFORenameDialog(const int lfo_id, const juce::Point where, juce::Component *r); @@ -708,6 +710,13 @@ class SurgeGUIEditor : public Surge::GUI::IComponentTagValue::Listener, std::unique_ptr okcWithToggleToggleButton; std::function okcWithToggleCallback; + enum AskAgainStates + { + DUNNO = 1, + ALWAYS = 10, + NEVER = 100 + }; + // Return whether we called the OK action automatically or not bool promptForOKCancelWithDontAskAgain(const ::std::string &title, const std::string &msg, Surge::Storage::DefaultKey dontAskAgainKey, diff --git a/src/surge-xt/gui/SurgeGUIEditorValueCallbacks.cpp b/src/surge-xt/gui/SurgeGUIEditorValueCallbacks.cpp index 47441ad93a2..ebaa7abf74c 100644 --- a/src/surge-xt/gui/SurgeGUIEditorValueCallbacks.cpp +++ b/src/surge-xt/gui/SurgeGUIEditorValueCallbacks.cpp @@ -2842,10 +2842,20 @@ void SurgeGUIEditor::valueChanged(Surge::GUI::IComponentTagValue *control) { closeOverlay(SAVE_PATCH); - if (control->getValue() > 0.5f) - synth->incrementCategory(true); - else - synth->incrementCategory(false); + loadPatchWithDirtyCheck((control->getValue() > 0.5f), true); + + return; + } + break; + case tag_mp_patch: + { + closeOverlay(SAVE_PATCH); + + auto insideCategory = Surge::Storage::getUserDefaultValue( + &(synth->storage), Surge::Storage::PatchJogWraparound, 1); + + loadPatchWithDirtyCheck((control->getValue() > 0.5f), false, insideCategory); + return; } break; @@ -2871,20 +2881,6 @@ void SurgeGUIEditor::valueChanged(Surge::GUI::IComponentTagValue *control) } } break; - case tag_mp_patch: - { - closeOverlay(SAVE_PATCH); - - auto insideCategory = Surge::Storage::getUserDefaultValue( - &(this->synth->storage), Surge::Storage::PatchJogWraparound, 1); - - if (control->getValue() > 0.5f) - synth->incrementPatch(true, insideCategory); - else - synth->incrementPatch(false, insideCategory); - return; - } - break; case tag_mp_jogfx: { if (fxMenu)