psychic-ui 0.0.0
Loading...
Searching...
No Matches
psychic_ui::StyleManager Class Reference

Public Member Functions

 StyleManager (const StyleManager &)=delete
StyleManager & operator= (const StyleManager &)=delete
 StyleManager (StyleManager &&)=delete
StyleManager & operator= (StyleManager &&)=delete
void reset ()
template<typename T>
void loadStyleSheet (bool reset=false)
template<class S>
StyleManager & skinSet ()
StyleManager & skinSet (std::unique_ptr< SkinSet > skinSet, ColorScheme scheme=ColorScheme::Unknown)
StyleManager & skinSet (std::unique_ptr< SkinSet > skinSet, Theme theme)
const SkinSet * skinSet () const
template<class SkinT>
std::shared_ptr< SkinT > skin (std::string_view variant="") const
std::size_t skinsGeneration () const
std::size_t rulesGeneration () const
const Theme & theme () const
StyleManager & theme (const Theme &theme)
Density density () const
bool density (Density density)
const Sizes & sizes () const
bool valid () const
void setValid ()
void invalidate ()
StyleManager & loadFont (const std::string &name, const std::string &path)
StyleManager & resourceRoot (std::filesystem::path root)
const std::filesystem::path & resourceRoot () const
sk_sp< SkTypeface > font (const std::string &name) const
sk_sp< skia::textlayout::FontCollection > fontCollection ()
Style & style (std::string selector)
Style computeStyle (const Div *component)
std::vector< std::string > matchedSelectors (const Div *component)

Static Public Member Functions

static std::shared_ptr< StyleManager > getInstance ()
static sk_sp< SkFontMgr > fontManager ()

Protected Member Functions

void loadStyleSheet (std::type_index type, const std::function< std::unique_ptr< StyleSheet >()> &make)
void clearRules ()
void reloadRules ()
void installSkinSet (std::unique_ptr< SkinSet > skinSet)
void match (const Div *component)
void matchCandidates (const std::vector< std::size_t > &candidates, const Div *component)
void matchCandidates (const std::unordered_map< std::string, std::vector< std::size_t > > &index, const std::string &key, const Div *component)

Protected Attributes

std::unordered_map< std::string, std::unique_ptr< StyleDeclaration > > _declarations {}
std::vector< StyleDeclaration * > _rules {}
StyleDeclaration * _universal {nullptr}
std::unordered_map< std::string, std::vector< std::size_t > > _rulesById {}
std::unordered_map< std::string, std::vector< std::size_t > > _rulesByClass {}
std::unordered_map< std::string, std::vector< std::size_t > > _rulesByTag {}
std::vector< std::size_t > _otherRules {}
std::vector< std::pair< int, std::size_t > > _matches {}
std::filesystem::path _resourceRoot {executableDirectory()}
std::unordered_map< std::string, sk_sp< SkTypeface > > _fonts {}
sk_sp< skia::textlayout::FontCollection > _fontCollection {nullptr}
std::vector< std::unique_ptr< SkinSet > > _skinSets {}
SkinRegistry _skins {}
std::size_t _skinsGeneration {0}
std::size_t _rulesGeneration {0}
bool _valid {false}
Theme _theme {themes::pivotalDark}
Density _density {Density::Pointer}
std::vector< std::pair< std::type_index, std::unique_ptr< StyleSheet > > > _styleSheets {}

Member Function Documentation

◆ clearRules()

void psychic_ui::StyleManager::clearRules ( )
protected

Drop every rule, keeping the fonts, the skins and the stylesheets

◆ computeStyle()

Style psychic_ui::StyleManager::computeStyle ( const Div * component)

The style a Div gets, from (lowest priority first) the * rule, what it inherits from its parent's computed style, the rules matching it, its inline style, and its default style for what is still unset

◆ density() [1/2]

Density psychic_ui::StyleManager::density ( ) const
inline

The density the stylesheets size controls for: Pointer until a window goes to touch mode

◆ density() [2/2]

bool psychic_ui::StyleManager::density ( Density density)

Switch densities, like themes: the stylesheets load again and read the new sizes(). Nothing happens when the density doesn't change.

Returns
Whether it changed

◆ font()

sk_sp< SkTypeface > psychic_ui::StyleManager::font ( const std::string & name) const

The font loaded under that name, otherwise the system font of that family, otherwise the system default

◆ fontCollection()

sk_sp< skia::textlayout::FontCollection > psychic_ui::StyleManager::fontCollection ( )

What text layout finds fonts in: the loaded fonts by name, then the system's fonts, which also fill in the characters a font lacks. Remade after a font is loaded.

◆ fontManager()

sk_sp< SkFontMgr > psychic_ui::StyleManager::fontManager ( )
static

The platform's font manager (Fontconfig on Linux, CoreText on macOS), shared by every style manager: it loads font files and finds the system's fonts

◆ getInstance()

std::shared_ptr< StyleManager > psychic_ui::StyleManager::getInstance ( )
static

The style manager windows use unless given another one

◆ installSkinSet()

void psychic_ui::StyleManager::installSkinSet ( std::unique_ptr< SkinSet > skinSet)
protected

Replace the skin set chain and the skins, bumping the generation; the rules are reloaded by the caller

◆ invalidate()

void psychic_ui::StyleManager::invalidate ( )
inline

Recompute every div's style on the next style pass, without touching the rules

◆ loadFont()

StyleManager & psychic_ui::StyleManager::loadFont ( const std::string & name,
const std::string & path )

Loads a font file under a name, which fontFamily styles use. A relative path is resolved against resourceRoot(), an absolute one is used as it is

◆ loadStyleSheet()

