TinyMaterialDesign
Loading...
Searching...
No Matches
NavigationRail.h
Go to the documentation of this file.
1#pragma once
2#include <algorithm>
3#include <functional>
4#include <vector>
5
6#include "TinyGPU/Color/RGB565.h"
7#include "TinyGPU/Color/RGB666.h"
8#include "TinyGPU/Color/RGB888.h"
9#include "TinyMaterialDesign/Core/LinearLayout.h"
10#include "TinyMaterialDesign/Core/Widget.h"
11#include "TinyMaterialDesign/Widgets/NavigationBar.h" // NavDestination
12
13namespace tinymd {
14
15/**
16 * @brief Side navigation rail: a narrow vertical column of 3-7 exclusive
17 * destinations, each an icon with a label underneath and a pill highlight
18 * behind the selected icon - the tablet/landscape counterpart of
19 * NavigationBar.
20 *
21 * Register with Screen::addFixedWidget() so it stays pinned to the side of
22 * the viewport while scrollable content behind it scrolls.
23 */
24template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
25class NavigationRail : public Widget<RGB_T> {
26 public:
27 NavigationRail() = default;
28 explicit NavigationRail(Bounds bounds) { this->bounds = bounds; }
29
30 std::function<void(int)> onChange;
31
32 void addDestination(IconPainter<RGB_T> icon, const char* label) {
33 destinations_.push_back(NavDestination<RGB_T>{icon, label});
34 }
35
36 int selectedIndex() const { return selectedIndex_; }
37 void setSelectedIndex(int index) {
38 if (index >= 0 && index < static_cast<int>(destinations_.size())) selectedIndex_ = index;
39 }
40
41 void draw(tinygpu::ISurface<RGB_T>& target) override {
42 const int count = static_cast<int>(destinations_.size());
43 if (count == 0) return;
44 target.fillRect(toPx(this->bounds.x), toPx(this->bounds.y), toPx(this->bounds.w),
45 toPx(this->bounds.h), this->theme().colors.surface);
46
47 const LinearLayout slots(this->bounds, LayoutAxis::Vertical, /*spacing=*/0);
48 tinygpu::IFont<RGB_T>& font = *this->theme().typography.label;
49
50 for (int i = 0; i < count; ++i) {
51 const NavDestination<RGB_T>& dest = destinations_[i];
52 const Bounds slot = slots.itemRect(i, count);
53 const int32_t slotY = slot.y;
54 const int32_t slotHeight = slot.h;
55 const bool selected = i == selectedIndex_;
56 RGB_T contentColor = selected ? this->theme().colors.onSecondaryContainer
57 : this->theme().colors.onSurfaceVariant;
58 if (!this->enabled) contentColor = blend(contentColor, this->theme().colors.surface, 0.5f);
59
60 constexpr int32_t kIconSize = 24;
61 constexpr int32_t kPillHeight = 32;
62 const int32_t pillWidth = std::min<int32_t>(this->bounds.w - 16, kIconSize + 24);
63 const Bounds pill(this->bounds.centerX() - pillWidth / 2, slotY + slotHeight / 2 - kPillHeight / 2 - 6,
64 pillWidth, kPillHeight);
65 if (selected) {
66 target.fillRoundRect(toPx(pill.x), toPx(pill.y), toPx(pill.w), toPx(pill.h),
67 toPx(pill.h / 2), this->theme().colors.secondaryContainer);
68 }
69
70 if (dest.icon != nullptr) {
71 const Bounds iconRect(pill.centerX() - kIconSize / 2, pill.centerY() - kIconSize / 2,
72 kIconSize, kIconSize);
73 dest.icon(target, iconRect, contentColor, /*thickness=*/2);
74 }
75
76 const size_t labelWidth = font.measureTextWidth(dest.label);
77 const int32_t labelX = this->bounds.centerX() - static_cast<int32_t>(labelWidth) / 2;
78 const int32_t labelY = pill.bottom() + 2;
79 font.drawText(target, static_cast<int16_t>(labelX), static_cast<int16_t>(labelY), dest.label,
80 contentColor, this->theme().colors.surface, false);
81 }
82 }
83
84 bool onGesture(const tinygpu::GestureEvent& event) override {
85 const int count = static_cast<int>(destinations_.size());
86 if (!isTapGesture(event.type) || count == 0) return false;
87 const int32_t slotHeight = this->bounds.h / count;
88 int index = (event.point.y - this->bounds.y) / slotHeight;
89 if (index < 0) index = 0;
90 if (index >= count) index = count - 1;
91 if (index != selectedIndex_) {
92 selectedIndex_ = index;
93 if (onChange) onChange(selectedIndex_);
94 }
95 return true;
96 }
97
98 private:
99 std::vector<NavDestination<RGB_T>> destinations_;
100 int selectedIndex_ = 0;
101};
102
106
107} // 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
Side navigation rail: a narrow vertical column of 3-7 exclusive destinations, each an icon with a lab...
Definition: NavigationRail.h:25
void addDestination(IconPainter< RGB_T > icon, const char *label)
Definition: NavigationRail.h:32
bool onGesture(const tinygpu::GestureEvent &event) override
Handles one gesture event. Returns true if this widget consumed it.
Definition: NavigationRail.h:84
void setSelectedIndex(int index)
Definition: NavigationRail.h:37
NavigationRail(Bounds bounds)
Definition: NavigationRail.h:28
void draw(tinygpu::ISurface< RGB_T > &target) override
Definition: NavigationRail.h:41
int selectedIndex() const
Definition: NavigationRail.h:36
std::function< void(int)> onChange
Definition: NavigationRail.h:30
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
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 h
Definition: Bounds.h:25
int32_t y
Definition: Bounds.h:23
One NavigationBar/NavigationRail destination: an icon plus a short label.
Definition: NavigationBar.h:16