TachyonUI 0.1.0
High-performance UI for building tools.
Loading...
Searching...
No Matches
Using styles from C++

Classes from the theme

theme is the GUIApplication member that holds the loaded theme. theme.GetStyle("class") returns the StyleRef for a class from any loaded file. A StyleRef is the handle a control takes to know which style to draw with:

Text("My themed app", theme.GetStyle("banner"));
Button("Careful!", [] {}, theme.GetStyle("warning-button"));
  • For an undefined class, GetStyle returns an empty StyleRef and logs a warning once.
  • theme.FindStyle("class") returns a Result<StyleRef> with an error instead.

    if (tachyon::Result<tachyon::StyleRef> banner = theme.FindStyle("banner"))
    Text("My themed app", *banner);
    else
    Text(banner.GetError().message);
    A value, or the Error saying why there is none.
    Definition gui_result.h:36
  • theme.GetColor("--accent", fallback) reads a palette color as a color4f.

    • It returns fallback when the palette doesn't define the color.

    For example, with a tachyon::StyleRef accentStyle; member:

    tachyon::Style accentBox;
    accentBox.states[tachyon::ElementState::None].backgroundColor =
    tachyon::ColorStyle::Create(theme.GetColor("--accent", tachyon::color4f{ 0.4f, 0.6f, 1.0f, 1.0f }));
    accentStyle = styleManager.Register(accentBox);
    static ColorStyle Create(color4b color)
    A SolidColor style.
    @ None
    Definition gui_style.h:43
    A complete style, as parsed from CSS or built in code: layout properties plus the visual properties o...
    Definition gui_style.h:687
    States states
    one per state slot
    Definition gui_style.h:711
    Definition gui_base_types.h:395
  • Library controls use styles registered by name, such as:
    • theme.labelStyleRef
    • theme.windowStyleRef
    • theme.regularControls.button
  • To restyle them, edit their classes: .label, .window, .button.
  • A reload clears every registered style, making old StyleRefs stale.
    • Look classes up each frame, or store them again in OnStylesReloaded (see How a theme loads).

Styles built in code

Fill in a style and register it with the StyleManager, the styleManager member of GUIApplication:

tachyon::StyleRef ref = styleManager.Register(s);
@ Hover
under the mouse (CSS: :hover)
Definition gui_style.h:45
Per-corner values (border radius).
Definition gui_style.h:90
Per-side values of the box model (margin, padding, border widths).
Definition gui_style.h:64
A registered style, as StyleManager::Register returns it: indices into the shared style pools,...
Definition gui_style_ref.h:25
BoxCorners borderRadius
CSS px.
Definition gui_style.h:717
BoxEdges borderWidths
CSS px.
Definition gui_style.h:716
BoxEdges padding
CSS px.
Definition gui_style.h:715
Definition gui_base_types.h:384
  • Style::states has 33 slots, indexed by ElementState.
    • Each slot is set separately.
    • A slot is any combination of Active, Hover, Focused, Selected and Selection, for example ElementState::Active | ElementState::Hover.
    • Disabled is a slot of its own, alone.
  • Register them again in OnStylesReloaded.
  • A style built in code doesn't get the font scale.

Text settings:

Where Holds
Style::textLayoutStyle (all states) Direction, wrapping, alignment, baseline, overflow
state textStyle Font, size, spacing, line height, language, decoration lines (underLine, overLine, ...) with thickness, offset, line style
state textColor CSS color. Unset means transparent text.
state decorationColors Indexed by DecorationLine. Unset takes the text color.
  • A state that changes only colors keeps the text layout.
  • A state that changes textStyle lays the text out again.
for (tachyon::StyleState& state : s.states)
{
state.textStyle.fontSize = 14.0f;
state.textColor = tachyon::ColorStyle::Create(tachyon::color4b{ 220, 223, 228, 255 });
}
uint16_t thickness
line thickness in CSS px; 0 = no line
Definition gui_style_text.h:234
float fontSize
CSS: font-size, in CSS px.
Definition gui_style_text.h:262
TextDecoration underLine
under the alphabetic baseline
Definition gui_style_text.h:279
@ Under
TextStyle::underLine.
Definition gui_style_text.h:222
The visual properties of one element state (normal, :hover, :active, ...).
Definition gui_style.h:656
ColorStyle textColor
CSS: color; none = transparent.
Definition gui_style.h:661
TextStyle textStyle
font and text metrics
Definition gui_style.h:657

UI scale

The UI scale is how much larger than CSS pixels the UI is drawn: the main window's display scale times SetUIScale.

  • Styles are written in CSS pixels.
  • They are stored in device pixels, the pixels of the screen.
  • StyleManager::Register scales them by the UI scale:
    • Font sizes, letter and word spacing, and px line heights scale exactly.
    • Everything else rounds to whole pixels: margins, padding, sizes, gaps, radii, shadows, icon sizes, fixed grid tracks.
    • A set border or text decoration stays at least 1 px.
    • auto stays auto.
Call Use
Register(style, tachyon::StyleUnits::Pixels) Style already in device pixels
styleManager.ToPixels(style) Scaled copy of a style
styleManager.Px(n) CSS px to whole device px; non-zero stays at least 1

For example, a style built in device pixels, with a tachyon::StyleRef padded; member:

p.padding = tachyon::BoxEdges(uint16_t(styleManager.Px(4)), uint16_t(styleManager.Px(8)));
padded = styleManager.Register(p, tachyon::StyleUnits::Pixels);
@ Pixels
Definition gui_style_manager.h:32

Previous: Properties | CSS and theming