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"
15template <
typename RGB_T>
22
23
24
25
26
27
39 destinations_.push_back(NavDestination<RGB_T>{icon, label});
47 if (index >= 0 && index <
static_cast<
int>(destinations_.size())) selectedIndex_ = index;
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);
57 tinygpu::IFont<RGB_T>& font = *
this->theme().typography.label;
59 for (
int i = 0; i < count; ++i) {
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);
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,
75 target.fillRoundRect(toPx(pill.x), toPx(pill.y), toPx(pill.w), toPx(pill.h),
76 toPx(pill.h / 2),
this->theme().colors.secondaryContainer);
79 if (dest.icon !=
nullptr) {
81 kIconSize
, kIconSize
);
82 dest.icon(target, iconRect, contentColor, 2);
85 const size_t labelWidth = font.measureTextWidth(dest.label);
86 const int32_t labelX = slotX + slotWidth / 2 -
static_cast<int32_t>(labelWidth) / 2;
88 font.drawText(target,
static_cast<int16_t>(labelX),
static_cast<int16_t>(labelY), dest.label,
89 contentColor,
this->theme().colors.surface,
false);
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_);
108 std::vector<NavDestination<RGB_T>> destinations_;
109 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
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
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