TinyMaterialDesign
Loading...
Searching...
No Matches
Dialog.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 "TinyGPU/Font/LinePrinter.h"
11#include "TinyMaterialDesign/Core/Widget.h"
12
13namespace tinymd {
14
15/**
16 * @brief Modal alert dialog: full-screen scrim + centered card + title +
17 * wrapped message + action widgets (typically Button).
18 *
19 * Show it with Screen::presentDialog(dialog); while presented, Screen routes
20 * every gesture to the dialog first. Actions (usually Button) are not owned
21 * by Dialog - position each one's bounds (actionRect() suggests a spot),
22 * add it with addAction(), and wire its onClick to call
23 * screen.dismissDialog() itself.
24 *
25 * Alternative to addAction(): setActionProvider(count, at) switches to
26 * callback-driven actions instead - see Container.h's class comment for the
27 * same pattern (`at(index)` returns a Widget reference, typically from a
28 * small reused pool). Not usually needed for a dialog's handful of actions,
29 * but available for consistency.
30 */
31template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
32class Dialog : public Widget<RGB_T> {
33 public:
34 using ActionCountFn = std::function<int()>;
35 using ActionAtFn = std::function<Widget<RGB_T>&(int index)>;
36
37 Dialog() = default;
38 /// `cardBounds` is the dialog card's own rect, not the full screen - the
39 /// scrim is drawn separately, covering the whole target surface.
40 Dialog(Bounds cardBounds, const char* title, const char* message)
42 this->bounds = cardBounds;
43 }
44
45 void setTitle(const char* title) { title_ = title; }
46 void setMessage(const char* message) { message_ = message; }
47
48 /// Registers `action` as content of this dialog. Ignored while an action
49 /// provider is active - see setActionProvider().
50 void addAction(Widget<RGB_T>& action) {
51 actions_.push_back(&action);
52 // Dialog might not have a theme yet if addAction() is called before
53 // the dialog itself is registered with Screen (via presentDialog()) -
54 // in that case setTheme() below will cascade to this action once it
55 // does.
56 if (this->theme_ != nullptr) action.setTheme(*this->theme_);
57 }
58
59 /// Switches to callback-driven actions - see the class comment.
60 void setActionProvider(ActionCountFn count, ActionAtFn at) {
61 actionCountFn_ = std::move(count);
62 actionAtFn_ = std::move(at);
63 }
64
65 /// Cascades the theme down to every action already added - see addAction().
66 /// In provider mode, each action is themed at the point it's fetched
67 /// instead (see actionAt()).
68 void setTheme(const MaterialTheme<RGB_T>& theme) override {
69 Widget<RGB_T>::setTheme(theme);
70 for (Widget<RGB_T>* action : actions_) {
71 if (action != nullptr) action->setTheme(theme);
72 }
73 }
74
75 /// Suggested rect for the `index`-th of `count` actions, right-aligned
76 /// along the card's bottom edge.
77 Bounds actionRect(int index, int count, int32_t width = 80, int32_t height = 36) const {
78 const int32_t pad = 8;
79 const int32_t totalWidth = count * width + (count - 1) * pad;
80 const int32_t startX = this->bounds.right() - pad - totalWidth;
81 const int32_t y = this->bounds.bottom() - pad - height;
82 return Bounds(startX + index * (width + pad), y, width, height);
83 }
84
85 void draw(tinygpu::ISurface<RGB_T>& target) override {
86 drawScrim(target);
87
88 const size_t radius = toPx(this->theme().shape.large);
89 target.fillRoundRect(toPx(this->bounds.x), toPx(this->bounds.y), toPx(this->bounds.w),
90 toPx(this->bounds.h), radius, this->theme().colors.surface);
91 target.drawRoundRect(toPx(this->bounds.x), toPx(this->bounds.y), toPx(this->bounds.w),
92 toPx(this->bounds.h), radius, this->theme().colors.outline);
93
94 const int32_t pad = this->theme().spacing;
95 int32_t cursorY = this->bounds.y + pad * 2;
96
97 tinygpu::IFont<RGB_T>& titleFont = *this->theme().typography.title;
98 titleFont.drawText(target, static_cast<int16_t>(this->bounds.x + pad),
99 static_cast<int16_t>(cursorY), title_.c_str(), this->theme().colors.onSurface,
100 this->theme().colors.surface, false);
101 cursorY += static_cast<int32_t>(titleFont.getHeight(1)) + pad;
102
103 const int32_t rightEdge = this->bounds.right() - pad;
104 const size_t rightBorder = rightEdge < static_cast<int32_t>(target.width())
105 ? target.width() - static_cast<size_t>(rightEdge)
106 : 0;
107 // Crops a message too long for the card to the card's own bounds
108 // instead of letting it spill past the bottom edge (see
109 // ISurface::pushClipRect()).
110 target.pushClipRect(toPx(this->bounds.x), toPx(this->bounds.y), toPx(this->bounds.w),
111 toPx(this->bounds.h));
112 tinygpu::LinePrinter<RGB_T> printer;
113 printer.setFont(*this->theme().typography.body);
114 printer.setTarget(target);
115 printer.setColor(this->theme().colors.onSurfaceVariant);
116 printer.setTopBorder(toPx(cursorY));
117 printer.setLeftBorder(toPx(this->bounds.x + pad));
118 printer.setRightBorder(rightBorder);
119 printer.print(message_.c_str());
120 target.popClipRect();
121
122 const int count = actionCount();
123 for (int i = 0; i < count; ++i) {
124 Widget<RGB_T>* action = actionAt(i);
125 if (action->visible) action->draw(target);
126 }
127 }
128
129 /// Screen routes every gesture here while the dialog is presented; only
130 /// taps landing on an action are forwarded to it, everything else is
131 /// swallowed (a modal dialog shouldn't leak touches through to whatever
132 /// is behind it).
133 bool onGesture(const tinygpu::GestureEvent& event) override {
134 const int count = actionCount();
135 for (int i = 0; i < count; ++i) {
136 Widget<RGB_T>* action = actionAt(i);
137 if (action->enabled && action->bounds.contains(event.point.x, event.point.y)) {
138 return action->onGesture(event);
139 }
140 }
141 return true;
142 }
143
144 bool update(uint32_t nowMs) override {
145 bool changed = false;
146 const int count = actionCount();
147 for (int i = 0; i < count; ++i) changed |= actionAt(i)->update(nowMs);
148 return changed;
149 }
150
151 /// True if there's an action at (x, y) - this dialog's own (screen-space)
152 /// coordinate space - that's draggable, recursing into it if it's itself
153 /// a composite - see Widget::isDraggableAt().
154 bool isDraggableAt(int32_t x, int32_t y) const override {
155 const int count = actionCount();
156 for (int i = 0; i < count; ++i) {
157 Widget<RGB_T>* action = actionAt(i);
158 if (action->enabled && action->bounds.contains(x, y)) return action->isDraggableAt(x, y);
159 }
160 return false;
161 }
162
163 private:
164 int actionCount() const {
165 return actionCountFn_ ? actionCountFn_() : static_cast<int>(actions_.size());
166 }
167
168 Widget<RGB_T>* actionAt(int index) const {
169 if (actionAtFn_) {
170 Widget<RGB_T>& action = actionAtFn_(index);
171 if (this->theme_ != nullptr) action.setTheme(*this->theme_);
172 return &action;
173 }
174 return actions_[static_cast<size_t>(index)];
175 }
176
177 std::string title_;
178 std::string message_;
179 std::vector<Widget<RGB_T>*> actions_;
180 ActionCountFn actionCountFn_;
181 ActionAtFn actionAtFn_;
182};
183
187
188} // namespace tinymd
Modal alert dialog: full-screen scrim + centered card + title + wrapped message + action widgets (typ...
Definition: Dialog.h:32
bool isDraggableAt(int32_t x, int32_t y) const override
Definition: Dialog.h:154
Dialog(Bounds cardBounds, const char *title, const char *message)
Definition: Dialog.h:40
bool onGesture(const tinygpu::GestureEvent &event) override
Definition: Dialog.h:133
Dialog()=default
Bounds actionRect(int index, int count, int32_t width=80, int32_t height=36) const
Definition: Dialog.h:77
void setActionProvider(ActionCountFn count, ActionAtFn at)
Switches to callback-driven actions - see the class comment.
Definition: Dialog.h:60
void draw(tinygpu::ISurface< RGB_T > &target) override
Definition: Dialog.h:85
void setMessage(const char *message)
Definition: Dialog.h:46
void setTitle(const char *title)
Definition: Dialog.h:45
bool update(uint32_t nowMs) override
Definition: Dialog.h:144
void setTheme(const MaterialTheme< RGB_T > &theme) override
Definition: Dialog.h:68
void addAction(Widget< RGB_T > &action)
Definition: Dialog.h:50
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