TinyMaterialDesign
Loading...
Searching...
No Matches
Page.h
Go to the documentation of this file.
1#pragma once
2#include <functional>
3#include <string>
4#include <vector>
5
6#include "TinyMaterialDesign/Core/Container.h"
7#include "TinyMaterialDesign/Core/Screen.h"
8#include "TinyMaterialDesign/Widgets/IconButton.h"
9#include "TinyMaterialDesign/Widgets/NavigationBar.h"
10
11namespace tinymd {
12
13/**
14 * @brief One page (tab) of an app: a scrollable Container with a title and
15 * an icon for the navigation, that builds its own widgets.
16 *
17 * Derive from it and add the widgets in build(); PageNavigator calls it once
18 * the page is on the screen (so it already has its theme and bounds):
19 *
20 * class ClockPage : public Page<RGB565> {
21 * public:
22 * ClockPage() : Page("Clock", drawClockIcon) {}
23 * void build() override { addChild(time_); ... }
24 * bool tick(uint32_t nowMs) override { ... return true; } // redraw
25 * };
26 */
27template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
28class Page : public Container<RGB_T> {
29 public:
30 explicit Page(const char* title = "", IconPainter<RGB_T> icon = nullptr) : title_(title), icon_(icon) {}
31
32 /// Label and icon of the page in a NavigationBar.
33 virtual const char* title() const { return title_.c_str(); }
34 virtual IconPainter<RGB_T> icon() const { return icon_; }
35 void setTitle(const char* title) { title_ = title; }
36 void setIcon(IconPainter<RGB_T> icon) { icon_ = icon; }
37
38 /// Creates the widgets (called once by PageNavigator::begin()).
39 virtual void build() {}
40 /// The page becomes visible.
41 virtual void onShow() {}
42 /// The page is hidden (another page is shown).
43 virtual void onHide() {}
44 /// Called regularly while the page is shown (PageNavigator::tick()):
45 /// return true if the page changed and must be redrawn.
46 virtual bool tick(uint32_t nowMs) {
47 (void)nowMs;
48 return false;
49 }
50
51 private:
52 std::string title_;
53 IconPainter<RGB_T> icon_ = nullptr;
54};
55
56/**
57 * @brief Connects Pages with a NavigationBar: one destination per page,
58 * only the selected page is visible. All pages share the same bounds, so the
59 * screen itself never scrolls; each page scrolls on its own.
60 *
61 * NavigationBar<RGB565> nav(Bounds(0, 264, 240, 56));
62 * PageNavigator<RGB565> pages(app.screen(), nav);
63 * pages.addPage(clockPage);
64 * pages.addPage(settingsPage);
65 * pages.begin(Bounds(0, 40, 240, 224)); // adds nav + pages, builds them
66 * // loop(): if (pages.tick(millis())) app.screen().invalidate();
67 */
68template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
70 public:
71 PageNavigator(Screen<RGB_T>& screen, NavigationBar<RGB_T>& nav) : screen_(screen), nav_(nav) {}
72
73 /// Called after the page was changed (by the user or show()).
74 std::function<void(int index)> onPageChanged;
75
76 /// Adds a page (before begin()).
77 void addPage(Page<RGB_T>& page) { pages_.push_back(&page); }
78
79 /**
80 * @brief Adds the navigation bar (fixed) and the pages (with the indicated
81 * bounds) to the screen, builds the pages and shows the first one.
82 */
83 void begin(Bounds pageBounds) {
84 for (Page<RGB_T>* page : pages_) nav_.addDestination(page->icon(), page->title());
85 nav_.onChange = [this](int index) { show(index); };
86 screen_.addFixedWidget(nav_);
87 for (Page<RGB_T>* page : pages_) {
88 page->bounds = pageBounds;
89 screen_.addWidget(*page); // gives it the theme
90 page->build();
91 }
92 begun_ = true;
93 current_ = -1;
94 show(0);
95 }
96
97 /// Shows the page with the index.
98 void show(int index) {
99 if (index < 0 || index >= count()) return;
100 if (begun_ && current_ >= 0 && current_ != index) pages_[current_]->onHide();
101 const bool changed = current_ != index;
102 current_ = index;
103 for (size_t i = 0; i < pages_.size(); ++i) pages_[i]->visible = static_cast<int>(i) == index;
104 nav_.setSelectedIndex(index);
105 pages_[current_]->onShow();
106 screen_.invalidate();
107 if (changed && onPageChanged) onPageChanged(index);
108 }
109
110 /// Calls tick() of the visible page: true if it must be redrawn.
111 bool tick(uint32_t nowMs) { return current_ >= 0 && pages_[current_]->tick(nowMs); }
112
113 int current() const { return current_; }
114 int count() const { return static_cast<int>(pages_.size()); }
115 Page<RGB_T>& page(int index) { return *pages_[index]; }
116 Page<RGB_T>& currentPage() { return *pages_[current_]; }
117
118 private:
119 Screen<RGB_T>& screen_;
120 NavigationBar<RGB_T>& nav_;
121 std::vector<Page<RGB_T>*> pages_;
122 int current_ = -1;
123 bool begun_ = false;
124};
125
126} // namespace tinymd
A widget that holds child widgets, draws/dispatches gestures to them, and scrolls its content vertica...
Definition: Container.h:56
Bottom navigation bar: 3-5 exclusive destinations, each an icon with a label underneath and a pill hi...
Definition: NavigationBar.h:29
Connects Pages with a NavigationBar: one destination per page, only the selected page is visible....
Definition: Page.h:69
void begin(Bounds pageBounds)
Adds the navigation bar (fixed) and the pages (with the indicated bounds) to the screen,...
Definition: Page.h:83
int count() const
Definition: Page.h:114
int current() const
Definition: Page.h:113
std::function< void(int index)> onPageChanged
Called after the page was changed (by the user or show()).
Definition: Page.h:74
Page< RGB_T > & page(int index)
Definition: Page.h:115
void addPage(Page< RGB_T > &page)
Adds a page (before begin()).
Definition: Page.h:77
void show(int index)
Shows the page with the index.
Definition: Page.h:98
PageNavigator(Screen< RGB_T > &screen, NavigationBar< RGB_T > &nav)
Definition: Page.h:71
Page< RGB_T > & currentPage()
Definition: Page.h:116
bool tick(uint32_t nowMs)
Calls tick() of the visible page: true if it must be redrawn.
Definition: Page.h:111
One page (tab) of an app: a scrollable Container with a title and an icon for the navigation,...
Definition: Page.h:28
virtual void onHide()
The page is hidden (another page is shown).
Definition: Page.h:43
virtual void onShow()
The page becomes visible.
Definition: Page.h:41
Page(const char *title="", IconPainter< RGB_T > icon=nullptr)
Definition: Page.h:30
virtual IconPainter< RGB_T > icon() const
Definition: Page.h:34
void setIcon(IconPainter< RGB_T > icon)
Definition: Page.h:36
virtual void build()
Creates the widgets (called once by PageNavigator::begin()).
Definition: Page.h:39
void setTitle(const char *title)
Definition: Page.h:35
virtual const char * title() const
Label and icon of the page in a NavigationBar.
Definition: Page.h:33
virtual bool tick(uint32_t nowMs)
Definition: Page.h:46
Owns a set of widgets, draws them, and routes gesture events to whichever one should handle each even...
Definition: Screen.h:71
Definition: AnchorLayout.h:4
Axis-aligned pixel rectangle used for widget bounds and hit-testing.
Definition: Bounds.h:21