#pragma once #include "UI/Element/Div.h" #include "UI/Element/Text.h" #include "UI/Style/Class.h" namespace Seele { namespace UI { // An accordion: a collapsible section with a header that toggles the body. // HTML analog:
// // The Accordion is a vertical container with a header (a clickable label) // and a body (arbitrary children). The body's visibility is toggled by the // owning view in response to the header's onClick. The Accordion itself does // not track open/closed state — the view does. // // Usage: // auto* header = new Text("Section Title"); // auto* body = new Div(UI::Attributes{}); // auto* acc = new Accordion<>(UI::Attributes{}, OElement(header), OElement(body)); // // The view owns the "open" state and toggles body's visibility: // // if (clicked) { bodyVisible = !bodyVisible; acc->setBodyVisible(bodyVisible); } template class Accordion : public Div { public: using Div::attr; using Div::style; using Div::markDirty; using Div::getTheme; using Div::children; Accordion(Attributes attr, OElement header, OElement body) : Div(std::move(attr), std::initializer_list{}) { _header = PElement(header); _body = PElement(body); children.add(std::move(header)); children.add(std::move(body)); if (children.size() >= 2) { children.back()->setParent(this); } style.backgroundColor = Vector(0.10f, 0.10f, 0.12f); style.gap = 0; if (_header != nullptr) { auto* h = _header.getHandle(); if (h != nullptr) { auto& hs = h->getStyle(); hs.backgroundColor = Vector(0.14f, 0.14f, 0.16f); hs.paddingLeft = 12; hs.paddingRight = 12; hs.paddingTop = 8; hs.paddingBottom = 8; } } } virtual ~Accordion() override {} void setBodyVisible(bool visible) { if (_body != nullptr) { auto* b = _body.getHandle(); if (b != nullptr) { b->getStyle().display = visible ? Display::Flex : Display::Hidden; b->markDirty(); } } } bool isBodyVisible() const { if (_body == nullptr) { return false; } auto* b = _body.getHandle(); return b != nullptr && b->getStyle().display == Display::Flex; } PElement getHeader() const { return _header; } PElement getBody() const { return _body; } void applyStyle(Style parentStyle, const Theme& theme) override { Div::applyStyle(parentStyle, theme); style.backgroundColor = Vector(0.10f, 0.10f, 0.12f); style.gap = 0; } private: PElement _header; PElement _body; }; } // namespace UI } // namespace Seele