mirror of
				https://github.com/LadybirdBrowser/ladybird.git
				synced 2025-11-04 07:10:57 +00:00 
			
		
		
		
	This commit un-deprecates DeprecatedString, and repurposes it as a byte
string.
As the null state has already been removed, there are no other
particularly hairy blockers in repurposing this type as a byte string
(what it _really_ is).
This commit is auto-generated:
  $ xs=$(ack -l \bDeprecatedString\b\|deprecated_string AK Userland \
    Meta Ports Ladybird Tests Kernel)
  $ perl -pie 's/\bDeprecatedString\b/ByteString/g;
    s/deprecated_string/byte_string/g' $xs
  $ clang-format --style=file -i \
    $(git diff --name-only | grep \.cpp\|\.h)
  $ gn format $(git ls-files '*.gn' '*.gni')
		
	
			
		
			
				
	
	
		
			128 lines
		
	
	
	
		
			2.6 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			128 lines
		
	
	
	
		
			2.6 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
/*
 | 
						|
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 | 
						|
 * Copyright (c) 2022, the SerenityOS developers.
 | 
						|
 *
 | 
						|
 * SPDX-License-Identifier: BSD-2-Clause
 | 
						|
 */
 | 
						|
 | 
						|
#include <AK/NonnullOwnPtr.h>
 | 
						|
#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 = MonotonicTime::now();
 | 
						|
 | 
						|
    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<ByteString> UndoStack::undo_action_text() const
 | 
						|
{
 | 
						|
    if (!can_undo())
 | 
						|
        return {};
 | 
						|
    return m_stack[m_stack_index - 1]->action_text();
 | 
						|
}
 | 
						|
 | 
						|
Optional<ByteString> UndoStack::redo_action_text() const
 | 
						|
{
 | 
						|
    if (!can_redo())
 | 
						|
        return {};
 | 
						|
    return m_stack[m_stack_index]->action_text();
 | 
						|
}
 | 
						|
 | 
						|
}
 |