Skip to content

Commit

Permalink
Add prompt to confirm patch load if current one is dirty (#5927)
Browse files Browse the repository at this point in the history
* Add prompt to confirm patch load if current one is dirty

* clangformat
  • Loading branch information
mkruselj authored Mar 3, 2022
1 parent ee01d41 commit 901b73f
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 40 deletions.
6 changes: 2 additions & 4 deletions src/common/SurgeStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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;

Expand Down
7 changes: 5 additions & 2 deletions src/common/SurgeSynthesizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
26 changes: 19 additions & 7 deletions src/common/SurgeSynthesizerIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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();

Expand All @@ -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;
Expand All @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions src/common/UserDefaults.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ void initMaps()
case PromptToActivateCategoryAndPatchOnKeypress:
r = "promptToActivateCategoryAndPatchOnKeypress";
break;
case PromptToLoadOverDirtyPatch:
r = "promptToLoadOverDirtyPatch";
break;
case InfoWindowPopupOnIdle:
r = "infoWindowPopupOnIdle";
break;
Expand Down
2 changes: 2 additions & 0 deletions src/common/UserDefaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
48 changes: 39 additions & 9 deletions src/surge-xt/gui/SurgeGUIEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3462,6 +3462,18 @@ juce::PopupMenu SurgeGUIEditor::makeWorkflowMenu(const juce::Point<int> &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),
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
}
9 changes: 9 additions & 0 deletions src/surge-xt/gui/SurgeGUIEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> where,
Surge::Widgets::ModulationSourceButton *msb);
void openLFORenameDialog(const int lfo_id, const juce::Point<int> where, juce::Component *r);
Expand Down Expand Up @@ -708,6 +710,13 @@ class SurgeGUIEditor : public Surge::GUI::IComponentTagValue::Listener,
std::unique_ptr<juce::ToggleButton> okcWithToggleToggleButton;
std::function<void()> 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,
Expand Down
32 changes: 14 additions & 18 deletions src/surge-xt/gui/SurgeGUIEditorValueCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down

0 comments on commit 901b73f

Please sign in to comment.