Files
Seele/src/Engine/UI/Element/Accordion.h
T
2026-11-10 18:18:42 +00:00

91 lines
2.9 KiB
C++

#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: <details><summary>
//
// 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<Font_Arial, Text_SM>("Section Title");
// auto* body = new Div<FlexCol>(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<StyleClass... classes>
class Accordion : public Div<classes...> {
public:
using Div<classes...>::attr;
using Div<classes...>::style;
using Div<classes...>::markDirty;
using Div<classes...>::getTheme;
using Div<classes...>::children;
Accordion(Attributes attr, OElement header, OElement body)
: Div<classes...>(std::move(attr), std::initializer_list<OElement>{}) {
_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<classes...>::applyStyle(parentStyle, theme);
style.backgroundColor = Vector(0.10f, 0.10f, 0.12f);
style.gap = 0;
}
private:
PElement _header;
PElement _body;
};
} // namespace UI
} // namespace Seele