TinyMaterialDesign
Loading...
Searching...
No Matches
IconButton.h
Go to the documentation of this file.
1#pragma once
2#include <algorithm>
3#include <functional>
4
5#include "TinyGPU/Color/RGB565.h"
6#include "TinyGPU/Color/RGB666.h"
7#include "TinyGPU/Color/RGB888.h"
8#include "TinyMaterialDesign/Core/Widget.h"
9#include "TinyMaterialDesign/Draw/Icons.h"
10
11namespace tinymd {
12
13/// Signature every Draw/Icons.h glyph function matches: draw `color`, with
14/// stroke width `thickness`, into `bounds` on `target`. A function pointer's
15/// type reflects every declared parameter regardless of default arguments,
16/// so this must list all 4 even though most Icons.h functions default the
17/// last one.
18template <typename RGB_T>
19using IconPainter = void (*)(tinygpu::ISurface<RGB_T>&, const Bounds&, RGB_T, uint8_t);
20
21/// kStandard is a plain flat tap target (the original IconButton look - app
22/// bar/toolbar icons). kFilled/kTonal add a colored, elevated circular
23/// background - for a dedicated Floating Action Button (elevation shadow,
24/// pill "extended" form with a label) see Fab.h's FloatingActionButton/FAB
25/// instead; this variant remains handy for a smaller status-dependent
26/// circular button paired with setColorOverride() (a play/stop control
27/// that's green or red, say).
29
30/// @brief Circular tap target hosting one vector glyph from Draw/Icons.h (or any
31/// function matching IconPainter). Tap feedback is a ripple, same rationale
32/// as Button (see Button.h's class comment).
33template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
34class IconButton : public Widget<RGB_T> {
35 public:
36 IconButton() = default;
37 IconButton(Bounds bounds, IconPainter<RGB_T> icon,
39 : icon_(icon), variant_(variant) {
40 this->bounds = bounds;
41 }
42
43 std::function<void()> onClick;
44
45 void setIcon(IconPainter<RGB_T> icon) { icon_ = icon; }
46 void setVariant(IconButtonVariant variant) { variant_ = variant; }
47
48 /// Overrides the variant's theme colors - e.g. a FAB that's green when
49 /// idle and red while active. Call clearColorOverride() to go back to
50 /// the variant's normal theme colors.
51 void setColorOverride(RGB_T background, RGB_T foreground) {
52 hasColorOverride_ = true;
53 backgroundOverride_ = background;
54 foregroundOverride_ = foreground;
55 }
56 void clearColorOverride() { hasColorOverride_ = false; }
57
58 /// See Widget::setThemeTint() - used only when this button has no
59 /// setColorOverride() of its own.
60 void setThemeTint(RGB_T background, RGB_T foreground) override {
61 hasAmbientTint_ = true;
62 ambientBackground_ = background;
63 ambientForeground_ = foreground;
64 }
65
66 void draw(tinygpu::ISurface<RGB_T>& target) override {
67 RGB_T background = this->theme().colors.surface;
68 RGB_T foreground = this->theme().colors.onSurfaceVariant;
69 const bool paintBackground = variant_ != IconButtonVariant::kStandard;
70
71 switch (variant_) {
73 background = this->theme().colors.primary;
74 foreground = this->theme().colors.onPrimary;
75 break;
77 background = this->theme().colors.secondaryContainer;
78 foreground = this->theme().colors.onSecondaryContainer;
79 break;
81 default:
82 break;
83 }
84 // Priority: explicit override > container's ambient tint > variant
85 // default - see setColorOverride()/setThemeTint().
86 if (hasAmbientTint_) {
87 background = ambientBackground_;
88 foreground = ambientForeground_;
89 }
90 if (hasColorOverride_) {
91 background = backgroundOverride_;
92 foreground = foregroundOverride_;
93 }
94 if (!this->enabled) {
95 background = blend(background, this->theme().colors.surface, 0.6f);
96 foreground = blend(foreground, this->theme().colors.surface, 0.5f);
97 }
98
99 const int32_t diameter = this->bounds.w < this->bounds.h ? this->bounds.w : this->bounds.h;
100 const size_t radius = toPx(diameter / 2);
101
102 if (paintBackground) {
103 if (this->enabled) {
104 const RGB_T shadow = blend(this->theme().colors.surface, this->theme().colors.onBackground, 0.35f);
105 target.fillCircle(toPx(this->bounds.centerX() + 1), toPx(this->bounds.centerY() + 2),
106 radius, shadow);
107 }
108 target.fillCircle(toPx(this->bounds.centerX()), toPx(this->bounds.centerY()), radius,
109 background);
110 }
111
112 if (this->theme().enableRipple && rippleActive_) {
113 const uint32_t elapsed = lastUpdateMs_ - rippleStartMs_;
114 const float t = std::min(1.0f, static_cast<float>(elapsed) / kRippleDurationMs);
115 const size_t rippleRadius = toPx(static_cast<int32_t>(t * diameter / 2));
116 const RGB_T rippleColor = blend(background, foreground, 0.15f * (1.0f - t));
117 target.fillCircle(toPx(this->bounds.centerX()), toPx(this->bounds.centerY()), rippleRadius,
118 rippleColor);
119 }
120
121 if (icon_ != nullptr) {
122 const Bounds iconRect = this->bounds.inset(this->bounds.w / 4);
123 icon_(target, iconRect, foreground, /*thickness=*/2);
124 }
125 }
126
127 bool onGesture(const tinygpu::GestureEvent& event) override {
128 if (!isTapGesture(event.type)) return false;
129 rippleStartMs_ = lastUpdateMs_;
130 rippleActive_ = true;
131 if (onClick) onClick();
132 return true;
133 }
134
135 bool update(uint32_t nowMs) override {
136 const bool wasActive = rippleActive_;
137 lastUpdateMs_ = nowMs;
138 if (rippleActive_ && (nowMs - rippleStartMs_) >= kRippleDurationMs) {
139 rippleActive_ = false;
140 }
141 return wasActive;
142 }
143
144 private:
145 static constexpr uint32_t kRippleDurationMs = 220;
146
147 IconPainter<RGB_T> icon_ = nullptr;
149 bool hasColorOverride_ = false;
150 RGB_T backgroundOverride_{};
151 RGB_T foregroundOverride_{};
152 bool hasAmbientTint_ = false;
153 RGB_T ambientBackground_{};
154 RGB_T ambientForeground_{};
155 bool rippleActive_ = false;
156 uint32_t rippleStartMs_ = 0;
157 uint32_t lastUpdateMs_ = 0;
158};
159
163
164} // namespace tinymd
Circular tap target hosting one vector glyph from Draw/Icons.h (or any function matching IconPainter)...
Definition: IconButton.h:34
void clearColorOverride()
Definition: IconButton.h:56
void setVariant(IconButtonVariant variant)
Definition: IconButton.h:46
bool onGesture(const tinygpu::GestureEvent &event) override
Handles one gesture event. Returns true if this widget consumed it.
Definition: IconButton.h:127
void setThemeTint(RGB_T background, RGB_T foreground) override
Definition: IconButton.h:60
void setIcon(IconPainter< RGB_T > icon)
Definition: IconButton.h:45
void draw(tinygpu::ISurface< RGB_T > &target) override
Definition: IconButton.h:66
IconButton()=default
IconButton(Bounds bounds, IconPainter< RGB_T > icon, IconButtonVariant variant=IconButtonVariant::kStandard)
Definition: IconButton.h:37
bool update(uint32_t nowMs) override
Definition: IconButton.h:135
std::function< void()> onClick
Definition: IconButton.h:43
void setColorOverride(RGB_T background, RGB_T foreground)
Definition: IconButton.h:51
Base class every TinyMaterialDesign widget derives from.
Definition: Widget.h:58
Definition: AnchorLayout.h:4
IconButtonVariant
Definition: IconButton.h:28
Axis-aligned pixel rectangle used for widget bounds and hit-testing.
Definition: Bounds.h:21