wijjit.styling.resolver.StyleResolver

class wijjit.styling.resolver.StyleResolver(theme, focus_color=None)[source]

Resolves element styles using theme and element state.

This class implements CSS-like style resolution with support for: - Base element styles from theme - Pseudo-class styles (:focus, :hover, :disabled, etc.) - Inline style overrides - Style cascade and merging - Style caching for performance optimization - Global focus color override

Parameters:
  • theme (Theme) – Theme to use for style resolution

  • focus_color (tuple of int, optional) – Global focus color override (R, G, B). When set, all :focus styles will have their fg_color overridden with this color.

Variables:
  • theme (Theme) – Current theme

  • focus_color (tuple or None) – Global focus color override

  • _style_cache (dict) – Cache of resolved styles keyed by (base_class, css_classes_key, state_key)

Notes

Style resolution is cached based on element type, CSS classes, and state. The cache is automatically cleared when the theme is changed. Inline overrides bypass the cache since they are unique per call.

Examples

Basic style resolution:

>>> from wijjit.styling.theme import DefaultTheme
>>> resolver = StyleResolver(DefaultTheme())
>>> style = resolver.resolve_style_by_class('button')
>>> style.bg_color  # From theme
(0, 100, 200)

Resolve with pseudo-class:

>>> style = resolver.resolve_style_by_class('button', pseudo_class='focus')
>>> style.bold  # button:focus is bold
True

Resolve with inline overrides:

>>> override = {'fg_color': (255, 255, 0)}
>>> style = resolver.resolve_style_by_class(
...     'button', inline_overrides=override
... )
>>> style.fg_color  # Overridden
(255, 255, 0)
__init__(theme, focus_color=None)[source]
Parameters:
Return type:

None

Methods

__init__(theme[, focus_color])

clear_cache()

Clear the style resolution cache.

get_theme()

Get the current theme.

resolve_style(element[, base_class, ...])

Resolve final style for an element with CSS class support.

resolve_style_by_class(class_name[, ...])

Resolve style by class name without an element instance.

set_focus_color(color)

Set the global focus color override.

set_theme(theme)

Change the theme used for resolution.

resolve_style(element, base_class=None, inline_overrides=None)[source]

Resolve final style for an element with CSS class support.

Parameters:
  • element (Element) – Element to resolve style for

  • base_class (str, optional) – Base element class (e.g., ‘button’). If None, inferred from element type

  • inline_overrides (dict, optional) – Dict of style properties to override (e.g., {‘fg_color’: (255, 0, 0)})

Returns:

Resolved style with all cascades and overrides applied

Return type:

Style

Notes

Resolution order (lowest to highest specificity): 1. Base element type style (inferred from element type) 2. User CSS classes (from element.classes, e.g., .btn-primary) 3. Base element pseudo-classes (e.g., button:focus) 4. CSS class pseudo-classes (e.g., .btn-primary:focus) 5. Inline overrides

Each layer is merged on top of the previous using Style.merge().

Examples

Basic resolution:

>>> from wijjit.elements.input.button import Button
>>> from wijjit.styling.theme import DefaultTheme
>>> resolver = StyleResolver(DefaultTheme())
>>> button = Button('Click me')
>>> style = resolver.resolve_style(button)

With CSS classes:

>>> button = Button('Click me', classes="btn-primary")
>>> style = resolver.resolve_style(button)
>>> # Applies both 'button' and '.btn-primary' styles

With focus:

>>> button.focused = True
>>> style = resolver.resolve_style(button)
>>> # Applies button:focus and .btn-primary:focus

With override:

>>> style = resolver.resolve_style(
...     button,
...     inline_overrides={'fg_color': (255, 255, 0)}
... )
>>> style.fg_color
(255, 255, 0)
resolve_style_by_class(class_name, pseudo_class=None, inline_overrides=None)[source]

Resolve style by class name without an element instance.

Parameters:
  • class_name (str) – Element class name (e.g., ‘button’, ‘input’)

  • pseudo_class (str, optional) – Pseudo-class to apply (e.g., ‘focus’, ‘hover’)

  • inline_overrides (dict, optional) – Dict of style properties to override

Returns:

Resolved style

Return type:

Style

Notes

This is a simpler version of resolve_style() that doesn’t require an element instance. Useful for manual style resolution or testing.

Examples

Resolve button style:

>>> from wijjit.styling.theme import DefaultTheme
>>> resolver = StyleResolver(DefaultTheme())
>>> style = resolver.resolve_style_by_class('button')
>>> style.bg_color
(0, 100, 200)

With pseudo-class:

>>> style = resolver.resolve_style_by_class('button', pseudo_class='focus')
>>> style.bold
True

With override:

>>> style = resolver.resolve_style_by_class(
...     'button',
...     inline_overrides={'fg_color': (255, 0, 0)}
... )
>>> style.fg_color
(255, 0, 0)
set_theme(theme)[source]

Change the theme used for resolution.

Parameters:

theme (Theme) – New theme to use

Return type:

None

Notes

This allows runtime theme switching. After calling this method, subsequent style resolutions will use the new theme. The style cache is automatically cleared when the theme changes.

Examples

>>> from wijjit.styling.theme import DefaultTheme, DarkTheme
>>> resolver = StyleResolver(DefaultTheme())
>>> resolver.set_theme(DarkTheme())
>>> resolver.theme.name
'dark'
set_focus_color(color)[source]

Set the global focus color override.

Parameters:

color (tuple of int or None) – RGB color tuple (R, G, B) to use for all focused elements, or None to use theme defaults

Return type:

None

Notes

When set, this color overrides the fg_color of all :focus styles. The style cache is automatically cleared when the focus color changes.

Examples

>>> resolver = StyleResolver(DefaultTheme())
>>> resolver.set_focus_color((255, 128, 0))  # Orange focus
>>> resolver.set_focus_color(None)  # Use theme defaults
get_theme()[source]

Get the current theme.

Returns:

Current theme

Return type:

Theme

Examples

>>> from wijjit.styling.theme import DefaultTheme
>>> resolver = StyleResolver(DefaultTheme())
>>> theme = resolver.get_theme()
>>> theme.name
'default'
clear_cache()[source]

Clear the style resolution cache.

Notes

This method clears all cached style resolutions. Useful for testing or when element states have changed significantly and you want to ensure fresh style computation.

The cache is also automatically cleared when set_theme() is called.

Return type:

None