TinyMaterialDesign
Loading...
Searching...
No Matches
ListItem.h
Go to the documentation of this file.
1#pragma once
2#include <functional>
3#include <string>
4
5#include "TinyGPU/Color/RGB565.h"
6#include "TinyGPU/Color/RGB666.h"
7#include "TinyGPU/Color/RGB888.h"
8#include "TinyMaterialDesign/Core/Widget.h"
9#include "TinyMaterialDesign/Widgets/IconButton.h"
10#include "TinyMaterialDesign/Widgets/Label.h" // TypographyRole, fontForTypographyRole()
11
12namespace tinymd {
13
14/**
15 * @brief Tappable row: optional leading icon + title, with a selected state.
16 *
17 * The building block for Drawer (Drawer.h), but usable standalone for any
18 * settings-style list. Selected items draw as a filled pill (Material's
19 * navigation-drawer look); unselected items are flat.
20 */
21template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
22class ListItem : public Widget<RGB_T> {
23 public:
24 ListItem() = default;
25 ListItem(Bounds bounds, const char* title, IconPainter<RGB_T> icon = nullptr)
26 : title_(title), icon_(icon) {
27 this->bounds = bounds;
28 }
29
30 std::function<void()> onClick;
31
32 void setTitle(const char* title) { title_ = title; }
33 void setIcon(IconPainter<RGB_T> icon) { icon_ = icon; }
34
35 bool selected() const { return selected_; }
36 void setSelected(bool selected) { selected_ = selected; }
37
38 /// Text size - kBody (the default, matching this row's original fixed
39 /// size) down to kLabel for a denser list (e.g. a navigation drawer
40 /// with several rows that don't need to be as prominent as body text).
41 void setTypographyRole(TypographyRole role) { role_ = role; }
42
43 void draw(tinygpu::ISurface<RGB_T>& target) override {
44 RGB_T background = selected_ ? this->theme().colors.secondaryContainer : this->theme().colors.surface;
45 RGB_T foreground = selected_ ? this->theme().colors.onSecondaryContainer : this->theme().colors.onSurfaceVariant;
46 RGB_T textColor = selected_ ? this->theme().colors.onSecondaryContainer : this->theme().colors.onSurface;
47 if (!this->enabled) {
48 background = blend(background, this->theme().colors.surface, 0.5f);
49 foreground = blend(foreground, this->theme().colors.surface, 0.5f);
50 textColor = blend(textColor, this->theme().colors.surface, 0.5f);
51 }
52
53 const int32_t margin = 8;
54 const Bounds pill = this->bounds.inset(margin);
55
56 if (selected_) {
57 target.fillRoundRect(toPx(pill.x), toPx(pill.y), toPx(pill.w), toPx(pill.h),
58 toPx(this->theme().shape.full), background);
59 }
60
61 const int32_t pad = 16;
62 const int32_t iconSize = pill.h - 16;
63 int32_t textX = pill.x + pad;
64 if (icon_ != nullptr) {
65 const Bounds iconRect(pill.x + pad, pill.y + (pill.h - iconSize) / 2, iconSize, iconSize);
66 icon_(target, iconRect, foreground, /*thickness=*/2);
67 textX = iconRect.right() + pad;
68 }
69
70 tinygpu::IFont<RGB_T>& font = *fontForTypographyRole(role_, this->theme());
71 const int32_t textY = pill.centerY() - static_cast<int32_t>(font.getHeight(1)) / 2;
72 font.drawText(target, static_cast<int16_t>(textX), static_cast<int16_t>(textY),
73 title_.c_str(), textColor, background, false);
74 }
75
76 bool onGesture(const tinygpu::GestureEvent& event) override {
77 if (!isTapGesture(event.type)) return false;
78 if (onClick) onClick();
79 return true;
80 }
81
82 private:
83 std::string title_;
84 IconPainter<RGB_T> icon_ = nullptr;
85 bool selected_ = false;
87};
88
92
93} // namespace tinymd
Tappable row: optional leading icon + title, with a selected state.
Definition: ListItem.h:22
void setTypographyRole(TypographyRole role)
Definition: ListItem.h:41
ListItem(Bounds bounds, const char *title, IconPainter< RGB_T > icon=nullptr)
Definition: ListItem.h:25
bool onGesture(const tinygpu::GestureEvent &event) override
Handles one gesture event. Returns true if this widget consumed it.
Definition: ListItem.h:76
void setIcon(IconPainter< RGB_T > icon)
Definition: ListItem.h:33
bool selected() const
Definition: ListItem.h:35
void draw(tinygpu::ISurface< RGB_T > &target) override
Definition: ListItem.h:43
void setTitle(const char *title)
Definition: ListItem.h:32
void setSelected(bool selected)
Definition: ListItem.h:36
std::function< void()> onClick
Definition: ListItem.h:30
ListItem()=default
Base class every TinyMaterialDesign widget derives from.
Definition: Widget.h:58
Definition: AnchorLayout.h:4
TypographyRole
Definition: Label.h:11
Axis-aligned pixel rectangle used for widget bounds and hit-testing.
Definition: Bounds.h:21
int32_t right() const
Definition: Bounds.h:30
Bounds(int32_t x, int32_t y, int32_t w, int32_t h)
Definition: Bounds.h:28
int32_t h
Definition: Bounds.h:25
int32_t y
Definition: Bounds.h:23
int32_t x
Definition: Bounds.h:22