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

Define UndoManager::VisitStates

This commit is contained in:
Paul Licameli 2019-07-18 13:17:10 -04:00
parent d49a888b98
commit 490271f259
4 changed files with 62 additions and 29 deletions

View File

@ -173,6 +173,6 @@ void ProjectHistory::SetStateTo(unsigned int n, bool doAutosave)
auto &undoManager = UndoManager::Get( project );
undoManager.SetStateTo(n,
[this, doAutosave]( const UndoState &state ){
PopState(state, doAutosave); } );
[this, doAutosave]( const UndoStackElem &elem ){
PopState(elem.state, doAutosave); } );
}

View File

@ -48,24 +48,6 @@ wxDEFINE_EVENT(EVT_UNDO_RESET, wxCommandEvent);
using SampleBlockID = long long;
struct UndoStackElem {
UndoStackElem(std::shared_ptr<TrackList> &&tracks_,
const TranslatableString &description_,
const TranslatableString &shortDescription_,
const SelectedRegion &selectedRegion_,
const std::shared_ptr<Tags> &tags_)
: state(std::move(tracks_), tags_, selectedRegion_)
, description(description_)
, shortDescription(shortDescription_)
{
}
UndoState state;
TranslatableString description;
TranslatableString shortDescription;
};
static const AudacityProject::AttachedObjects::RegisteredFactory key{
[](AudacityProject &project)
{ return std::make_unique<UndoManager>( project ); }
@ -370,7 +352,7 @@ void UndoManager::SetStateTo(unsigned int n, const Consumer &consumer)
lastAction = {};
mayConsolidate = false;
consumer( stack[current]->state );
consumer( *stack[current] );
// wxWidgets will own the event object
mProject.QueueEvent( safenew wxCommandEvent{ EVT_UNDO_RESET } );
@ -385,7 +367,7 @@ void UndoManager::Undo(const Consumer &consumer)
lastAction = {};
mayConsolidate = false;
consumer( stack[current]->state );
consumer( *stack[current] );
// wxWidgets will own the event object
mProject.QueueEvent( safenew wxCommandEvent{ EVT_UNDO_OR_REDO } );
@ -413,12 +395,39 @@ void UndoManager::Redo(const Consumer &consumer)
lastAction = {};
mayConsolidate = false;
consumer( stack[current]->state );
consumer( *stack[current] );
// wxWidgets will own the event object
mProject.QueueEvent( safenew wxCommandEvent{ EVT_UNDO_OR_REDO } );
}
void UndoManager::VisitStates( const Consumer &consumer, bool newestFirst )
{
auto fn = [&]( decltype(stack[0]) &ptr ){ consumer( *ptr ); };
if (newestFirst)
std::for_each(stack.rbegin(), stack.rend(), fn);
else
std::for_each(stack.begin(), stack.end(), fn);
}
void UndoManager::VisitStates(
const Consumer &consumer, size_t begin, size_t end )
{
auto size = stack.size();
if (begin < end) {
end = std::min(end, size);
for (auto ii = begin; ii < end; ++ii)
consumer(*stack[ii]);
}
else {
if (size == 0)
return;
begin = std::min(begin, size - 1);
for (auto ii = begin; ii > end; --ii)
consumer(*stack[ii]);
}
}
bool UndoManager::UnsavedChanges() const
{
return (saved != current);

View File

@ -76,7 +76,6 @@ class Tags;
class Track;
class TrackList;
struct UndoStackElem;
struct UndoState {
UndoState(std::shared_ptr<TrackList> &&tracks_,
const std::shared_ptr<Tags> &tags_,
@ -89,6 +88,24 @@ struct UndoState {
SelectedRegion selectedRegion; // by value
};
struct UndoStackElem {
UndoStackElem(std::shared_ptr<TrackList> &&tracks_,
const TranslatableString &description_,
const TranslatableString &shortDescription_,
const SelectedRegion &selectedRegion_,
const std::shared_ptr<Tags> &tags_)
: state(std::move(tracks_), tags_, selectedRegion_)
, description(description_)
, shortDescription(shortDescription_)
{
}
UndoState state;
TranslatableString description;
TranslatableString shortDescription;
};
using UndoStack = std::vector <std::unique_ptr<UndoStackElem>>;
using SpaceArray = std::vector <unsigned long long> ;
@ -150,11 +167,18 @@ class AUDACITY_DLL_API UndoManager final
// These functions accept a callback that uses the state,
// and then they send to the project EVT_UNDO_RESET or EVT_UNDO_OR_REDO when
// that has finished.
using Consumer = std::function< void( const UndoState & ) >;
using Consumer = std::function< void( const UndoStackElem & ) >;
void SetStateTo(unsigned int n, const Consumer &consumer);
void Undo(const Consumer &consumer);
void Redo(const Consumer &consumer);
//! Give read-only access to all states
void VisitStates( const Consumer &consumer, bool newestFirst );
//! Visit a specified range of states
/*! end is exclusive; visit newer states first if end < begin */
void VisitStates(
const Consumer &consumer, size_t begin, size_t end );
bool UndoAvailable();
bool RedoAvailable();

View File

@ -183,8 +183,8 @@ void OnUndo(const CommandContext &context)
}
undoManager.Undo(
[&]( const UndoState &state ){
ProjectHistory::Get( project ).PopState( state ); } );
[&]( const UndoStackElem &elem ){
ProjectHistory::Get( project ).PopState( elem.state ); } );
auto t = *tracks.Selected().begin();
if (!t)
@ -213,8 +213,8 @@ void OnRedo(const CommandContext &context)
}
undoManager.Redo(
[&]( const UndoState &state ){
ProjectHistory::Get( project ).PopState( state ); } );
[&]( const UndoStackElem &elem ){
ProjectHistory::Get( project ).PopState( elem.state ); } );
auto t = *tracks.Selected().begin();
if (!t)