TinyMaterialDesign
Loading...
Searching...
No Matches
TextArea.h
Go to the documentation of this file.
1#pragma once
2#include <functional>
3#include <string>
4
5#include "TinyGPU/Color/RGB565.h"
6#include "TinyGPU/Color/RGB666.h"
7#include "TinyGPU/Color/RGB888.h"
8#include "TinyGPU/Font/LinePrinter.h"
9#include "TinyMaterialDesign/Core/TextInputTarget.h"
10#include "TinyMaterialDesign/Core/Widget.h"
11
12namespace tinymd {
13
14/**
15 * @brief Multi-line, word-wrapped text input box.
16 *
17 * TextArea is TextField's multi-line counterpart - same focus/keyboard
18 * wiring, same TextInputTarget contract (see Core/TextInputTarget.h and
19 * TextField.h's class comment for the manage()/Keyboard pairing). Note that
20 * a Keyboard's dedicated Enter key (not Done) is what inserts a newline
21 * here - Done always finishes editing and closes the keyboard, exactly like
22 * TextField, via the same handleEnter()/submit() pair.
23 *
24 * Wrapping reuses TinyGPU's existing LinePrinter (the same one Card and
25 * Dialog use for their body text) rather than reimplementing word wrap. The
26 * blinking end-of-text cursor uses LinePrinter::cursorX()/cursorY(), which
27 * report where the next character would land after a print() call.
28 *
29 * Known limitation: no scrolling - text past the box's bottom edge is
30 * cropped to the box (see ISurface::pushClipRect()) rather than scrolling
31 * into view, and the cursor is skipped once it would fall past the box for
32 * the same reason. Fine for a few lines of notes; not a substitute for a
33 * real multi-page, scrollable text editor.
34 */
35template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
36class TextArea : public Widget<RGB_T>, public TextInputTarget {
37 public:
38 TextArea() = default;
39 TextArea(Bounds bounds, const char* label = nullptr, const char* placeholder = nullptr) {
40 this->bounds = bounds;
41 if (label != nullptr) {
42 label_ = label;
43 hasLabel_ = true;
44 }
45 if (placeholder != nullptr) placeholder_ = placeholder;
46 }
47
48 /// Fired when the field becomes focused - a Keyboard's manage() wires
49 /// this to attach itself, exactly like TextField.
50 std::function<void()> onFocus;
51 /// Fired after every character insertion/deletion.
52 std::function<void(const std::string&)> onChange;
53 /// Fired when a Keyboard's Done key (or submit()) is pressed.
54 std::function<void()> onSubmit;
55
56 const std::string& text() const { return text_; }
57 void setText(const char* text) {
58 text_ = text != nullptr ? text : "";
59 if (onChange) onChange(text_);
60 }
61 /// 0 (the default) means unlimited.
62 void setMaxLength(size_t maxLength) { maxLength_ = maxLength; }
63
64 bool focused() const { return focused_; }
65 void focus() {
66 focused_ = true;
67 if (onFocus) onFocus();
68 }
69 void blur() override { focused_ = false; }
70
71 void insertChar(char c) override {
72 if (maxLength_ != 0 && text_.size() >= maxLength_) return;
73 text_.push_back(c);
74 if (onChange) onChange(text_);
75 }
76 void backspace() override {
77 if (text_.empty()) return;
78 text_.pop_back();
79 if (onChange) onChange(text_);
80 }
81 /// Loses focus and fires onSubmit - called directly, or via a Keyboard's
82 /// Done key through handleEnter(). Inserting a newline is the dedicated
83 /// Enter key's job (Keyboard calls insertChar('\n') for that directly,
84 /// bypassing this), so - like TextField - Done always finishes editing.
85 void submit() {
86 focused_ = false;
87 if (onSubmit) onSubmit();
88 }
89
90 /// TextInputTarget: closes the keyboard, same as TextField - see submit().
91 bool handleEnter() override {
93 return true;
94 }
95
96 void draw(tinygpu::ISurface<RGB_T>& target) override {
97 const int32_t labelHeight = hasLabel_ ? 14 : 0;
98 const Bounds box(this->bounds.x, this->bounds.y + labelHeight, this->bounds.w,
99 this->bounds.h - labelHeight);
100
101 if (hasLabel_) {
102 RGB_T labelColor = focused_ ? this->theme().colors.primary : this->theme().colors.onSurfaceVariant;
103 if (!this->enabled) labelColor = blend(labelColor, this->theme().colors.surface, 0.5f);
104 this->theme().typography.label->drawText(target, static_cast<int16_t>(this->bounds.x),
105 static_cast<int16_t>(this->bounds.y), label_.c_str(),
106 labelColor, this->theme().colors.surface, false);
107 }
108
109 RGB_T borderColor = focused_ ? this->theme().colors.primary : this->theme().colors.outline;
110 if (!this->enabled) borderColor = blend(borderColor, this->theme().colors.surface, 0.5f);
111 const size_t radius = toPx(this->theme().shape.small);
112 target.fillRoundRect(toPx(box.x), toPx(box.y), toPx(box.w), toPx(box.h), radius,
113 this->theme().colors.surface);
114 target.drawRoundRect(toPx(box.x), toPx(box.y), toPx(box.w), toPx(box.h), radius, borderColor);
115
116 const int32_t pad = 8;
117 const bool showPlaceholder = text_.empty() && !focused_ && !placeholder_.empty();
118 const std::string& shown = showPlaceholder ? placeholder_ : text_;
119
120 // Defaults for an empty field - overwritten below once the printer
121 // actually runs, so the cursor still has somewhere sensible to sit.
122 size_t cursorX = toPx(box.x + pad);
123 size_t cursorY = toPx(box.y + pad);
124
125 if (!shown.empty()) {
126 const int32_t rightEdge = box.right() - pad;
127 const size_t rightBorder = rightEdge < static_cast<int32_t>(target.width())
128 ? target.width() - static_cast<size_t>(rightEdge)
129 : 0;
130
131 // Crops text past the box's bottom edge to the box (see the class
132 // comment) instead of letting it draw past it - LinePrinter/drawText
133 // have no notion of "this widget's own box", only the whole surface,
134 // so without this a long enough value would spill below the box.
135 target.pushClipRect(toPx(box.x), toPx(box.y), toPx(box.w), toPx(box.h));
136 tinygpu::LinePrinter<RGB_T> printer;
137 printer.setFont(*this->theme().typography.body);
138 printer.setTarget(target);
139 printer.setColor(showPlaceholder ? this->theme().colors.onSurfaceVariant : this->theme().colors.onSurface);
140 printer.setTopBorder(toPx(box.y + pad));
141 printer.setLeftBorder(toPx(box.x + pad));
142 printer.setRightBorder(rightBorder);
143 printer.print(shown.c_str());
144 cursorX = printer.cursorX();
145 cursorY = printer.cursorY();
146 target.popClipRect();
147 }
148
149 if (focused_ && cursorVisible_ && !showPlaceholder) {
150 const size_t lineHeight = this->theme().typography.body->getHeight(1);
151 if (cursorY + lineHeight <= toPx(box.bottom())) {
152 target.drawLine(cursorX + 1, cursorY, cursorX + 1, cursorY + lineHeight,
153 this->theme().colors.primary);
154 }
155 }
156 }
157
158 bool onGesture(const tinygpu::GestureEvent& event) override {
159 if (!isTapGesture(event.type)) return false;
160 focus();
161 return true;
162 }
163
164 bool update(uint32_t nowMs) override {
165 const bool wasVisible = cursorVisible_;
166 if (!focused_) {
167 cursorVisible_ = false;
168 return wasVisible != cursorVisible_;
169 }
170 if (nowMs - lastBlinkMs_ >= kBlinkIntervalMs) {
171 cursorVisible_ = !cursorVisible_;
172 lastBlinkMs_ = nowMs;
173 }
174 return wasVisible != cursorVisible_;
175 }
176
177 private:
178 static constexpr uint32_t kBlinkIntervalMs = 500;
179
180 std::string text_;
181 std::string label_;
182 std::string placeholder_;
183 bool hasLabel_ = false;
184 size_t maxLength_ = 0;
185 bool focused_ = false;
186 bool cursorVisible_ = true;
187 uint32_t lastBlinkMs_ = 0;
188};
189
193
194} // namespace tinymd
Multi-line, word-wrapped text input box.
Definition: TextArea.h:36
void setMaxLength(size_t maxLength)
0 (the default) means unlimited.
Definition: TextArea.h:62
TextArea(Bounds bounds, const char *label=nullptr, const char *placeholder=nullptr)
Definition: TextArea.h:39
void backspace() override
Definition: TextArea.h:76
bool onGesture(const tinygpu::GestureEvent &event) override
Handles one gesture event. Returns true if this widget consumed it.
Definition: TextArea.h:158
bool handleEnter() override
TextInputTarget: closes the keyboard, same as TextField - see submit().
Definition: TextArea.h:91
void blur() override
Definition: TextArea.h:69
void setText(const char *text)
Definition: TextArea.h:57
std::function< void(const std::string &)> onChange
Fired after every character insertion/deletion.
Definition: TextArea.h:52
void draw(tinygpu::ISurface< RGB_T > &target) override
Definition: TextArea.h:96
std::function< void()> onFocus
Definition: TextArea.h:50
const std::string & text() const
Definition: TextArea.h:56
bool focused() const
Definition: TextArea.h:64
void focus()
Definition: TextArea.h:65
bool update(uint32_t nowMs) override
Definition: TextArea.h:164
void insertChar(char c) override
Definition: TextArea.h:71
TextArea()=default
void submit()
Definition: TextArea.h:85
std::function< void()> onSubmit
Fired when a Keyboard's Done key (or submit()) is pressed.
Definition: TextArea.h:54
Definition: TextInputTarget.h:22
Base class every TinyMaterialDesign widget derives from.
Definition: Widget.h:58
Definition: AnchorLayout.h:4
Axis-aligned pixel rectangle used for widget bounds and hit-testing.
Definition: Bounds.h:21
int32_t right() const
Definition: Bounds.h:30