1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 21:08:12 +00:00
serenity/Userland/Applications/HexEditor/HexDocument.h
Sam Atkins cbd28c9110 HexEditor: Add annotations system
Allow the user to highlight sections of the edited document, giving them
arbitrary background colors. These annotations can be created from a
selection, or by manually specifying the start and end offsets.
Annotations can be edited or deleted by right-clicking them.

Any color can be used for the background. Dark colors automatically make
the text white for easier readability. When creating a new annotation,
we use whatever color the user last picked as this is slightly more
likely to be the one they want.

Icons contributed by Cubic Love.

Co-authored-by: Cubic Love <7754483+cubiclove@users.noreply.github.com>
2024-01-14 13:45:02 +00:00

129 lines
3.6 KiB
C++

/*
* Copyright (c) 2021, Arne Elster <arne@elster.li>
* Copyright (c) 2024, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/HashMap.h>
#include <AK/NonnullOwnPtr.h>
#include <AK/StringView.h>
#include <AK/Time.h>
#include <AK/Types.h>
#include <AK/WeakPtr.h>
#include <LibCore/Forward.h>
#include <LibGUI/Command.h>
#include <LibGfx/Color.h>
constexpr Duration COMMAND_COMMIT_TIME = Duration::from_milliseconds(400);
struct Annotation {
size_t start_offset { 0 };
size_t end_offset { 0 };
Gfx::Color background_color { Color::from_argb(0xfffce94f) };
bool operator==(Annotation const& other) const = default;
};
class HexDocument : public Weakable<HexDocument> {
public:
enum class Type {
Memory,
File
};
struct Cell {
u8 value;
bool modified;
};
virtual ~HexDocument() = default;
virtual Cell get(size_t position) = 0;
virtual u8 get_unchanged(size_t position) = 0;
virtual void set(size_t position, u8 value);
virtual size_t size() const = 0;
virtual Type type() const = 0;
virtual bool is_dirty() const;
virtual void clear_changes() = 0;
ReadonlySpan<Annotation> annotations() const { return m_annotations; }
void add_annotation(Annotation);
void delete_annotation(Annotation const&);
Optional<Annotation&> closest_annotation_at(size_t position);
protected:
HashMap<size_t, u8> m_changes;
Vector<Annotation> m_annotations;
};
class HexDocumentMemory final : public HexDocument {
public:
explicit HexDocumentMemory(ByteBuffer&& buffer);
virtual ~HexDocumentMemory() = default;
Cell get(size_t position) override;
u8 get_unchanged(size_t position) override;
size_t size() const override;
Type type() const override;
void clear_changes() override;
ErrorOr<void> write_to_file(Core::File& file);
private:
ByteBuffer m_buffer;
};
class HexDocumentFile final : public HexDocument {
public:
static ErrorOr<NonnullOwnPtr<HexDocumentFile>> create(NonnullOwnPtr<Core::File> file);
virtual ~HexDocumentFile() = default;
HexDocumentFile(HexDocumentFile&&) = default;
HexDocumentFile(HexDocumentFile const&) = delete;
ErrorOr<void> set_file(NonnullOwnPtr<Core::File> file);
NonnullOwnPtr<Core::File> const& file() const;
ErrorOr<void> write_to_file();
ErrorOr<void> write_to_file(Core::File& file);
Cell get(size_t position) override;
u8 get_unchanged(size_t position) override;
size_t size() const override;
Type type() const override;
void clear_changes() override;
private:
explicit HexDocumentFile(NonnullOwnPtr<Core::File> file);
ErrorOr<void> initialize_internal_state();
void ensure_position_in_buffer(size_t position);
NonnullOwnPtr<Core::File> m_file;
size_t m_file_size;
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 ByteString 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 MonotonicTime::now() - m_timestamp >= COMMAND_COMMIT_TIME; }
MonotonicTime m_timestamp = MonotonicTime::now();
WeakPtr<HexDocument> m_document;
size_t m_position;
ByteBuffer m_old;
ByteBuffer m_new;
};