2020-01-18 05:38:21 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2020-02-16 05:17:49 -03:00
|
|
|
#include <LibGUI/Command.h>
|
2020-02-06 16:33:02 -03:00
|
|
|
#include <LibGUI/UndoStack.h>
|
2019-11-30 11:36:17 -03:00
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
namespace GUI {
|
|
|
|
|
|
|
|
|
|
UndoStack::UndoStack()
|
2019-11-30 11:36:17 -03:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
UndoStack::~UndoStack()
|
2019-11-30 11:36:17 -03:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-08 08:09:24 -03:00
|
|
|
bool UndoStack::can_undo() const
|
|
|
|
|
{
|
2021-05-08 16:08:41 -03:00
|
|
|
return m_stack_index > 0;
|
2021-05-08 08:09:24 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool UndoStack::can_redo() const
|
|
|
|
|
{
|
|
|
|
|
if (m_stack.is_empty())
|
|
|
|
|
return false;
|
2021-05-08 16:08:41 -03:00
|
|
|
return m_stack_index != m_stack.size();
|
2021-05-08 08:09:24 -03:00
|
|
|
}
|
|
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
void UndoStack::undo()
|
2019-11-30 11:36:17 -03:00
|
|
|
{
|
|
|
|
|
if (!can_undo())
|
|
|
|
|
return;
|
|
|
|
|
|
2021-05-08 16:08:41 -03:00
|
|
|
auto& command = m_stack[--m_stack_index];
|
|
|
|
|
command.undo();
|
2021-05-08 08:14:52 -03:00
|
|
|
|
|
|
|
|
if (on_state_change)
|
|
|
|
|
on_state_change();
|
2019-11-30 11:36:17 -03:00
|
|
|
}
|
|
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
void UndoStack::redo()
|
2019-11-30 11:36:17 -03:00
|
|
|
{
|
|
|
|
|
if (!can_redo())
|
|
|
|
|
return;
|
|
|
|
|
|
2021-05-08 16:08:41 -03:00
|
|
|
auto& command = m_stack[m_stack_index++];
|
|
|
|
|
command.redo();
|
2021-05-08 08:14:52 -03:00
|
|
|
|
|
|
|
|
if (on_state_change)
|
|
|
|
|
on_state_change();
|
2021-05-08 08:09:24 -03:00
|
|
|
}
|
|
|
|
|
|
2021-05-08 16:08:41 -03:00
|
|
|
void UndoStack::push(NonnullOwnPtr<Command> command)
|
2021-05-08 08:09:24 -03:00
|
|
|
{
|
2021-05-08 16:08:41 -03:00
|
|
|
// 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)
|
2021-12-02 09:53:02 -03:00
|
|
|
(void)m_stack.take_last();
|
2021-05-08 16:08:41 -03:00
|
|
|
|
2021-05-08 08:09:24 -03:00
|
|
|
if (m_clean_index.has_value() && m_clean_index.value() > m_stack.size())
|
|
|
|
|
m_clean_index = {};
|
2021-05-05 14:09:43 -03:00
|
|
|
|
2021-08-14 20:03:37 -03:00
|
|
|
if (!m_stack.is_empty() && is_current_modified()) {
|
2021-05-08 16:08:41 -03:00
|
|
|
if (m_stack.last().merge_with(*command))
|
2021-05-08 11:49:36 -03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-08 16:08:41 -03:00
|
|
|
m_stack.append(move(command));
|
|
|
|
|
m_stack_index = m_stack.size();
|
2021-05-08 08:14:52 -03:00
|
|
|
|
|
|
|
|
if (on_state_change)
|
|
|
|
|
on_state_change();
|
2019-11-30 11:36:17 -03:00
|
|
|
}
|
|
|
|
|
|
2021-05-05 14:09:43 -03:00
|
|
|
void UndoStack::set_current_unmodified()
|
|
|
|
|
{
|
2021-05-08 08:14:52 -03:00
|
|
|
if (m_clean_index.has_value() && m_clean_index.value() == m_stack_index)
|
|
|
|
|
return;
|
2021-05-08 16:08:41 -03:00
|
|
|
|
2021-05-08 08:09:24 -03:00
|
|
|
m_clean_index = m_stack_index;
|
2022-01-04 13:32:19 -03:00
|
|
|
m_last_unmodified_timestamp = Time::now_monotonic();
|
2021-05-08 08:14:52 -03:00
|
|
|
|
|
|
|
|
if (on_state_change)
|
|
|
|
|
on_state_change();
|
2021-05-05 14:09:43 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool UndoStack::is_current_modified() const
|
|
|
|
|
{
|
2021-05-08 16:08:41 -03:00
|
|
|
if (m_stack.is_empty())
|
|
|
|
|
return false;
|
2021-05-08 08:40:33 -03:00
|
|
|
if (!m_clean_index.has_value())
|
|
|
|
|
return true;
|
|
|
|
|
if (m_stack_index != m_clean_index.value())
|
|
|
|
|
return true;
|
|
|
|
|
return false;
|
2019-11-30 11:36:17 -03:00
|
|
|
}
|
2020-02-02 11:07:41 -03:00
|
|
|
|
2021-05-05 12:21:45 -03:00
|
|
|
void UndoStack::clear()
|
|
|
|
|
{
|
2021-05-08 08:14:52 -03:00
|
|
|
if (m_stack.is_empty() && m_stack_index == 0 && !m_clean_index.has_value())
|
|
|
|
|
return;
|
|
|
|
|
|
2021-05-05 12:21:45 -03:00
|
|
|
m_stack.clear();
|
|
|
|
|
m_stack_index = 0;
|
2021-05-05 14:09:43 -03:00
|
|
|
m_clean_index.clear();
|
2021-05-08 08:14:52 -03:00
|
|
|
|
|
|
|
|
if (on_state_change)
|
|
|
|
|
on_state_change();
|
2021-05-05 12:21:45 -03:00
|
|
|
}
|
|
|
|
|
|
2021-05-08 16:18:51 -03:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
}
|