forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
doc_undo.cpp
336 lines (287 loc) · 8.56 KB
/
doc_undo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// Aseprite
// Copyright (C) 2022-2023 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello
//
// This program is distributed under the terms of
// the End-User License Agreement for Aseprite.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "app/doc_undo.h"
#include "app/app.h"
#include "app/cmd.h"
#include "app/cmd_transaction.h"
#include "app/console.h"
#include "app/context.h"
#include "app/doc_undo_observer.h"
#include "app/pref/preferences.h"
#include "base/mem_utils.h"
#include "base/scoped_value.h"
#include "undo/undo_history.h"
#include "undo/undo_state.h"
#include <cassert>
#include <stdexcept>
#define UNDO_TRACE(...)
#define STATE_CMD(state) (static_cast<CmdTransaction*>(state->cmd()))
namespace app {
DocUndo::DocUndo()
: m_undoHistory(this)
{
}
void DocUndo::setContext(Context* ctx)
{
m_ctx = ctx;
}
void DocUndo::add(CmdTransaction* cmd)
{
ASSERT(cmd);
if (m_undoing) {
delete cmd;
throw CannotModifyWhenUndoingException();
}
UNDO_TRACE("UNDO: Add state <%s> of %s to %s\n",
cmd->label().c_str(),
base::get_pretty_memory_size(cmd->memSize()).c_str(),
base::get_pretty_memory_size(m_totalUndoSize).c_str());
// A linear undo history is the default behavior
if (!App::instance() ||
!App::instance()->preferences().undo.allowNonlinearHistory()) {
clearRedo();
}
m_undoHistory.add(cmd);
m_totalUndoSize += cmd->memSize();
notify_observers(&DocUndoObserver::onAddUndoState, this);
notify_observers(&DocUndoObserver::onTotalUndoSizeChange, this);
if (App::instance()) {
const size_t undoLimitSize =
int(App::instance()->preferences().undo.sizeLimit())
* 1024 * 1024;
// If undo limit is 0, it means "no limit", so we ignore the
// complete logic to discard undo states.
if (undoLimitSize > 0 &&
m_totalUndoSize > undoLimitSize) {
UNDO_TRACE("UNDO: Reducing undo history from %s to %s\n",
base::get_pretty_memory_size(m_totalUndoSize).c_str(),
base::get_pretty_memory_size(undoLimitSize).c_str());
while (m_undoHistory.firstState() &&
m_totalUndoSize > undoLimitSize) {
if (!m_undoHistory.deleteFirstState())
break;
}
}
}
UNDO_TRACE("UNDO: New undo size %s\n",
base::get_pretty_memory_size(m_totalUndoSize).c_str());
}
bool DocUndo::canUndo() const
{
return m_undoHistory.canUndo();
}
bool DocUndo::canRedo() const
{
return m_undoHistory.canRedo();
}
void DocUndo::undo()
{
ASSERT(!m_undoing);
base::ScopedValue undoing(m_undoing, true);
const size_t oldSize = m_totalUndoSize;
{
const undo::UndoState* state = nextUndo();
ASSERT(state);
const Cmd* cmd = STATE_CMD(state);
m_totalUndoSize -= cmd->memSize();
m_undoHistory.undo();
m_totalUndoSize += cmd->memSize();
}
// This notification could execute a script that modifies the sprite
// again (e.g. a script that is listening the "change" event, check
// the SpriteEvents class). If the sprite is modified, the "cmd" is
// not valid anymore.
notify_observers(&DocUndoObserver::onCurrentUndoStateChange, this);
if (m_totalUndoSize != oldSize)
notify_observers(&DocUndoObserver::onTotalUndoSizeChange, this);
}
void DocUndo::redo()
{
ASSERT(!m_undoing);
base::ScopedValue undoing(m_undoing, true);
const size_t oldSize = m_totalUndoSize;
{
const undo::UndoState* state = nextRedo();
ASSERT(state);
const Cmd* cmd = STATE_CMD(state);
m_totalUndoSize -= cmd->memSize();
m_undoHistory.redo();
m_totalUndoSize += cmd->memSize();
}
notify_observers(&DocUndoObserver::onCurrentUndoStateChange, this);
if (m_totalUndoSize != oldSize)
notify_observers(&DocUndoObserver::onTotalUndoSizeChange, this);
}
void DocUndo::clearRedo()
{
// Do nothing
if (currentState() == lastState())
return;
m_undoHistory.clearRedo();
notify_observers(&DocUndoObserver::onClearRedo, this);
}
bool DocUndo::isInSavedStateOrSimilar() const
{
if (m_savedStateIsLost)
return false;
// Here we try to find if we can reach the saved state from the
// currentState() undoing or redoing and the sprite is exactly the
// same as the saved state, e.g. this can happen if the undo states
// don't modify the sprite (like actions that change the current
// selection/mask boundaries).
bool savedStateWithUndoes = true;
auto state = currentState();
while (state) {
if (m_savedState == state) {
return true;
}
else if (STATE_CMD(state)->doesChangeSavedState()) {
savedStateWithUndoes = false;
break;
}
state = state->prev();
}
// If we reached the end of the undo history (e.g. because all undo
// states do not modify the sprite), the only way to be in the saved
// state is if the initial point of history is the saved state too
// i.e. when m_savedState is nullptr (and m_savedStateIsLost is
// false).
if (savedStateWithUndoes && m_savedState == nullptr)
return true;
// Now we try with redoes.
state = (currentState() ? currentState()->next(): firstState());
while (state) {
if (STATE_CMD(state)->doesChangeSavedState()) {
return false;
}
else if (m_savedState == state) {
return true;
}
state = state->next();
}
return false;
}
void DocUndo::markSavedState()
{
m_savedState = currentState();
m_savedStateIsLost = false;
notify_observers(&DocUndoObserver::onNewSavedState, this);
}
void DocUndo::impossibleToBackToSavedState()
{
// Now there is no state related to the disk state.
m_savedState = nullptr;
m_savedStateIsLost = true;
notify_observers(&DocUndoObserver::onNewSavedState, this);
}
std::string DocUndo::nextUndoLabel() const
{
const undo::UndoState* state = nextUndo();
if (state)
return STATE_CMD(state)->label();
else
return "";
}
std::string DocUndo::nextRedoLabel() const
{
const undo::UndoState* state = nextRedo();
if (state)
return STATE_CMD(state)->label();
else
return "";
}
SpritePosition DocUndo::nextUndoSpritePosition() const
{
const undo::UndoState* state = nextUndo();
if (state)
return STATE_CMD(state)->spritePositionBeforeExecute();
else
return SpritePosition();
}
SpritePosition DocUndo::nextRedoSpritePosition() const
{
const undo::UndoState* state = nextRedo();
if (state)
return STATE_CMD(state)->spritePositionAfterExecute();
else
return SpritePosition();
}
std::istream* DocUndo::nextUndoDocRange() const
{
const undo::UndoState* state = nextUndo();
if (state)
return STATE_CMD(state)->documentRangeBeforeExecute();
else
return nullptr;
}
std::istream* DocUndo::nextRedoDocRange() const
{
const undo::UndoState* state = nextRedo();
if (state)
return STATE_CMD(state)->documentRangeAfterExecute();
else
return nullptr;
}
Cmd* DocUndo::lastExecutedCmd() const
{
const undo::UndoState* state = m_undoHistory.currentState();
if (state)
return STATE_CMD(state);
else
return NULL;
}
void DocUndo::moveToState(const undo::UndoState* state)
{
ASSERT(!m_undoing);
base::ScopedValue undoing(m_undoing, true);
m_undoHistory.moveTo(state);
// After onCurrentUndoStateChange don't use the "state" argument, it
// might be deleted because some script might have modified the
// sprite on its "change" event.
notify_observers(&DocUndoObserver::onCurrentUndoStateChange, this);
// Recalculate the total undo size
size_t oldSize = m_totalUndoSize;
m_totalUndoSize = 0;
const undo::UndoState* s = m_undoHistory.firstState();
while (s) {
m_totalUndoSize += STATE_CMD(s)->memSize();
s = s->next();
}
if (m_totalUndoSize != oldSize)
notify_observers(&DocUndoObserver::onTotalUndoSizeChange, this);
}
const undo::UndoState* DocUndo::nextUndo() const
{
return m_undoHistory.currentState();
}
const undo::UndoState* DocUndo::nextRedo() const
{
const undo::UndoState* state = m_undoHistory.currentState();
if (state)
return state->next();
else
return m_undoHistory.firstState();
}
void DocUndo::onDeleteUndoState(undo::UndoState* state)
{
ASSERT(state);
Cmd* cmd = STATE_CMD(state);
UNDO_TRACE("UNDO: Deleting undo state <%s> of %s from %s\n",
cmd->label().c_str(),
base::get_pretty_memory_size(cmd->memSize()).c_str(),
base::get_pretty_memory_size(m_totalUndoSize).c_str());
m_totalUndoSize -= cmd->memSize();
notify_observers(&DocUndoObserver::onDeleteUndoState, this, state);
// Mark this document as impossible to match the version on disk
// because we're just going to delete the saved state.
if (m_savedState == state)
impossibleToBackToSavedState();
}
} // namespace app