forked from Dynamitos/Seele
72 lines
2.2 KiB
C++
72 lines
2.2 KiB
C++
#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: <ul role="tablist">
|
||
|
|
//
|
||
|
|
// 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<StyleClass... classes>
|
||
|
|
class TabBar : public Div<classes...> {
|
||
|
|
public:
|
||
|
|
using Div<classes...>::attr;
|
||
|
|
using Div<classes...>::style;
|
||
|
|
using Div<classes...>::markDirty;
|
||
|
|
using Div<classes...>::getTheme;
|
||
|
|
using Div<classes...>::children;
|
||
|
|
|
||
|
|
TabBar(Attributes attr, std::initializer_list<OElement> tabs)
|
||
|
|
: Div<classes...>(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<classes...>::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
|