CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
MarkdownView.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5
6#include "cdc_core/Raii.h"
7#include "cdc_ui/IView.h"
9
10namespace cdc::ui {
11
22class MarkdownView : public ViewBase {
23public:
24 static constexpr uint32_t MAX_SOURCE = 64u * 1024u;
25 static constexpr uint16_t MAX_ROWS = 600;
26
33 void init(const char* title, const char* src, size_t len);
34
44 void initPlain(const char* title, const char* src, size_t len);
45
47 void appendParsed(const StyledLine& line);
48
49 void render(bool partial) override;
50 InputResult onKey(char key) override;
51 InputResult onLongPress(char key) override;
52 void onExit() override;
53 const char* getName() const override { return "MarkdownView"; }
54 const char* getFooterHint() const override;
55
61 void selectMarker(uint16_t linkNum);
62
64 enum class Interact : uint8_t { Link, Check, Submit, Image };
65
67 struct InteractRef {
68 uint16_t row;
70 uint16_t id;
71 uint32_t srcOff;
72 };
73
75 void cycleInteractive(int dir);
76
78 void clearSelection() { interactSel_ = -1; highlightRow_ = -1; dirty_ = true; }
79
81 uint16_t interactiveCount() const { return interactCount_; }
82
88 using CheckSaveFn = void (*)(void* ud, const char* cp437Source, size_t len);
89 void setCheckSave(CheckSaveFn fn, void* ud) { checkSave_ = fn; checkSaveUd_ = ud; }
90
91protected:
94 virtual void rebuildInteractive();
95
97 virtual void activateSelected();
98
99 // Helpers for subclasses (avoid exposing the private Row type).
100 uint16_t mdRowCount() const { return rowCount_; }
101 void mdRowInfo(uint16_t i, const char*& text, uint16_t& len, bool& first,
102 uint32_t& srcOff) const;
103 void addInteract(uint16_t row, Interact kind, uint16_t id, uint32_t srcOff);
104 void clearInteract() { interactCount_ = 0; interactSel_ = -1; }
105 const InteractRef* selectedRef() const;
107 bool flipCheckChar(uint32_t srcOff);
109 void fireCheckSave();
110
111private:
112 static constexpr uint16_t MAX_TITLE_LEN = 64;
113 static constexpr uint16_t MAX_INTERACT = 256;
114 static constexpr size_t kLinkPoolCap = 8192;
115
116 struct Row {
117 const char* text;
118 uint16_t len;
119 uint8_t font;
120 uint8_t indent;
121 MdLineKind kind;
122 bool inverted;
123 bool first;
124 };
125
126 char titleBuf_[MAX_TITLE_LEN];
129 uint16_t rowCount_ = 0;
130 uint16_t scrollRow_ = 0;
131 bool truncated_ = false;
132 bool plain_ = false;
133 int textAreaWidth_ = 0;
134 int highlightRow_ = -1;
135
137 uint16_t interactCount_ = 0;
138 int interactSel_ = -1;
139 CheckSaveFn checkSave_ = nullptr;
140 void* checkSaveUd_ = nullptr;
141
142 // Markdown inline-link targets: the rewrite replaces "[text](url)" with the
143 // delimited form "\x10text\x11" and records each url here, indexed by the
144 // delimiter's document order (matches the ordinal scanned in rebuildInteractive).
147 uint16_t linkPoolLen_ = 0;
148 uint16_t inlineLinkCount_ = 0;
149
150 void addRow(const char* text, uint16_t len, uint8_t font, uint8_t indent,
151 MdLineKind kind, bool inverted, bool first);
152 uint16_t visibleRowCount() const;
153 void rewriteInlineLinks();
154 void drawStyledRow(void* gfx, const char* text, uint16_t len, int x, int yTop,
155 uint16_t fg);
156};
157
165MarkdownView* showMarkdown(const char* title, const char* src, size_t len);
166
174MarkdownView* showPlainText(const char* title, const char* src, size_t len);
175
176} // namespace cdc::ui
Shared RAII wrappers for firmware resources.
Scrollable viewer that renders a Markdown source.
void initPlain(const char *title, const char *src, size_t len)
Loads a source as plain, unrendered text (no Markdown parsing).
uint16_t mdRowCount() const
const char * getFooterHint() const override
void cycleInteractive(int dir)
Move the interactive selection by dir (+1/-1), highlighting its row.
void selectMarker(uint16_t linkNum)
Highlight the row containing the inline marker "[linkNum]" and scroll it into view....
void fireCheckSave()
Run the persistence callback with the current source (if set).
void setCheckSave(CheckSaveFn fn, void *ud)
void addInteract(uint16_t row, Interact kind, uint16_t id, uint32_t srcOff)
void clearSelection()
Clear the current interactive selection.
const char * getName() const override
static constexpr uint32_t MAX_SOURCE
Source byte cap.
void init(const char *title, const char *src, size_t len)
Loads and lays out a Markdown source.
virtual void rebuildInteractive()
Rebuild the interactive-element list from the rendered rows. Base finds task-list checkboxes; subclas...
uint16_t interactiveCount() const
Number of interactive elements on the page.
void onExit() override
void(*)(void *ud, const char *cp437Source, size_t len) CheckSaveFn
Persistence callback fired after a task-list checkbox toggles. Receives the full modified CP437 sourc...
void mdRowInfo(uint16_t i, const char *&text, uint16_t &len, bool &first, uint32_t &srcOff) const
InputResult onKey(char key) override
virtual void activateSelected()
Activate the selected element (Y). Base toggles checkboxes.
void appendParsed(const StyledLine &line)
Sink callback: wraps one parsed styled line into visual rows.
static constexpr uint16_t MAX_ROWS
Visual-row cap.
const InteractRef * selectedRef() const
InputResult onLongPress(char key) override
Interact
Interactive element kinds the view can select (4/6) and activate (Y).
bool flipCheckChar(uint32_t srcOff)
Toggle a checkbox state char in the source.
std::unique_ptr< T[], CapsFreeDeleter > PsramUniquePtr
Definition Raii.h:44
Centralized key-code constants for cdc_views.
Definition IModule.h:8
MarkdownView * showMarkdown(const char *title, const char *src, size_t len)
Parses and shows a Markdown source on the view stack.
MdLineKind
Block role of a styled line, used by the view to apply layout/markers.
InputResult
Definition IView.h:11
MarkdownView * showPlainText(const char *title, const char *src, size_t len)
Shows a source as plain, unrendered text on the view stack.
One selectable interactive element discovered in the rendered rows.
uint16_t id
Link: 1-based marker number; Check/Submit: ordinal.
uint16_t row
Visual row index (highlight/scroll target).
uint32_t srcOff
Check: byte offset of the state char in the source; else 0.
One rendered logical line produced by the Markdown parser.