2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2019-11-30 17:36:17 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/NonnullOwnPtrVector.h>
|
2020-02-16 11:17:49 +03:00
|
|
|
#include <LibGUI/Forward.h>
|
2019-11-30 17:36:17 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
namespace GUI {
|
2019-11-30 17:36:17 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
class UndoStack {
|
2019-11-30 17:36:17 +03:00
|
|
|
public:
|
2020-02-02 17:07:41 +03:00
|
|
|
UndoStack();
|
|
|
|
~UndoStack();
|
2019-11-30 17:36:17 +03:00
|
|
|
|
2020-02-02 17:07:41 +03:00
|
|
|
void push(NonnullOwnPtr<Command>&&);
|
2019-11-30 17:36:17 +03:00
|
|
|
|
|
|
|
bool can_undo() const { return m_stack_index < m_stack.size() && !m_stack.is_empty(); }
|
2021-05-08 00:33:31 +03:00
|
|
|
bool can_redo() const { return m_stack_index > 0 && !m_stack.is_empty() && m_stack[m_stack_index - 1].commands.size() > 0; }
|
2019-11-30 17:36:17 +03:00
|
|
|
|
|
|
|
void undo();
|
|
|
|
void redo();
|
|
|
|
|
|
|
|
void finalize_current_combo();
|
|
|
|
|
2021-05-05 20:09:43 +03:00
|
|
|
void set_current_unmodified();
|
|
|
|
bool is_current_modified() const;
|
|
|
|
|
2021-05-05 18:21:45 +03:00
|
|
|
void clear();
|
|
|
|
|
2019-11-30 17:36:17 +03:00
|
|
|
private:
|
2021-05-08 00:33:31 +03:00
|
|
|
struct Combo {
|
|
|
|
NonnullOwnPtrVector<Command> commands;
|
2019-11-30 17:36:17 +03:00
|
|
|
};
|
|
|
|
|
2021-05-08 00:33:31 +03:00
|
|
|
NonnullOwnPtrVector<Combo> m_stack;
|
2020-02-25 16:49:47 +03:00
|
|
|
size_t m_stack_index { 0 };
|
2021-05-05 20:09:43 +03:00
|
|
|
Optional<size_t> m_clean_index;
|
2019-11-30 17:36:17 +03:00
|
|
|
};
|
2020-02-02 17:07:41 +03:00
|
|
|
|
|
|
|
}
|