summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGUI/UndoStack.h
blob: 5a0df357dcf033a7558d5bb34f14f92276fe6ef6 (plain)
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
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 * Copyright (c) 2022, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Function.h>
#include <AK/Time.h>
#include <AK/Vector.h>
#include <LibGUI/Forward.h>

namespace GUI {

class UndoStack {
public:
    UndoStack() = default;
    ~UndoStack() = default;

    void push(NonnullOwnPtr<Command>);
    ErrorOr<void> try_push(NonnullOwnPtr<Command>);

    bool can_undo() const;
    bool can_redo() const;

    void undo();
    void redo();

    void set_current_unmodified();
    bool is_current_modified() const;

    Optional<Time> last_unmodified_timestamp() const { return m_last_unmodified_timestamp; }

    void clear();

    Optional<DeprecatedString> undo_action_text() const;
    Optional<DeprecatedString> redo_action_text() const;

    Function<void()> on_state_change;

private:
    Vector<NonnullOwnPtr<Command>> m_stack;
    size_t m_stack_index { 0 };
    Optional<size_t> m_clean_index;
    Optional<Time> m_last_unmodified_timestamp;
};

}