2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-02-26 10:50:04 -07:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2019-11-30 15:36:17 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
2021-05-08 14:00:56 +02:00
|
|
|
#include <AK/Function.h>
|
2019-11-30 15:36:17 +01:00
|
|
|
#include <AK/NonnullOwnPtrVector.h>
|
2022-01-04 17:32:19 +01:00
|
|
|
#include <AK/Time.h>
|
2020-02-16 09:17:49 +01:00
|
|
|
#include <LibGUI/Forward.h>
|
2019-11-30 15:36:17 +01:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
namespace GUI {
|
2019-11-30 15:36:17 +01:00
|
|
|
|
2020-02-02 15:07:41 +01:00
|
|
|
class UndoStack {
|
2019-11-30 15:36:17 +01:00
|
|
|
public:
|
2022-02-26 10:50:04 -07:00
|
|
|
UndoStack() = default;
|
|
|
|
|
~UndoStack() = default;
|
2019-11-30 15:36:17 +01:00
|
|
|
|
2021-05-08 21:08:41 +02:00
|
|
|
void push(NonnullOwnPtr<Command>);
|
2022-07-04 21:36:30 -04:00
|
|
|
ErrorOr<void> try_push(NonnullOwnPtr<Command>);
|
2019-11-30 15:36:17 +01:00
|
|
|
|
2021-05-08 13:09:24 +02:00
|
|
|
bool can_undo() const;
|
|
|
|
|
bool can_redo() const;
|
2019-11-30 15:36:17 +01:00
|
|
|
|
|
|
|
|
void undo();
|
|
|
|
|
void redo();
|
|
|
|
|
|
2021-05-05 14:09:43 -03:00
|
|
|
void set_current_unmodified();
|
|
|
|
|
bool is_current_modified() const;
|
|
|
|
|
|
2022-01-04 17:32:19 +01:00
|
|
|
Optional<Time> last_unmodified_timestamp() const { return m_last_unmodified_timestamp; }
|
|
|
|
|
|
2021-05-05 12:21:45 -03:00
|
|
|
void clear();
|
|
|
|
|
|
2021-05-08 21:18:51 +02:00
|
|
|
Optional<String> undo_action_text() const;
|
|
|
|
|
Optional<String> redo_action_text() const;
|
|
|
|
|
|
2021-05-08 13:14:52 +02:00
|
|
|
Function<void()> on_state_change;
|
|
|
|
|
|
2019-11-30 15:36:17 +01:00
|
|
|
private:
|
2021-05-08 21:08:41 +02:00
|
|
|
NonnullOwnPtrVector<Command> m_stack;
|
2020-02-25 14:49:47 +01:00
|
|
|
size_t m_stack_index { 0 };
|
2021-05-05 14:09:43 -03:00
|
|
|
Optional<size_t> m_clean_index;
|
2022-01-04 17:32:19 +01:00
|
|
|
Optional<Time> m_last_unmodified_timestamp;
|
2019-11-30 15:36:17 +01:00
|
|
|
};
|
2020-02-02 15:07:41 +01:00
|
|
|
|
|
|
|
|
}
|