Show the undo size in the Undo History window

This commit is contained in:
David Capello 2017-10-26 21:42:25 -03:00
parent d748cc7582
commit 7e86f31cb4
4 changed files with 66 additions and 15 deletions

View File

@ -1,5 +1,5 @@
// Aseprite // Aseprite
// Copyright (C) 2015-2016 David Capello // Copyright (C) 2015-2017 David Capello
// //
// This program is distributed under the terms of // This program is distributed under the terms of
// the End-User License Agreement for Aseprite. // the End-User License Agreement for Aseprite.
@ -52,8 +52,9 @@ public:
}; };
UndoHistoryWindow(Context* ctx) UndoHistoryWindow(Context* ctx)
: m_ctx(ctx), : m_ctx(ctx)
m_document(nullptr) { , m_document(nullptr) {
m_title = text();
actions()->Change.connect(&UndoHistoryWindow::onChangeAction, this); actions()->Change.connect(&UndoHistoryWindow::onChangeAction, this);
} }
@ -168,6 +169,10 @@ private:
refillList(history); refillList(history);
} }
void onTotalUndoSizeChange(DocumentUndo* history) override {
updateTitle();
}
void attachDocument(app::Document* document) { void attachDocument(app::Document* document) {
detachDocument(); detachDocument();
@ -179,6 +184,7 @@ private:
history->add_observer(this); history->add_observer(this);
refillList(history); refillList(history);
updateTitle();
} }
void detachDocument() { void detachDocument() {
@ -188,6 +194,7 @@ private:
clearList(); clearList();
m_document->undoHistory()->remove_observer(this); m_document->undoHistory()->remove_observer(this);
m_document = nullptr; m_document = nullptr;
updateTitle();
} }
void clearList() { void clearList() {
@ -232,9 +239,19 @@ private:
} }
} }
void updateTitle() {
if (!m_document)
setText(m_title);
else
setTextf("%s (%s)",
m_title.c_str(),
base::get_pretty_memory_size(m_document->undoHistory()->totalUndoSize()).c_str());
}
Context* m_ctx; Context* m_ctx;
app::Document* m_document; app::Document* m_document;
doc::frame_t m_frame; doc::frame_t m_frame;
std::string m_title;
}; };
class UndoHistoryCommand : public Command { class UndoHistoryCommand : public Command {

View File

@ -24,6 +24,7 @@
#include <stdexcept> #include <stdexcept>
#define UNDO_TRACE(...) #define UNDO_TRACE(...)
#define STATE_CMD(state) (static_cast<CmdTransaction*>(state->cmd()))
namespace app { namespace app {
@ -59,6 +60,7 @@ void DocumentUndo::add(CmdTransaction* cmd)
m_totalUndoSize += cmd->memSize(); m_totalUndoSize += cmd->memSize();
notify_observers(&DocumentUndoObserver::onAddUndoState, this); notify_observers(&DocumentUndoObserver::onAddUndoState, this);
notify_observers(&DocumentUndoObserver::onTotalUndoSizeChange, this);
if (App::instance()) { if (App::instance()) {
const size_t undoLimitSize = const size_t undoLimitSize =
@ -97,14 +99,34 @@ bool DocumentUndo::canRedo() const
void DocumentUndo::undo() void DocumentUndo::undo()
{ {
m_undoHistory.undo(); const undo::UndoState* state = nextUndo();
notify_observers(&DocumentUndoObserver::onAfterUndo, this); ASSERT(state);
const Cmd* cmd = STATE_CMD(state);
size_t oldSize = m_totalUndoSize;
m_totalUndoSize -= cmd->memSize();
{
m_undoHistory.undo();
notify_observers(&DocumentUndoObserver::onAfterUndo, this);
}
m_totalUndoSize += cmd->memSize();
if (m_totalUndoSize != oldSize)
notify_observers(&DocumentUndoObserver::onTotalUndoSizeChange, this);
} }
void DocumentUndo::redo() void DocumentUndo::redo()
{ {
m_undoHistory.redo(); const undo::UndoState* state = nextRedo();
notify_observers(&DocumentUndoObserver::onAfterRedo, this); ASSERT(state);
const Cmd* cmd = STATE_CMD(state);
size_t oldSize = m_totalUndoSize;
m_totalUndoSize -= cmd->memSize();
{
m_undoHistory.redo();
notify_observers(&DocumentUndoObserver::onAfterRedo, this);
}
m_totalUndoSize += cmd->memSize();
if (m_totalUndoSize != oldSize)
notify_observers(&DocumentUndoObserver::onTotalUndoSizeChange, this);
} }
void DocumentUndo::clearRedo() void DocumentUndo::clearRedo()
@ -133,7 +155,7 @@ std::string DocumentUndo::nextUndoLabel() const
{ {
const undo::UndoState* state = nextUndo(); const undo::UndoState* state = nextUndo();
if (state) if (state)
return static_cast<Cmd*>(state->cmd())->label(); return STATE_CMD(state)->label();
else else
return ""; return "";
} }
@ -142,7 +164,7 @@ std::string DocumentUndo::nextRedoLabel() const
{ {
const undo::UndoState* state = nextRedo(); const undo::UndoState* state = nextRedo();
if (state) if (state)
return static_cast<const Cmd*>(state->cmd())->label(); return STATE_CMD(state)->label();
else else
return ""; return "";
} }
@ -151,8 +173,7 @@ SpritePosition DocumentUndo::nextUndoSpritePosition() const
{ {
const undo::UndoState* state = nextUndo(); const undo::UndoState* state = nextUndo();
if (state) if (state)
return static_cast<const CmdTransaction*>(state->cmd()) return STATE_CMD(state)->spritePositionBeforeExecute();
->spritePositionBeforeExecute();
else else
return SpritePosition(); return SpritePosition();
} }
@ -161,8 +182,7 @@ SpritePosition DocumentUndo::nextRedoSpritePosition() const
{ {
const undo::UndoState* state = nextRedo(); const undo::UndoState* state = nextRedo();
if (state) if (state)
return static_cast<const CmdTransaction*>(state->cmd()) return STATE_CMD(state)->spritePositionAfterExecute();
->spritePositionAfterExecute();
else else
return SpritePosition(); return SpritePosition();
} }
@ -171,7 +191,7 @@ Cmd* DocumentUndo::lastExecutedCmd() const
{ {
const undo::UndoState* state = m_undoHistory.currentState(); const undo::UndoState* state = m_undoHistory.currentState();
if (state) if (state)
return static_cast<Cmd*>(state->cmd()); return STATE_CMD(state);
else else
return NULL; return NULL;
} }
@ -179,6 +199,17 @@ Cmd* DocumentUndo::lastExecutedCmd() const
void DocumentUndo::moveToState(const undo::UndoState* state) void DocumentUndo::moveToState(const undo::UndoState* state)
{ {
m_undoHistory.moveTo(state); m_undoHistory.moveTo(state);
// 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(&DocumentUndoObserver::onTotalUndoSizeChange, this);
} }
const undo::UndoState* DocumentUndo::nextUndo() const const undo::UndoState* DocumentUndo::nextUndo() const
@ -197,7 +228,7 @@ const undo::UndoState* DocumentUndo::nextRedo() const
void DocumentUndo::onDeleteUndoState(undo::UndoState* state) void DocumentUndo::onDeleteUndoState(undo::UndoState* state)
{ {
Cmd* cmd = static_cast<Cmd*>(state->cmd()); Cmd* cmd = STATE_CMD(state);
UNDO_TRACE("UNDO: Deleting undo state <%s> of %s from %s\n", UNDO_TRACE("UNDO: Deleting undo state <%s> of %s from %s\n",
cmd->label().c_str(), cmd->label().c_str(),

View File

@ -32,6 +32,8 @@ namespace app {
public: public:
DocumentUndo(); DocumentUndo();
size_t totalUndoSize() const { return m_totalUndoSize; }
void setContext(doc::Context* ctx); void setContext(doc::Context* ctx);
void add(CmdTransaction* cmd); void add(CmdTransaction* cmd);

View File

@ -25,6 +25,7 @@ class DocumentUndo;
virtual void onAfterUndo(DocumentUndo* history) = 0; virtual void onAfterUndo(DocumentUndo* history) = 0;
virtual void onAfterRedo(DocumentUndo* history) = 0; virtual void onAfterRedo(DocumentUndo* history) = 0;
virtual void onClearRedo(DocumentUndo* history) = 0; virtual void onClearRedo(DocumentUndo* history) = 0;
virtual void onTotalUndoSizeChange(DocumentUndo* history) = 0;
}; };
} // namespace app } // namespace app