5#include "TinyGPU/Color/RGB565.h"
6#include "TinyGPU/Color/RGB666.h"
7#include "TinyGPU/Color/RGB888.h"
8#include "TinyMaterialDesign/Core/TextInputTarget.h"
9#include "TinyMaterialDesign/Core/Widget.h"
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
44 TextField(
Bounds bounds,
const char* label =
nullptr,
const char* placeholder =
nullptr) {
45 this->bounds = bounds;
46 if (label !=
nullptr) {
50 if (placeholder !=
nullptr) placeholder_ = placeholder;
57 std::function<
void(
const std::string&)>
onChange;
71 void setError(
const char* text) { error_ = text !=
nullptr ? text :
""; }
72 bool hasError()
const {
return !error_.empty(); }
73 const std::string&
errorText()
const {
return error_; }
77 void setValidator(std::function<
bool(
const std::string&)> valid,
const char* errorText) {
78 validator_ = std::move(valid);
79 validatorError_ = errorText !=
nullptr ? errorText :
"";
83 bool isValid()
const {
return !validator_ || validator_(text_); }
95 const std::string&
text()
const {
return text_; }
97 text_ = text !=
nullptr ? text :
"";
99 if (onChange) onChange(text_);
107 if (onFocus) onFocus();
110 const bool was = focused_;
112 if (was && onBlur) onBlur();
118 if (maxLength_ != 0 && text_.size() >= maxLength_)
return;
119 if (inputType_ ==
InputType::kNumber && !((c >=
'0' && c <=
'9') || c ==
'.' || c ==
'-' || c ==
'+' || c ==
':')) {
124 if (onChange) onChange(text_);
127 if (text_.empty())
return;
130 if (onChange) onChange(text_);
135 const bool was = focused_;
137 if (onSubmit) onSubmit();
138 if (was && onBlur) onBlur();
148 void draw(tinygpu::ISurface<RGB_T>& target)
override {
149 const int32_t labelHeight = hasLabel_ ? 14 : 0;
150 const int32_t errorHeight = error_.empty() ? 0 : 14;
151 const Bounds box(
this->bounds.x,
this->bounds.y + labelHeight,
this->bounds.w,
152 this->bounds.h - labelHeight - errorHeight);
153 const RGB_T outside = onBackground_ ?
this->theme().colors.background :
this->theme().colors.surface;
154 const bool error = !error_.empty();
157 RGB_T labelColor = error ?
this->theme().colors.error
158 : focused_ ?
this->theme().colors.primary
159 :
this->theme().colors.onSurfaceVariant;
160 if (!
this->enabled) labelColor = blend(labelColor,
this->theme().colors.surface, 0.5f);
161 this->theme().typography.label->drawText(target,
static_cast<int16_t>(
this->bounds.x),
162 static_cast<int16_t>(
this->bounds.y), label_.c_str(),
163 labelColor, outside,
false);
166 this->theme().typography.label->drawText(target,
static_cast<int16_t>(
this->bounds.x + 4),
167 static_cast<int16_t>(box.bottom() + 3), error_.c_str(),
168 this->theme().colors.error, outside,
false);
171 RGB_T borderColor = error ?
this->theme().colors.error
172 : focused_ ?
this->theme().colors.primary
173 :
this->theme().colors.outline;
174 if (!
this->enabled) borderColor = blend(borderColor,
this->theme().colors.surface, 0.5f);
175 const size_t radius = toPx(
this->theme().shape.small);
176 target.fillRoundRect(toPx(box.x), toPx(box.y), toPx(box.w), toPx(box.h), radius,
177 this->theme().colors.surface);
178 target.drawRoundRect(toPx(box.x), toPx(box.y), toPx(box.w), toPx(box.h), radius, borderColor);
180 const int32_t pad = 8;
181 const int32_t textX = box
.x + pad;
182 const int32_t textY = box
.centerY() -
static_cast<int32_t>(
this->theme().typography.body->getHeight(1)) / 2;
184 if (text_.empty() && !focused_) {
185 if (!placeholder_.empty()) {
186 this->theme().typography.body->drawText(target,
static_cast<int16_t>(textX),
187 static_cast<int16_t>(textY), placeholder_.c_str(),
188 this->theme().colors.onSurfaceVariant,
this->theme().colors.surface,
false);
193 const std::string shown = password_ ? std::string(text_.size(),
'*') : text_;
194 this->theme().typography.body->drawText(target,
static_cast<int16_t>(textX),
195 static_cast<int16_t>(textY), shown.c_str(),
196 this->theme().colors.onSurface,
this->theme().colors.surface,
false);
198 if (focused_ && cursorVisible_) {
199 const size_t textWidth =
this->theme().typography.body->measureTextWidth(shown.c_str());
200 const int32_t cursorX = textX +
static_cast<int32_t>(textWidth) + 1;
201 target.drawLine(toPx(cursorX), toPx(box.y + 6), toPx(cursorX), toPx(box.bottom() - 6),
202 this->theme().colors.primary);
206 bool onGesture(
const tinygpu::GestureEvent& event)
override {
207 if (!isTapGesture(event.type))
return false;
213 const bool wasVisible = cursorVisible_;
215 cursorVisible_ =
false;
216 return wasVisible != cursorVisible_;
218 if (nowMs - lastBlinkMs_ >= kBlinkIntervalMs) {
219 cursorVisible_ = !cursorVisible_;
220 lastBlinkMs_ = nowMs;
222 return wasVisible != cursorVisible_;
226 static constexpr uint32_t kBlinkIntervalMs = 500;
230 std::string placeholder_;
232 std::string validatorError_;
233 std::function<
bool(
const std::string&)> validator_;
235 bool onBackground_ =
false;
236 bool password_ =
false;
237 bool hasLabel_ =
false;
240 if (validator_) error_ = validator_(text_) ?
"" : validatorError_;
242 size_t maxLength_ = 0;
243 bool focused_ =
false;
244 bool cursorVisible_ =
true;
245 uint32_t lastBlinkMs_ = 0;
Single-line text input box.
Definition: TextField.h:41
void setMaxLength(size_t maxLength)
0 (the default) means unlimited.
Definition: TextField.h:102
void backspace() override
Definition: TextField.h:126
bool onGesture(const tinygpu::GestureEvent &event) override
Handles one gesture event. Returns true if this widget consumed it.
Definition: TextField.h:206
TextField(Bounds bounds, const char *label=nullptr, const char *placeholder=nullptr)
Definition: TextField.h:44
void setValidator(std::function< bool(const std::string &)> valid, const char *errorText)
Definition: TextField.h:77
bool isValid() const
True if there is no validator or it accepts the text.
Definition: TextField.h:83
bool handleEnter() override
Definition: TextField.h:143
InputType inputType() const override
The kind of input - Keyboard picks its layout from it. Default: text.
Definition: TextField.h:67
const std::string & errorText() const
Definition: TextField.h:73
bool hasError() const
Definition: TextField.h:72
void blur() override
Definition: TextField.h:109
void setText(const char *text)
Definition: TextField.h:96
bool passwordMode() const
Definition: TextField.h:88
std::function< void(const std::string &)> onChange
Fired after every character insertion/deletion.
Definition: TextField.h:57
void draw(tinygpu::ISurface< RGB_T > &target) override
Definition: TextField.h:148
std::function< void()> onFocus
Definition: TextField.h:55
const std::string & text() const
Definition: TextField.h:95
bool focused() const
Definition: TextField.h:104
void focus()
Definition: TextField.h:105
bool update(uint32_t nowMs) override
Definition: TextField.h:212
void setPasswordMode(bool password)
Definition: TextField.h:87
void setOnBackground(bool onBackground)
Definition: TextField.h:93
void insertChar(char c) override
Definition: TextField.h:117
std::function< void()> onBlur
Definition: TextField.h:62
void setInputType(InputType type)
Definition: TextField.h:66
void submit()
Definition: TextField.h:134
void setError(const char *text)
Definition: TextField.h:71
std::function< void()> onSubmit
Fired when a Keyboard's Enter/Done key (or submit()) is pressed.
Definition: TextField.h:59
Definition: TextInputTarget.h:22
Definition: AnchorLayout.h:4
InputType
Minimal interface a Keyboard drives.
Definition: TextInputTarget.h:20
Axis-aligned pixel rectangle used for widget bounds and hit-testing.
Definition: Bounds.h:21
int32_t centerY() const
Definition: Bounds.h:33
int32_t x
Definition: Bounds.h:22