TinyMaterialDesign
Loading...
Searching...
No Matches
Tooltip.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 Small floating label, shown briefly above an anchor widget.
13 *
14 * Not driven by hover (this library targets touchscreens, not pointers) -
15 * call showFor(anchorBounds, text) yourself, typically from a long-press
16 * gesture on the widget it explains, and it hides itself automatically
17 * after its duration via update(). Add once with Screen::addFixedWidget()
18 * (last, so it draws on top); starts invisible so it costs nothing while
19 * idle.
20 */
21template <typename RGB_T = TINYMD_DEFAULT_RGB_T>
22class Tooltip : public Widget<RGB_T> {
23 public:
24 Tooltip() { this->visible = false; }
25
26 /// Shows `text` centered just above `anchor` for `durationMs`.
27 void showFor(Bounds anchor, const char* text, uint32_t durationMs = 1500) {
28 text_ = text != nullptr ? text : "";
29 durationMs_ = durationMs;
30
31 tinygpu::IFont<RGB_T>* font = this->theme_ != nullptr ? this->theme().typography.label : nullptr;
32 const int32_t textWidth = font != nullptr ? static_cast<int32_t>(font->measureTextWidth(text_.c_str())) : 0;
33 const int32_t textHeight = font != nullptr ? static_cast<int32_t>(font->getHeight(1)) : 12;
34 const int32_t pad = 8;
35 const int32_t w = textWidth + pad * 2;
36 const int32_t h = textHeight + pad;
37 this->bounds = Bounds(anchor.centerX() - w / 2, anchor.y - h - 6, w, h);
38
39 this->visible = true;
40 started_ = false;
41 }
42
43 void hide() { this->visible = false; }
44
45 void draw(tinygpu::ISurface<RGB_T>& target) override {
46 const RGB_T background = this->theme().colors.onSurface;
47 const RGB_T foreground = this->theme().colors.surface;
48 const size_t radius = toPx(this->theme().shape.small);
49
50 target.fillRoundRect(toPx(this->bounds.x), toPx(this->bounds.y), toPx(this->bounds.w),
51 toPx(this->bounds.h), radius, background);
52
53 tinygpu::IFont<RGB_T>& font = *this->theme().typography.label;
54 const size_t textWidth = font.measureTextWidth(text_.c_str());
55 const int32_t textX = this->bounds.centerX() - static_cast<int32_t>(textWidth) / 2;
56 const int32_t textY = this->bounds.centerY() - static_cast<int32_t>(font.getHeight(1)) / 2;
57 font.drawText(target, static_cast<int16_t>(textX), static_cast<int16_t>(textY), text_.c_str(),
58 foreground, background, false);
59 }
60
61 bool update(uint32_t nowMs) override {
62 if (!this->visible) return false;
63 if (!started_) {
64 started_ = true;
65 startMs_ = nowMs;
66 return true;
67 }
68 if (nowMs - startMs_ >= durationMs_) {
69 this->visible = false;
70 return true;
71 }
72 return false;
73 }
74
75 private:
76 std::string text_;
77 uint32_t durationMs_ = 1500;
78 bool started_ = false;
79 uint32_t startMs_ = 0;
80};
81
85
86} // namespace tinymd
Small floating label, shown briefly above an anchor widget.
Definition: Tooltip.h:22
void showFor(Bounds anchor, const char *text, uint32_t durationMs=1500)
Shows text centered just above anchor for durationMs.
Definition: Tooltip.h:27
void draw(tinygpu::ISurface< RGB_T > &target) override
Definition: Tooltip.h:45
Tooltip()
Definition: Tooltip.h:24
bool update(uint32_t nowMs) override
Definition: Tooltip.h:61
void hide()
Definition: Tooltip.h:43
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
Bounds(int32_t x, int32_t y, int32_t w, int32_t h)
Definition: Bounds.h:28
int32_t centerX() const
Definition: Bounds.h:32
int32_t y
Definition: Bounds.h:23