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"
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
39 TextArea(
Bounds bounds,
const char* label =
nullptr,
const char* placeholder =
nullptr) {
40 this->bounds = bounds;
41 if (label !=
nullptr) {
45 if (placeholder !=
nullptr) placeholder_ = placeholder;
52 std::function<
void(
const std::string&)>
onChange;
56 const std::string&
text()
const {
return text_; }
58 text_ = text !=
nullptr ? text :
"";
59 if (onChange) onChange(text_);
67 if (onFocus) onFocus();
69 void blur()
override { focused_ =
false; }
72 if (maxLength_ != 0 && text_.size() >= maxLength_)
return;
74 if (onChange) onChange(text_);
77 if (text_.empty())
return;
79 if (onChange) onChange(text_);
87 if (onSubmit) onSubmit();
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);
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);
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);
116 const int32_t pad = 8;
117 const bool showPlaceholder = text_.empty() && !focused_ && !placeholder_.empty();
118 const std::string& shown = showPlaceholder ? placeholder_ : text_;
122 size_t cursorX = toPx(box.x + pad);
123 size_t cursorY = toPx(box.y + pad);
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)
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();
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);
158 bool onGesture(
const tinygpu::GestureEvent& event)
override {
159 if (!isTapGesture(event.type))
return false;
165 const bool wasVisible = cursorVisible_;
167 cursorVisible_ =
false;
168 return wasVisible != cursorVisible_;
170 if (nowMs - lastBlinkMs_ >= kBlinkIntervalMs) {
171 cursorVisible_ = !cursorVisible_;
172 lastBlinkMs_ = nowMs;
174 return wasVisible != cursorVisible_;
178 static constexpr uint32_t kBlinkIntervalMs = 500;
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;
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
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
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