summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGUI/UndoStack.cpp
blob: cb273f4d37c7e666d48413706e5b55c0eb3ed847 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 * Copyright (c) 2022, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibGUI/Command.h>
#include <LibGUI/UndoStack.h>

namespace GUI {

bool UndoStack::can_undo() const
{
    return m_stack_index > 0;
}

bool UndoStack::can_redo() const
{
    if (m_stack.is_empty())
        return false;
    return m_stack_index != m_stack.size();
}

void UndoStack::undo()
{
    if (!can_undo())
        return;

    auto& command = m_stack[--m_stack_index];
    command.undo();

    if (on_state_change)
        on_state_change();
}

void UndoStack::redo()
{
    if (!can_redo())
        return;

    auto& command = m_stack[m_stack_index++];
    command.redo();

    if (on_state_change)
        on_state_change();
}

ErrorOr<void> UndoStack::try_push(NonnullOwnPtr<Command> command)
{
    // If the stack cursor is behind the top of the stack, nuke everything from here to the top.
    while (m_stack.size() != m_stack_index)
        (void)m_stack.take_last();

    if (m_clean_index.has_value() && m_clean_index.value() > m_stack.size())
        m_clean_index = {};

    if (!m_stack.is_empty() && is_current_modified()) {
        if (m_stack.last().merge_with(*command))
            return {};
    }

    TRY(m_stack.try_append(move(command)));
    m_stack_index = m_stack.size();

    if (on_state_change)
        on_state_change();

    return {};
}

void UndoStack::push(NonnullOwnPtr<Command> command)
{
    MUST(try_push(move(command)));
}

void UndoStack::set_current_unmodified()
{
    if (m_clean_index.has_value() && m_clean_index.value() == m_stack_index)
        return;

    m_clean_index = m_stack_index;
    m_last_unmodified_timestamp = Time::now_monotonic();

    if (on_state_change)
        on_state_change();
}

bool UndoStack::is_current_modified() const
{
    if (m_stack.is_empty())
        return false;
    if (!m_clean_index.has_value())
        return true;
    if (m_stack_index != m_clean_index.value())
        return true;
    return false;
}

void UndoStack::clear()
{
    if (m_stack.is_empty() && m_stack_index == 0 && !m_clean_index.has_value())
        return;

    m_stack.clear();
    m_stack_index = 0;
    m_clean_index.clear();

    if (on_state_change)
        on_state_change();
}

Optional<String> UndoStack::undo_action_text() const
{
    if (!can_undo())
        return {};
    return m_stack[m_stack_index - 1].action_text();
}

Optional<String> UndoStack::redo_action_text() const
{
    if (!can_redo())
        return {};
    return m_stack[m_stack_index].action_text();
}

}