5#include "TinyGPU/Color/RGB565.h"
6#include "TinyGPU/Color/RGB666.h"
7#include "TinyGPU/Color/RGB888.h"
8#include "TinyMaterialDesign/Core/Widget.h"
13
14
15
16
17
18
19
20
26 this->bounds = bounds;
27 this->visible =
false;
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;
46 if (!
this->visible)
return;
47 this->visible =
false;
48 if (onDismiss) onDismiss();
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);
56 target.fillRoundRect(toPx(
this->bounds.x), toPx(
this->bounds.y), toPx(
this->bounds.w),
57 toPx(
this->bounds.h), radius, background);
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);
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,
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();
88 bool update(uint32_t nowMs)
override {
89 if (!
this->visible)
return false;
95 if (nowMs - startMs_ >= durationMs_) {
103 std::string message_;
104 std::string actionLabel_;
106 uint32_t durationMs_ = 4000;
107 bool started_ =
false;
108 uint32_t startMs_ = 0;
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
Definition: AnchorLayout.h:4
Axis-aligned pixel rectangle used for widget bounds and hit-testing.
Definition: Bounds.h:21