TinyMaterialDesign
Loading...
Searching...
No Matches
NavigationBar.h
Go to the documentation of this file.
1#pragma once
2#include <functional>
3#include <vector>
4
5#include "TinyGPU/Color/RGB565.h"
6#include "TinyGPU/Color/RGB666.h"
7#include "TinyGPU/Color/RGB888.h"
8#include "TinyMaterialDesign/Core/LinearLayout.h"
9#include "TinyMaterialDesign/Core/Widget.h"
10#include "TinyMaterialDesign/Widgets/IconButton.h" // IconPainter
11
12namespace tinymd {
13
14/// One NavigationBar/NavigationRail destination: an icon plus a short label.
15template <typename RGB_T>
18 const char* label = "";
19};
20
21/**
22 * @brief Bottom navigation bar: 3-5 exclusive destinations, each an icon
23 * with a label underneath and a pill highlight behind the selected icon.
24 *
25 * Register with Screen::addFixedWidget() so it stays pinned to the bottom
26 * of the viewport while scrollable content behind it scrolls.
27 */
28template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
29class NavigationBar : public Widget<RGB_T> {
30 public:
31 NavigationBar() = default;
32 explicit NavigationBar(Bounds bounds) { this->bounds = bounds; }
33
34 /// Fired with the new selected index whenever a different destination is
35 /// tapped.
36 std::function<void(int)> onChange;
37
38 void addDestination(IconPainter<RGB_T> icon, const char* label) {
39 destinations_.push_back(NavDestination<RGB_T>{icon, label});
40 }
41
42 int selectedIndex() const { return selectedIndex_; }
43 /// No destination is highlighted (e.g. while a screen outside the
44 /// navigation is shown); a tap on any destination then selects it.
45 void clearSelection() { selectedIndex_ = -1; }
46 void setSelectedIndex(int index) {
47 if (index >= 0 && index < static_cast<int>(destinations_.size())) selectedIndex_ = index;
48 }
49
50 void draw(tinygpu::ISurface<RGB_T>& target) override {
51 const int count = static_cast<int>(destinations_.size());
52 if (count == 0) return;
53 target.fillRect(toPx(this->bounds.x), toPx(this->bounds.y), toPx(this->bounds.w),
54 toPx(this->bounds.h), this->theme().colors.surface);
55
56 const LinearLayout slots(this->bounds, LayoutAxis::Horizontal, /*spacing=*/0);
57 tinygpu::IFont<RGB_T>& font = *this->theme().typography.label;
58
59 for (int i = 0; i < count; ++i) {
60 const NavDestination<RGB_T>& dest = destinations_[i];
61 const Bounds slot = slots.itemRect(i, count);
62 const int32_t slotX = slot.x;
63 const int32_t slotWidth = slot.w;
64 const bool selected = i == selectedIndex_;
65 RGB_T contentColor = selected ? this->theme().colors.onSecondaryContainer
66 : this->theme().colors.onSurfaceVariant;
67 if (!this->enabled) contentColor = blend(contentColor, this->theme().colors.surface, 0.5f);
68
69 constexpr int32_t kIconSize = 24;
70 constexpr int32_t kPillHeight = 32;
71 const int32_t pillWidth = kIconSize + 24;
72 const Bounds pill(slotX + slotWidth / 2 - pillWidth / 2, this->bounds.y + 8, pillWidth,
73 kPillHeight);
74 if (selected) {
75 target.fillRoundRect(toPx(pill.x), toPx(pill.y), toPx(pill.w), toPx(pill.h),
76 toPx(pill.h / 2), this->theme().colors.secondaryContainer);
77 }
78
79 if (dest.icon != nullptr) {
80 const Bounds iconRect(pill.centerX() - kIconSize / 2, pill.centerY() - kIconSize / 2,
81 kIconSize, kIconSize);
82 dest.icon(target, iconRect, contentColor, /*thickness=*/2);
83 }
84
85 const size_t labelWidth = font.measureTextWidth(dest.label);
86 const int32_t labelX = slotX + slotWidth / 2 - static_cast<int32_t>(labelWidth) / 2;
87 const int32_t labelY = pill.bottom() + 2;
88 font.drawText(target, static_cast<int16_t>(labelX), static_cast<int16_t>(labelY), dest.label,
89 contentColor, this->theme().colors.surface, false);
90 }
91 }
92
93 bool onGesture(const tinygpu::GestureEvent& event) override {
94 const int count = static_cast<int>(destinations_.size());
95 if (!isTapGesture(event.type) || count == 0) return false;
96 const int32_t slotWidth = this->bounds.w / count;
97 int index = (event.point.x - this->bounds.x) / slotWidth;
98 if (index < 0) index = 0;
99 if (index >= count) index = count - 1;
100 if (index != selectedIndex_) {
101 selectedIndex_ = index;
102 if (onChange) onChange(selectedIndex_);
103 }
104 return true;
105 }
106
107 private:
108 std::vector<NavDestination<RGB_T>> destinations_;
109 int selectedIndex_ = 0;
110};
111
115
116} // namespace tinymd
Computes equal- or weighted-split rects along one axis of a container.
Definition: LinearLayout.h:24
Bounds itemRect(int index, int count) const
Bounds for the index-th of count equally-sized items (0-based).
Definition: LinearLayout.h:30
Bottom navigation bar: 3-5 exclusive destinations, each an icon with a label underneath and a pill hi...
Definition: NavigationBar.h:29
void addDestination(IconPainter< RGB_T > icon, const char *label)
Definition: NavigationBar.h:38
bool onGesture(const tinygpu::GestureEvent &event) override
Handles one gesture event. Returns true if this widget consumed it.
Definition: NavigationBar.h:93
void setSelectedIndex(int index)
Definition: NavigationBar.h:46
void draw(tinygpu::ISurface< RGB_T > &target) override
Definition: NavigationBar.h:50
int selectedIndex() const
Definition: NavigationBar.h:42
NavigationBar(Bounds bounds)
Definition: NavigationBar.h:32
void clearSelection()
Definition: NavigationBar.h:45
std::function< void(int)> onChange
Definition: NavigationBar.h:36
Base class every TinyMaterialDesign widget derives from.
Definition: Widget.h:58
Definition: AnchorLayout.h:4
LayoutAxis
Definition: LinearLayout.h:6
Axis-aligned pixel rectangle used for widget bounds and hit-testing.
Definition: Bounds.h:21
int32_t bottom() const
Definition: Bounds.h:31
int32_t w
Definition: Bounds.h:24
Bounds(int32_t x, int32_t y, int32_t w, int32_t h)
Definition: Bounds.h:28
int32_t centerY() const
Definition: Bounds.h:33
int32_t centerX() const
Definition: Bounds.h:32
int32_t x
Definition: Bounds.h:22
One NavigationBar/NavigationRail destination: an icon plus a short label.
Definition: NavigationBar.h:16
IconPainter< RGB_T > icon
Definition: NavigationBar.h:17
const char * label
Definition: NavigationBar.h:18