TinyMaterialDesign
Loading...
Searching...
No Matches
Menu.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 Popover list of items (typically ListItem), anchored near whatever
15 * opened it - e.g. a dropdown from a Button, or a context menu from a
16 * long-press. Scrolls automatically once the items overflow the panel's
17 * height - the item area is a Container (Core/Container.h) under the hood.
18 *
19 * Same modal-presentation shape as Dialog/Drawer: show with
20 * Screen::presentDialog(menu) and dismiss from an item's onClick or
21 * onOutsideTap. Unlike Dialog/Drawer it does *not* dim the rest of the
22 * screen (a menu is a lightweight popover, not a heavyweight interruption)
23 * - it just draws its own panel and swallows every gesture outside it.
24 *
25 * Menu<RGB565> menu(Bounds(140, 60, 160, 3 * 40));
26 * ListItem<RGB565> item(menu.itemRect(0), "Rename");
27 * item.onClick = []() { doRename(); screen.dismissDialog(); };
28 * menu.addItem(item);
29 * menu.onOutsideTap = []() { screen.dismissDialog(); };
30 * screen.presentDialog(menu);
31 *
32 * Items are not owned by Menu - same non-owning addItem() convention as
33 * Drawer::addItem()/Dialog::addAction().
34 *
35 * Known limitation: same as Drawer's - Screen::drawDirect()'s per-widget-
36 * buffer rendering path draws items at their unscrolled position; only the
37 * regular draw() path scrolls correctly. See Drawer.h's class comment.
38 */
39template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
40class Menu : public Widget<RGB_T> {
41 public:
42 Menu() = default;
43 explicit Menu(Bounds bounds) {
44 this->bounds = bounds;
45 items_.bounds = bounds;
46 }
47
48 /// Fired when a tap lands outside the panel - wire to
49 /// Screen::dismissDialog() for tap-outside-to-close.
50 std::function<void()> onOutsideTap;
51
52 void addItem(Widget<RGB_T>& item) { items_.addChild(item); }
53
54 /// Removes all items (they are not deleted) - e.g. to fill the menu anew.
55 void clearItems() { items_.clearChildren(); }
56
57 /// Switches to callback-driven items - see Container.h's class comment
58 /// and Drawer.h's own setItemProvider() for the same pattern.
59 void setItemProvider(typename Container<RGB_T>::ChildCountFn count,
60 typename Container<RGB_T>::ChildAtFn at) {
61 items_.setChildProvider(std::move(count), std::move(at));
62 }
63
64 void setTheme(const MaterialTheme<RGB_T>& theme) override {
65 Widget<RGB_T>::setTheme(theme);
66 items_.setTheme(theme);
67 }
68
69 /// Suggested rect for the `index`-th item, stacked from the panel's top.
70 Bounds itemRect(int index, int32_t height = 40) const {
71 return Bounds(this->bounds.x, this->bounds.y + index * height, this->bounds.w, height);
72 }
73
74 void draw(tinygpu::ISurface<RGB_T>& target) override {
75 drawBackground(target);
76 items_.bounds = this->bounds;
77 items_.draw(target);
78 }
79
80 /// Panel fill only, no items - see Widget::drawBackground() and
81 /// childCount()/child() below (same rationale as Drawer's).
82 void drawBackground(tinygpu::ISurface<RGB_T>& target) override {
83 const size_t radius = toPx(this->theme().shape.medium);
84 target.fillRoundRect(toPx(this->bounds.x), toPx(this->bounds.y), toPx(this->bounds.w),
85 toPx(this->bounds.h), radius, this->theme().colors.surface);
86 target.drawRoundRect(toPx(this->bounds.x), toPx(this->bounds.y), toPx(this->bounds.w),
87 toPx(this->bounds.h), radius, this->theme().colors.outline);
88 }
89
90 int childCount() const override { return items_.childCount(); }
91 Widget<RGB_T>* child(int index) override { return items_.child(index); }
92
93 bool onGesture(const tinygpu::GestureEvent& event) override {
94 items_.bounds = this->bounds;
95
96 if (Container<RGB_T>::isContinuousType(event.type)) {
97 if (event.phase == tinygpu::GesturePhase::kBegan) {
98 routingToItems_ = items_.bounds.contains(event.startPoint.x, event.startPoint.y);
99 }
100 return routingToItems_ ? items_.onGesture(event) : true;
101 }
102
103 if (items_.bounds.contains(event.point.x, event.point.y) && items_.onGesture(event)) {
104 return true;
105 }
106 if (isTapGesture(event.type) && !this->bounds.contains(event.point.x, event.point.y)) {
107 if (onOutsideTap) onOutsideTap();
108 }
109 return true;
110 }
111
112 bool update(uint32_t nowMs) override { return items_.update(nowMs); }
113
114 /// True if there's an item at (x, y) that's draggable, recursing through
115 /// the item Container - see Widget::isDraggableAt().
116 bool isDraggableAt(int32_t x, int32_t y) const override {
117 return items_.bounds.contains(x, y) && items_.isDraggableAt(x, y);
118 }
119
120 private:
121 Container<RGB_T> items_;
122 bool routingToItems_ = false;
123};
124
128
129} // namespace tinymd
A widget that holds child widgets, draws/dispatches gestures to them, and scrolls its content vertica...
Definition: Container.h:56
Popover list of items (typically ListItem), anchored near whatever opened it - e.g....
Definition: Menu.h:40
bool isDraggableAt(int32_t x, int32_t y) const override
Definition: Menu.h:116
bool onGesture(const tinygpu::GestureEvent &event) override
Handles one gesture event. Returns true if this widget consumed it.
Definition: Menu.h:93
void setItemProvider(typename Container< RGB_T >::ChildCountFn count, typename Container< RGB_T >::ChildAtFn at)
Definition: Menu.h:59
Widget< RGB_T > * child(int index) override
Definition: Menu.h:91
std::function< void()> onOutsideTap
Definition: Menu.h:50
Menu(Bounds bounds)
Definition: Menu.h:43
void addItem(Widget< RGB_T > &item)
Definition: Menu.h:52
Menu()=default
void clearItems()
Removes all items (they are not deleted) - e.g. to fill the menu anew.
Definition: Menu.h:55
void draw(tinygpu::ISurface< RGB_T > &target) override
Definition: Menu.h:74
Bounds itemRect(int index, int32_t height=40) const
Suggested rect for the index-th item, stacked from the panel's top.
Definition: Menu.h:70
bool update(uint32_t nowMs) override
Definition: Menu.h:112
int childCount() const override
Definition: Menu.h:90
void setTheme(const MaterialTheme< RGB_T > &theme) override
Definition: Menu.h:64
void drawBackground(tinygpu::ISurface< RGB_T > &target) override
Definition: Menu.h:82
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