diff options
author | Andreas Kling <kling@serenityos.org> | 2021-05-08 16:49:36 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-05-08 22:17:50 +0200 |
commit | ff912946ae3e16c0d338199d77a42923a62a3f71 (patch) | |
tree | cd4fdc6ef14f74109b9546dd200579025a0af3db /Userland/Libraries/LibGUI | |
parent | 244665d99c3acb43081e44f6a88523388761e37e (diff) | |
download | serenity-ff912946ae3e16c0d338199d77a42923a62a3f71.zip |
LibGUI: Support merging of adjacent commands on the UndoStack
When pushing a new command on an undo stack, we will now attempt to
merge it into the stack's current command.
Merging is implemented by overriding the "merge_with(Command const&)"
virtual on GUI::Command. :^)
Diffstat (limited to 'Userland/Libraries/LibGUI')
-rw-r--r-- | Userland/Libraries/LibGUI/Command.h | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibGUI/UndoStack.cpp | 6 |
2 files changed, 8 insertions, 0 deletions
diff --git a/Userland/Libraries/LibGUI/Command.h b/Userland/Libraries/LibGUI/Command.h index 10d4a65cff..82626fd7cb 100644 --- a/Userland/Libraries/LibGUI/Command.h +++ b/Userland/Libraries/LibGUI/Command.h @@ -19,6 +19,8 @@ public: String action_text() const { return m_action_text; } + virtual bool merge_with(Command const&) { return false; } + protected: Command() { } void set_action_text(const String& text) { m_action_text = text; } diff --git a/Userland/Libraries/LibGUI/UndoStack.cpp b/Userland/Libraries/LibGUI/UndoStack.cpp index dec25df49c..2df8460070 100644 --- a/Userland/Libraries/LibGUI/UndoStack.cpp +++ b/Userland/Libraries/LibGUI/UndoStack.cpp @@ -77,6 +77,12 @@ void UndoStack::push(NonnullOwnPtr<Command>&& command) finalize_current_combo(); } + if (!m_stack.last().commands.is_empty()) { + bool merged = m_stack.last().commands.last().merge_with(*command); + if (merged) + return; + } + m_stack.last().commands.append(move(command)); if (on_state_change) |