TinyMaterialDesign
Loading...
Searching...
No Matches
Fab.h
Go to the documentation of this file.
1#pragma once
2#include <algorithm>
3#include <functional>
4#include <string>
5
6#include "TinyGPU/Color/RGB565.h"
7#include "TinyGPU/Color/RGB666.h"
8#include "TinyGPU/Color/RGB888.h"
9#include "TinyMaterialDesign/Core/Bounds.h"
10#include "TinyMaterialDesign/Core/Widget.h"
11#include "TinyMaterialDesign/Widgets/IconButton.h" // IconPainter
12
13namespace tinymd {
14
15/// kSurface (default M3 look) / kPrimary / kSecondary / kTertiary pick which
16/// theme color pair the FAB is filled with.
18
19/**
20 * @brief Floating Action Button: a circular (or pill, when it has a label)
21 * elevated icon button for a screen's single primary action.
22 *
23 * Construct with just an icon for the standard circular FAB, or with a
24 * label too for the "extended" pill-shaped variant. Bounds picks the size -
25 * conventionally 40x40 (small), 56x56 (regular), 96x96 (large) for the
26 * circular form, or width x 56 for extended.
27 */
28template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
29class FloatingActionButton : public Widget<RGB_T> {
30 public:
32 FloatingActionButton(Bounds bounds, IconPainter<RGB_T> icon, const char* label = nullptr,
34 : icon_(icon), color_(color) {
35 this->bounds = bounds;
36 if (label != nullptr) {
37 label_ = label;
38 extended_ = true;
39 }
40 }
41
42 std::function<void()> onClick;
43
44 void setIcon(IconPainter<RGB_T> icon) { icon_ = icon; }
45 void setLabel(const char* label) {
46 label_ = label != nullptr ? label : "";
47 extended_ = !label_.empty();
48 }
49 void setColor(FabColor color) { color_ = color; }
50
51 void draw(tinygpu::ISurface<RGB_T>& target) override {
52 RGB_T background;
53 RGB_T foreground;
54 switch (color_) {
56 background = this->theme().colors.primaryContainer;
57 foreground = this->theme().colors.onPrimaryContainer;
58 break;
60 background = this->theme().colors.secondaryContainer;
61 foreground = this->theme().colors.onSecondaryContainer;
62 break;
64 background = this->theme().colors.secondary;
65 foreground = this->theme().colors.onSecondary;
66 break;
68 default:
69 background = this->theme().colors.surfaceVariant;
70 foreground = this->theme().colors.primary;
71 break;
72 }
73 if (!this->enabled) {
74 background = blend(background, this->theme().colors.surface, 0.6f);
75 foreground = blend(foreground, this->theme().colors.surface, 0.5f);
76 }
77
78 const size_t radius = extended_ ? toPx(this->theme().shape.large) : toPx(this->bounds.h / 2);
79
80 if (this->enabled) {
81 const RGB_T shadow = blend(this->theme().colors.surface, this->theme().colors.onBackground, 0.4f);
82 target.fillRoundRect(toPx(this->bounds.x + 1), toPx(this->bounds.y + 2), toPx(this->bounds.w),
83 toPx(this->bounds.h), radius, shadow);
84 }
85 target.fillRoundRect(toPx(this->bounds.x), toPx(this->bounds.y), toPx(this->bounds.w),
86 toPx(this->bounds.h), radius, background);
87
88 if (this->theme().enableRipple && rippleActive_) {
89 const uint32_t elapsed = lastUpdateMs_ - rippleStartMs_;
90 const float t = std::min(1.0f, static_cast<float>(elapsed) / kRippleDurationMs);
91 const int32_t maxRadius = this->bounds.w > this->bounds.h ? this->bounds.w : this->bounds.h;
92 const RGB_T rippleColor = blend(background, foreground, 0.18f * (1.0f - t));
93 target.fillCircle(toPx(this->bounds.centerX()), toPx(this->bounds.centerY()),
94 toPx(static_cast<int32_t>(t * maxRadius)), rippleColor);
95 }
96
97 const int32_t iconSize = std::min<int32_t>(this->bounds.h - 16, 32);
98 const int32_t iconPad = extended_ ? this->theme().spacing * 2 : (this->bounds.w - iconSize) / 2;
99 const Bounds iconRect(this->bounds.x + iconPad, this->bounds.centerY() - iconSize / 2, iconSize,
100 iconSize);
101 if (icon_ != nullptr) icon_(target, iconRect, foreground, /*thickness=*/2);
102
103 if (extended_) {
104 tinygpu::IFont<RGB_T>& font = *this->theme().typography.label;
105 const int32_t textX = icon_ != nullptr ? iconRect.right() + this->theme().spacing
106 : this->bounds.x + this->theme().spacing * 2;
107 const int32_t textY = this->bounds.centerY() - static_cast<int32_t>(font.getHeight(1)) / 2;
108 font.drawText(target, static_cast<int16_t>(textX), static_cast<int16_t>(textY), label_.c_str(),
109 foreground, background, false);
110 }
111 }
112
113 bool onGesture(const tinygpu::GestureEvent& event) override {
114 if (!isTapGesture(event.type)) return false;
115 rippleStartMs_ = lastUpdateMs_;
116 rippleActive_ = true;
117 if (onClick) onClick();
118 return true;
119 }
120
121 bool update(uint32_t nowMs) override {
122 const bool wasActive = rippleActive_;
123 lastUpdateMs_ = nowMs;
124 if (rippleActive_ && (nowMs - rippleStartMs_) >= kRippleDurationMs) rippleActive_ = false;
125 return wasActive;
126 }
127
128 private:
129 static constexpr uint32_t kRippleDurationMs = 260;
130
131 IconPainter<RGB_T> icon_ = nullptr;
132 std::string label_;
133 bool extended_ = false;
135
136 bool rippleActive_ = false;
137 uint32_t rippleStartMs_ = 0;
138 uint32_t lastUpdateMs_ = 0;
139};
140
142
149
150} // namespace tinymd
Floating Action Button: a circular (or pill, when it has a label) elevated icon button for a screen's...
Definition: Fab.h:29
void setLabel(const char *label)
Definition: Fab.h:45
bool onGesture(const tinygpu::GestureEvent &event) override
Handles one gesture event. Returns true if this widget consumed it.
Definition: Fab.h:113
void setColor(FabColor color)
Definition: Fab.h:49
void setIcon(IconPainter< RGB_T > icon)
Definition: Fab.h:44
void draw(tinygpu::ISurface< RGB_T > &target) override
Definition: Fab.h:51
FloatingActionButton(Bounds bounds, IconPainter< RGB_T > icon, const char *label=nullptr, FabColor color=FabColor::kPrimary)
Definition: Fab.h:32
bool update(uint32_t nowMs) override
Definition: Fab.h:121
std::function< void()> onClick
Definition: Fab.h:42
Base class every TinyMaterialDesign widget derives from.
Definition: Widget.h:58
Definition: AnchorLayout.h:4
FabColor
Definition: Fab.h:17
Axis-aligned pixel rectangle used for widget bounds and hit-testing.
Definition: Bounds.h:21