TinyMaterialDesign
Loading...
Searching...
No Matches
AppBar.h
Go to the documentation of this file.
1#pragma once
2#include <string>
3
4#include "TinyGPU/Color/RGB565.h"
5#include "TinyGPU/Color/RGB666.h"
6#include "TinyGPU/Color/RGB888.h"
7#include "TinyMaterialDesign/Core/Widget.h"
8
9namespace tinymd {
10
11/**
12 * @brief Top app bar: a title plus optional leading/trailing icon widgets
13 * (typically IconButton).
14 *
15 * `leading`/`trailing` are children AppBar forwards draw()/update()/
16 * onGesture() to internally - do not also add them to Screen directly, or
17 * they'd be hit-tested twice. Position them with leadingRect()/
18 * trailingRect() before attaching.
19 */
20template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
21class AppBar : public Widget<RGB_T> {
22 public:
23 AppBar() = default;
24 AppBar(Bounds bounds, const char* title) : title_(title) { this->bounds = bounds; }
25
26 Widget<RGB_T>* leading = nullptr;
27 Widget<RGB_T>* trailing = nullptr;
28
29 void setTitle(const char* title) { title_ = title; }
30
31 /// Overrides the theme's surface/onSurface colors for this bar only - e.g.
32 /// a blue title bar while the rest of the screen stays on the default
33 /// theme. Call clearColorOverride() to go back to theme.colors.surface.
34 void setColorOverride(RGB_T background, RGB_T foreground) {
35 hasColorOverride_ = true;
36 backgroundOverride_ = background;
37 foregroundOverride_ = foreground;
38 }
39 void clearColorOverride() { hasColorOverride_ = false; }
40
41 /// Suggested square icon slot at the bar's left edge.
43 const int32_t size = this->bounds.h - 8;
44 return Bounds(this->bounds.x + 4, this->bounds.y + (this->bounds.h - size) / 2, size, size);
45 }
46 /// Suggested square icon slot at the bar's right edge.
48 const int32_t size = this->bounds.h - 8;
49 return Bounds(this->bounds.right() - size - 4, this->bounds.y + (this->bounds.h - size) / 2,
50 size, size);
51 }
52
53 void draw(tinygpu::ISurface<RGB_T>& target) override {
54 const RGB_T background = hasColorOverride_ ? backgroundOverride_ : this->theme().colors.surface;
55 const RGB_T foreground = hasColorOverride_ ? foregroundOverride_ : this->theme().colors.onSurface;
56
57 target.fillRect(toPx(this->bounds.x), toPx(this->bounds.y), toPx(this->bounds.w),
58 toPx(this->bounds.h), background);
59
60 tinygpu::IFont<RGB_T>& font = *this->theme().typography.title;
61 const size_t textHeight = font.getHeight(1);
62 const int32_t textY = this->bounds.centerY() - static_cast<int32_t>(textHeight) / 2;
63 const int32_t textX = leading != nullptr ? leadingRect().right() + 8 : this->bounds.x + 16;
64 font.drawText(target, static_cast<int16_t>(textX), static_cast<int16_t>(textY),
65 title_.c_str(), foreground, background, false);
66
67 // See Widget::setThemeTint() - lets e.g. a standard-variant IconButton
68 // (whose own default color is tuned for theme.colors.surface, not
69 // whatever this bar's background actually is) read clearly against
70 // this bar by default, without needing its own explicit
71 // setColorOverride() call in the sketch. A no-op for any child that
72 // doesn't override setThemeTint(), and loses to that child's own
73 // explicit override when it has one (e.g. a status-dependent icon).
74 //
75 // leading/trailing are plain public pointer fields the sketch assigns
76 // directly (no registration hook to catch that assignment - see the
77 // class comment), so the theme is pushed into them lazily here, right
78 // before each draw(), rather than once at assignment time.
79 if (leading != nullptr && leading->visible) {
80 leading->setTheme(this->theme());
81 leading->setThemeTint(background, foreground);
82 leading->draw(target);
83 }
84 if (trailing != nullptr && trailing->visible) {
85 trailing->setTheme(this->theme());
86 trailing->setThemeTint(background, foreground);
87 trailing->draw(target);
88 }
89 }
90
91 bool onGesture(const tinygpu::GestureEvent& event) override {
92 if (leading != nullptr && leading->enabled &&
93 leading->bounds.contains(event.point.x, event.point.y)) {
94 return leading->onGesture(event);
95 }
96 if (trailing != nullptr && trailing->enabled &&
97 trailing->bounds.contains(event.point.x, event.point.y)) {
98 return trailing->onGesture(event);
99 }
100 return false;
101 }
102
103 bool update(uint32_t nowMs) override {
104 bool changed = false;
105 if (leading != nullptr) changed |= leading->update(nowMs);
106 if (trailing != nullptr) changed |= trailing->update(nowMs);
107 return changed;
108 }
109
110 private:
111 std::string title_;
112 bool hasColorOverride_ = false;
113 RGB_T backgroundOverride_;
114 RGB_T foregroundOverride_;
115};
116
120
121} // namespace tinymd
Top app bar: a title plus optional leading/trailing icon widgets (typically IconButton).
Definition: AppBar.h:21
void clearColorOverride()
Definition: AppBar.h:39
Widget< RGB_T > * leading
Definition: AppBar.h:26
bool onGesture(const tinygpu::GestureEvent &event) override
Handles one gesture event. Returns true if this widget consumed it.
Definition: AppBar.h:91
Bounds trailingRect() const
Suggested square icon slot at the bar's right edge.
Definition: AppBar.h:47
AppBar()=default
Bounds leadingRect() const
Suggested square icon slot at the bar's left edge.
Definition: AppBar.h:42
void draw(tinygpu::ISurface< RGB_T > &target) override
Definition: AppBar.h:53
Widget< RGB_T > * trailing
Definition: AppBar.h:27
void setTitle(const char *title)
Definition: AppBar.h:29
bool update(uint32_t nowMs) override
Definition: AppBar.h:103
void setColorOverride(RGB_T background, RGB_T foreground)
Definition: AppBar.h:34
AppBar(Bounds bounds, const char *title)
Definition: AppBar.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