TinyMaterialDesign
Loading...
Searching...
No Matches
Chip.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
10namespace tinymd {
11
12/// @brief Small rounded-rect label, optionally a toggle (filter chip) rather than
13/// a one-shot action (assist chip).
14template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
15class Chip : public Widget<RGB_T> {
16 public:
17 Chip() = default;
18 Chip(Bounds bounds, const char* label, bool selectable = false, bool selected = false)
19 : label_(label), selectable_(selectable), selected_(selected) {
20 this->bounds = bounds;
21 }
22
23 /// Fires on every tap.
24 std::function<void()> onClick;
25 /// Fires only when selectable == true, with the new selected state.
26 std::function<void(bool)> onChange;
27
28 bool selected() const { return selected_; }
29 void setSelected(bool selected) { selected_ = selected; }
30
31 void draw(tinygpu::ISurface<RGB_T>& target) override {
32 RGB_T background = selected_ ? this->theme().colors.secondaryContainer : this->theme().colors.surfaceVariant;
33 RGB_T foreground = selected_ ? this->theme().colors.onSecondaryContainer : this->theme().colors.onSurfaceVariant;
34 RGB_T outline = this->theme().colors.outline;
35 if (!this->enabled) {
36 background = blend(background, this->theme().colors.surface, 0.5f);
37 foreground = blend(foreground, this->theme().colors.surface, 0.5f);
38 outline = blend(outline, this->theme().colors.surface, 0.5f);
39 }
40
41 const size_t radius = toPx(this->bounds.h / 2);
42 target.fillRoundRect(toPx(this->bounds.x), toPx(this->bounds.y), toPx(this->bounds.w),
43 toPx(this->bounds.h), radius, background);
44 if (!selected_) {
45 target.drawRoundRect(toPx(this->bounds.x), toPx(this->bounds.y), toPx(this->bounds.w),
46 toPx(this->bounds.h), radius, outline);
47 }
48
49 tinygpu::IFont<RGB_T>& font = *this->theme().typography.label;
50 const size_t textWidth = font.measureTextWidth(label_.c_str());
51 const size_t textHeight = font.getHeight(1);
52 const int32_t textX = this->bounds.centerX() - static_cast<int32_t>(textWidth) / 2;
53 const int32_t textY = this->bounds.centerY() - static_cast<int32_t>(textHeight) / 2;
54 font.drawText(target, static_cast<int16_t>(textX), static_cast<int16_t>(textY),
55 label_.c_str(), foreground, background, false);
56 }
57
58 bool onGesture(const tinygpu::GestureEvent& event) override {
59 if (!isTapGesture(event.type)) return false;
60 if (selectable_) {
61 selected_ = !selected_;
62 if (onChange) onChange(selected_);
63 }
64 if (onClick) onClick();
65 return true;
66 }
67
68 private:
69 std::string label_;
70 bool selectable_ = false;
71 bool selected_ = false;
72};
73
77
78} // namespace tinymd
Small rounded-rect label, optionally a toggle (filter chip) rather than a one-shot action (assist chi...
Definition: Chip.h:15
bool onGesture(const tinygpu::GestureEvent &event) override
Handles one gesture event. Returns true if this widget consumed it.
Definition: Chip.h:58
std::function< void(bool)> onChange
Fires only when selectable == true, with the new selected state.
Definition: Chip.h:26
Chip()=default
Chip(Bounds bounds, const char *label, bool selectable=false, bool selected=false)
Definition: Chip.h:18
bool selected() const
Definition: Chip.h:28
void draw(tinygpu::ISurface< RGB_T > &target) override
Definition: Chip.h:31
void setSelected(bool selected)
Definition: Chip.h:29
std::function< void()> onClick
Fires on every tap.
Definition: Chip.h:24
Base class every TinyMaterialDesign widget derives from.
Definition: Widget.h:58
Definition: AnchorLayout.h:4
Axis-aligned pixel rectangle used for widget bounds and hit-testing.
Definition: Bounds.h:21