1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 20:07:34 +00:00

HexEditor: Implement undo and redo actions

This commit is contained in:
kamp 2022-09-07 23:41:37 +02:00 committed by Linus Groh
parent 24f729d0ef
commit 8b0a464f5c
6 changed files with 204 additions and 7 deletions

View file

@ -7,10 +7,15 @@
#pragma once
#include <AK/StringView.h>
#include <AK/Time.h>
#include <AK/Types.h>
#include <AK/WeakPtr.h>
#include <LibCore/File.h>
#include <LibGUI/Command.h>
class HexDocument {
constexpr Time COMMAND_COMMIT_TIME = Time::from_milliseconds(400);
class HexDocument : public Weakable<HexDocument> {
public:
enum class Type {
Memory,
@ -77,3 +82,26 @@ private:
Array<u8, 2048> m_buffer;
size_t m_buffer_file_pos;
};
class HexDocumentUndoCommand : public GUI::Command {
public:
HexDocumentUndoCommand(WeakPtr<HexDocument> document, size_t position);
virtual void undo() override;
virtual void redo() override;
virtual String action_text() const override { return "Update cell"; }
virtual bool merge_with(GUI::Command const& other) override;
ErrorOr<void> try_add_changed_byte(u8 old_value, u8 new_value);
ErrorOr<void> try_add_changed_bytes(ByteBuffer old_values, ByteBuffer new_values);
private:
bool commit_time_expired() const { return Time::now_monotonic() - m_timestamp >= COMMAND_COMMIT_TIME; }
Time m_timestamp = Time::now_monotonic();
WeakPtr<HexDocument> m_document;
size_t m_position;
ByteBuffer m_old;
ByteBuffer m_new;
};