1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-06-25 16:48:44 +02:00

Some inversion of control flow when popping Undo state

This commit is contained in:
Paul Licameli 2018-02-16 14:49:26 -05:00
parent 2741d58880
commit 4f7d308ca3
4 changed files with 19 additions and 25 deletions

View File

@ -4673,6 +4673,8 @@ void AudacityProject::ModifyState(bool bWantsAutoSave)
// Need to keep it and its tracks "t" available for Undo/Redo/SetStateTo. // Need to keep it and its tracks "t" available for Undo/Redo/SetStateTo.
void AudacityProject::PopState(const UndoState &state) void AudacityProject::PopState(const UndoState &state)
{ {
mViewInfo.selectedRegion = state.selectedRegion;
// Restore tags // Restore tags
mTags = state.tags; mTags = state.tags;
@ -4725,9 +4727,8 @@ void AudacityProject::PopState(const UndoState &state)
void AudacityProject::SetStateTo(unsigned int n) void AudacityProject::SetStateTo(unsigned int n)
{ {
const UndoState &state = GetUndoManager()->SetStateTo(n,
GetUndoManager()->SetStateTo(n, &mViewInfo.selectedRegion); [this]( const UndoState &state ){ PopState(state); } );
PopState(state);
HandleResize(); HandleResize();
mTrackPanel->SetFocusedTrack(NULL); mTrackPanel->SetFocusedTrack(NULL);

View File

@ -300,8 +300,7 @@ void UndoManager::PushState(const TrackList * l,
lastAction = longDescription; lastAction = longDescription;
} }
const UndoState &UndoManager::SetStateTo void UndoManager::SetStateTo(unsigned int n, const Consumer &consumer)
(unsigned int n, SelectedRegion *selectedRegion)
{ {
n -= 1; n -= 1;
@ -309,36 +308,30 @@ const UndoState &UndoManager::SetStateTo
current = n; current = n;
*selectedRegion = stack[current]->state.selectedRegion;
lastAction = wxT(""); lastAction = wxT("");
mayConsolidate = false; mayConsolidate = false;
return stack[current]->state; consumer( stack[current]->state );
} }
const UndoState &UndoManager::Undo(SelectedRegion *selectedRegion) void UndoManager::Undo(const Consumer &consumer)
{ {
wxASSERT(UndoAvailable()); wxASSERT(UndoAvailable());
current--; current--;
*selectedRegion = stack[current]->state.selectedRegion;
lastAction = wxT(""); lastAction = wxT("");
mayConsolidate = false; mayConsolidate = false;
return stack[current]->state; consumer( stack[current]->state );
} }
const UndoState &UndoManager::Redo(SelectedRegion *selectedRegion) void UndoManager::Redo(const Consumer &consumer)
{ {
wxASSERT(RedoAvailable()); wxASSERT(RedoAvailable());
current++; current++;
*selectedRegion = stack[current]->state.selectedRegion;
/* /*
if (!RedoAvailable()) { if (!RedoAvailable()) {
*sel0 = stack[current]->sel0; *sel0 = stack[current]->sel0;
@ -355,7 +348,7 @@ const UndoState &UndoManager::Redo(SelectedRegion *selectedRegion)
lastAction = wxT(""); lastAction = wxT("");
mayConsolidate = false; mayConsolidate = false;
return stack[current]->state; consumer( stack[current]->state );
} }
bool UndoManager::UnsavedChanges() bool UndoManager::UnsavedChanges()

View File

@ -118,9 +118,11 @@ class AUDACITY_DLL_API UndoManager {
wxLongLong_t GetLongDescription(unsigned int n, wxString *desc, wxString *size); wxLongLong_t GetLongDescription(unsigned int n, wxString *desc, wxString *size);
void SetLongDescription(unsigned int n, const wxString &desc); void SetLongDescription(unsigned int n, const wxString &desc);
const UndoState &SetStateTo(unsigned int n, SelectedRegion *selectedRegion); // These functions accept a callback that uses the state
const UndoState &Undo(SelectedRegion *selectedRegion); using Consumer = std::function< void( const UndoState & ) >;
const UndoState &Redo(SelectedRegion *selectedRegion); void SetStateTo(unsigned int n, const Consumer &consumer);
void Undo(const Consumer &consumer);
void Redo(const Consumer &consumer);
bool UndoAvailable(); bool UndoAvailable();
bool RedoAvailable(); bool RedoAvailable();

View File

@ -219,7 +219,6 @@ void DoUndo(AudacityProject &project)
{ {
auto trackPanel = project.GetTrackPanel(); auto trackPanel = project.GetTrackPanel();
auto &undoManager = *project.GetUndoManager(); auto &undoManager = *project.GetUndoManager();
auto &selectedRegion = project.GetViewInfo().selectedRegion;
auto mixerBoard = project.GetMixerBoard(); auto mixerBoard = project.GetMixerBoard();
auto historyWindow = project.GetHistoryWindow(); auto historyWindow = project.GetHistoryWindow();
@ -233,8 +232,8 @@ void DoUndo(AudacityProject &project)
return; return;
} }
const UndoState &state = undoManager.Undo(&selectedRegion); undoManager.Undo(
project.PopState(state); [&]( const UndoState &state ){ project.PopState( state ); } );
trackPanel->EnsureVisible(trackPanel->GetFirstSelectedTrack()); trackPanel->EnsureVisible(trackPanel->GetFirstSelectedTrack());
@ -264,7 +263,6 @@ void OnRedo(const CommandContext &context)
auto &project = context.project; auto &project = context.project;
auto trackPanel = project.GetTrackPanel(); auto trackPanel = project.GetTrackPanel();
auto &undoManager = *project.GetUndoManager(); auto &undoManager = *project.GetUndoManager();
auto &selectedRegion = project.GetViewInfo().selectedRegion;
auto mixerBoard = project.GetMixerBoard(); auto mixerBoard = project.GetMixerBoard();
auto historyWindow = project.GetHistoryWindow(); auto historyWindow = project.GetHistoryWindow();
@ -277,8 +275,8 @@ void OnRedo(const CommandContext &context)
return; return;
} }
const UndoState &state = undoManager.Redo(&selectedRegion); undoManager.Redo(
project.PopState(state); [&]( const UndoState &state ){ project.PopState( state ); } );
trackPanel->EnsureVisible(trackPanel->GetFirstSelectedTrack()); trackPanel->EnsureVisible(trackPanel->GetFirstSelectedTrack());