TinyMaterialDesign
Loading...
Searching...
No Matches
SearchBar.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 "TinyMaterialDesign/Core/TextInputTarget.h"
9#include "TinyMaterialDesign/Core/Widget.h"
10#include "TinyMaterialDesign/Draw/Icons.h"
11
12namespace tinymd {
13
14/**
15 * @brief Pill-shaped search input: leading magnifier glyph, typed text, and
16 * a trailing "x" clear glyph shown once there's something to clear.
17 *
18 * Implements TextInputTarget the same way TextField does, so it can be
19 * driven by a Keyboard via Keyboard::manage() - see TextField.h's class
20 * comment for the field/keyboard wiring example, which applies here
21 * unchanged. Enter submits, same single-line semantics as TextField.
22 */
23template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
24class SearchBar : public Widget<RGB_T>, public TextInputTarget {
25 public:
26 SearchBar() = default;
27 SearchBar(Bounds bounds, const char* placeholder = nullptr) {
28 this->bounds = bounds;
29 if (placeholder != nullptr) placeholder_ = placeholder;
30 }
31
32 std::function<void()> onFocus;
33 std::function<void(const std::string&)> onChange;
34 std::function<void()> onSubmit;
35
36 const std::string& text() const { return text_; }
37 void setText(const char* text) {
38 text_ = text != nullptr ? text : "";
39 if (onChange) onChange(text_);
40 }
41
42 bool focused() const { return focused_; }
43 void focus() {
44 focused_ = true;
45 if (onFocus) onFocus();
46 }
47 void blur() override { focused_ = false; }
48
49 void insertChar(char c) override {
50 text_.push_back(c);
51 if (onChange) onChange(text_);
52 }
53 void backspace() override {
54 if (text_.empty()) return;
55 text_.pop_back();
56 if (onChange) onChange(text_);
57 }
58 void submit() {
59 focused_ = false;
60 if (onSubmit) onSubmit();
61 }
62 bool handleEnter() override {
64 return true;
65 }
66
67 void draw(tinygpu::ISurface<RGB_T>& target) override {
68 RGB_T borderColor = focused_ ? this->theme().colors.primary : this->theme().colors.outline;
69 if (!this->enabled) borderColor = blend(borderColor, this->theme().colors.surface, 0.5f);
70 const size_t radius = toPx(this->bounds.h / 2);
71
72 target.fillRoundRect(toPx(this->bounds.x), toPx(this->bounds.y), toPx(this->bounds.w),
73 toPx(this->bounds.h), radius, this->theme().colors.surfaceVariant);
74 if (focused_) {
75 target.drawRoundRect(toPx(this->bounds.x), toPx(this->bounds.y), toPx(this->bounds.w),
76 toPx(this->bounds.h), radius, borderColor);
77 }
78
79 const int32_t iconSize = this->bounds.h - 16;
80 const RGB_T iconColor = this->theme().colors.onSurfaceVariant;
81 const Bounds searchIconRect(this->bounds.x + 12, this->bounds.centerY() - iconSize / 2, iconSize,
82 iconSize);
83 drawSearch(target, searchIconRect, iconColor, /*thickness=*/2);
84
85 const int32_t textX = searchIconRect.right() + 8;
86 const int32_t clearIconWidth = !text_.empty() ? iconSize + 12 : 0;
87 const int32_t textRight = this->bounds.right() - 12 - clearIconWidth;
88
89 tinygpu::IFont<RGB_T>& font = *this->theme().typography.body;
90 const int32_t textY = this->bounds.centerY() - static_cast<int32_t>(font.getHeight(1)) / 2;
91
92 if (text_.empty()) {
93 if (!focused_ && !placeholder_.empty()) {
94 font.drawText(target, static_cast<int16_t>(textX), static_cast<int16_t>(textY),
95 placeholder_.c_str(), this->theme().colors.onSurfaceVariant,
96 this->theme().colors.surfaceVariant, false);
97 }
98 } else {
99 font.drawText(target, static_cast<int16_t>(textX), static_cast<int16_t>(textY), text_.c_str(),
100 this->theme().colors.onSurface, this->theme().colors.surfaceVariant, false);
101 clearIconRect_ = Bounds(textRight, this->bounds.centerY() - iconSize / 2, iconSize, iconSize);
102 drawClose(target, clearIconRect_, iconColor, /*thickness=*/2);
103 }
104
105 if (focused_ && cursorVisible_) {
106 const size_t textWidth = font.measureTextWidth(text_.c_str());
107 const int32_t cursorX = textX + static_cast<int32_t>(textWidth) + 1;
108 target.drawLine(toPx(cursorX), toPx(this->bounds.y + 6), toPx(cursorX),
109 toPx(this->bounds.bottom() - 6), this->theme().colors.primary);
110 }
111 }
112
113 bool onGesture(const tinygpu::GestureEvent& event) override {
114 if (!isTapGesture(event.type)) return false;
115 if (!text_.empty() && clearIconRect_.contains(event.point.x, event.point.y)) {
116 setText("");
117 return true;
118 }
119 focus();
120 return true;
121 }
122
123 bool update(uint32_t nowMs) override {
124 const bool wasVisible = cursorVisible_;
125 if (!focused_) {
126 cursorVisible_ = false;
127 return wasVisible != cursorVisible_;
128 }
129 if (nowMs - lastBlinkMs_ >= kBlinkIntervalMs) {
130 cursorVisible_ = !cursorVisible_;
131 lastBlinkMs_ = nowMs;
132 }
133 return wasVisible != cursorVisible_;
134 }
135
136 private:
137 static constexpr uint32_t kBlinkIntervalMs = 500;
138
139 std::string text_;
140 std::string placeholder_;
141 bool focused_ = false;
142 bool cursorVisible_ = true;
143 uint32_t lastBlinkMs_ = 0;
144 Bounds clearIconRect_;
145};
146
150
151} // namespace tinymd
Pill-shaped search input: leading magnifier glyph, typed text, and a trailing "x" clear glyph shown o...
Definition: SearchBar.h:24
void backspace() override
Definition: SearchBar.h:53
bool onGesture(const tinygpu::GestureEvent &event) override
Handles one gesture event. Returns true if this widget consumed it.
Definition: SearchBar.h:113
bool handleEnter() override
Definition: SearchBar.h:62
SearchBar()=default
void blur() override
Definition: SearchBar.h:47
void setText(const char *text)
Definition: SearchBar.h:37
std::function< void(const std::string &)> onChange
Definition: SearchBar.h:33
void draw(tinygpu::ISurface< RGB_T > &target) override
Definition: SearchBar.h:67
std::function< void()> onFocus
Definition: SearchBar.h:32
const std::string & text() const
Definition: SearchBar.h:36
bool focused() const
Definition: SearchBar.h:42
void focus()
Definition: SearchBar.h:43
bool update(uint32_t nowMs) override
Definition: SearchBar.h:123
void insertChar(char c) override
Definition: SearchBar.h:49
SearchBar(Bounds bounds, const char *placeholder=nullptr)
Definition: SearchBar.h:27
void submit()
Definition: SearchBar.h:58
std::function< void()> onSubmit
Definition: SearchBar.h:34
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