TinyMaterialDesign
Loading...
Searching...
No Matches
Snackbar.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/Widget.h"
9
10namespace tinymd {
11
12/**
13 * @brief Transient bottom message bar with an optional single text action.
14 *
15 * Not a modal - add it once with Screen::addFixedWidget() (last, so it
16 * draws on top) and drive its lifetime with show()/dismiss(); it hides
17 * itself automatically after its duration via update(), same mechanism a
18 * ripple fade uses. visible stays false until show() is called, so it costs
19 * nothing extra when idle - Screen already skips invisible widgets.
20 */
21template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
22class Snackbar : public Widget<RGB_T> {
23 public:
24 Snackbar() { this->visible = false; }
25 explicit Snackbar(Bounds bounds) {
26 this->bounds = bounds;
27 this->visible = false;
28 }
29
30 /// Fired when the (optional) action text is tapped.
31 std::function<void()> onAction;
32 /// Fired when the snackbar hides itself, whether by timeout or dismiss().
33 std::function<void()> onDismiss;
34
35 /// Shows the bar with `message` (and optional `actionLabel`) for
36 /// `durationMs` (default 4s, matching M3 guidance).
37 void show(const char* message, const char* actionLabel = nullptr, uint32_t durationMs = 4000) {
38 message_ = message != nullptr ? message : "";
39 actionLabel_ = actionLabel != nullptr ? actionLabel : "";
40 durationMs_ = durationMs;
41 this->visible = true;
42 started_ = false;
43 }
44
45 void dismiss() {
46 if (!this->visible) return;
47 this->visible = false;
48 if (onDismiss) onDismiss();
49 }
50
51 void draw(tinygpu::ISurface<RGB_T>& target) override {
52 const RGB_T background = this->theme().colors.onSurface;
53 const RGB_T foreground = this->theme().colors.surface;
54 const size_t radius = toPx(this->theme().shape.small);
55
56 target.fillRoundRect(toPx(this->bounds.x), toPx(this->bounds.y), toPx(this->bounds.w),
57 toPx(this->bounds.h), radius, background);
58
59 tinygpu::IFont<RGB_T>& font = *this->theme().typography.body;
60 const int32_t pad = this->theme().spacing * 2;
61 const int32_t textY = this->bounds.centerY() - static_cast<int32_t>(font.getHeight(1)) / 2;
62 font.drawText(target, static_cast<int16_t>(this->bounds.x + pad), static_cast<int16_t>(textY),
63 message_.c_str(), foreground, background, false);
64
65 if (!actionLabel_.empty()) {
66 tinygpu::IFont<RGB_T>& actionFont = *this->theme().typography.label;
67 const size_t actionWidth = actionFont.measureTextWidth(actionLabel_.c_str());
68 const int32_t actionX = this->bounds.right() - pad - static_cast<int32_t>(actionWidth);
69 const int32_t actionY = this->bounds.centerY() - static_cast<int32_t>(actionFont.getHeight(1)) / 2;
70 actionFont.drawText(target, static_cast<int16_t>(actionX), static_cast<int16_t>(actionY),
71 actionLabel_.c_str(), this->theme().colors.primaryContainer, background, false);
72 actionRect_ = Bounds(actionX - 8, this->bounds.y, static_cast<int32_t>(actionWidth) + 16,
73 this->bounds.h);
74 } else {
75 actionRect_ = Bounds();
76 }
77 }
78
79 bool onGesture(const tinygpu::GestureEvent& event) override {
80 if (!isTapGesture(event.type)) return false;
81 if (!actionLabel_.empty() && actionRect_.contains(event.point.x, event.point.y)) {
82 if (onAction) onAction();
83 }
85 return true;
86 }
87
88 bool update(uint32_t nowMs) override {
89 if (!this->visible) return false;
90 if (!started_) {
91 started_ = true;
92 startMs_ = nowMs;
93 return true; // just became visible
94 }
95 if (nowMs - startMs_ >= durationMs_) {
97 return true;
98 }
99 return false;
100 }
101
102 private:
103 std::string message_;
104 std::string actionLabel_;
105 Bounds actionRect_;
106 uint32_t durationMs_ = 4000;
107 bool started_ = false;
108 uint32_t startMs_ = 0;
109};
110
114
115} // namespace tinymd
Transient bottom message bar with an optional single text action.
Definition: Snackbar.h:22
Snackbar(Bounds bounds)
Definition: Snackbar.h:25
void show(const char *message, const char *actionLabel=nullptr, uint32_t durationMs=4000)
Definition: Snackbar.h:37
bool onGesture(const tinygpu::GestureEvent &event) override
Handles one gesture event. Returns true if this widget consumed it.
Definition: Snackbar.h:79
std::function< void()> onDismiss
Fired when the snackbar hides itself, whether by timeout or dismiss().
Definition: Snackbar.h:33
std::function< void()> onAction
Fired when the (optional) action text is tapped.
Definition: Snackbar.h:31
void dismiss()
Definition: Snackbar.h:45
void draw(tinygpu::ISurface< RGB_T > &target) override
Definition: Snackbar.h:51
Snackbar()
Definition: Snackbar.h:24
bool update(uint32_t nowMs) override
Definition: Snackbar.h:88
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
Bounds()=default