TinyMaterialDesign
Loading...
Searching...
No Matches
BottomSheet.h
Go to the documentation of this file.
1#pragma once
2#include <functional>
3#include <string>
4#include <utility>
5
6#include "TinyGPU/Color/RGB565.h"
7#include "TinyGPU/Color/RGB666.h"
8#include "TinyGPU/Color/RGB888.h"
9#include "TinyMaterialDesign/Core/Container.h"
10#include "TinyMaterialDesign/Core/Widget.h"
11
12namespace tinymd {
13
14/**
15 * @brief Modal panel sliding up from the bottom edge, full width, with
16 * rounded top corners and a drag-handle bar - Material's "modal bottom
17 * sheet". Holds a list of items (typically ListItem or Button) the same
18 * non-owning way Drawer holds its items, in a Container (Core/Container.h)
19 * that scrolls automatically once they overflow the sheet's height.
20 *
21 * `bounds` is the sheet's own rect (e.g. Bounds(0, screenH - 220, screenW,
22 * 220)) - not the full screen; the scrim is drawn separately covering the
23 * whole target surface, same convention as Dialog/Drawer. Show it with
24 * Screen::presentDialog(sheet) and dismiss it from an item's onClick or
25 * from onScrimTap.
26 *
27 * Known limitation: same as Drawer's - Screen::drawDirect()'s per-widget-
28 * buffer rendering path draws items at their unscrolled position; only the
29 * regular draw() path scrolls correctly. See Drawer.h's class comment.
30 */
31template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
32class BottomSheet : public Widget<RGB_T> {
33 public:
34 BottomSheet() = default;
35 explicit BottomSheet(Bounds bounds) {
36 this->bounds = bounds;
37 syncItemsBounds();
38 }
39
40 BottomSheet(Bounds bounds, const char* title) : title_(title != nullptr ? title : "") {
41 this->bounds = bounds;
42 syncItemsBounds();
43 }
44
45 void setTitle(const char* title) { title_ = title != nullptr ? title : ""; }
46
47 /// Fired when a tap lands outside the panel (i.e. on the scrim).
48 std::function<void()> onScrimTap;
49
50 void addItem(Widget<RGB_T>& item) { items_.addChild(item); }
51
52 /// Switches to callback-driven items - see Container.h's class comment
53 /// and Drawer.h's own setItemProvider() for the same pattern.
54 void setItemProvider(typename Container<RGB_T>::ChildCountFn count,
55 typename Container<RGB_T>::ChildAtFn at) {
56 items_.setChildProvider(std::move(count), std::move(at));
57 }
58
59 void setTheme(const MaterialTheme<RGB_T>& theme) override {
60 Widget<RGB_T>::setTheme(theme);
61 items_.setTheme(theme);
62 }
63
64 /// Suggested rect for the `index`-th item, stacked below the handle/title.
65 /// Deliberately theme-independent (fixed constants, not
66 /// theme().spacing/typography) - like Drawer::itemRect()/Menu::itemRect(),
67 /// this is meant to be callable at global-scope construction time, before
68 /// this sheet has ever been given a theme (see Drawer.h's class comment
69 /// for that usage pattern).
70 Bounds itemRect(int index, int32_t height = 48) const {
71 return Bounds(this->bounds.x, contentTop() + index * height, this->bounds.w, height);
72 }
73
74 void draw(tinygpu::ISurface<RGB_T>& target) override {
75 drawBackground(target);
76 syncItemsBounds();
77 items_.draw(target);
78 }
79
80 /// Scrim + panel fill + handle + title, no items - see
81 /// Widget::drawBackground() and childCount()/child() (same rationale as
82 /// Drawer's own).
83 void drawBackground(tinygpu::ISurface<RGB_T>& target) override {
84 drawScrim(target);
85
86 const size_t radius = toPx(this->theme().shape.large);
87 // Rounded top corners only: draw a fully-rounded rect, then square off
88 // the bottom half by overpainting it flat - cheaper than a 4-independent-
89 // corner primitive, which ISurface doesn't expose.
90 target.fillRoundRect(toPx(this->bounds.x), toPx(this->bounds.y), toPx(this->bounds.w),
91 toPx(this->bounds.h), radius, this->theme().colors.surface);
92 const int32_t squareTop = this->bounds.y + static_cast<int32_t>(radius);
93 if (squareTop < this->bounds.bottom()) {
94 target.fillRect(toPx(this->bounds.x), toPx(squareTop), toPx(this->bounds.w),
95 toPx(this->bounds.bottom() - squareTop), this->theme().colors.surface);
96 }
97
98 constexpr int32_t kHandleWidth = 32;
99 constexpr int32_t kHandleHeight = 4;
100 const int32_t handleY = this->bounds.y + 8;
101 target.fillRoundRect(toPx(this->bounds.centerX() - kHandleWidth / 2), toPx(handleY),
102 toPx(kHandleWidth), toPx(kHandleHeight), toPx(kHandleHeight / 2),
103 this->theme().colors.outline);
104
105 if (!title_.empty()) {
106 tinygpu::IFont<RGB_T>& font = *this->theme().typography.title;
107 const int32_t pad = this->theme().spacing;
108 font.drawText(target, static_cast<int16_t>(this->bounds.x + pad * 2),
109 static_cast<int16_t>(handleY + kHandleHeight + pad), title_.c_str(),
110 this->theme().colors.onSurface, this->theme().colors.surface, false);
111 }
112 }
113
114 int childCount() const override { return items_.childCount(); }
115 Widget<RGB_T>* child(int index) override { return items_.child(index); }
116
117 /// A continuous drag starting over the item list scrolls it (see
118 /// Container::onGesture()); a tap on an item is forwarded to it; a tap
119 /// outside the panel fires onScrimTap; everything else is swallowed.
120 bool onGesture(const tinygpu::GestureEvent& event) override {
121 syncItemsBounds();
122
123 if (Container<RGB_T>::isContinuousType(event.type)) {
124 if (event.phase == tinygpu::GesturePhase::kBegan) {
125 routingToItems_ = items_.bounds.contains(event.startPoint.x, event.startPoint.y);
126 }
127 return routingToItems_ ? items_.onGesture(event) : true;
128 }
129
130 if (items_.bounds.contains(event.point.x, event.point.y) && items_.onGesture(event)) {
131 return true;
132 }
133 if (isTapGesture(event.type) && !this->bounds.contains(event.point.x, event.point.y)) {
134 if (onScrimTap) onScrimTap();
135 }
136 return true;
137 }
138
139 bool update(uint32_t nowMs) override { return items_.update(nowMs); }
140
141 /// True if there's an item at (x, y) that's draggable, recursing through
142 /// the item Container - see Widget::isDraggableAt().
143 bool isDraggableAt(int32_t x, int32_t y) const override {
144 return items_.bounds.contains(x, y) && items_.isDraggableAt(x, y);
145 }
146
147 private:
148 int32_t contentTop() const {
149 constexpr int32_t kHandleHeight = 4;
150 constexpr int32_t kPad = 8;
151 // Approximates the title typography's line height without touching
152 // theme() (see itemRect()'s comment) - close enough for row layout, and
153 // drawBackground() still measures the real font for the title itself.
154 constexpr int32_t kApproxTitleHeight = 20;
155 const int32_t afterHandle = this->bounds.y + 8 + kHandleHeight + kPad;
156 if (title_.empty()) return afterHandle;
157 return afterHandle + kApproxTitleHeight + kPad;
158 }
159
160 /// Keeps the item Container's own bounds in sync with this sheet's -
161 /// items start below the handle/title, not at bounds.y itself.
162 void syncItemsBounds() {
163 const int32_t top = contentTop();
164 items_.bounds = Bounds(this->bounds.x, top, this->bounds.w, this->bounds.bottom() - top);
165 }
166
167 std::string title_;
168 Container<RGB_T> items_;
169 bool routingToItems_ = false;
170};
171
175
176} // namespace tinymd
Modal panel sliding up from the bottom edge, full width, with rounded top corners and a drag-handle b...
Definition: BottomSheet.h:32
bool isDraggableAt(int32_t x, int32_t y) const override
Definition: BottomSheet.h:143
bool onGesture(const tinygpu::GestureEvent &event) override
Definition: BottomSheet.h:120
void setItemProvider(typename Container< RGB_T >::ChildCountFn count, typename Container< RGB_T >::ChildAtFn at)
Definition: BottomSheet.h:54
Widget< RGB_T > * child(int index) override
Definition: BottomSheet.h:115
BottomSheet(Bounds bounds)
Definition: BottomSheet.h:35
void addItem(Widget< RGB_T > &item)
Definition: BottomSheet.h:50
Bounds itemRect(int index, int32_t height=48) const
Definition: BottomSheet.h:70
void draw(tinygpu::ISurface< RGB_T > &target) override
Definition: BottomSheet.h:74
std::function< void()> onScrimTap
Fired when a tap lands outside the panel (i.e. on the scrim).
Definition: BottomSheet.h:48
void setTitle(const char *title)
Definition: BottomSheet.h:45
bool update(uint32_t nowMs) override
Definition: BottomSheet.h:139
int childCount() const override
Definition: BottomSheet.h:114
void setTheme(const MaterialTheme< RGB_T > &theme) override
Definition: BottomSheet.h:59
void drawBackground(tinygpu::ISurface< RGB_T > &target) override
Definition: BottomSheet.h:83
BottomSheet(Bounds bounds, const char *title)
Definition: BottomSheet.h:40
A widget that holds child widgets, draws/dispatches gestures to them, and scrolls its content vertica...
Definition: Container.h:56
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
Bundles a color scheme, shape tokens, typography, and spacing into one theme, passed to widgets so th...
Definition: MaterialTheme.h:50