Repository navigation
Expand file tree
/
Copy pathtext_entry.cpp
More file actions
62 lines (50 loc) · 1.45 KB
/
Copy pathtext_entry.cpp
File metadata and controls
62 lines (50 loc) · 1.45 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
#include "text_entry.h"
namespace {
constexpr const char *kAlphanumericCharset =
" ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
constexpr const char *kHexCharset = "0123456789ABCDEF";
} // namespace
TextEntryWidget::TextEntryWidget(CharSet charSet, size_t maxLength) {
reset(charSet, maxLength);
}
void TextEntryWidget::reset(CharSet charSet, size_t maxLength) {
if (charSet == CharSet::kHex) {
charset_ = kHexCharset;
charsetLength_ = 16;
} else {
charset_ = kAlphanumericCharset;
charsetLength_ = 37;
}
maxLength_ = maxLength > kMaxBufferLength ? kMaxBufferLength : maxLength;
cursor_ = 0;
buffer_[0] = '\0';
length_ = 0;
done_ = false;
}
void TextEntryWidget::scrollNext() {
if (done_) return;
cursor_ = (cursor_ + 1) % (charsetLength_ + 1);
}
void TextEntryWidget::scrollPrev() {
if (done_) return;
cursor_ = (cursor_ == 0) ? charsetLength_ : cursor_ - 1;
}
void TextEntryWidget::commitChar() {
if (done_) return;
if (isDoneSelected()) {
done_ = true;
return;
}
if (length_ >= maxLength_) {
return; // buffer full — only "done" is a valid path forward
}
buffer_[length_++] = charset_[cursor_];
buffer_[length_] = '\0';
cursor_ = 0;
}
void TextEntryWidget::backspace() {
if (done_ || length_ == 0) return;
buffer_[--length_] = '\0';
}
bool TextEntryWidget::isDoneSelected() const { return cursor_ == charsetLength_; }
char TextEntryWidget::currentChar() const { return charset_[cursor_]; }