TinyMaterialDesign
Loading...
Searching...
No Matches
Drawer.h
Go to the documentation of this file.
1#pragma once
2#include <functional>
3#include <utility>
4
5#include "TinyGPU/Color/RGB565.h"
6#include "TinyGPU/Color/RGB666.h"
7#include "TinyGPU/Color/RGB888.h"
8#include "TinyMaterialDesign/Core/Container.h"
9#include "TinyMaterialDesign/Core/Widget.h"
10
11namespace tinymd {
12
13/**
14 * @brief Modal side navigation panel, holding a list of items (typically
15 * ListItem) that scrolls automatically once they overflow the panel's
16 * height - the item area is a Container (Core/Container.h) under the hood.
17 *
18 * `bounds` is the panel's own rect - typically the left portion of the
19 * screen, e.g. Bounds(0, 0, 220, kHeight) - not the full screen; like
20 * Dialog, the scrim is drawn separately covering the whole target surface.
21 * Show it with Screen::presentDialog(drawer) (Screen's modal-presentation
22 * mechanism isn't specific to alert dialogs - any Widget works, including
23 * this one) and dismiss it from an item's onClick or from onScrimTap.
24 *
25 * Drawer<RGB565> drawer(Bounds(0, 0, 220, kHeight));
26 * ListItem<RGB565> radios(drawer.itemRect(0), "Radios", drawIcon);
27 * radios.onClick = []() { showRadios(); screen.dismissDialog(); };
28 * drawer.addItem(radios);
29 * drawer.onScrimTap = []() { screen.dismissDialog(); };
30 *
31 * Items (like Dialog's actions) are not owned by Drawer - position each
32 * one's bounds (itemRect() suggests a spot) and add it with addItem().
33 *
34 * Alternative to addItem(): setItemProvider(count, at) switches to
35 * callback-driven items - see Container.h's class comment for the pattern
36 * (useful for a long settings-style list too large to keep every ListItem
37 * resident at once).
38 *
39 * Known limitation: Screen::drawDirect()'s per-widget-buffer rendering path
40 * (see childCount()/child() and Screen.h's own class comment) draws each
41 * item at its authored, unscrolled position regardless of the current
42 * scroll offset - correct only while the drawer isn't scrolled. The regular
43 * draw() path (used by the common draw()+writeData(surface) loop) scrolls
44 * correctly in both cases; only the rare low-RAM direct-render path has
45 * this gap.
46 */
47template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
48class Drawer : public Widget<RGB_T> {
49 public:
50 Drawer() = default;
51 explicit Drawer(Bounds bounds) {
52 this->bounds = bounds;
53 items_.bounds = bounds;
54 }
55
56 /// Fired when a tap lands outside the panel (i.e. on the scrim) - wire
57 /// this to Screen::dismissDialog() for tap-outside-to-close.
58 std::function<void()> onScrimTap;
59
60 void addItem(Widget<RGB_T>& item) { items_.addChild(item); }
61
62 /// Switches to callback-driven items - see the class comment.
63 void setItemProvider(typename Container<RGB_T>::ChildCountFn count,
64 typename Container<RGB_T>::ChildAtFn at) {
65 items_.setChildProvider(std::move(count), std::move(at));
66 }
67
68 /// Cascades the theme down to every item already added - see addItem().
69 void setTheme(const MaterialTheme<RGB_T>& theme) override {
70 Widget<RGB_T>::setTheme(theme);
71 items_.setTheme(theme);
72 }
73
74 /// Suggested rect for the `index`-th item, stacked from the panel's top.
75 Bounds itemRect(int index, int32_t height = 48) const {
76 return Bounds(this->bounds.x, this->bounds.y + index * height, this->bounds.w, height);
77 }
78
79 void draw(tinygpu::ISurface<RGB_T>& target) override {
80 drawBackground(target);
81 items_.bounds = this->bounds;
82 items_.draw(target);
83 }
84
85 /// Scrim + panel fill only, no items - see Widget::drawBackground() and
86 /// childCount()/child() below, which together let
87 /// Screen::drawDirect() draw this (on a board without enough RAM for a
88 /// full-screen buffer) as this small background plus each item
89 /// individually, rather than needing one buffer sized to the whole
90 /// drawer (its own bounds are often nearly full-screen).
91 void drawBackground(tinygpu::ISurface<RGB_T>& target) override {
92 drawScrim(target);
93
94 target.fillRect(toPx(this->bounds.x), toPx(this->bounds.y), toPx(this->bounds.w),
95 toPx(this->bounds.h), this->theme().colors.surface);
96 }
97
98 int childCount() const override { return items_.childCount(); }
99 Widget<RGB_T>* child(int index) override { return items_.child(index); }
100
101 /// Screen routes every gesture here while the drawer is presented: a
102 /// continuous drag starting over the item list scrolls it (see
103 /// Container::onGesture()); a tap on an item is forwarded to it; a tap
104 /// outside the panel fires onScrimTap; everything else is swallowed
105 /// rather than leaking through to whatever is behind the drawer.
106 bool onGesture(const tinygpu::GestureEvent& event) override {
107 items_.bounds = this->bounds;
108
109 if (Container<RGB_T>::isContinuousType(event.type)) {
110 if (event.phase == tinygpu::GesturePhase::kBegan) {
111 routingToItems_ = items_.bounds.contains(event.startPoint.x, event.startPoint.y);
112 }
113 return routingToItems_ ? items_.onGesture(event) : true;
114 }
115
116 if (items_.bounds.contains(event.point.x, event.point.y) && items_.onGesture(event)) {
117 return true;
118 }
119 if (isTapGesture(event.type) && !this->bounds.contains(event.point.x, event.point.y)) {
120 if (onScrimTap) onScrimTap();
121 }
122 return true;
123 }
124
125 bool update(uint32_t nowMs) override { return items_.update(nowMs); }
126
127 /// True if there's an item at (x, y) that's draggable, recursing through
128 /// the item Container - see Widget::isDraggableAt().
129 bool isDraggableAt(int32_t x, int32_t y) const override {
130 return items_.bounds.contains(x, y) && items_.isDraggableAt(x, y);
131 }
132
133 private:
134 Container<RGB_T> items_;
135 bool routingToItems_ = false;
136};
137
141
142} // namespace tinymd
A widget that holds child widgets, draws/dispatches gestures to them, and scrolls its content vertica...
Definition: Container.h:56
Modal side navigation panel, holding a list of items (typically ListItem) that scrolls automatically ...
Definition: Drawer.h:48
bool isDraggableAt(int32_t x, int32_t y) const override
Definition: Drawer.h:129
bool onGesture(const tinygpu::GestureEvent &event) override
Definition: Drawer.h:106
void setItemProvider(typename Container< RGB_T >::ChildCountFn count, typename Container< RGB_T >::ChildAtFn at)
Switches to callback-driven items - see the class comment.
Definition: Drawer.h:63
Widget< RGB_T > * child(int index) override
Definition: Drawer.h:99
void addItem(Widget< RGB_T > &item)
Definition: Drawer.h:60
Drawer(Bounds bounds)
Definition: Drawer.h:51
Bounds itemRect(int index, int32_t height=48) const
Suggested rect for the index-th item, stacked from the panel's top.
Definition: Drawer.h:75
void draw(tinygpu::ISurface< RGB_T > &target) override
Definition: Drawer.h:79
std::function< void()> onScrimTap
Definition: Drawer.h:58
bool update(uint32_t nowMs) override
Definition: Drawer.h:125
int childCount() const override
Definition: Drawer.h:98
void setTheme(const MaterialTheme< RGB_T > &theme) override
Cascades the theme down to every item already added - see addItem().
Definition: Drawer.h:69
Drawer()=default
void drawBackground(tinygpu::ISurface< RGB_T > &target) override
Definition: Drawer.h:91
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