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:
- Variables:
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)
Methods
__init__(theme[, focus_color])Clear the style resolution cache.
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:
- 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:
- 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 (
tupleofintorNone) – 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