TachyonUI 0.1.0
High-performance UI for building tools.
Loading...
Searching...
No Matches
custom_theme/custom_theme.cpp

Your own CSS classes next to the library's, and switching palettes and font size at runtime.

Your own CSS classes next to the library's, and switching palettes and font size at runtime. Its classes are in custom_theme.css. Walkthrough: Custom theme.

// Custom theme: the app adds its own CSS file next to the library's styles,
// uses its classes by name, and switches between the dark and light palettes.
#include <cstdio>
#include <string_view>
class ThemeApp : public tachyon::GUIApplication
{
public:
tachyon::Result<void> Initialize(const InitializeOptions& options = {}) override
{
// The library's rules first, then the app's (relative paths are next to the palette).
const std::string_view styleFiles[] = { "styles.css", TACHYON_EXAMPLE_DIR "/custom_theme.css" };
SetStyleFiles(styleFiles);
if (tachyon::Result<void> result = GUIApplication::Initialize(options); !result)
return result;
windowID = idManager.CreateStaticID();
return {};
}
void OnGUI() override
{
// The scope ends the window when it leaves, open or not.
auto window = Window(windowID, "Custom theme", tachyon::LayoutOverride{}.Size(400, 220));
if (!window)
return;
BeginVertical(tachyon::LayoutOverride{}.FlexGrow(1));
// Look the classes up every frame: a theme reload registers new styles.
Text("My themed app", theme.GetStyle("banner"));
BeginHorizontal();
Button("Dark", [this] { RequestReloadTheme("themes/dark.css", GetGlobalFontScale()); });
Button("Light", [this] { RequestReloadTheme("themes/light.css", GetGlobalFontScale()); });
EndHorizontal();
BeginHorizontal();
Button("Smaller text", [this] { RequestReloadTheme(GetThemePath(), GetGlobalFontScale() * 0.9f); });
Button("Larger text", [this] { RequestReloadTheme(GetThemePath(), GetGlobalFontScale() * 1.1f); });
EndHorizontal();
Button("Careful!", [] {}, theme.GetStyle("warning-button"));
EndVertical();
}
private:
};
int main()
{
ThemeApp app;
tachyon::Result<void> result = app.Initialize();
if (result)
result = app.Run();
app.Cleanup();
if (!result)
{
std::fprintf(stderr, "%s\n", result.GetError().message.c_str());
return 1;
}
return 0;
}
The application: owns the windows, the systems and the frame loop.
Definition gui_application.h:185
A value, or the Error saying why there is none.
Definition gui_result.h:36
const Error & GetError() const
The error; only valid when !HasValue().
Definition gui_result.h:61
std::string message
what went wrong, for logs and users
Definition gui_result.h:28
A 64-bit element ID whose bit layout depends on its IDMode.
Definition gui_id.h:23
static constexpr ID Invalid()
The "no element" ID (all bits zero); also the default for optional ID parameters.
Definition gui_id.h:66
Per-element overrides of style layout properties.
Definition gui_constraints.h:150
LayoutOverride & FlexGrow(int16_t v) &
Overrides the style's flex-grow / flex-shrink.
Definition gui_constraints.h:317
int2 Size() const
The explicit sizes; kAuto on axes without one.
Definition gui_constraints.h:205