resolve method

  1. @useResult
V resolve(
  1. Set<FVariant> variants
)

Returns most specific matching variant, or base if no constraints match.

Specificity is determined tier-by-tier from highest to lowest. At each tier, the constraint with more operands wins. This means a compound constraint with two tier-2 operands beats one with only one tier-2 operand, even if the latter has additional lower-tier operands. Typically platform < interaction < semantic.

final variants = FVariants(
  'base',
  variants: {
    [.hovered]: 'A',
    [.hovered.and(.focused)]: 'B',
    [.disabled]: 'C',
  },
);

variants.resolve({.hovered});           // 'A'
variants.resolve({.hovered, .focused}); // 'B' (more tier-1 operands)
variants.resolve({.hovered, .disabled}); // 'C' (tier 2 > tier 1)
variants.resolve({.pressed});           // 'base' (no match)

Implementation

@useResult
V resolve(Set<FVariant> variants) {
  K? constraint;
  V? variant;
  for (final MapEntry(key: current, :value) in this.variants.entries) {
    if (!current.satisfiedBy(variants)) {
      continue;
    }

    if (constraint == null || FVariantConstraint.max(constraint, current) != constraint) {
      constraint = current;
      variant = value;
    }
  }

  return variant ?? base;
}