Resolver Struct

This page doesn't need deliberate learning, just read to have an impression. When developing views, directly use Resolver Derive Macro is enough.

Resolver is Flor framework's internal style/layout resolver, used to manage state variants, unit conversion and calculation cache. View authors usually don't need to directly operate Resolver, instead through Resolver Derive Macro automatically generate implementation.

use flor::view::resolver::Resolver;

Overview

Resolver core responsibilities:

  • Store style variants under different ControlState
  • Perform unit conversion through UnitResolver
  • Cache calculation results, avoid repeated calculation
  • Support multi-layer style stacking (layer)

View authors only need to know: Resolver will automatically select corresponding style variant based on current view state (Normal, Hover, Focus, Active, Disabled), and calculate final result.

new_with_compute_func

new_with_compute_func is Resolver's constructor entry, needs to pass ViewId and calculation function:

pub fn new_with_compute_func(view_id: ViewId, compute_func: F) -> Self

Calculation function signature:

F: for<'a> Fn(&UnitResolver, &ResolverComputeMap<K, V>) -> D

It receives unit resolver and merged style variants, returns calculated data.

Example: LayoutResolver

LayoutResolver is an instance of Resolver, used to calculate Taffy layout style:

use flor::view::resolver::LayoutResolver;

impl LayoutResolver {
    pub fn new(view_id: ViewId) -> Self {
        Self::new_with_compute_func(view_id, computed_layout)
    }
}

computed_layout function and LayoutResolver type are both automatically generated by #[derive(Resolver)] derive macro. It will traverse all layout variants, call UnitResolver for unit conversion, finally generate taffy::Style.

Cache Mechanism

Resolver internally maintains cache:

  • cache_data: Cache calculation results by ControlState
  • dirty: Dirty flag, used to determine whether need to recalculate

When style variant changes, cache will be cleared. Getting data will automatically trigger calculation.

View authors don't need to care about cache details, only need to know: multiple calls to get_data_clone or get_data_borrow won't repeat calculation.

Usage Method

View authors shouldn't manually create Resolver. Correct way is:

  1. Define style enum, for example Layout or custom style type
  2. Use #[derive(Resolver)] derive macro to automatically generate Resolver implementation
  3. Call generated new method in view constructor

See Resolver Derive Macro for details.