CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
ConfirmView.cpp
Go to the documentation of this file.
1
6
10#include "cdc_ui/I18n.h"
11#include "cdc_ui/ViewStack.h"
12#include "cdc_hal/IDisplay.h"
13#include <goodisplay/gdey029T94.h>
14#include <cstring>
15
16namespace cdc::ui {
17
24void ConfirmView::init(const char* message, Icon icon) {
25 if (message) {
26 strncpy(message_, message, MAX_MSG_LEN - 1);
27 message_[MAX_MSG_LEN - 1] = '\0';
28 } else {
29 message_[0] = '\0';
30 }
31 icon_ = icon;
32 dirty_ = true;
33}
34
41 if (key == KEY_YES) {
43 if (onConfirm_) {
44 onConfirm_(confirmUserData_);
45 }
47 }
48
49 if (key == KEY_NO) {
51 if (onCancel_) {
52 onCancel_(cancelUserData_);
53 }
55 }
56
58}
59
65void ConfirmView::render(bool partial) {
66 (void)partial;
67
69 if (!display) return;
70
71 auto* gfx = static_cast<Gdey029T94*>(display->getNativeHandle());
72 if (!gfx) return;
73
74 const uint16_t width = display->getWidth();
75 const uint16_t height = display->getHeight();
76
77 // Calculate centered position
78 int boxX = (width - BOX_WIDTH) / 2;
79 int boxY = (height - BOX_HEIGHT) / 2;
80
81 // Draw white box with double black border
82 render::drawDialogFrame(gfx, boxX, boxY, BOX_WIDTH, BOX_HEIGHT);
83
84 gfx->setTextColor(EPD_BLACK);
85 gfx->setTextSize(1);
86
87 // Text position (adjusted if icon present)
88 int textX = boxX + 15;
89 int textY = boxY + 20;
90
91 // Draw icon if present
92 if (icon_ != Icon::NONE) {
93 int iconX = boxX + 20;
94 int iconY = boxY + 22;
95
96 switch (icon_) {
97 case Icon::QUESTION:
98 // Question mark in circle
99 gfx->drawCircle(iconX, iconY, 8, EPD_BLACK);
100 gfx->setCursor(iconX - 3, iconY + 4);
101 gfx->print("?");
102 break;
103
104 case Icon::WARNING:
105 // Warning triangle
106 gfx->drawTriangle(iconX, iconY - 7, iconX - 7, iconY + 6, iconX + 7, iconY + 6, EPD_BLACK);
107 gfx->fillRect(iconX - 1, iconY - 2, 2, 5, EPD_BLACK);
108 gfx->fillRect(iconX - 1, iconY + 4, 2, 2, EPD_BLACK);
109 break;
110
111 case Icon::ERROR:
112 // X in circle
113 gfx->drawCircle(iconX, iconY, 8, EPD_BLACK);
114 gfx->drawLine(iconX - 4, iconY - 4, iconX + 4, iconY + 4, EPD_BLACK);
115 gfx->drawLine(iconX - 4, iconY + 4, iconX + 4, iconY - 4, EPD_BLACK);
116 break;
117
118 default:
119 break;
120 }
121
122 textX = boxX + 40; // Shift text right when icon present
123 }
124
125 // Draw message text (possibly multi-line for long messages)
126 gfx->setCursor(textX, textY);
127
128 // Simple word wrap for longer messages
129 const char* ptr = message_;
130 int lineY = textY;
131 int maxLineWidth = BOX_WIDTH - (textX - boxX) - 10;
132 char lineBuf[48];
133 int lineLen = 0;
134
135 while (*ptr) {
136 // Find next word boundary
137 const char* wordEnd = ptr;
138 while (*wordEnd && *wordEnd != ' ' && *wordEnd != '\n') wordEnd++;
139
140 int wordLen = wordEnd - ptr;
141
142 // Check if word fits on current line
143 if (lineLen + wordLen + 1 < (int)sizeof(lineBuf) - 1 &&
144 (lineLen + wordLen) * 6 < maxLineWidth) {
145 // Add space if not first word
146 if (lineLen > 0) {
147 lineBuf[lineLen++] = ' ';
148 }
149 memcpy(lineBuf + lineLen, ptr, wordLen);
150 lineLen += wordLen;
151 } else {
152 // Print current line and start new one
153 if (lineLen > 0) {
154 lineBuf[lineLen] = '\0';
155 gfx->setCursor(textX, lineY);
156 render::printText(gfx, lineBuf);
157 lineY += 12;
158 lineLen = 0;
159 }
160 // Start new line with current word
161 memcpy(lineBuf, ptr, wordLen);
162 lineLen = wordLen;
163 }
164
165 ptr = wordEnd;
166 while (*ptr == ' ') ptr++;
167 if (*ptr == '\n') {
168 // Force line break
169 lineBuf[lineLen] = '\0';
170 gfx->setCursor(textX, lineY);
171 render::printText(gfx, lineBuf);
172 lineY += 12;
173 lineLen = 0;
174 ptr++;
175 }
176 }
177
178 // Print remaining text
179 if (lineLen > 0) {
180 lineBuf[lineLen] = '\0';
181 gfx->setCursor(textX, lineY);
182 render::printText(gfx, lineBuf);
183 }
184
185 const char* hint = ui::tr("core.hint_approve_deny");
186 int hintWidth = static_cast<int>(std::strlen(hint)) * 6;
187 gfx->setCursor(boxX + BOX_WIDTH / 2 - hintWidth / 2, boxY + BOX_HEIGHT - 12);
188 render::printText(gfx, hint);
189
190 dirty_ = false;
191}
192
196
198
208void showConfirm(const char* message,
212 void* userData) {
213 s_sharedConfirm.init(message, icon);
214 s_sharedConfirm.setOnConfirm(onConfirm, userData);
215 s_sharedConfirm.setOnCancel(onCancel, userData);
218}
219
220} // namespace cdc::ui
Internationalization with English fallbacks in code and overlay translations loaded at runtime from a...
InputResult onKey(char key) override
Handles key input for the confirmation dialog.
void render(bool partial) override
Renders the confirmation dialog.
void(*)(void *userData) CancelCallback
Definition ConfirmView.h:28
void init(const char *message, Icon icon=Icon::QUESTION)
Initializes confirm dialog message and icon.
void(*)(void *userData) ConfirmCallback
Definition ConfirmView.h:27
void render(bool synchronous=false)
Render current view (and modal if present) and flush to display.
static ViewStack & instance()
Returns singleton view-stack instance.
Definition ViewStack.cpp:34
void showModal(IView *modal)
IDisplay * getDisplayInstance()
Returns lazily created singleton display instance.
void drawDialogFrame(Gdey029T94 *gfx, int x, int y, int w, int h)
Draws a framed dialog box with double border.
void printText(Gdey029T94 *gfx, const char *text)
Draws CP437 text with the built-in 6x8 glyph font, byte-for-byte.
Centralized key-code constants for cdc_views.
Definition IModule.h:8
const char * tr(const char *key)
Look up a translation by string key.
Definition I18n.h:208
Gdey029T94 * display
InputResult
Definition IView.h:10
void showConfirm(const char *message, ConfirmView::ConfirmCallback onConfirm, ConfirmView::CancelCallback onCancel=nullptr, ConfirmView::Icon icon=ConfirmView::Icon::QUESTION, void *userData=nullptr)
Shows a shared modal confirmation dialog instance.
static constexpr char KEY_NO
Cancel / Back / Backspace.
Definition KeyCodes.h:44
static ConfirmView s_sharedConfirm
Convenience helper functions.
static constexpr char KEY_YES
Confirm / OK / Save.
Definition KeyCodes.h:41