#pragma once
#include "UI/Element/Div.h"
#include "UI/Element/Button.h"
#include "UI/Style/Class.h"
namespace Seele {
namespace UI {
// A tab bar: a horizontal row of tab buttons. One tab is highlighted as active.
// HTML analog:
//
// Usage:
// auto* tab1 = new Button<>(UI::Attributes{});
// auto* tab2 = new Button<>(UI::Attributes{});
// auto* tabs = new TabBar<>(UI::Attributes{}, {OElement(tab1), OElement(tab2)});
// // per-frame: tabs->setActiveTab(0)
//
// The tab bar takes a fixed set of Button children at construction (the
// fixed-children constraint). The active tab gets an accent color; the
// others get a dimmed color. The view handles onClick on each button to
// change the active tab.
template
class TabBar : public Div {
public:
using Div::attr;
using Div::style;
using Div::markDirty;
using Div::getTheme;
using Div::children;
TabBar(Attributes attr, std::initializer_list tabs)
: Div(std::move(attr), tabs) {
style.direction = FlexDirection::Row;
style.gap = 4;
}
virtual ~TabBar() override {}
void setActiveTab(int index) {
_activeTab = index;
markDirty();
}
int getActiveTab() const { return _activeTab; }
void applyStyle(Style parentStyle, const Theme& theme) override {
Div::applyStyle(parentStyle, theme);
style.direction = FlexDirection::Row;
style.gap = 4;
}
void applyOverrides() override {
const Theme& th = getTheme();
int i = 0;
for (auto& child : children) {
if (child == nullptr) {
continue;
}
auto& cs = child->getStyle();
if (i == _activeTab) {
cs.backgroundColor = th.color(ColorToken::Blue);
cs.textColor = th.color(ColorToken::White);
} else {
cs.backgroundColor = Vector(0.14f, 0.14f, 0.16f);
cs.textColor = th.color(ColorToken::Gray500);
}
i++;
}
}
private:
int _activeTab = 0;
};
} // namespace UI
} // namespace Seele