TinyMaterialDesign
Loading...
Searching...
No Matches
Banner.h
Go to the documentation of this file.
1#pragma once
2#include <functional>
3#include <string>
4#include <utility>
5#include <vector>
6
7#include "TinyGPU/Color/RGB565.h"
8#include "TinyGPU/Color/RGB666.h"
9#include "TinyGPU/Color/RGB888.h"
10#include "TinyMaterialDesign/Core/Widget.h"
11#include "TinyGPU/Font/LinePrinter.h"
12
13namespace tinymd {
14
15/**
16 * @brief Persistent, non-modal inline message with text actions - Material's
17 * "banner", e.g. "You're offline. [Retry] [Dismiss]" pinned below an app
18 * bar.
19 *
20 * Unlike Dialog/Snackbar this isn't presented modally - add it with
21 * Screen::addWidget()/addFixedWidget() like any other widget and toggle
22 * `visible` yourself. Actions are not owned by Banner - same non-owning
23 * addAction() convention as Dialog::addAction().
24 *
25 * Alternative to addAction(): setActionProvider(count, at) switches to
26 * callback-driven actions - see Container.h's class comment for the pattern.
27 */
28template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
29class Banner : public Widget<RGB_T> {
30 public:
31 using ActionCountFn = std::function<int()>;
32 using ActionAtFn = std::function<Widget<RGB_T>&(int index)>;
33
34 Banner() = default;
35 Banner(Bounds bounds, const char* message) : message_(message != nullptr ? message : "") {
36 this->bounds = bounds;
37 }
38
39 void setMessage(const char* message) { message_ = message != nullptr ? message : ""; }
40
41 /// Registers `action` as content of this banner. Ignored while an action
42 /// provider is active - see setActionProvider().
43 void addAction(Widget<RGB_T>& action) {
44 actions_.push_back(&action);
45 if (this->theme_ != nullptr) action.setTheme(*this->theme_);
46 }
47
48 /// Switches to callback-driven actions - see the class comment.
49 void setActionProvider(ActionCountFn count, ActionAtFn at) {
50 actionCountFn_ = std::move(count);
51 actionAtFn_ = std::move(at);
52 }
53
54 void setTheme(const MaterialTheme<RGB_T>& theme) override {
55 Widget<RGB_T>::setTheme(theme);
56 for (Widget<RGB_T>* action : actions_) {
57 if (action != nullptr) action->setTheme(theme);
58 }
59 }
60
61 /// Suggested rect for the `index`-th of `count` actions, right-aligned
62 /// along the banner's bottom edge - same layout convention as
63 /// Dialog::actionRect().
64 Bounds actionRect(int index, int count, int32_t width = 80, int32_t height = 32) const {
65 const int32_t pad = 8;
66 const int32_t totalWidth = count * width + (count - 1) * pad;
67 const int32_t startX = this->bounds.right() - pad - totalWidth;
68 const int32_t y = this->bounds.bottom() - pad - height;
69 return Bounds(startX + index * (width + pad), y, width, height);
70 }
71
72 void draw(tinygpu::ISurface<RGB_T>& target) override {
73 target.fillRect(toPx(this->bounds.x), toPx(this->bounds.y), toPx(this->bounds.w),
74 toPx(this->bounds.h), this->theme().colors.surfaceVariant);
75 target.drawLine(toPx(this->bounds.x), toPx(this->bounds.bottom() - 1), toPx(this->bounds.right()),
76 toPx(this->bounds.bottom() - 1), this->theme().colors.outline);
77
78 const int32_t pad = this->theme().spacing;
79 const int32_t rightEdge = this->bounds.right() - pad;
80 const size_t rightBorder = rightEdge < static_cast<int32_t>(target.width())
81 ? target.width() - static_cast<size_t>(rightEdge)
82 : 0;
83 // Crops a message too long for the banner to its own bounds instead of
84 // letting it spill past the bottom edge (see ISurface::pushClipRect()).
85 target.pushClipRect(toPx(this->bounds.x), toPx(this->bounds.y), toPx(this->bounds.w),
86 toPx(this->bounds.h));
87 tinygpu::LinePrinter<RGB_T> printer;
88 printer.setFont(*this->theme().typography.body);
89 printer.setTarget(target);
90 printer.setColor(this->theme().colors.onSurfaceVariant);
91 printer.setTopBorder(toPx(this->bounds.y + pad));
92 printer.setLeftBorder(toPx(this->bounds.x + pad));
93 printer.setRightBorder(rightBorder);
94 printer.print(message_.c_str());
95 target.popClipRect();
96
97 const int count = actionCount();
98 for (int i = 0; i < count; ++i) {
99 Widget<RGB_T>* action = actionAt(i);
100 if (action->visible) action->draw(target);
101 }
102 }
103
104 bool onGesture(const tinygpu::GestureEvent& event) override {
105 const int count = actionCount();
106 for (int i = 0; i < count; ++i) {
107 Widget<RGB_T>* action = actionAt(i);
108 if (action->enabled && action->bounds.contains(event.point.x, event.point.y)) {
109 return action->onGesture(event);
110 }
111 }
112 return false;
113 }
114
115 bool update(uint32_t nowMs) override {
116 bool changed = false;
117 const int count = actionCount();
118 for (int i = 0; i < count; ++i) changed |= actionAt(i)->update(nowMs);
119 return changed;
120 }
121
122 /// True if there's an action at (x, y) that's draggable, recursing into
123 /// it if it's itself a composite - see Widget::isDraggableAt().
124 bool isDraggableAt(int32_t x, int32_t y) const override {
125 const int count = actionCount();
126 for (int i = 0; i < count; ++i) {
127 Widget<RGB_T>* action = actionAt(i);
128 if (action->enabled && action->bounds.contains(x, y)) return action->isDraggableAt(x, y);
129 }
130 return false;
131 }
132
133 private:
134 int actionCount() const {
135 return actionCountFn_ ? actionCountFn_() : static_cast<int>(actions_.size());
136 }
137
138 Widget<RGB_T>* actionAt(int index) const {
139 if (actionAtFn_) {
140 Widget<RGB_T>& action = actionAtFn_(index);
141 if (this->theme_ != nullptr) action.setTheme(*this->theme_);
142 return &action;
143 }
144 return actions_[static_cast<size_t>(index)];
145 }
146
147 std::string message_;
148 std::vector<Widget<RGB_T>*> actions_;
149 ActionCountFn actionCountFn_;
150 ActionAtFn actionAtFn_;
151};
152
156
157} // namespace tinymd
Persistent, non-modal inline message with text actions - Material's "banner", e.g....
Definition: Banner.h:29
Banner(Bounds bounds, const char *message)
Definition: Banner.h:35
bool isDraggableAt(int32_t x, int32_t y) const override
Definition: Banner.h:124
bool onGesture(const tinygpu::GestureEvent &event) override
Handles one gesture event. Returns true if this widget consumed it.
Definition: Banner.h:104
Bounds actionRect(int index, int count, int32_t width=80, int32_t height=32) const
Definition: Banner.h:64
void setActionProvider(ActionCountFn count, ActionAtFn at)
Switches to callback-driven actions - see the class comment.
Definition: Banner.h:49
void draw(tinygpu::ISurface< RGB_T > &target) override
Definition: Banner.h:72
void setMessage(const char *message)
Definition: Banner.h:39
bool update(uint32_t nowMs) override
Definition: Banner.h:115
void setTheme(const MaterialTheme< RGB_T > &theme) override
Definition: Banner.h:54
void addAction(Widget< RGB_T > &action)
Definition: Banner.h:43
Banner()=default
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(int32_t x, int32_t y, int32_t w, int32_t h)
Definition: Bounds.h:28
Bundles a color scheme, shape tokens, typography, and spacing into one theme, passed to widgets so th...
Definition: MaterialTheme.h:50