TinyMaterialDesign
Loading...
Searching...
No Matches
TabBar.h
Go to the documentation of this file.
1#pragma once
2#include <functional>
3#include <string>
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
12namespace tinymd {
13
14/// @brief Row of evenly-spaced exclusive-selection labels (primary tabs), with a
15/// sliding indicator under the selected one. Tabs are fixed text strings
16/// added via addTab() rather than separate child widgets, so the whole row
17/// is one hit-testable/drawable Widget.
18template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
19class TabBar : public Widget<RGB_T> {
20 public:
21 TabBar() = default;
22 explicit TabBar(Bounds bounds) { this->bounds = bounds; }
23
24 /// Fired with the new selected index whenever a different tab is tapped.
25 std::function<void(int)> onChange;
26
27 void addTab(const char* label) { tabs_.push_back(label); }
28 void clearTabs() { tabs_.clear(); }
29
30 int selectedIndex() const { return selectedIndex_; }
31 void setSelectedIndex(int index) {
32 if (index >= 0 && index < static_cast<int>(tabs_.size())) selectedIndex_ = index;
33 }
34
35 void draw(tinygpu::ISurface<RGB_T>& target) override {
36 const int count = static_cast<int>(tabs_.size());
37 if (count == 0) return;
38 const LinearLayout slots(this->bounds, LayoutAxis::Horizontal, /*spacing=*/0);
39 tinygpu::IFont<RGB_T>& font = *this->theme().typography.label;
40
41 for (int i = 0; i < count; ++i) {
42 const Bounds slot = slots.itemRect(i, count);
43 const bool selected = i == selectedIndex_;
44 RGB_T textColor = selected ? this->theme().colors.primary : this->theme().colors.onSurfaceVariant;
45 if (!this->enabled) textColor = blend(textColor, this->theme().colors.surface, 0.5f);
46
47 const size_t textWidth = font.measureTextWidth(tabs_[i]);
48 const int32_t textX = slot.centerX() - static_cast<int32_t>(textWidth) / 2;
49 const int32_t textY = this->bounds.centerY() - static_cast<int32_t>(font.getHeight(1)) / 2;
50 font.drawText(target, static_cast<int16_t>(textX), static_cast<int16_t>(textY), tabs_[i],
51 textColor, this->theme().colors.surface, false);
52 }
53
54 constexpr int32_t kIndicatorHeight = 3;
55 const Bounds selectedSlot = slots.itemRect(selectedIndex_, count);
56 RGB_T indicatorColor =
57 this->enabled ? this->theme().colors.primary
58 : blend(this->theme().colors.primary, this->theme().colors.surface, 0.5f);
59 target.fillRect(toPx(selectedSlot.x), toPx(this->bounds.bottom() - kIndicatorHeight),
60 toPx(selectedSlot.w), toPx(kIndicatorHeight), indicatorColor);
61 }
62
63 bool onGesture(const tinygpu::GestureEvent& event) override {
64 const int count = static_cast<int>(tabs_.size());
65 if (!isTapGesture(event.type) || count == 0) return false;
66 const int32_t tabWidth = this->bounds.w / count;
67 int index = (event.point.x - this->bounds.x) / tabWidth;
68 if (index < 0) index = 0;
69 if (index >= count) index = count - 1;
70 if (index != selectedIndex_) {
71 selectedIndex_ = index;
72 if (onChange) onChange(selectedIndex_);
73 }
74 return true;
75 }
76
77 private:
78 std::vector<const char*> tabs_;
79 int selectedIndex_ = 0;
80};
81
85
86} // 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
Row of evenly-spaced exclusive-selection labels (primary tabs), with a sliding indicator under the se...
Definition: TabBar.h:19
void addTab(const char *label)
Definition: TabBar.h:27
bool onGesture(const tinygpu::GestureEvent &event) override
Handles one gesture event. Returns true if this widget consumed it.
Definition: TabBar.h:63
void clearTabs()
Definition: TabBar.h:28
void setSelectedIndex(int index)
Definition: TabBar.h:31
void draw(tinygpu::ISurface< RGB_T > &target) override
Definition: TabBar.h:35
int selectedIndex() const
Definition: TabBar.h:30
TabBar()=default
std::function< void(int)> onChange
Fired with the new selected index whenever a different tab is tapped.
Definition: TabBar.h:25
TabBar(Bounds bounds)
Definition: TabBar.h:22
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