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"
16
17
18
19
20
21
22
23
33 destinations_.push_back(NavDestination<RGB_T>{icon, label});
38 if (index >= 0 && index <
static_cast<
int>(destinations_.size())) selectedIndex_ = index;
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);
48 tinygpu::IFont<RGB_T>& font = *
this->theme().typography.label;
50 for (
int i = 0; i < count; ++i) {
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);
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);
66 target.fillRoundRect(toPx(pill.x), toPx(pill.y), toPx(pill.w), toPx(pill.h),
67 toPx(pill.h / 2),
this->theme().colors.secondaryContainer);
70 if (dest.icon !=
nullptr) {
72 kIconSize
, kIconSize
);
73 dest.icon(target, iconRect, contentColor, 2);
76 const size_t labelWidth = font.measureTextWidth(dest.label);
77 const int32_t labelX =
this->bounds.centerX() -
static_cast<int32_t>(labelWidth) / 2;
79 font.drawText(target,
static_cast<int16_t>(labelX),
static_cast<int16_t>(labelY), dest.label,
80 contentColor,
this->theme().colors.surface,
false);
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_);
99 std::vector<NavDestination<RGB_T>> destinations_;
100 int selectedIndex_ = 0;
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
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