157 lines
5.5 KiB
C++
157 lines
5.5 KiB
C++
#pragma once
|
|
#include "UI/Style/Style.h"
|
|
#include "UI/Style/Theme.h"
|
|
#include "Asset/AssetRegistry.h"
|
|
#include "Asset/FontAsset.h"
|
|
#include "MinimalEngine.h"
|
|
#include <string_view>
|
|
|
|
namespace Seele {
|
|
namespace UI {
|
|
// a style class is anything with a static apply(Style&, const Theme&)
|
|
template <typename C> concept StyleClass = requires(Style& s, const Theme& t) { C::apply(s, t); };
|
|
|
|
// --- size ---
|
|
template <uint32 w, DimensionType widthType> struct WidthClass {
|
|
static void apply(Style& s, const Theme&) {
|
|
s.width = w;
|
|
s.widthType = widthType;
|
|
}
|
|
};
|
|
using W_Full = WidthClass<100, DimensionType::Percent>;
|
|
template <uint32 h> struct HeightClass {
|
|
static void apply(Style& s, const Theme&) {
|
|
s.height = h;
|
|
s.heightType = DimensionType::Pixel;
|
|
}
|
|
};
|
|
using H_Full = HeightClass<100>;
|
|
|
|
// --- display ---
|
|
template <OuterDisplayType outer, InnerDisplayType inner> struct DisplayClass {
|
|
static void apply(Style& s, const Theme&) {
|
|
s.outerDisplay = outer;
|
|
s.innerDisplay = inner;
|
|
}
|
|
};
|
|
using Hidden = DisplayClass<OuterDisplayType::Hidden, InnerDisplayType::Flow>;
|
|
using Block = DisplayClass<OuterDisplayType::Block, InnerDisplayType::Flow>;
|
|
using Inline = DisplayClass<OuterDisplayType::Inline, InnerDisplayType::Flow>;
|
|
|
|
// --- fonts (looked up at style() time; fonts must be loaded before the first
|
|
// System::style() call, see the invariant in System.h) ---
|
|
// a string literal can't be a C++ template argument, so font *names* are carried
|
|
// as runtime data on Style::fontName by a font class tag (e.g. UI::Font_Arial in
|
|
// Fonts.h); Text::applyStyle resolves the name to a PFontAsset (Theme::defaultFont
|
|
// as fallback). the font scale below is what remains a pure static class.
|
|
|
|
// tailwind text scale: (fontSize, lineHeight) in px, scaled by Theme at style() time
|
|
template <uint32 fontSize, uint32 lineHeight> struct FontSizeClass {
|
|
static void apply(Style& s, const Theme& t) {
|
|
s.fontSize = t.font(fontSize);
|
|
s.lineHeight = t.font(lineHeight);
|
|
}
|
|
};
|
|
|
|
template <ColorToken token> struct BackgroundColorClass {
|
|
static void apply(Style& s, const Theme& t) { s.backgroundColor = t.color(token); }
|
|
};
|
|
template <ColorToken token> struct TextColorClass {
|
|
static void apply(Style& s, const Theme& t) { s.textColor = t.color(token); }
|
|
};
|
|
using BG_White = BackgroundColorClass<ColorToken::White>;
|
|
using BG_Gray950 = BackgroundColorClass<ColorToken::Gray950>;
|
|
using BG_Gray900 = BackgroundColorClass<ColorToken::Gray900>;
|
|
using BG_Gray800 = BackgroundColorClass<ColorToken::Gray800>;
|
|
using BG_Gray700 = BackgroundColorClass<ColorToken::Gray700>;
|
|
using BG_Gray600 = BackgroundColorClass<ColorToken::Gray600>;
|
|
using BG_Red = BackgroundColorClass<ColorToken::Red>;
|
|
using BG_Green = BackgroundColorClass<ColorToken::Green>;
|
|
using BG_Blue = BackgroundColorClass<ColorToken::Blue>;
|
|
using BG_Amber = BackgroundColorClass<ColorToken::Amber>;
|
|
using C_White = TextColorClass<ColorToken::White>;
|
|
using C_Gray400 = TextColorClass<ColorToken::Gray400>;
|
|
using C_Gray500 = TextColorClass<ColorToken::Gray500>;
|
|
using C_Red = TextColorClass<ColorToken::Red>;
|
|
using C_Green = TextColorClass<ColorToken::Green>;
|
|
using C_Blue = TextColorClass<ColorToken::Blue>;
|
|
|
|
// --- spacing (themed: scaled by Theme::spacingScale for DPI/accessibility) ---
|
|
// a side left as ~0U (4294967295) is left untouched
|
|
template <uint32 ml, uint32 mr, uint32 mt, uint32 mb> struct MarginClass {
|
|
static void apply(Style& s, const Theme& t) {
|
|
if constexpr (ml != ~0U)
|
|
s.marginLeft = t.spacing(ml);
|
|
if constexpr (mr != ~0U)
|
|
s.marginRight = t.spacing(mr);
|
|
if constexpr (mt != ~0U)
|
|
s.marginTop = t.spacing(mt);
|
|
if constexpr (mb != ~0U)
|
|
s.marginBottom = t.spacing(mb);
|
|
}
|
|
};
|
|
template <uint32 ml, uint32 mr, uint32 mt, uint32 mb> struct PaddingClass {
|
|
static void apply(Style& s, const Theme& t) {
|
|
if constexpr (ml != ~0U)
|
|
s.paddingLeft = t.spacing(ml);
|
|
if constexpr (mr != ~0U)
|
|
s.paddingRight = t.spacing(mr);
|
|
if constexpr (mt != ~0U)
|
|
s.paddingTop = t.spacing(mt);
|
|
if constexpr (mb != ~0U)
|
|
s.paddingBottom = t.spacing(mb);
|
|
}
|
|
};
|
|
template <uint32 g> struct GapClass {
|
|
static void apply(Style& s, const Theme& t) { s.gap = t.spacing(g); }
|
|
};
|
|
|
|
// curated tailwind spacing scale (px @ scale 1.0).
|
|
// intermediate steps: declare a local MarginClass<N,...>/PaddingClass<N,...> alias
|
|
// in your own namespace — StyleClass is a concept, not a closed set.
|
|
#define SPACE(n) \
|
|
using M_##n = MarginClass<n, n, n, n>; \
|
|
using P_##n = PaddingClass<n, n, n, n>; \
|
|
using M_X##n = MarginClass<n, n, ~0U, ~0U>; \
|
|
using M_Y##n = MarginClass<~0U, ~0U, n, n>; \
|
|
using P_X##n = PaddingClass<n, n, ~0U, ~0U>; \
|
|
using P_Y##n = PaddingClass<~0U, ~0U, n, n>; \
|
|
using G_##n = GapClass<n>;
|
|
|
|
SPACE(0)
|
|
SPACE(1)
|
|
SPACE(2)
|
|
SPACE(4)
|
|
SPACE(6)
|
|
SPACE(8)
|
|
SPACE(10)
|
|
SPACE(12)
|
|
SPACE(16)
|
|
SPACE(20)
|
|
SPACE(24)
|
|
SPACE(32)
|
|
SPACE(40)
|
|
SPACE(48)
|
|
SPACE(64)
|
|
SPACE(80)
|
|
|
|
// tailwind text scale: (fontSize, lineHeight) @ scale 1.0.
|
|
// names must be valid identifiers (no leading digit), and the macro body ends in ';'
|
|
// or the next token gets glued to the using-declaration.
|
|
#define TEXT_STEP(name, size, line) using Text_##name = FontSizeClass<size, line>;
|
|
TEXT_STEP(XS, 12, 16)
|
|
TEXT_STEP(SM, 14, 20)
|
|
TEXT_STEP(BASE, 16, 24)
|
|
TEXT_STEP(LG, 18, 28)
|
|
TEXT_STEP(XL, 20, 28)
|
|
TEXT_STEP(XXL, 24, 32)
|
|
TEXT_STEP(3XL, 30, 36)
|
|
TEXT_STEP(4XL, 36, 40)
|
|
TEXT_STEP(5XL, 48, 48)
|
|
TEXT_STEP(6XL, 60, 60)
|
|
TEXT_STEP(7XL, 72, 72)
|
|
TEXT_STEP(8XL, 96, 96)
|
|
|
|
} // namespace UI
|
|
} // namespace Seele
|