#pragma once #include "UI/Element/Div.h" #include "UI/Element/Text.h" #include "UI/Style/Class.h" namespace Seele { namespace UI { // A tooltip: an element that shows a tooltip bubble when hovered. // HTML analog: title="..." attribute / CSS :hover tooltip // // The Tooltip wraps a child element and, when the child is hovered, shows // a floating label with the tooltip text. The tooltip is rendered as a // child element (a Text with a dark background) that is hidden until hover. // // Usage: // auto* target = new Button<>(UI::Attributes{}); // auto* tip = new Tooltip<>(UI::Attributes{}, OElement(target), "Helpful info"); // // NOTE: the tooltip child is added to the tree at construction. Its position // is set by the view (absolute positioning) — the Tooltip itself does not // compute the tooltip's position. template class Tooltip : public Div { public: using Div::attr; using Div::style; using Div::markDirty; using Div::getTheme; using Div::children; Tooltip(Attributes attr, OElement target, std::string text) : Div(std::move(attr), std::initializer_list{}) { // Wrap the target children.add(std::move(target)); if (children.size() > 0) { children.back()->setParent(this); } _tipText = std::move(text); // Build the tooltip bubble: a dark panel with the text auto* bubble = new Text(_tipText); { auto& bs = bubble->getStyle(); bs.backgroundColor = Vector(0.05f, 0.05f, 0.06f); bs.paddingLeft = 8; bs.paddingRight = 8; bs.paddingTop = 4; bs.paddingBottom = 4; bs.display = Display::Hidden; } _bubble = PElement(bubble); children.add(OElement(bubble)); children.back()->setParent(this); } virtual ~Tooltip() override {} void setText(std::string text) { _tipText = std::move(text); if (_bubble != nullptr) { if (auto* t = dynamic_cast*>(_bubble.getHandle())) { t->setText(_tipText); } } markDirty(); } void setHovered(bool hovered) override { _hovered = hovered; if (_bubble != nullptr) { if (auto* b = dynamic_cast(_bubble.getHandle())) { if (b != nullptr) { b->getStyle().display = hovered ? Display::Flex : Display::Hidden; b->markDirty(); } } } } void applyStyle(Style parentStyle, const Theme& theme) override { Div::applyStyle(parentStyle, theme); } void applyOverrides() override { const Theme& th = getTheme(); if (_bubble != nullptr) { if (auto* b = dynamic_cast*>(_bubble.getHandle())) { if (b != nullptr) { b->getStyle().textColor = th.color(ColorToken::White); } } } } private: std::string _tipText; PElement _bubble; bool _hovered = false; }; } // namespace UI } // namespace Seele