template<typename T>
void psychic_ui::StyleManager::loadStyleSheet ( bool reset = false)
inline

Load a stylesheet: its rules apply after (and over, on equal weight) those loaded before it. The style manager keeps it, to load it again when the theme changes; loading a stylesheet type that is already loaded runs it again in its place.

◆ match()

void psychic_ui::StyleManager::match ( const Div * component)
protected

Fill _matches with the rules matching a Div, in the order they apply

◆ matchCandidates()

void psychic_ui::StyleManager::matchCandidates ( const std::vector< std::size_t > & candidates,
const Div * component )
protected

Add the candidates of one index bucket that match the Div to _matches

◆ matchedSelectors()

std::vector< std::string > psychic_ui::StyleManager::matchedSelectors ( const Div * component)

The selectors of the rules matching a Div, in the order they apply. For debugging.

◆ reloadRules()

void psychic_ui::StyleManager::reloadRules ( )
protected

Clear the rules, then load the skin sets' rules and the stylesheets', in order

◆ reset()

void psychic_ui::StyleManager::reset ( )

Forget the fonts, the skin set, the loaded stylesheets and every rule. The rules' styles are emptied but stay allocated, so a Style & obtained from style() stays valid: declaring the same selector again (usually by loading a stylesheet) brings that rule back.

◆ resourceRoot()

StyleManager & psychic_ui::StyleManager::resourceRoot ( std::filesystem::path root)

The directory relative resource paths (fonts) resolve against: the executable's directory by default, where psychic_ui_copy_resources copies res/. Set it before installing a skin set: a font is loaded once

◆ rulesGeneration()

std::size_t psychic_ui::StyleManager::rulesGeneration ( ) const
inline

Bumped every time every style is to be computed again: a rule set or changed, a stylesheet or a font loaded, the rules loaded again (a theme, a density or a skin set). Styles computed in such a pass change at once, without transitions: those follow a div's own state (classes, pseudo-classes, its inline style).

◆ sizes()

const Sizes & psychic_ui::StyleManager::sizes ( ) const
inline

The theme's sizes for the current density

◆ skin()

template<class SkinT>
std::shared_ptr< SkinT > psychic_ui::StyleManager::skin ( std::string_view variant = "") const
inline

A new skin implementing SkinT from the skin set: the variant (from the set, else its fallback sets), else SkinT's default. nullptr only without a skin set, or when no set in the chain implements SkinT.

◆ skinSet() [1/3]

template<class S>
StyleManager & psychic_ui::StyleManager::skinSet ( )
inline

Use a skin set, with its default theme: its skins (and its fallback sets') replace the current set's, its rules load first, under the other stylesheets, and every component hatches a new skin on the next style pass. Loading a skin set with loadStyleSheet does the same.

◆ skinSet() [2/3]

const SkinSet * psychic_ui::StyleManager::skinSet ( ) const
inline

The skin set in use, nullptr before one is set

◆ skinSet() [3/3]

StyleManager & psychic_ui::StyleManager::skinSet ( std::unique_ptr< SkinSet > skinSet,
Theme theme )

Use a skin set with that theme rather than its own

◆ skinsGeneration()

std::size_t psychic_ui::StyleManager::skinsGeneration ( ) const
inline

Bumped every time the skin set changes: components hatch a new skin when it does

◆ style()

Style & psychic_ui::StyleManager::style ( std::string selector)

Get the rule for a selector, declaring it if needed. Selectors are case insensitive. Rules apply like CSS: by specificity (weight), and on equal specificity the rule declared last wins.

manager->style("Button:hover").backgroundColor(rgb(0x3C414F));
Exceptions
std::invalid_argumentwhen the selector can't be parsed

◆ theme() [1/2]

const Theme & psychic_ui::StyleManager::theme ( ) const
inline

The theme stylesheets read (manager->theme().palette.accent), Pivotal Dark until another is set

◆ theme() [2/2]

StyleManager & psychic_ui::StyleManager::theme ( const Theme & theme)

Switch themes: the rules are cleared and the loaded stylesheets load again, in the same order, with the new values. Fonts and skins stay; the next style pass recomputes every div.

Member Data Documentation

◆ _declarations

std::unordered_map<std::string, std::unique_ptr<StyleDeclaration> > psychic_ui::StyleManager::_declarations {}
protected

Every rule ever declared, by lowercase selector. Never shrinks, so that a Style & handed out stays valid.

◆ _matches

std::vector<std::pair<int, std::size_t> > psychic_ui::StyleManager::_matches {}
protected

Weight and index in _rules of the rules matching the Div being computed, kept to reuse its memory

◆ _rules

std::vector<StyleDeclaration *> psychic_ui::StyleManager::_rules {}
protected

The rules in effect, in declaration order: on equal weight, a later rule wins

◆ _rulesById

std::unordered_map<std::string, std::vector<std::size_t> > psychic_ui::StyleManager::_rulesById {}
protected

The rules (indices in _rules) by what their rightmost part requires: an id, else its first class, else its tag. A Div only needs testing against the rules indexed under its id, classes and tags, plus those that require none of them (pseudo-classes only).

◆ _skinSets

std::vector<std::unique_ptr<SkinSet> > psychic_ui::StyleManager::_skinSets {}
protected

The skin set in use, last, after the sets it falls back on

◆ _styleSheets

std::vector<std::pair<std::type_index, std::unique_ptr<StyleSheet> > > psychic_ui::StyleManager::_styleSheets {}
protected

The loaded stylesheets, in load order, with their type so that each type is loaded once

◆ _universal

StyleDeclaration* psychic_ui::StyleManager::_universal {nullptr}
protected

The * rule, if declared


The documentation for this class was generated from the following files: