Repository navigation
Expand file tree
/
Copy pathui_document.h
More file actions
81 lines (65 loc) · 3.48 KB
/
Copy pathui_document.h
File metadata and controls
81 lines (65 loc) · 3.48 KB
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
// This file is part of the Diffractor photo and video organizer
// Copyright 2026 Zac Walker
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
// Purpose: Presents decoded documents in the common flex panel, using libdesktop's Markdown and CSV
// views and source-preserving text and dump contents. Selection and column sizes survive panel rebuilds.
#pragma once
#include "model.h"
#include "model_document.h"
#include "ui_elements.h"
#include "libdesktop/ui/views/document_panel.h"
class document_content final : public std::enable_shared_from_this<document_content>, public view_element
{
public:
// The display's document, decoded when its bytes were read.
document_content(display_state_ptr display, const view_element_options& style_in) noexcept;
// A dump of bytes this control holds itself.
document_content(std::vector<uint8_t> bytes, const view_element_options& style_in) noexcept;
sizei measure(render::measure_context& mc, int width_limit) const override;
void render(render::draw_context& dc, pointi element_offset) const override;
void layout(render::measure_context& mc, recti bounds_in) override;
void visit_children(const std::function<void(const view_element_ptr&)>& visit) const override;
view_controller_ptr controller_from_location(const view_host_ptr& host, pointi loc, pointi element_offset,
hit_test_context& ctx) override;
document_state& document() const { return *_document; }
// The display whose document this is; none for a dump of bytes the control holds.
const display_state_ptr& display() const { return _display; }
core::cspan bytes() const;
// The position under a point. Above the document is its start and below it its end, so a drag
// that leaves the document still selects to the edge it left by. With character set, the
// character under the point rather than the gap nearest it - what a double click selects.
document_position position_from_point(pointi loc, pointi element_offset, bool character = false) const;
private:
static constexpr int padding = 4;
static constexpr int byte_step = 4;
static constexpr int max_bytes_per_line = 32;
display_state_ptr _display;
std::vector<uint8_t> _bytes;
std::shared_ptr<document_state> _document;
render::font_face _font = render::font_face::code;
mutable int _line_height = 0;
mutable double _char_width = 0.0;
// A dump is centred on the width of its rows, as the hex view always was.
mutable int _dump_width = 0;
std::shared_ptr<ui::doc_view> _formatted;
int text_left(recti logical_bounds) const;
};
class document_control final : public ui::document_panel
{
std::shared_ptr<document_content> _content;
document_control(std::shared_ptr<document_content> content, const view_element_options& options);
public:
document_control(display_state_ptr display, const view_element_options& options);
document_control(std::vector<uint8_t> bytes, const view_element_options& options);
document_state& document() const { return _content->document(); }
const display_state_ptr& display() const { return _content->display(); }
core::cspan bytes() const { return _content->bytes(); }
recti content_bounds() const { return _content->bounds; }
document_position position_from_point(pointi loc, pointi element_offset, bool character = false) const
{
return _content->position_from_point(loc, element_offset, character);
}
};