Skip to main content

vizia_core/style/
mod.rs

1//! Styling determines the appearance of a view.
2//!
3//! # Styling Views
4//! Vizia provides two ways to style views:
5//! - Inline
6//! - Shared
7//!
8//! ## Inline Styling
9//! Inline styling refers to setting the style and layout properties of a view using view [modifiers](crate::modifiers).
10//! ```
11//! # use vizia_core::prelude::*;
12//! # let cx = &mut Context::default();
13//! Element::new(cx).background_color(Color::red());
14//! ```
15//! Properties set inline affect only the modified view and override any shared styling for the same property.
16//!
17//! ## Shared Styling
18//! Shared styling refers to setting the style and layout properties using css rules.
19//! ```
20//! # use vizia_core::prelude::*;
21//! # let cx = &mut Context::default();
22//! Element::new(cx).class("foo");
23//! ```
24//! ```css
25//! .foo {
26//!     background-color: red;
27//! }
28//! ```
29//! Rules defined in css can apply to many views but are overridden by inline properties on a view.
30//!
31//! ### Adding Stylesheets
32//! To add a css string to an application, use [`add_theme()`](crate::context::Context::add_theme()) on [`Context`].
33//! This can be used with the `include_str!()` macro to embed an external stylesheet file into the application binary when compiled.
34//! Alternatively a constant string literal can be used to embed the CSS in the application.
35//!
36//! ```
37//! # use vizia_core::prelude::*;
38//! # let cx = &mut Context::default();
39//!
40//! const STYLE: &str = r#"
41//!     .foo {
42//!         background-color: red;
43//!     }
44//! "#;
45//!
46//! cx.add_stylesheet(STYLE);
47//!
48//! Element::new(cx).class("foo");
49//! ```
50//!
51//! To add an external css stylesheet which is read from a file at runtime, use [`add_stylesheet()`](crate::context::Context::add_stylesheet()) on [`Context`].
52//! Stylesheets added this way can be hot-reloaded by pressing the F5 key in the application window.
53//!
54//! ```
55//! # use vizia_core::prelude::*;
56//! # let cx = &mut Context::default();
57//!
58//! cx.add_stylesheet("path/to/stylesheet.css");
59//!
60//! Element::new(cx).class("foo");
61//! ```
62
63use accesskit::SortDirection;
64use hashbrown::{HashMap, HashSet};
65use indexmap::IndexMap;
66use log::warn;
67use std::fmt::Debug;
68use std::hash::{DefaultHasher, Hash, Hasher};
69use std::ops::Range;
70use vizia_style::selectors::parser::{AncestorHashes, Selector};
71
72use crate::prelude::*;
73use crate::storage::animatable_var_set::AnimatableVarSet;
74
75pub use vizia_style::{
76    Alignment, Angle, AspectRatio, BackgroundImage, BackgroundRepeat, BackgroundSize,
77    BorderStyleKeyword, ClipPath, Color, CssRule, CursorIcon, Direction, Display, Filter,
78    FontFamily, FontSize, FontSizeKeyword, FontSlant, FontVariation, FontWeight, FontWeightKeyword,
79    FontWidth, GenericFontFamily, Gradient, HorizontalPosition, HorizontalPositionKeyword,
80    LayoutWrap, Length, LengthOrPercentage, LengthValue, LetterSpacing, LineClamp, LineDirection,
81    LineHeight, LinearGradient, Matrix, Opacity, Overflow, PointerEvents, Position, PositionType,
82    RGBA, Scale, Shadow, TextAlign, TextDecorationLine, TextDecorationStyle, TextOverflow,
83    TextStroke, TextStrokeStyle, Transform, Transition, Translate, VerticalPosition,
84    VerticalPositionKeyword, Visibility,
85};
86
87use cssparser::Token as CssToken;
88use vizia_style::{
89    BlendMode, EasingFunction, KeyframeSelector, ParserOptions, Property, Selectors, StyleSheet,
90    TokenList, TokenOrValue, Variable,
91};
92
93mod rule;
94pub(crate) use rule::Rule;
95
96mod pseudoclass;
97pub(crate) use pseudoclass::*;
98
99mod transform;
100pub(crate) use transform::*;
101
102pub(crate) mod invalidation;
103use invalidation::InvalidationMap;
104
105use crate::animation::{AnimationState, Interpolator, Keyframe, TimingFunction};
106use crate::storage::animatable_set::AnimatableSet;
107use crate::storage::style_set::StyleSet;
108use bitflags::bitflags;
109use vizia_id::IdManager;
110use vizia_storage::SparseSet;
111
112bitflags! {
113    /// Describes the capabilities of a view with respect to user interaction.
114    #[derive(Debug, Clone, Copy)]
115    pub(crate) struct Abilities: u8 {
116        // Whether a view will be included in hit tests and receive mouse input events.
117        const HOVERABLE = 1 << 0;
118        // Whether a view can be focused to receive keyboard events.
119        const FOCUSABLE = 1 << 1;
120        // Whether a view can be checked.
121        const CHECKABLE = 1 << 2;
122        // Whether a view can be focused via keyboard navigation.
123        const NAVIGABLE = 1 << 3;
124        // Whether a view can be dragged during a drag and drop.
125        const DRAGGABLE = 1 << 4;
126    }
127}
128
129impl Default for Abilities {
130    fn default() -> Abilities {
131        Abilities::HOVERABLE
132    }
133}
134
135bitflags! {
136    pub(crate) struct SystemFlags: u8 {
137        const RELAYOUT = 1;
138        const RESTYLE = 1 << 1;
139        const REFLOW = 1 << 2;
140        const REDRAW = 1 << 3;
141        const RETRANSFORM = 1 << 4;
142        const RECLIP = 1 << 5;
143        const REACCESS = 1 << 6;
144    }
145}
146
147impl Default for SystemFlags {
148    fn default() -> Self {
149        SystemFlags::all()
150    }
151}
152
153/// An enum which represents an image or a gradient.
154#[derive(Debug, Clone, PartialEq)]
155pub enum ImageOrGradient {
156    /// Represents an image by name.
157    Image(String),
158    /// A gradient.
159    Gradient(Gradient),
160}
161
162/// A font-family.
163#[derive(Debug, Clone, PartialEq, Eq)]
164pub enum FamilyOwned {
165    /// A generic font-family.
166    Generic(GenericFontFamily),
167    /// A named front-family.
168    Named(String),
169}
170
171impl AsRef<str> for FamilyOwned {
172    fn as_ref(&self) -> &str {
173        match self {
174            FamilyOwned::Generic(generic) => match generic {
175                GenericFontFamily::Serif => "serif",
176                GenericFontFamily::SansSerif => "sans-serif",
177                GenericFontFamily::Cursive => todo!(),
178                GenericFontFamily::Fantasy => todo!(),
179                GenericFontFamily::Monospace => "Cascadia Mono",
180            },
181            FamilyOwned::Named(family) => family.as_str(),
182        }
183    }
184}
185
186pub(crate) struct StyleRule {
187    pub(crate) selector: Selector<Selectors>,
188    /// The ancestor hashes associated with the selector.
189    pub(crate) hashes: AncestorHashes,
190}
191
192impl StyleRule {
193    pub(crate) fn new(selector: Selector<Selectors>) -> Self {
194        let hashes = AncestorHashes::new(&selector, vizia_style::QuirksMode::NoQuirks);
195        Self { selector, hashes }
196    }
197}
198
199/// Stores the style properties of all entities in the application.
200#[derive(Default)]
201pub struct Style {
202    pub(crate) rule_manager: IdManager<Rule>,
203
204    // Creates and destroys animation ids
205    pub(crate) animation_manager: IdManager<Animation>,
206    pub(crate) animations: HashMap<String, Animation>,
207    // List of animations to be started on the next frame
208    pub(crate) pending_animations: Vec<(Entity, Animation, Duration, Duration)>,
209
210    // List of rules
211    pub(crate) rules: IndexMap<Rule, StyleRule>,
212    /// Incremented whenever the style rules change. Rule ids are reused, so this is used to
213    /// detect when data derived from the rules is stale.
214    pub(crate) rules_generation: u64,
215    /// Rules which define custom properties (variables).
216    pub(crate) variable_rules: HashSet<Rule>,
217    /// Describes how the rules depend on entity state, built lazily from the rules.
218    invalidation: Option<InvalidationMap>,
219    /// Parents whose layout subtrees have been marked for restyle since the last style pass.
220    restyle_sibling_parents: HashSet<Entity>,
221
222    /// The rules matched by each entity in the last style pass, used to skip relinking style data
223    /// when the matched rules of an entity haven't changed.
224    pub(crate) matched_rules: HashMap<Entity, Vec<(Rule, u32)>>,
225    /// The rules generation the matched rules were computed for.
226    pub(crate) matched_rules_generation: u64,
227    /// The tree and inline style structure generations at the last inline inheritance pass.
228    pub(crate) inline_inheritance_key: Option<(u64, u64)>,
229    /// The tree and inline style structure generations at the last style pass.
230    pub(crate) style_structure_key: Option<(u64, u64)>,
231
232    pub(crate) default_font: Vec<FamilyOwned>,
233
234    // CSS Selector Properties
235    pub(crate) element: SparseSet<u32>,
236    pub(crate) ids: SparseSet<String>,
237    pub(crate) classes: SparseSet<HashSet<String>>,
238    pub(crate) pseudo_classes: SparseSet<PseudoClassFlags>,
239    pub(crate) disabled: StyleSet<bool>,
240    pub(crate) abilities: SparseSet<Abilities>,
241
242    // Accessibility Properties
243    pub(crate) name: StyleSet<String>,
244    pub(crate) role: SparseSet<Role>,
245    pub(crate) live: SparseSet<Live>,
246    pub(crate) labelled_by: SparseSet<String>,
247    pub(crate) described_by: SparseSet<String>,
248    pub(crate) controls: SparseSet<String>,
249    pub(crate) active_descendant: SparseSet<String>,
250    pub(crate) expanded: SparseSet<bool>,
251    pub(crate) selected: SparseSet<bool>,
252    pub(crate) multiselectable: SparseSet<bool>,
253    pub(crate) sort_direction: SparseSet<SortDirection>,
254    pub(crate) level: SparseSet<usize>,
255    pub(crate) size_of_set: SparseSet<usize>,
256    pub(crate) position_in_set: SparseSet<usize>,
257    pub(crate) hidden: SparseSet<bool>,
258    pub(crate) orientation: SparseSet<Orientation>,
259    pub(crate) text_value: SparseSet<String>,
260    pub(crate) numeric_value: SparseSet<f64>,
261
262    // Visibility
263    pub(crate) visibility: StyleSet<Visibility>,
264
265    // Opacity
266    pub(crate) opacity: AnimatableVarSet<Opacity>,
267
268    // Z Order
269    pub(crate) z_index: StyleSet<i32>,
270
271    // Controls whether an entity should ignore ancestor clipping during drawing.
272    pub(crate) ignore_clipping: StyleSet<bool>,
273
274    // Clipping
275    pub(crate) clip_path: AnimatableSet<ClipPath>,
276
277    // Overflow
278    pub(crate) overflowx: StyleSet<Overflow>,
279    pub(crate) overflowy: StyleSet<Overflow>,
280
281    // Filters
282    pub(crate) filter: AnimatableSet<Filter>,
283    pub(crate) backdrop_filter: AnimatableSet<Filter>,
284    /// Entities which may have a filter or backdrop filter, i.e. a superset of the entities for
285    /// which either set returns a value. Used by the draw system to avoid walking the tree.
286    pub(crate) filter_entities: HashSet<Entity>,
287
288    pub(crate) blend_mode: StyleSet<BlendMode>,
289
290    // Transform
291    pub(crate) transform: AnimatableSet<Vec<Transform>>,
292    pub(crate) transform_origin: AnimatableSet<Translate>,
293    pub(crate) translate: AnimatableSet<Translate>,
294    pub(crate) rotate: AnimatableSet<Angle>,
295    pub(crate) scale: AnimatableSet<Scale>,
296
297    // Border widths (per side)
298    pub(crate) border_top_width: AnimatableVarSet<LengthOrPercentage>,
299    pub(crate) border_right_width: AnimatableVarSet<LengthOrPercentage>,
300    pub(crate) border_bottom_width: AnimatableVarSet<LengthOrPercentage>,
301    pub(crate) border_left_width: AnimatableVarSet<LengthOrPercentage>,
302
303    // Border colors (per side)
304    pub(crate) border_top_color: AnimatableVarSet<Color>,
305    pub(crate) border_right_color: AnimatableVarSet<Color>,
306    pub(crate) border_bottom_color: AnimatableVarSet<Color>,
307    pub(crate) border_left_color: AnimatableVarSet<Color>,
308
309    // Border styles (per side)
310    pub(crate) border_top_style: StyleSet<BorderStyleKeyword>,
311    pub(crate) border_right_style: StyleSet<BorderStyleKeyword>,
312    pub(crate) border_bottom_style: StyleSet<BorderStyleKeyword>,
313    pub(crate) border_left_style: StyleSet<BorderStyleKeyword>,
314
315    // Corner Radius
316    pub(crate) corner_top_left_radius: AnimatableVarSet<LengthOrPercentage>,
317    pub(crate) corner_top_right_radius: AnimatableVarSet<LengthOrPercentage>,
318    pub(crate) corner_bottom_left_radius: AnimatableVarSet<LengthOrPercentage>,
319    pub(crate) corner_bottom_right_radius: AnimatableVarSet<LengthOrPercentage>,
320
321    // Outline
322    pub(crate) outline_width: AnimatableVarSet<LengthOrPercentage>,
323    pub(crate) outline_color: AnimatableVarSet<Color>,
324    pub(crate) outline_offset: AnimatableVarSet<LengthOrPercentage>,
325
326    // Background
327    pub(crate) background_color: AnimatableVarSet<Color>,
328    pub(crate) background_image: AnimatableSet<Vec<ImageOrGradient>>,
329    /// Entities which may have a background image, i.e. a superset of the entities for which
330    /// `background_image` returns a value. Used to find image references without walking the tree.
331    pub(crate) background_image_entities: HashSet<Entity>,
332    pub(crate) background_position: AnimatableSet<Vec<Position>>,
333    pub(crate) background_repeat: AnimatableSet<Vec<BackgroundRepeat>>,
334    pub(crate) background_size: AnimatableSet<Vec<BackgroundSize>>,
335
336    // Shadow
337    pub(crate) shadow: AnimatableVarSet<Vec<Shadow>>,
338
339    // Text
340    pub(crate) text: SparseSet<String>,
341    pub(crate) text_wrap: StyleSet<bool>,
342    pub(crate) text_overflow: StyleSet<TextOverflow>,
343    pub(crate) letter_spacing: AnimatableVarSet<LetterSpacing>,
344    pub(crate) line_height: AnimatableVarSet<LineHeight>,
345    pub(crate) line_clamp: StyleSet<LineClamp>,
346    pub(crate) text_align: StyleSet<TextAlign>,
347    pub(crate) text_decoration_line: StyleSet<TextDecorationLine>,
348    pub(crate) text_decoration_style: StyleSet<TextDecorationStyle>,
349    pub(crate) text_decoration_color: AnimatableVarSet<Color>,
350    pub(crate) text_stroke_width: StyleSet<Length>,
351    pub(crate) text_stroke_style: StyleSet<TextStrokeStyle>,
352    pub(crate) font_family: StyleSet<Vec<FamilyOwned>>,
353    pub(crate) font_color: AnimatableVarSet<Color>,
354    pub(crate) font_size: AnimatableVarSet<FontSize>,
355    pub(crate) font_weight: StyleSet<FontWeight>,
356    pub(crate) font_slant: StyleSet<FontSlant>,
357    pub(crate) font_width: StyleSet<FontWidth>,
358    pub(crate) font_variation_settings: StyleSet<Vec<FontVariation>>,
359    pub(crate) caret_color: AnimatableVarSet<Color>,
360    pub(crate) selection_color: AnimatableVarSet<Color>,
361
362    pub(crate) fill: AnimatableVarSet<Color>,
363
364    // cursor Icon
365    pub(crate) cursor: StyleSet<CursorIcon>,
366
367    pub(crate) pointer_events: StyleSet<PointerEvents>,
368
369    // LAYOUT
370
371    // Display
372    pub(crate) display: AnimatableSet<Display>,
373
374    // Layout Type
375    pub(crate) layout_type: StyleSet<LayoutType>,
376
377    // Position
378    pub(crate) position_type: StyleSet<PositionType>,
379
380    pub(crate) alignment: StyleSet<Alignment>,
381    pub(crate) direction: StyleSet<Direction>,
382    pub(crate) wrap: StyleSet<LayoutWrap>,
383
384    // Grid
385    pub(crate) grid_columns: StyleSet<Vec<Units>>,
386    pub(crate) grid_rows: StyleSet<Vec<Units>>,
387
388    pub(crate) column_start: StyleSet<usize>,
389    pub(crate) column_span: StyleSet<usize>,
390    pub(crate) row_start: StyleSet<usize>,
391    pub(crate) row_span: StyleSet<usize>,
392
393    // Spacing
394    pub(crate) left: AnimatableVarSet<Units>,
395    pub(crate) right: AnimatableVarSet<Units>,
396    pub(crate) top: AnimatableVarSet<Units>,
397    pub(crate) bottom: AnimatableVarSet<Units>,
398
399    // Padding
400    pub(crate) padding_left: AnimatableVarSet<Units>,
401    pub(crate) padding_right: AnimatableVarSet<Units>,
402    pub(crate) padding_top: AnimatableVarSet<Units>,
403    pub(crate) padding_bottom: AnimatableVarSet<Units>,
404    pub(crate) vertical_gap: AnimatableVarSet<Units>,
405    pub(crate) horizontal_gap: AnimatableVarSet<Units>,
406
407    // Size
408    pub(crate) width: AnimatableVarSet<Units>,
409    pub(crate) height: AnimatableVarSet<Units>,
410    pub(crate) aspect_ratio: StyleSet<AspectRatio>,
411
412    // Size Constraints
413    pub(crate) min_width: AnimatableVarSet<Units>,
414    pub(crate) max_width: AnimatableVarSet<Units>,
415    pub(crate) min_height: AnimatableVarSet<Units>,
416    pub(crate) max_height: AnimatableVarSet<Units>,
417
418    // Gap Constraints
419    pub(crate) min_horizontal_gap: AnimatableVarSet<Units>,
420    pub(crate) max_horizontal_gap: AnimatableVarSet<Units>,
421    pub(crate) min_vertical_gap: AnimatableVarSet<Units>,
422    pub(crate) max_vertical_gap: AnimatableVarSet<Units>,
423
424    pub(crate) system_flags: SystemFlags,
425
426    /// Whether the layout debug overlay is enabled. Mirrored from the [`Environment`] model so
427    /// that the layout and draw systems can read it directly.
428    pub(crate) debug_layout: bool,
429
430    pub(crate) restyle: HashSet<Entity>,
431    /// Set of entities which need (incremental) relayout. Marking the root signals a full
432    /// relayout of the whole tree.
433    pub(crate) relayout: HashSet<Entity>,
434    /// Debug overlay: set of entities which underwent layout in the last relayout pass. Only
435    /// populated when the layout debug overlay is enabled (see `debug_layout`).
436    pub(crate) laid_out: HashSet<Entity>,
437    pub(crate) text_construction: HashSet<Entity>,
438    pub(crate) text_layout: HashSet<Entity>,
439    pub(crate) reaccess: HashSet<Entity>,
440    pub(crate) retransform: HashSet<Entity>,
441    pub(crate) reclip: HashSet<Entity>,
442
443    pub(crate) text_range: SparseSet<Range<usize>>,
444    pub(crate) text_span: SparseSet<bool>,
445
446    /// This includes both the system's HiDPI scaling factor as well as `cx.user_scale_factor`.
447    pub(crate) dpi_factor: f64,
448
449    pub(crate) custom_color_props: HashMap<u64, AnimatableVarSet<Color>>,
450    pub(crate) custom_length_props: HashMap<u64, AnimatableVarSet<LengthOrPercentage>>,
451    pub(crate) custom_font_size_props: HashMap<u64, AnimatableVarSet<FontSize>>,
452    pub(crate) custom_letter_spacing_props: HashMap<u64, AnimatableVarSet<LetterSpacing>>,
453    pub(crate) custom_line_height_props: HashMap<u64, AnimatableVarSet<LineHeight>>,
454    pub(crate) custom_units_props: HashMap<u64, AnimatableVarSet<Units>>,
455    pub(crate) custom_opacity_props: HashMap<u64, AnimatableVarSet<Opacity>>,
456    pub(crate) custom_shadow_props: HashMap<u64, AnimatableVarSet<Vec<Shadow>>>,
457}
458
459impl Style {
460    /// Returns the scale factor of the application.
461    pub fn scale_factor(&self) -> f32 {
462        self.dpi_factor as f32
463    }
464
465    /// Function to convert logical points to physical pixels.
466    pub fn logical_to_physical(&self, logical: f32) -> f32 {
467        (logical * self.dpi_factor as f32).round()
468    }
469
470    /// Function to convert physical pixels to logical points.
471    pub fn physical_to_logical(&self, physical: f32) -> f32 {
472        physical / self.dpi_factor as f32
473    }
474
475    pub(crate) fn remove_rules(&mut self) {
476        self.rule_manager.reset();
477        self.rules.clear();
478        self.variable_rules.clear();
479        self.rules_generation = self.rules_generation.wrapping_add(1);
480    }
481
482    /// Returns the invalidation map for the current rules, building it if necessary.
483    fn invalidation_map(&mut self) -> &InvalidationMap {
484        if self.invalidation.as_ref().is_none_or(|map| map.generation() != self.rules_generation) {
485            self.invalidation = Some(InvalidationMap::new(
486                self.rules_generation,
487                &self.rules,
488                &self.variable_rules,
489            ));
490        }
491
492        self.invalidation.as_ref().unwrap()
493    }
494
495    /// Whether the rules contain selectors which depend on the position of an entity among its
496    /// siblings, such as `:first-child`.
497    pub(crate) fn has_positional_rules(&mut self) -> bool {
498        self.invalidation_map().positional
499    }
500
501    /// Marks the entities which may be affected by a change to the selector attributes (element,
502    /// id, classes, disabled state, ...) of an entity as needing to be restyled.
503    pub(crate) fn needs_restyle_with_dependents(&mut self, tree: &Tree<Entity>, entity: Entity) {
504        if entity == Entity::null() {
505            return;
506        }
507
508        let map = self.invalidation_map();
509        let sibling_dependent = map.sibling_dependent;
510        let ancestor_dependent = map.ancestor_dependent;
511
512        if sibling_dependent {
513            let parent = tree.get_layout_parent(entity).unwrap_or(entity);
514            if self.restyle_sibling_parents.insert(parent) {
515                invalidation::mark_scope(
516                    &mut self.restyle,
517                    tree,
518                    entity,
519                    invalidation::Scope::Siblings,
520                );
521            }
522        } else {
523            invalidation::mark_scope(
524                &mut self.restyle,
525                tree,
526                entity,
527                invalidation::Scope::Descendants,
528            );
529        }
530
531        if ancestor_dependent {
532            invalidation::mark_ancestors(&mut self.restyle, tree, entity);
533        }
534    }
535
536    /// Marks the entities which may be affected by a change to the given pseudo-class state
537    /// flags of an entity as needing to be restyled. Does nothing if no selector depends on the
538    /// changed flags.
539    pub(crate) fn needs_restyle_for_state(
540        &mut self,
541        tree: &Tree<Entity>,
542        entity: Entity,
543        changed: PseudoClassFlags,
544    ) {
545        if changed.is_empty() {
546            return;
547        }
548
549        self.needs_restyle_for_change(tree, entity, invalidation::Change::State(changed));
550    }
551
552    /// Marks the entities which may be affected by adding or removing a class of an entity as
553    /// needing to be restyled. Does nothing if no selector uses the class.
554    pub(crate) fn needs_restyle_for_class(
555        &mut self,
556        tree: &Tree<Entity>,
557        entity: Entity,
558        class: &str,
559    ) {
560        self.needs_restyle_for_change(tree, entity, invalidation::Change::Class(class));
561    }
562
563    /// Marks the entities which may be affected by setting or removing the id of an entity as
564    /// needing to be restyled. Does nothing if no selector uses the id.
565    pub(crate) fn needs_restyle_for_id(&mut self, tree: &Tree<Entity>, entity: Entity, id: &str) {
566        self.needs_restyle_for_change(tree, entity, invalidation::Change::Id(id));
567    }
568
569    /// Adds or removes a class of an entity and restyles the entities the change can affect.
570    pub(crate) fn set_class(
571        &mut self,
572        tree: &Tree<Entity>,
573        entity: Entity,
574        class: &str,
575        applied: bool,
576    ) {
577        let changed = match self.classes.get_mut(entity) {
578            Some(classes) if applied => classes.insert(class.to_owned()),
579            Some(classes) => classes.remove(class),
580            None if applied => {
581                self.classes.insert(entity, HashSet::from([class.to_owned()]));
582                true
583            }
584            None => false,
585        };
586
587        if changed {
588            self.needs_restyle_for_class(tree, entity, class);
589        }
590    }
591
592    /// Sets the id of an entity and restyles the entities the change can affect.
593    pub(crate) fn set_id(&mut self, tree: &Tree<Entity>, entity: Entity, id: String) {
594        let previous = self.ids.get(entity).cloned();
595        if previous.as_ref() == Some(&id) {
596            return;
597        }
598
599        self.ids.insert(entity, id.clone());
600        if let Some(previous) = previous {
601            self.needs_restyle_for_id(tree, entity, &previous);
602        }
603        self.needs_restyle_for_id(tree, entity, &id);
604    }
605
606    fn needs_restyle_for_change(
607        &mut self,
608        tree: &Tree<Entity>,
609        entity: Entity,
610        change: invalidation::Change,
611    ) {
612        if entity == Entity::null() {
613            return;
614        }
615
616        self.invalidation_map();
617        let Some(map) = self.invalidation.as_ref() else { return };
618        let invalidation = map.invalidation(self, entity, change);
619        let path_entities = map.path_entities(self, tree, entity, &invalidation);
620        if let Some(scope) = invalidation.scope {
621            invalidation::mark_scope(&mut self.restyle, tree, entity, scope);
622        }
623        self.restyle.extend(path_entities);
624    }
625
626    pub(crate) fn get_animation(&self, name: &str) -> Option<&Animation> {
627        self.animations.get(name)
628    }
629
630    pub(crate) fn add_keyframe(
631        &mut self,
632        animation_id: Animation,
633        time: f32,
634        properties: &[Property],
635    ) {
636        fn insert_keyframe<T: 'static + Interpolator + Debug + Clone + PartialEq + Default>(
637            storage: &mut AnimatableSet<T>,
638            animation_id: Animation,
639            time: f32,
640            value: T,
641        ) {
642            let keyframe = Keyframe { time, value, timing_function: TimingFunction::linear() };
643
644            if let Some(anim_state) = storage.get_animation_mut(animation_id) {
645                anim_state.keyframes.push(keyframe)
646            } else {
647                let anim_state = AnimationState::new(animation_id).with_keyframe(keyframe);
648                storage.insert_animation(animation_id, anim_state);
649            }
650        }
651
652        fn insert_keyframe2<T: 'static + Interpolator + Debug + Clone + PartialEq + Default>(
653            storage: &mut AnimatableVarSet<T>,
654            animation_id: Animation,
655            time: f32,
656            value: T,
657        ) {
658            let keyframe = Keyframe { time, value, timing_function: TimingFunction::linear() };
659
660            if let Some(anim_state) = storage.get_animation_mut(animation_id) {
661                anim_state.keyframes.push(keyframe)
662            } else {
663                let anim_state = AnimationState::new(animation_id).with_keyframe(keyframe);
664                storage.insert_animation(animation_id, anim_state);
665            }
666        }
667
668        for property in properties.iter() {
669            match property {
670                // DISPLAY
671                Property::Display(value) => {
672                    insert_keyframe(&mut self.display, animation_id, time, *value);
673                }
674
675                Property::Opacity(value) => {
676                    insert_keyframe2(&mut self.opacity, animation_id, time, *value);
677                }
678
679                Property::ClipPath(value) => {
680                    insert_keyframe(&mut self.clip_path, animation_id, time, value.clone());
681                }
682
683                // TRANSFORM
684                Property::Transform(value) => {
685                    insert_keyframe(&mut self.transform, animation_id, time, value.clone());
686                }
687
688                Property::TransformOrigin(transform_origin) => {
689                    let x = transform_origin.x.to_length_or_percentage();
690                    let y = transform_origin.y.to_length_or_percentage();
691                    let value = Translate { x, y };
692                    insert_keyframe(&mut self.transform_origin, animation_id, time, value);
693                }
694
695                Property::Translate(value) => {
696                    insert_keyframe(&mut self.translate, animation_id, time, value.clone());
697                }
698
699                Property::Rotate(value) => {
700                    insert_keyframe(&mut self.rotate, animation_id, time, *value);
701                }
702
703                Property::Scale(value) => {
704                    insert_keyframe(&mut self.scale, animation_id, time, *value);
705                }
706
707                // BORDER
708                Property::Border(value) => {
709                    if let Some(color) = value.color {
710                        insert_keyframe2(&mut self.border_top_color, animation_id, time, color);
711                        insert_keyframe2(&mut self.border_right_color, animation_id, time, color);
712                        insert_keyframe2(&mut self.border_bottom_color, animation_id, time, color);
713                        insert_keyframe2(&mut self.border_left_color, animation_id, time, color);
714                    }
715                    if let Some(width) = value.width.clone() {
716                        let w: LengthOrPercentage = width.into();
717                        insert_keyframe2(&mut self.border_top_width, animation_id, time, w.clone());
718                        insert_keyframe2(
719                            &mut self.border_right_width,
720                            animation_id,
721                            time,
722                            w.clone(),
723                        );
724                        insert_keyframe2(
725                            &mut self.border_bottom_width,
726                            animation_id,
727                            time,
728                            w.clone(),
729                        );
730                        insert_keyframe2(&mut self.border_left_width, animation_id, time, w);
731                    }
732                }
733
734                Property::BorderTop(value) => {
735                    if let Some(color) = value.color {
736                        insert_keyframe2(&mut self.border_top_color, animation_id, time, color);
737                    }
738                    if let Some(width) = value.width.clone() {
739                        insert_keyframe2(
740                            &mut self.border_top_width,
741                            animation_id,
742                            time,
743                            width.into(),
744                        );
745                    }
746                }
747
748                Property::BorderRight(value) => {
749                    if let Some(color) = value.color {
750                        insert_keyframe2(&mut self.border_right_color, animation_id, time, color);
751                    }
752                    if let Some(width) = value.width.clone() {
753                        insert_keyframe2(
754                            &mut self.border_right_width,
755                            animation_id,
756                            time,
757                            width.into(),
758                        );
759                    }
760                }
761
762                Property::BorderBottom(value) => {
763                    if let Some(color) = value.color {
764                        insert_keyframe2(&mut self.border_bottom_color, animation_id, time, color);
765                    }
766                    if let Some(width) = value.width.clone() {
767                        insert_keyframe2(
768                            &mut self.border_bottom_width,
769                            animation_id,
770                            time,
771                            width.into(),
772                        );
773                    }
774                }
775
776                Property::BorderLeft(value) => {
777                    if let Some(color) = value.color {
778                        insert_keyframe2(&mut self.border_left_color, animation_id, time, color);
779                    }
780                    if let Some(width) = value.width.clone() {
781                        insert_keyframe2(
782                            &mut self.border_left_width,
783                            animation_id,
784                            time,
785                            width.into(),
786                        );
787                    }
788                }
789
790                Property::BorderWidth(value) => {
791                    insert_keyframe2(
792                        &mut self.border_top_width,
793                        animation_id,
794                        time,
795                        value.top.0.clone(),
796                    );
797                    insert_keyframe2(
798                        &mut self.border_right_width,
799                        animation_id,
800                        time,
801                        value.right.0.clone(),
802                    );
803                    insert_keyframe2(
804                        &mut self.border_bottom_width,
805                        animation_id,
806                        time,
807                        value.bottom.0.clone(),
808                    );
809                    insert_keyframe2(
810                        &mut self.border_left_width,
811                        animation_id,
812                        time,
813                        value.left.0.clone(),
814                    );
815                }
816
817                Property::BorderTopWidth(value) => {
818                    insert_keyframe2(
819                        &mut self.border_top_width,
820                        animation_id,
821                        time,
822                        value.0.clone(),
823                    );
824                }
825
826                Property::BorderRightWidth(value) => {
827                    insert_keyframe2(
828                        &mut self.border_right_width,
829                        animation_id,
830                        time,
831                        value.0.clone(),
832                    );
833                }
834
835                Property::BorderBottomWidth(value) => {
836                    insert_keyframe2(
837                        &mut self.border_bottom_width,
838                        animation_id,
839                        time,
840                        value.0.clone(),
841                    );
842                }
843
844                Property::BorderLeftWidth(value) => {
845                    insert_keyframe2(
846                        &mut self.border_left_width,
847                        animation_id,
848                        time,
849                        value.0.clone(),
850                    );
851                }
852
853                Property::BorderColor(value) => {
854                    insert_keyframe2(&mut self.border_top_color, animation_id, time, *value);
855                    insert_keyframe2(&mut self.border_right_color, animation_id, time, *value);
856                    insert_keyframe2(&mut self.border_bottom_color, animation_id, time, *value);
857                    insert_keyframe2(&mut self.border_left_color, animation_id, time, *value);
858                }
859
860                Property::BorderTopColor(value) => {
861                    insert_keyframe2(&mut self.border_top_color, animation_id, time, *value);
862                }
863
864                Property::BorderRightColor(value) => {
865                    insert_keyframe2(&mut self.border_right_color, animation_id, time, *value);
866                }
867
868                Property::BorderBottomColor(value) => {
869                    insert_keyframe2(&mut self.border_bottom_color, animation_id, time, *value);
870                }
871
872                Property::BorderLeftColor(value) => {
873                    insert_keyframe2(&mut self.border_left_color, animation_id, time, *value);
874                }
875
876                Property::CornerTopLeftRadius(value) => {
877                    insert_keyframe2(
878                        &mut self.corner_top_left_radius,
879                        animation_id,
880                        time,
881                        value.clone(),
882                    );
883                }
884
885                Property::CornerTopRightRadius(value) => {
886                    insert_keyframe2(
887                        &mut self.corner_top_right_radius,
888                        animation_id,
889                        time,
890                        value.clone(),
891                    );
892                }
893
894                Property::CornerBottomLeftRadius(value) => {
895                    insert_keyframe2(
896                        &mut self.corner_bottom_left_radius,
897                        animation_id,
898                        time,
899                        value.clone(),
900                    );
901                }
902
903                Property::CornerBottomRightRadius(value) => {
904                    insert_keyframe2(
905                        &mut self.corner_bottom_right_radius,
906                        animation_id,
907                        time,
908                        value.clone(),
909                    );
910                }
911
912                // OUTLINE
913                Property::OutlineWidth(value) => {
914                    insert_keyframe2(
915                        &mut self.outline_width,
916                        animation_id,
917                        time,
918                        value.left.0.clone(),
919                    );
920                }
921
922                Property::OutlineColor(value) => {
923                    insert_keyframe2(&mut self.outline_color, animation_id, time, *value);
924                }
925
926                Property::OutlineOffset(value) => {
927                    insert_keyframe2(&mut self.outline_offset, animation_id, time, value.clone());
928                }
929
930                // BACKGROUND
931                Property::BackgroundColor(value) => {
932                    insert_keyframe2(&mut self.background_color, animation_id, time, *value);
933                }
934
935                Property::BackgroundImage(images) => {
936                    let images = images
937                        .iter()
938                        .filter_map(|img| match img {
939                            BackgroundImage::None => None,
940                            BackgroundImage::Gradient(gradient) => {
941                                Some(ImageOrGradient::Gradient(*gradient.clone()))
942                            }
943                            BackgroundImage::Url(url) => {
944                                Some(ImageOrGradient::Image(url.url.to_string()))
945                            }
946                        })
947                        .collect::<Vec<_>>();
948                    insert_keyframe(&mut self.background_image, animation_id, time, images);
949                }
950
951                Property::BackgroundPosition(value) => {
952                    insert_keyframe(
953                        &mut self.background_position,
954                        animation_id,
955                        time,
956                        value.clone(),
957                    );
958                }
959
960                Property::BackgroundSize(value) => {
961                    insert_keyframe(&mut self.background_size, animation_id, time, value.clone());
962                }
963
964                Property::BackgroundRepeat(value) => {
965                    insert_keyframe(&mut self.background_repeat, animation_id, time, value.clone());
966                }
967
968                // BOX SHADOW
969                Property::Shadow(value) => {
970                    insert_keyframe2(&mut self.shadow, animation_id, time, value.clone());
971                }
972
973                // TEXT
974                Property::FontColor(value) => {
975                    insert_keyframe2(&mut self.font_color, animation_id, time, *value);
976                }
977
978                Property::FontSize(value) => {
979                    insert_keyframe2(&mut self.font_size, animation_id, time, value.clone());
980                }
981
982                Property::LetterSpacing(value) => {
983                    insert_keyframe2(&mut self.letter_spacing, animation_id, time, value.clone());
984                }
985
986                Property::LineHeight(value) => {
987                    insert_keyframe2(&mut self.line_height, animation_id, time, value.clone());
988                }
989
990                Property::CaretColor(value) => {
991                    insert_keyframe2(&mut self.caret_color, animation_id, time, *value);
992                }
993
994                Property::SelectionColor(value) => {
995                    insert_keyframe2(&mut self.selection_color, animation_id, time, *value);
996                }
997
998                // SPACE
999                Property::Left(value) => {
1000                    insert_keyframe2(&mut self.left, animation_id, time, *value);
1001                }
1002
1003                Property::Right(value) => {
1004                    insert_keyframe2(&mut self.right, animation_id, time, *value);
1005                }
1006
1007                Property::Top(value) => {
1008                    insert_keyframe2(&mut self.top, animation_id, time, *value);
1009                }
1010
1011                Property::Bottom(value) => {
1012                    insert_keyframe2(&mut self.bottom, animation_id, time, *value);
1013                }
1014
1015                // Padding
1016                Property::PaddingLeft(value) => {
1017                    insert_keyframe2(&mut self.padding_left, animation_id, time, *value);
1018                }
1019
1020                Property::PaddingRight(value) => {
1021                    insert_keyframe2(&mut self.padding_right, animation_id, time, *value);
1022                }
1023
1024                Property::PaddingTop(value) => {
1025                    insert_keyframe2(&mut self.padding_top, animation_id, time, *value);
1026                }
1027
1028                Property::PaddingBottom(value) => {
1029                    insert_keyframe2(&mut self.padding_bottom, animation_id, time, *value);
1030                }
1031
1032                Property::HorizontalGap(value) => {
1033                    insert_keyframe2(&mut self.horizontal_gap, animation_id, time, *value);
1034                }
1035
1036                Property::VerticalGap(value) => {
1037                    insert_keyframe2(&mut self.vertical_gap, animation_id, time, *value);
1038                }
1039
1040                Property::Gap(value) => {
1041                    insert_keyframe2(&mut self.horizontal_gap, animation_id, time, *value);
1042                    insert_keyframe2(&mut self.vertical_gap, animation_id, time, *value);
1043                }
1044
1045                // GAP CONSSTRAINTS
1046                Property::MinGap(value) => {
1047                    insert_keyframe2(&mut self.min_horizontal_gap, animation_id, time, *value);
1048                    insert_keyframe2(&mut self.min_vertical_gap, animation_id, time, *value);
1049                }
1050
1051                Property::MaxGap(value) => {
1052                    insert_keyframe2(&mut self.max_horizontal_gap, animation_id, time, *value);
1053                    insert_keyframe2(&mut self.max_vertical_gap, animation_id, time, *value);
1054                }
1055
1056                Property::MinHorizontalGap(value) => {
1057                    insert_keyframe2(&mut self.min_horizontal_gap, animation_id, time, *value);
1058                }
1059
1060                Property::MaxHorizontalGap(value) => {
1061                    insert_keyframe2(&mut self.max_horizontal_gap, animation_id, time, *value);
1062                }
1063
1064                Property::MinVerticalGap(value) => {
1065                    insert_keyframe2(&mut self.min_vertical_gap, animation_id, time, *value);
1066                }
1067
1068                Property::MaxVerticalGap(value) => {
1069                    insert_keyframe2(&mut self.max_vertical_gap, animation_id, time, *value);
1070                }
1071
1072                // SIZE
1073                Property::Width(value) => {
1074                    insert_keyframe2(&mut self.width, animation_id, time, *value);
1075                }
1076
1077                Property::Height(value) => {
1078                    insert_keyframe2(&mut self.height, animation_id, time, *value);
1079                }
1080
1081                // SIZE CONSTRAINTS
1082                Property::MinWidth(value) => {
1083                    insert_keyframe2(&mut self.min_width, animation_id, time, *value);
1084                }
1085
1086                Property::MaxWidth(value) => {
1087                    insert_keyframe2(&mut self.max_width, animation_id, time, *value);
1088                }
1089
1090                Property::MinHeight(value) => {
1091                    insert_keyframe2(&mut self.min_height, animation_id, time, *value);
1092                }
1093
1094                Property::MaxHeight(value) => {
1095                    insert_keyframe2(&mut self.max_height, animation_id, time, *value);
1096                }
1097
1098                Property::TextDecorationColor(value) => {
1099                    insert_keyframe2(&mut self.text_decoration_color, animation_id, time, *value);
1100                }
1101
1102                Property::Fill(value) => {
1103                    insert_keyframe2(&mut self.fill, animation_id, time, *value);
1104                }
1105
1106                _ => {}
1107            }
1108        }
1109    }
1110
1111    pub(crate) fn add_animation(&mut self, animation: AnimationBuilder) -> Animation {
1112        let animation_id = self.animation_manager.create();
1113        for keyframe in animation.keyframes.iter() {
1114            self.add_keyframe(animation_id, keyframe.time, &keyframe.properties);
1115        }
1116
1117        animation_id
1118    }
1119
1120    pub(crate) fn enqueue_animation(
1121        &mut self,
1122        entity: Entity,
1123        animation: Animation,
1124        duration: Duration,
1125        delay: Duration,
1126    ) {
1127        self.pending_animations.push((entity, animation, duration, delay));
1128    }
1129
1130    pub(crate) fn play_pending_animations(&mut self) {
1131        let start_time = Instant::now();
1132
1133        let pending_animations = std::mem::take(&mut self.pending_animations);
1134
1135        for (entity, animation, duration, delay) in pending_animations {
1136            self.play_animation(entity, animation, start_time + delay, duration, delay)
1137        }
1138    }
1139
1140    pub(crate) fn play_animation(
1141        &mut self,
1142        entity: Entity,
1143        animation: Animation,
1144        start_time: Instant,
1145        duration: Duration,
1146        delay: Duration,
1147    ) {
1148        self.display.play_animation(entity, animation, start_time, duration, delay);
1149        self.opacity.play_animation(entity, animation, start_time, duration, delay);
1150        self.clip_path.play_animation(entity, animation, start_time, duration, delay);
1151
1152        self.transform.play_animation(entity, animation, start_time, duration, delay);
1153        self.transform_origin.play_animation(entity, animation, start_time, duration, delay);
1154        self.translate.play_animation(entity, animation, start_time, duration, delay);
1155        self.rotate.play_animation(entity, animation, start_time, duration, delay);
1156        self.scale.play_animation(entity, animation, start_time, duration, delay);
1157
1158        self.border_top_width.play_animation(entity, animation, start_time, duration, delay);
1159        self.border_right_width.play_animation(entity, animation, start_time, duration, delay);
1160        self.border_bottom_width.play_animation(entity, animation, start_time, duration, delay);
1161        self.border_left_width.play_animation(entity, animation, start_time, duration, delay);
1162        self.border_top_color.play_animation(entity, animation, start_time, duration, delay);
1163        self.border_right_color.play_animation(entity, animation, start_time, duration, delay);
1164        self.border_bottom_color.play_animation(entity, animation, start_time, duration, delay);
1165        self.border_left_color.play_animation(entity, animation, start_time, duration, delay);
1166
1167        self.corner_top_left_radius.play_animation(entity, animation, start_time, duration, delay);
1168        self.corner_top_right_radius.play_animation(entity, animation, start_time, duration, delay);
1169        self.corner_bottom_left_radius
1170            .play_animation(entity, animation, start_time, duration, delay);
1171        self.corner_bottom_right_radius
1172            .play_animation(entity, animation, start_time, duration, delay);
1173
1174        self.outline_width.play_animation(entity, animation, start_time, duration, delay);
1175        self.outline_color.play_animation(entity, animation, start_time, duration, delay);
1176        self.outline_offset.play_animation(entity, animation, start_time, duration, delay);
1177
1178        self.background_color.play_animation(entity, animation, start_time, duration, delay);
1179        self.background_image.play_animation(entity, animation, start_time, duration, delay);
1180        if self.background_image.has_active_animation(entity, animation) {
1181            self.background_image_entities.insert(entity);
1182        }
1183        self.background_position.play_animation(entity, animation, start_time, duration, delay);
1184        self.background_repeat.play_animation(entity, animation, start_time, duration, delay);
1185        self.background_size.play_animation(entity, animation, start_time, duration, delay);
1186
1187        self.shadow.play_animation(entity, animation, start_time, duration, delay);
1188
1189        self.font_color.play_animation(entity, animation, start_time, duration, delay);
1190        self.font_size.play_animation(entity, animation, start_time, duration, delay);
1191        self.letter_spacing.play_animation(entity, animation, start_time, duration, delay);
1192        self.line_height.play_animation(entity, animation, start_time, duration, delay);
1193        self.caret_color.play_animation(entity, animation, start_time, duration, delay);
1194        self.selection_color.play_animation(entity, animation, start_time, duration, delay);
1195
1196        self.left.play_animation(entity, animation, start_time, duration, delay);
1197        self.right.play_animation(entity, animation, start_time, duration, delay);
1198        self.top.play_animation(entity, animation, start_time, duration, delay);
1199        self.bottom.play_animation(entity, animation, start_time, duration, delay);
1200
1201        self.padding_left.play_animation(entity, animation, start_time, duration, delay);
1202        self.padding_right.play_animation(entity, animation, start_time, duration, delay);
1203        self.padding_top.play_animation(entity, animation, start_time, duration, delay);
1204        self.padding_bottom.play_animation(entity, animation, start_time, duration, delay);
1205        self.horizontal_gap.play_animation(entity, animation, start_time, duration, delay);
1206        self.vertical_gap.play_animation(entity, animation, start_time, duration, delay);
1207
1208        self.width.play_animation(entity, animation, start_time, duration, delay);
1209        self.height.play_animation(entity, animation, start_time, duration, delay);
1210
1211        self.min_width.play_animation(entity, animation, start_time, duration, delay);
1212        self.max_width.play_animation(entity, animation, start_time, duration, delay);
1213        self.min_height.play_animation(entity, animation, start_time, duration, delay);
1214        self.max_height.play_animation(entity, animation, start_time, duration, delay);
1215
1216        self.min_horizontal_gap.play_animation(entity, animation, start_time, duration, delay);
1217        self.max_horizontal_gap.play_animation(entity, animation, start_time, duration, delay);
1218        self.min_vertical_gap.play_animation(entity, animation, start_time, duration, delay);
1219        self.max_vertical_gap.play_animation(entity, animation, start_time, duration, delay);
1220
1221        self.text_decoration_color.play_animation(entity, animation, start_time, duration, delay);
1222
1223        self.fill.play_animation(entity, animation, start_time, duration, delay);
1224
1225        // Play animations on custom color properties
1226        for store in self.custom_color_props.values_mut() {
1227            store.play_animation(entity, animation, start_time, duration, delay);
1228        }
1229        // Play animations on custom length properties
1230        for store in self.custom_length_props.values_mut() {
1231            store.play_animation(entity, animation, start_time, duration, delay);
1232        }
1233        // Play animations on custom font-size properties
1234        for store in self.custom_font_size_props.values_mut() {
1235            store.play_animation(entity, animation, start_time, duration, delay);
1236        }
1237        // Play animations on custom letter-spacing properties
1238        for store in self.custom_letter_spacing_props.values_mut() {
1239            store.play_animation(entity, animation, start_time, duration, delay);
1240        }
1241        // Play animations on custom line-height properties
1242        for store in self.custom_line_height_props.values_mut() {
1243            store.play_animation(entity, animation, start_time, duration, delay);
1244        }
1245        // Play animations on custom units properties
1246        for store in self.custom_units_props.values_mut() {
1247            store.play_animation(entity, animation, start_time, duration, delay);
1248        }
1249        // Play animations on custom opacity properties
1250        for store in self.custom_opacity_props.values_mut() {
1251            store.play_animation(entity, animation, start_time, duration, delay);
1252        }
1253        // Play animations on custom shadow properties
1254        for store in self.custom_shadow_props.values_mut() {
1255            store.play_animation(entity, animation, start_time, duration, delay);
1256        }
1257    }
1258
1259    pub(crate) fn is_animating(&self, entity: Entity, animation: Animation) -> bool {
1260        self.display.has_active_animation(entity, animation)
1261            | self.opacity.has_active_animation(entity, animation)
1262            | self.clip_path.has_active_animation(entity, animation)
1263            | self.transform.has_active_animation(entity, animation)
1264            | self.transform_origin.has_active_animation(entity, animation)
1265            | self.translate.has_active_animation(entity, animation)
1266            | self.rotate.has_active_animation(entity, animation)
1267            | self.scale.has_active_animation(entity, animation)
1268            | self.border_top_width.has_active_animation(entity, animation)
1269            | self.border_right_width.has_active_animation(entity, animation)
1270            | self.border_bottom_width.has_active_animation(entity, animation)
1271            | self.border_left_width.has_active_animation(entity, animation)
1272            | self.border_top_color.has_active_animation(entity, animation)
1273            | self.border_right_color.has_active_animation(entity, animation)
1274            | self.border_bottom_color.has_active_animation(entity, animation)
1275            | self.border_left_color.has_active_animation(entity, animation)
1276            | self.corner_top_left_radius.has_active_animation(entity, animation)
1277            | self.corner_top_right_radius.has_active_animation(entity, animation)
1278            | self.corner_bottom_left_radius.has_active_animation(entity, animation)
1279            | self.corner_bottom_right_radius.has_active_animation(entity, animation)
1280            | self.outline_width.has_active_animation(entity, animation)
1281            | self.outline_color.has_active_animation(entity, animation)
1282            | self.outline_offset.has_active_animation(entity, animation)
1283            | self.background_color.has_active_animation(entity, animation)
1284            | self.background_image.has_active_animation(entity, animation)
1285            | self.background_position.has_active_animation(entity, animation)
1286            | self.background_repeat.has_active_animation(entity, animation)
1287            | self.background_size.has_active_animation(entity, animation)
1288            | self.shadow.has_active_animation(entity, animation)
1289            | self.font_color.has_active_animation(entity, animation)
1290            | self.font_size.has_active_animation(entity, animation)
1291            | self.letter_spacing.has_active_animation(entity, animation)
1292            | self.line_height.has_active_animation(entity, animation)
1293            | self.caret_color.has_active_animation(entity, animation)
1294            | self.selection_color.has_active_animation(entity, animation)
1295            | self.left.has_active_animation(entity, animation)
1296            | self.right.has_active_animation(entity, animation)
1297            | self.top.has_active_animation(entity, animation)
1298            | self.bottom.has_active_animation(entity, animation)
1299            | self.padding_left.has_active_animation(entity, animation)
1300            | self.padding_right.has_active_animation(entity, animation)
1301            | self.padding_top.has_active_animation(entity, animation)
1302            | self.padding_bottom.has_active_animation(entity, animation)
1303            | self.horizontal_gap.has_active_animation(entity, animation)
1304            | self.vertical_gap.has_active_animation(entity, animation)
1305            | self.width.has_active_animation(entity, animation)
1306            | self.height.has_active_animation(entity, animation)
1307            | self.min_width.has_active_animation(entity, animation)
1308            | self.max_width.has_active_animation(entity, animation)
1309            | self.min_height.has_active_animation(entity, animation)
1310            | self.max_height.has_active_animation(entity, animation)
1311            | self.min_horizontal_gap.has_active_animation(entity, animation)
1312            | self.max_horizontal_gap.has_active_animation(entity, animation)
1313            | self.min_vertical_gap.has_active_animation(entity, animation)
1314            | self.max_vertical_gap.has_active_animation(entity, animation)
1315            | self.text_decoration_color.has_active_animation(entity, animation)
1316            | self.fill.has_active_animation(entity, animation)
1317    }
1318
1319    pub(crate) fn parse_theme(&mut self, stylesheet: &str) {
1320        self.rules_generation = self.rules_generation.wrapping_add(1);
1321        if let Ok(stylesheet) = StyleSheet::parse(stylesheet, ParserOptions::new()) {
1322            let rules = stylesheet.rules.0;
1323
1324            for rule in rules {
1325                match rule {
1326                    CssRule::Style(style_rule) => {
1327                        // let selectors = style_rule.selectors;
1328
1329                        let defines_variables = style_rule
1330                            .declarations
1331                            .declarations
1332                            .iter()
1333                            .any(|property| matches!(property, Property::Custom(_)));
1334
1335                        for selector in style_rule.selectors.slice() {
1336                            let rule_id = self.rule_manager.create();
1337
1338                            if defines_variables {
1339                                self.variable_rules.insert(rule_id);
1340                            }
1341
1342                            for property in style_rule.declarations.declarations.iter() {
1343                                match property {
1344                                    Property::Transition(transitions) => {
1345                                        for transition in transitions.iter() {
1346                                            self.insert_transition(rule_id, transition);
1347                                        }
1348                                    }
1349
1350                                    _ => {
1351                                        self.insert_property(rule_id, property);
1352                                    }
1353                                }
1354                            }
1355
1356                            self.rules.insert(rule_id, StyleRule::new(selector.clone()));
1357                        }
1358                    }
1359
1360                    CssRule::Keyframes(keyframes_rule) => {
1361                        let name = keyframes_rule.name.as_string();
1362
1363                        let animation_id = self.animation_manager.create();
1364
1365                        for keyframes in keyframes_rule.keyframes {
1366                            for selector in keyframes.selectors.iter() {
1367                                let time = match selector {
1368                                    KeyframeSelector::From => 0.0,
1369                                    KeyframeSelector::To => 1.0,
1370                                    KeyframeSelector::Percentage(percentage) => {
1371                                        percentage.0 / 100.0
1372                                    }
1373                                };
1374
1375                                self.add_keyframe(
1376                                    animation_id,
1377                                    time,
1378                                    &keyframes.declarations.declarations,
1379                                );
1380                            }
1381                        }
1382
1383                        self.animations.insert(name, animation_id);
1384                    }
1385
1386                    _ => {}
1387                }
1388            }
1389        } else {
1390            println!("Failed to parse stylesheet");
1391        }
1392    }
1393
1394    fn insert_transition(&mut self, rule_id: Rule, transition: &Transition) {
1395        let animation = self.animation_manager.create();
1396        match transition.property.as_ref() {
1397            "display" => {
1398                self.display.insert_animation(animation, self.add_transition(transition));
1399                self.display.insert_transition(rule_id, animation);
1400            }
1401
1402            "opacity" => {
1403                self.opacity.insert_animation(animation, self.add_transition(transition));
1404                self.opacity.insert_transition(rule_id, animation);
1405            }
1406
1407            "clip-path" => {
1408                self.clip_path.insert_animation(animation, self.add_transition(transition));
1409                self.clip_path.insert_transition(rule_id, animation);
1410            }
1411
1412            "transform" => {
1413                self.transform.insert_animation(animation, self.add_transition(transition));
1414                self.transform.insert_transition(rule_id, animation);
1415            }
1416
1417            "transform-origin" => {
1418                self.transform_origin.insert_animation(animation, self.add_transition(transition));
1419                self.transform_origin.insert_transition(rule_id, animation);
1420            }
1421
1422            "translate" => {
1423                self.translate.insert_animation(animation, self.add_transition(transition));
1424                self.translate.insert_transition(rule_id, animation);
1425            }
1426
1427            "rotate" => {
1428                self.rotate.insert_animation(animation, self.add_transition(transition));
1429                self.rotate.insert_transition(rule_id, animation);
1430            }
1431
1432            "scale" => {
1433                self.scale.insert_animation(animation, self.add_transition(transition));
1434                self.scale.insert_transition(rule_id, animation);
1435            }
1436
1437            "border" => {
1438                self.border_top_width.insert_animation(animation, self.add_transition(transition));
1439                self.border_top_width.insert_transition(rule_id, animation);
1440                self.border_right_width
1441                    .insert_animation(animation, self.add_transition(transition));
1442                self.border_right_width.insert_transition(rule_id, animation);
1443                self.border_bottom_width
1444                    .insert_animation(animation, self.add_transition(transition));
1445                self.border_bottom_width.insert_transition(rule_id, animation);
1446                self.border_left_width.insert_animation(animation, self.add_transition(transition));
1447                self.border_left_width.insert_transition(rule_id, animation);
1448                self.border_top_color.insert_animation(animation, self.add_transition(transition));
1449                self.border_top_color.insert_transition(rule_id, animation);
1450                self.border_right_color
1451                    .insert_animation(animation, self.add_transition(transition));
1452                self.border_right_color.insert_transition(rule_id, animation);
1453                self.border_bottom_color
1454                    .insert_animation(animation, self.add_transition(transition));
1455                self.border_bottom_color.insert_transition(rule_id, animation);
1456                self.border_left_color.insert_animation(animation, self.add_transition(transition));
1457                self.border_left_color.insert_transition(rule_id, animation);
1458            }
1459
1460            "border-width" => {
1461                self.border_top_width.insert_animation(animation, self.add_transition(transition));
1462                self.border_top_width.insert_transition(rule_id, animation);
1463                self.border_right_width
1464                    .insert_animation(animation, self.add_transition(transition));
1465                self.border_right_width.insert_transition(rule_id, animation);
1466                self.border_bottom_width
1467                    .insert_animation(animation, self.add_transition(transition));
1468                self.border_bottom_width.insert_transition(rule_id, animation);
1469                self.border_left_width.insert_animation(animation, self.add_transition(transition));
1470                self.border_left_width.insert_transition(rule_id, animation);
1471            }
1472
1473            "border-top" => {
1474                self.border_top_width.insert_animation(animation, self.add_transition(transition));
1475                self.border_top_width.insert_transition(rule_id, animation);
1476                self.border_top_color.insert_animation(animation, self.add_transition(transition));
1477                self.border_top_color.insert_transition(rule_id, animation);
1478            }
1479
1480            "border-right" => {
1481                self.border_right_width
1482                    .insert_animation(animation, self.add_transition(transition));
1483                self.border_right_width.insert_transition(rule_id, animation);
1484                self.border_right_color
1485                    .insert_animation(animation, self.add_transition(transition));
1486                self.border_right_color.insert_transition(rule_id, animation);
1487            }
1488
1489            "border-bottom" => {
1490                self.border_bottom_width
1491                    .insert_animation(animation, self.add_transition(transition));
1492                self.border_bottom_width.insert_transition(rule_id, animation);
1493                self.border_bottom_color
1494                    .insert_animation(animation, self.add_transition(transition));
1495                self.border_bottom_color.insert_transition(rule_id, animation);
1496            }
1497
1498            "border-left" => {
1499                self.border_left_width.insert_animation(animation, self.add_transition(transition));
1500                self.border_left_width.insert_transition(rule_id, animation);
1501                self.border_left_color.insert_animation(animation, self.add_transition(transition));
1502                self.border_left_color.insert_transition(rule_id, animation);
1503            }
1504
1505            "border-top-width" => {
1506                self.border_top_width.insert_animation(animation, self.add_transition(transition));
1507                self.border_top_width.insert_transition(rule_id, animation);
1508            }
1509
1510            "border-right-width" => {
1511                self.border_right_width
1512                    .insert_animation(animation, self.add_transition(transition));
1513                self.border_right_width.insert_transition(rule_id, animation);
1514            }
1515
1516            "border-bottom-width" => {
1517                self.border_bottom_width
1518                    .insert_animation(animation, self.add_transition(transition));
1519                self.border_bottom_width.insert_transition(rule_id, animation);
1520            }
1521
1522            "border-left-width" => {
1523                self.border_left_width.insert_animation(animation, self.add_transition(transition));
1524                self.border_left_width.insert_transition(rule_id, animation);
1525            }
1526
1527            "border-color" => {
1528                self.border_top_color.insert_animation(animation, self.add_transition(transition));
1529                self.border_top_color.insert_transition(rule_id, animation);
1530                self.border_right_color
1531                    .insert_animation(animation, self.add_transition(transition));
1532                self.border_right_color.insert_transition(rule_id, animation);
1533                self.border_bottom_color
1534                    .insert_animation(animation, self.add_transition(transition));
1535                self.border_bottom_color.insert_transition(rule_id, animation);
1536                self.border_left_color.insert_animation(animation, self.add_transition(transition));
1537                self.border_left_color.insert_transition(rule_id, animation);
1538            }
1539
1540            "border-top-color" => {
1541                self.border_top_color.insert_animation(animation, self.add_transition(transition));
1542                self.border_top_color.insert_transition(rule_id, animation);
1543            }
1544
1545            "border-right-color" => {
1546                self.border_right_color
1547                    .insert_animation(animation, self.add_transition(transition));
1548                self.border_right_color.insert_transition(rule_id, animation);
1549            }
1550
1551            "border-bottom-color" => {
1552                self.border_bottom_color
1553                    .insert_animation(animation, self.add_transition(transition));
1554                self.border_bottom_color.insert_transition(rule_id, animation);
1555            }
1556
1557            "border-left-color" => {
1558                self.border_left_color.insert_animation(animation, self.add_transition(transition));
1559                self.border_left_color.insert_transition(rule_id, animation);
1560            }
1561
1562            "corner-radius" => {
1563                self.corner_bottom_left_radius
1564                    .insert_animation(animation, self.add_transition(transition));
1565                self.corner_bottom_left_radius.insert_transition(rule_id, animation);
1566                self.corner_bottom_right_radius
1567                    .insert_animation(animation, self.add_transition(transition));
1568                self.corner_bottom_right_radius.insert_transition(rule_id, animation);
1569                self.corner_top_left_radius
1570                    .insert_animation(animation, self.add_transition(transition));
1571                self.corner_top_left_radius.insert_transition(rule_id, animation);
1572                self.corner_top_right_radius
1573                    .insert_animation(animation, self.add_transition(transition));
1574                self.corner_top_right_radius.insert_transition(rule_id, animation);
1575            }
1576
1577            "corner-top-left-radius" => {
1578                self.corner_top_left_radius
1579                    .insert_animation(animation, self.add_transition(transition));
1580                self.corner_top_left_radius.insert_transition(rule_id, animation);
1581            }
1582
1583            "corner-top-right-radius" => {
1584                self.corner_top_right_radius
1585                    .insert_animation(animation, self.add_transition(transition));
1586                self.corner_top_right_radius.insert_transition(rule_id, animation);
1587            }
1588
1589            "corner-bottom-left-radius" => {
1590                self.corner_bottom_left_radius
1591                    .insert_animation(animation, self.add_transition(transition));
1592                self.corner_bottom_left_radius.insert_transition(rule_id, animation);
1593            }
1594
1595            "corner-bottom-right-radius" => {
1596                self.corner_bottom_right_radius
1597                    .insert_animation(animation, self.add_transition(transition));
1598                self.corner_bottom_right_radius.insert_transition(rule_id, animation);
1599            }
1600
1601            "outline" => {
1602                self.outline_width.insert_animation(animation, self.add_transition(transition));
1603                self.outline_width.insert_transition(rule_id, animation);
1604                self.outline_color.insert_animation(animation, self.add_transition(transition));
1605                self.outline_color.insert_transition(rule_id, animation);
1606            }
1607
1608            "outline-width" => {
1609                self.outline_width.insert_animation(animation, self.add_transition(transition));
1610                self.outline_width.insert_transition(rule_id, animation);
1611            }
1612
1613            "outline-color" => {
1614                self.outline_color.insert_animation(animation, self.add_transition(transition));
1615                self.outline_color.insert_transition(rule_id, animation);
1616            }
1617
1618            "outline-offset" => {
1619                self.outline_offset.insert_animation(animation, self.add_transition(transition));
1620                self.outline_offset.insert_transition(rule_id, animation);
1621            }
1622
1623            "background-color" => {
1624                self.background_color.insert_animation(animation, self.add_transition(transition));
1625                self.background_color.insert_transition(rule_id, animation);
1626            }
1627
1628            "background-image" => {
1629                self.background_image.insert_animation(animation, self.add_transition(transition));
1630                self.background_image.insert_transition(rule_id, animation);
1631            }
1632
1633            "background-position" => {
1634                self.background_position
1635                    .insert_animation(animation, self.add_transition(transition));
1636                self.background_position.insert_transition(rule_id, animation);
1637            }
1638
1639            "background-size" => {
1640                self.background_size.insert_animation(animation, self.add_transition(transition));
1641                self.background_size.insert_transition(rule_id, animation);
1642            }
1643
1644            "background-repeat" => {
1645                self.background_repeat.insert_animation(animation, self.add_transition(transition));
1646                self.background_repeat.insert_transition(rule_id, animation);
1647            }
1648
1649            "shadow" => {
1650                self.shadow.insert_animation(animation, self.add_transition(transition));
1651                self.shadow.insert_transition(rule_id, animation);
1652            }
1653
1654            "color" => {
1655                self.font_color.insert_animation(animation, self.add_transition(transition));
1656                self.font_color.insert_transition(rule_id, animation);
1657            }
1658
1659            "font-size" => {
1660                self.font_size.insert_animation(animation, self.add_transition(transition));
1661                self.font_size.insert_transition(rule_id, animation);
1662            }
1663
1664            "letter-spacing" => {
1665                self.letter_spacing.insert_animation(animation, self.add_transition(transition));
1666                self.letter_spacing.insert_transition(rule_id, animation);
1667            }
1668
1669            "line-height" => {
1670                self.line_height.insert_animation(animation, self.add_transition(transition));
1671                self.line_height.insert_transition(rule_id, animation);
1672            }
1673
1674            "caret-color" => {
1675                self.caret_color.insert_animation(animation, self.add_transition(transition));
1676                self.caret_color.insert_transition(rule_id, animation);
1677            }
1678
1679            "selection-color" => {
1680                self.selection_color.insert_animation(animation, self.add_transition(transition));
1681                self.selection_color.insert_transition(rule_id, animation);
1682            }
1683
1684            "left" => {
1685                self.left.insert_animation(animation, self.add_transition(transition));
1686                self.left.insert_transition(rule_id, animation);
1687            }
1688
1689            "right" => {
1690                self.right.insert_animation(animation, self.add_transition(transition));
1691                self.right.insert_transition(rule_id, animation);
1692            }
1693
1694            "top" => {
1695                self.top.insert_animation(animation, self.add_transition(transition));
1696                self.top.insert_transition(rule_id, animation);
1697            }
1698
1699            "bottom" => {
1700                self.bottom.insert_animation(animation, self.add_transition(transition));
1701                self.bottom.insert_transition(rule_id, animation);
1702            }
1703
1704            "padding-left" => {
1705                self.padding_left.insert_animation(animation, self.add_transition(transition));
1706                self.padding_left.insert_transition(rule_id, animation);
1707            }
1708
1709            "padding-right" => {
1710                self.padding_right.insert_animation(animation, self.add_transition(transition));
1711                self.padding_right.insert_transition(rule_id, animation);
1712            }
1713
1714            "padding-top" => {
1715                self.padding_top.insert_animation(animation, self.add_transition(transition));
1716                self.padding_top.insert_transition(rule_id, animation);
1717            }
1718
1719            "padding-bottom" => {
1720                self.padding_bottom.insert_animation(animation, self.add_transition(transition));
1721                self.padding_bottom.insert_transition(rule_id, animation);
1722            }
1723
1724            "horizontal-gap" => {
1725                self.horizontal_gap.insert_animation(animation, self.add_transition(transition));
1726                self.horizontal_gap.insert_transition(rule_id, animation);
1727            }
1728
1729            "vertical-gap" => {
1730                self.vertical_gap.insert_animation(animation, self.add_transition(transition));
1731                self.vertical_gap.insert_transition(rule_id, animation);
1732            }
1733
1734            "gap" => {
1735                self.horizontal_gap.insert_animation(animation, self.add_transition(transition));
1736                self.horizontal_gap.insert_transition(rule_id, animation);
1737                self.vertical_gap.insert_animation(animation, self.add_transition(transition));
1738                self.vertical_gap.insert_transition(rule_id, animation);
1739            }
1740
1741            "width" => {
1742                self.width.insert_animation(animation, self.add_transition(transition));
1743                self.width.insert_transition(rule_id, animation);
1744            }
1745
1746            "height" => {
1747                self.height.insert_animation(animation, self.add_transition(transition));
1748                self.height.insert_transition(rule_id, animation);
1749            }
1750
1751            "min-width" => {
1752                self.min_width.insert_animation(animation, self.add_transition(transition));
1753                self.min_width.insert_transition(rule_id, animation);
1754            }
1755
1756            "max-width" => {
1757                self.max_width.insert_animation(animation, self.add_transition(transition));
1758                self.max_width.insert_transition(rule_id, animation);
1759            }
1760
1761            "min-height" => {
1762                self.min_height.insert_animation(animation, self.add_transition(transition));
1763                self.min_height.insert_transition(rule_id, animation);
1764            }
1765
1766            "max-height" => {
1767                self.max_height.insert_animation(animation, self.add_transition(transition));
1768                self.max_height.insert_transition(rule_id, animation);
1769            }
1770
1771            "min-horizontal-gap" => {
1772                self.min_horizontal_gap
1773                    .insert_animation(animation, self.add_transition(transition));
1774                self.min_horizontal_gap.insert_transition(rule_id, animation);
1775            }
1776
1777            "max-horizontal-gap" => {
1778                self.max_horizontal_gap
1779                    .insert_animation(animation, self.add_transition(transition));
1780                self.max_horizontal_gap.insert_transition(rule_id, animation);
1781            }
1782
1783            "min-vertical-gap" => {
1784                self.min_vertical_gap.insert_animation(animation, self.add_transition(transition));
1785                self.min_vertical_gap.insert_transition(rule_id, animation);
1786            }
1787
1788            "max-vertical-gap" => {
1789                self.max_vertical_gap.insert_animation(animation, self.add_transition(transition));
1790                self.max_vertical_gap.insert_transition(rule_id, animation);
1791            }
1792
1793            "text-decoration-color" => {
1794                self.text_decoration_color
1795                    .insert_animation(animation, self.add_transition(transition));
1796                self.text_decoration_color.insert_transition(rule_id, animation);
1797            }
1798
1799            "fill" => {
1800                self.fill.insert_animation(animation, self.add_transition(transition));
1801                self.fill.insert_transition(rule_id, animation);
1802            }
1803
1804            property_name if property_name.starts_with("--") => {
1805                let mut s = DefaultHasher::new();
1806                property_name.hash(&mut s);
1807                let variable_name_hash = s.finish();
1808                // Pre-compute one typed AnimationState per store before taking any mutable
1809                // borrows (add_transition takes &self so this is fine).
1810                let anim_color: AnimationState<Color> = self.add_transition(transition);
1811                let anim_length: AnimationState<LengthOrPercentage> =
1812                    self.add_transition(transition);
1813                let anim_font_size: AnimationState<FontSize> = self.add_transition(transition);
1814                let anim_letter_spacing: AnimationState<LetterSpacing> =
1815                    self.add_transition(transition);
1816                let anim_line_height: AnimationState<LineHeight> = self.add_transition(transition);
1817                let anim_units: AnimationState<Units> = self.add_transition(transition);
1818                let anim_opacity: AnimationState<Opacity> = self.add_transition(transition);
1819
1820                // Register in every custom-property store so whichever store the variable's
1821                // concrete value ends up in will actually animate.
1822                if let Some(store) = self.custom_color_props.get_mut(&variable_name_hash) {
1823                    store.insert_animation(animation, anim_color);
1824                    store.insert_transition(rule_id, animation);
1825                } else {
1826                    let mut store = AnimatableVarSet::default();
1827                    store.insert_animation(animation, anim_color);
1828                    store.insert_transition(rule_id, animation);
1829                    self.custom_color_props.insert(variable_name_hash, store);
1830                }
1831
1832                if let Some(store) = self.custom_length_props.get_mut(&variable_name_hash) {
1833                    store.insert_animation(animation, anim_length);
1834                    store.insert_transition(rule_id, animation);
1835                } else {
1836                    let mut store = AnimatableVarSet::default();
1837                    store.insert_animation(animation, anim_length);
1838                    store.insert_transition(rule_id, animation);
1839                    self.custom_length_props.insert(variable_name_hash, store);
1840                }
1841
1842                if let Some(store) = self.custom_font_size_props.get_mut(&variable_name_hash) {
1843                    store.insert_animation(animation, anim_font_size);
1844                    store.insert_transition(rule_id, animation);
1845                } else {
1846                    let mut store = AnimatableVarSet::default();
1847                    store.insert_animation(animation, anim_font_size);
1848                    store.insert_transition(rule_id, animation);
1849                    self.custom_font_size_props.insert(variable_name_hash, store);
1850                }
1851
1852                if let Some(store) = self.custom_letter_spacing_props.get_mut(&variable_name_hash) {
1853                    store.insert_animation(animation, anim_letter_spacing);
1854                    store.insert_transition(rule_id, animation);
1855                } else {
1856                    let mut store = AnimatableVarSet::default();
1857                    store.insert_animation(animation, anim_letter_spacing);
1858                    store.insert_transition(rule_id, animation);
1859                    self.custom_letter_spacing_props.insert(variable_name_hash, store);
1860                }
1861
1862                if let Some(store) = self.custom_line_height_props.get_mut(&variable_name_hash) {
1863                    store.insert_animation(animation, anim_line_height);
1864                    store.insert_transition(rule_id, animation);
1865                } else {
1866                    let mut store = AnimatableVarSet::default();
1867                    store.insert_animation(animation, anim_line_height);
1868                    store.insert_transition(rule_id, animation);
1869                    self.custom_line_height_props.insert(variable_name_hash, store);
1870                }
1871
1872                if let Some(store) = self.custom_units_props.get_mut(&variable_name_hash) {
1873                    store.insert_animation(animation, anim_units);
1874                    store.insert_transition(rule_id, animation);
1875                } else {
1876                    let mut store = AnimatableVarSet::default();
1877                    store.insert_animation(animation, anim_units);
1878                    store.insert_transition(rule_id, animation);
1879                    self.custom_units_props.insert(variable_name_hash, store);
1880                }
1881
1882                if let Some(store) = self.custom_opacity_props.get_mut(&variable_name_hash) {
1883                    store.insert_animation(animation, anim_opacity);
1884                    store.insert_transition(rule_id, animation);
1885                } else {
1886                    let mut store = AnimatableVarSet::default();
1887                    store.insert_animation(animation, anim_opacity);
1888                    store.insert_transition(rule_id, animation);
1889                    self.custom_opacity_props.insert(variable_name_hash, store);
1890                }
1891            }
1892
1893            _ => {}
1894        }
1895    }
1896
1897    fn insert_property(&mut self, rule_id: Rule, property: &Property) {
1898        fn variable_hash(var: &Variable<'_>) -> u64 {
1899            let mut s = DefaultHasher::new();
1900            var.name.hash(&mut s);
1901            s.finish()
1902        }
1903
1904        fn first_fallback_token<'i>(var: &'i Variable<'i>) -> Option<&'i TokenOrValue<'i>> {
1905            var.fallback.as_ref().and_then(|TokenList(tokens)| tokens.first())
1906        }
1907
1908        fn color_fallback(var: &Variable<'_>) -> Option<Color> {
1909            match first_fallback_token(var) {
1910                Some(TokenOrValue::Color(color)) => Some(*color),
1911                _ => None,
1912            }
1913        }
1914
1915        fn length_fallback(var: &Variable<'_>) -> Option<LengthOrPercentage> {
1916            match first_fallback_token(var) {
1917                Some(TokenOrValue::Token(CssToken::Dimension { value, unit, .. }))
1918                    if unit.as_ref().eq_ignore_ascii_case("px") =>
1919                {
1920                    Some(LengthOrPercentage::Length(Length::Value(LengthValue::Px(*value))))
1921                }
1922
1923                Some(TokenOrValue::Token(CssToken::Percentage { unit_value, .. })) => {
1924                    Some(LengthOrPercentage::Percentage(*unit_value * 100.0))
1925                }
1926
1927                _ => None,
1928            }
1929        }
1930
1931        fn font_size_fallback(var: &Variable<'_>) -> Option<FontSize> {
1932            match first_fallback_token(var) {
1933                Some(TokenOrValue::Token(CssToken::Dimension { value, unit, .. }))
1934                    if unit.as_ref().eq_ignore_ascii_case("px") =>
1935                {
1936                    Some(FontSize(Length::Value(LengthValue::Px(*value))))
1937                }
1938
1939                _ => None,
1940            }
1941        }
1942
1943        fn letter_spacing_fallback(var: &Variable<'_>) -> Option<LetterSpacing> {
1944            match first_fallback_token(var) {
1945                Some(TokenOrValue::Token(CssToken::Dimension { value, unit, .. }))
1946                    if unit.as_ref().eq_ignore_ascii_case("px") =>
1947                {
1948                    Some(LetterSpacing::Length(Length::Value(LengthValue::Px(*value))))
1949                }
1950
1951                Some(TokenOrValue::Token(CssToken::Ident(ident)))
1952                    if ident.as_ref().eq_ignore_ascii_case("normal") =>
1953                {
1954                    Some(LetterSpacing::Normal)
1955                }
1956
1957                _ => None,
1958            }
1959        }
1960
1961        fn line_height_fallback(var: &Variable<'_>) -> Option<LineHeight> {
1962            match first_fallback_token(var) {
1963                Some(TokenOrValue::Token(CssToken::Dimension { value, unit, .. }))
1964                    if unit.as_ref().eq_ignore_ascii_case("px") =>
1965                {
1966                    Some(LineHeight::Length(Length::Value(LengthValue::Px(*value))))
1967                }
1968
1969                Some(TokenOrValue::Token(CssToken::Percentage { unit_value, .. })) => {
1970                    Some(LineHeight::Percentage(*unit_value * 100.0))
1971                }
1972
1973                Some(TokenOrValue::Token(CssToken::Number { value, .. })) => {
1974                    Some(LineHeight::Number(*value))
1975                }
1976
1977                Some(TokenOrValue::Token(CssToken::Ident(ident)))
1978                    if ident.as_ref().eq_ignore_ascii_case("normal") =>
1979                {
1980                    Some(LineHeight::Normal)
1981                }
1982
1983                _ => None,
1984            }
1985        }
1986
1987        fn units_fallback(var: &Variable<'_>) -> Option<Units> {
1988            match first_fallback_token(var) {
1989                Some(TokenOrValue::Token(CssToken::Dimension { value, unit, .. }))
1990                    if unit.as_ref().eq_ignore_ascii_case("px") =>
1991                {
1992                    Some(Units::Pixels(*value))
1993                }
1994
1995                Some(TokenOrValue::Token(CssToken::Dimension { value, unit, .. }))
1996                    if unit.as_ref().eq_ignore_ascii_case("s") =>
1997                {
1998                    Some(Units::Stretch(*value))
1999                }
2000
2001                Some(TokenOrValue::Token(CssToken::Ident(ident)))
2002                    if ident.as_ref().eq_ignore_ascii_case("auto") =>
2003                {
2004                    Some(Units::Auto)
2005                }
2006
2007                Some(TokenOrValue::Token(CssToken::Percentage { unit_value, .. })) => {
2008                    Some(Units::Percentage(*unit_value * 100.0))
2009                }
2010
2011                _ => None,
2012            }
2013        }
2014
2015        fn opacity_fallback(var: &Variable<'_>) -> Option<Opacity> {
2016            match first_fallback_token(var) {
2017                Some(TokenOrValue::Token(CssToken::Percentage { unit_value, .. })) => {
2018                    Some(Opacity(*unit_value))
2019                }
2020
2021                Some(TokenOrValue::Token(CssToken::Number { value, .. })) => Some(Opacity(*value)),
2022
2023                _ => None,
2024            }
2025        }
2026
2027        fn parse_shadow_list(tokens: &[TokenOrValue<'_>]) -> Option<Vec<Shadow>> {
2028            fn parse_shadow_length(token: &TokenOrValue<'_>) -> Option<Length> {
2029                match token {
2030                    TokenOrValue::Token(CssToken::Dimension { value, unit, .. })
2031                        if unit.as_ref().eq_ignore_ascii_case("px") =>
2032                    {
2033                        Some(Length::Value(LengthValue::Px(*value)))
2034                    }
2035
2036                    TokenOrValue::Token(CssToken::Number { value, .. }) if *value == 0.0 => {
2037                        Some(Length::Value(LengthValue::Px(0.0)))
2038                    }
2039
2040                    _ => None,
2041                }
2042            }
2043
2044            fn parse_single_shadow(tokens: &[TokenOrValue<'_>]) -> Option<Shadow> {
2045                let mut lengths: Vec<Length> = Vec::new();
2046                let mut color = None;
2047                let mut inset = false;
2048
2049                for token in tokens {
2050                    match token {
2051                        TokenOrValue::Token(CssToken::WhiteSpace(_)) => {}
2052
2053                        TokenOrValue::Color(c) => {
2054                            if color.is_some() {
2055                                return None;
2056                            }
2057                            color = Some(*c);
2058                        }
2059
2060                        TokenOrValue::Token(CssToken::Ident(ident))
2061                            if ident.as_ref().eq_ignore_ascii_case("inset") =>
2062                        {
2063                            if inset {
2064                                return None;
2065                            }
2066                            inset = true;
2067                        }
2068
2069                        other => {
2070                            let length = parse_shadow_length(other)?;
2071                            lengths.push(length);
2072                        }
2073                    }
2074                }
2075
2076                if !(2..=4).contains(&lengths.len()) {
2077                    return None;
2078                }
2079
2080                let x_offset = lengths[0].clone();
2081                let y_offset = lengths[1].clone();
2082                let blur_radius = lengths.get(2).cloned();
2083                let spread_radius = lengths.get(3).cloned();
2084
2085                Some(Shadow::new(x_offset, y_offset, blur_radius, spread_radius, color, inset))
2086            }
2087
2088            let mut parts = Vec::<&[TokenOrValue<'_>]>::new();
2089            let mut start = 0usize;
2090            for (idx, token) in tokens.iter().enumerate() {
2091                if matches!(token, TokenOrValue::Token(CssToken::Comma)) {
2092                    parts.push(&tokens[start..idx]);
2093                    start = idx + 1;
2094                }
2095            }
2096            parts.push(&tokens[start..]);
2097
2098            let mut parsed = Vec::new();
2099            for part in parts {
2100                let shadow = parse_single_shadow(part)?;
2101                parsed.push(shadow);
2102            }
2103
2104            Some(parsed)
2105        }
2106
2107        fn shadow_fallback(var: &Variable<'_>) -> Option<Vec<Shadow>> {
2108            var.fallback.as_ref().and_then(|TokenList(tokens)| parse_shadow_list(tokens))
2109        }
2110
2111        match property.clone() {
2112            // Display
2113            Property::Display(display) => {
2114                self.display.insert_rule(rule_id, display);
2115            }
2116
2117            // Visibility
2118            Property::Visibility(visibility) => {
2119                self.visibility.insert_rule(rule_id, visibility);
2120            }
2121
2122            // Opacity
2123            Property::Opacity(opacity) => {
2124                self.opacity.insert_rule(rule_id, opacity);
2125            }
2126
2127            // Clipping
2128            Property::ClipPath(clip) => {
2129                self.clip_path.insert_rule(rule_id, clip);
2130            }
2131
2132            // Filters
2133            Property::Filter(filter) => {
2134                self.filter.insert_rule(rule_id, filter);
2135            }
2136
2137            Property::BackdropFilter(filter) => {
2138                self.backdrop_filter.insert_rule(rule_id, filter);
2139            }
2140
2141            // Blend Mode
2142            Property::BlendMode(blend_mode) => {
2143                self.blend_mode.insert_rule(rule_id, blend_mode);
2144            }
2145
2146            // Layout Type
2147            Property::LayoutType(layout_type) => {
2148                self.layout_type.insert_rule(rule_id, layout_type);
2149            }
2150
2151            // Position Type
2152            Property::PositionType(position) => {
2153                self.position_type.insert_rule(rule_id, position);
2154            }
2155
2156            Property::Alignment(alignment) => {
2157                self.alignment.insert_rule(rule_id, alignment);
2158            }
2159
2160            Property::Direction(direction) => {
2161                self.direction.insert_rule(rule_id, direction);
2162            }
2163
2164            Property::Wrap(value) => {
2165                self.wrap.insert_rule(rule_id, value);
2166            }
2167            Property::GridColumns(columns) => {
2168                self.grid_columns.insert_rule(rule_id, columns);
2169            }
2170
2171            Property::GridRows(rows) => {
2172                self.grid_rows.insert_rule(rule_id, rows);
2173            }
2174
2175            Property::ColumnStart(start) => {
2176                self.column_start.insert_rule(rule_id, start);
2177            }
2178
2179            Property::ColumnSpan(span) => {
2180                self.column_span.insert_rule(rule_id, span);
2181            }
2182
2183            Property::RowStart(start) => {
2184                self.row_start.insert_rule(rule_id, start);
2185            }
2186
2187            Property::RowSpan(span) => {
2188                self.row_span.insert_rule(rule_id, span);
2189            }
2190
2191            // Space
2192            Property::Space(space) => {
2193                self.left.insert_rule(rule_id, space);
2194                self.right.insert_rule(rule_id, space);
2195                self.top.insert_rule(rule_id, space);
2196                self.bottom.insert_rule(rule_id, space);
2197            }
2198
2199            Property::Left(left) => {
2200                self.left.insert_rule(rule_id, left);
2201            }
2202
2203            Property::Right(right) => {
2204                self.right.insert_rule(rule_id, right);
2205            }
2206
2207            Property::Top(top) => {
2208                self.top.insert_rule(rule_id, top);
2209            }
2210
2211            Property::Bottom(bottom) => {
2212                self.bottom.insert_rule(rule_id, bottom);
2213            }
2214
2215            // Size
2216            Property::Size(size) => {
2217                self.width.insert_rule(rule_id, size);
2218                self.height.insert_rule(rule_id, size);
2219            }
2220
2221            Property::Width(width) => {
2222                self.width.insert_rule(rule_id, width);
2223            }
2224
2225            Property::Height(height) => {
2226                self.height.insert_rule(rule_id, height);
2227            }
2228
2229            Property::AspectRatio(aspect_ratio) => {
2230                self.aspect_ratio.insert_rule(rule_id, aspect_ratio);
2231            }
2232
2233            // Padding
2234            Property::Padding(padding) => {
2235                self.padding_left.insert_rule(rule_id, padding);
2236                self.padding_right.insert_rule(rule_id, padding);
2237                self.padding_top.insert_rule(rule_id, padding);
2238                self.padding_bottom.insert_rule(rule_id, padding);
2239            }
2240
2241            Property::PaddingLeft(padding_left) => {
2242                self.padding_left.insert_rule(rule_id, padding_left);
2243            }
2244
2245            Property::PaddingRight(padding_right) => {
2246                self.padding_right.insert_rule(rule_id, padding_right);
2247            }
2248
2249            Property::PaddingTop(padding_top) => {
2250                self.padding_top.insert_rule(rule_id, padding_top);
2251            }
2252
2253            Property::PaddingBottom(padding_bottom) => {
2254                self.padding_bottom.insert_rule(rule_id, padding_bottom);
2255            }
2256
2257            Property::VerticalGap(vertical_gap) => {
2258                self.vertical_gap.insert_rule(rule_id, vertical_gap);
2259            }
2260
2261            Property::HorizontalGap(horizontal_gap) => {
2262                self.horizontal_gap.insert_rule(rule_id, horizontal_gap);
2263            }
2264
2265            Property::Gap(gap) => {
2266                self.horizontal_gap.insert_rule(rule_id, gap);
2267                self.vertical_gap.insert_rule(rule_id, gap);
2268            }
2269
2270            // Size Constraints
2271            Property::MinSize(min_size) => {
2272                self.min_width.insert_rule(rule_id, min_size);
2273                self.min_height.insert_rule(rule_id, min_size);
2274            }
2275
2276            Property::MinWidth(min_width) => {
2277                self.min_width.insert_rule(rule_id, min_width);
2278            }
2279
2280            Property::MinHeight(min_height) => {
2281                self.min_height.insert_rule(rule_id, min_height);
2282            }
2283
2284            Property::MaxSize(max_size) => {
2285                self.max_width.insert_rule(rule_id, max_size);
2286                self.max_height.insert_rule(rule_id, max_size);
2287            }
2288
2289            Property::MaxWidth(max_width) => {
2290                self.max_width.insert_rule(rule_id, max_width);
2291            }
2292
2293            Property::MaxHeight(max_height) => {
2294                self.max_height.insert_rule(rule_id, max_height);
2295            }
2296
2297            // Gap Constraints
2298            Property::MinGap(min_gap) => {
2299                self.min_horizontal_gap.insert_rule(rule_id, min_gap);
2300                self.min_vertical_gap.insert_rule(rule_id, min_gap);
2301            }
2302
2303            Property::MinHorizontalGap(min_gap) => {
2304                self.min_horizontal_gap.insert_rule(rule_id, min_gap);
2305            }
2306
2307            Property::MinVerticalGap(min_gap) => {
2308                self.min_vertical_gap.insert_rule(rule_id, min_gap);
2309            }
2310
2311            Property::MaxGap(max_gap) => {
2312                self.max_horizontal_gap.insert_rule(rule_id, max_gap);
2313                self.max_vertical_gap.insert_rule(rule_id, max_gap);
2314            }
2315
2316            Property::MaxHorizontalGap(max_gap) => {
2317                self.max_horizontal_gap.insert_rule(rule_id, max_gap);
2318            }
2319
2320            Property::MaxVerticalGap(max_gap) => {
2321                self.max_vertical_gap.insert_rule(rule_id, max_gap);
2322            }
2323
2324            // Background Colour
2325            Property::BackgroundColor(color) => {
2326                self.background_color.insert_rule(rule_id, color);
2327            }
2328
2329            // Border
2330            Property::Border(border) => {
2331                if let Some(color) = border.color {
2332                    self.border_top_color.insert_rule(rule_id, color);
2333                    self.border_right_color.insert_rule(rule_id, color);
2334                    self.border_bottom_color.insert_rule(rule_id, color);
2335                    self.border_left_color.insert_rule(rule_id, color);
2336                }
2337
2338                if let Some(width) = border.width {
2339                    let w: LengthOrPercentage = width.into();
2340                    self.border_top_width.insert_rule(rule_id, w.clone());
2341                    self.border_right_width.insert_rule(rule_id, w.clone());
2342                    self.border_bottom_width.insert_rule(rule_id, w.clone());
2343                    self.border_left_width.insert_rule(rule_id, w);
2344                }
2345
2346                if let Some(style) = border.style {
2347                    self.border_top_style.insert_rule(rule_id, style.top);
2348                    self.border_right_style.insert_rule(rule_id, style.right);
2349                    self.border_bottom_style.insert_rule(rule_id, style.bottom);
2350                    self.border_left_style.insert_rule(rule_id, style.left);
2351                }
2352            }
2353
2354            // Border side shorthands
2355            Property::BorderTop(border) => {
2356                if let Some(color) = border.color {
2357                    self.border_top_color.insert_rule(rule_id, color);
2358                }
2359                if let Some(width) = border.width {
2360                    self.border_top_width.insert_rule(rule_id, width.into());
2361                }
2362                if let Some(style) = border.style {
2363                    self.border_top_style.insert_rule(rule_id, style.top);
2364                }
2365            }
2366
2367            Property::BorderRight(border) => {
2368                if let Some(color) = border.color {
2369                    self.border_right_color.insert_rule(rule_id, color);
2370                }
2371                if let Some(width) = border.width {
2372                    self.border_right_width.insert_rule(rule_id, width.into());
2373                }
2374                if let Some(style) = border.style {
2375                    self.border_right_style.insert_rule(rule_id, style.right);
2376                }
2377            }
2378
2379            Property::BorderBottom(border) => {
2380                if let Some(color) = border.color {
2381                    self.border_bottom_color.insert_rule(rule_id, color);
2382                }
2383                if let Some(width) = border.width {
2384                    self.border_bottom_width.insert_rule(rule_id, width.into());
2385                }
2386                if let Some(style) = border.style {
2387                    self.border_bottom_style.insert_rule(rule_id, style.bottom);
2388                }
2389            }
2390
2391            Property::BorderLeft(border) => {
2392                if let Some(color) = border.color {
2393                    self.border_left_color.insert_rule(rule_id, color);
2394                }
2395                if let Some(width) = border.width {
2396                    self.border_left_width.insert_rule(rule_id, width.into());
2397                }
2398                if let Some(style) = border.style {
2399                    self.border_left_style.insert_rule(rule_id, style.left);
2400                }
2401            }
2402
2403            // Border Width
2404            Property::BorderWidth(border_width) => {
2405                self.border_top_width.insert_rule(rule_id, border_width.top.0);
2406                self.border_right_width.insert_rule(rule_id, border_width.right.0);
2407                self.border_bottom_width.insert_rule(rule_id, border_width.bottom.0);
2408                self.border_left_width.insert_rule(rule_id, border_width.left.0);
2409            }
2410
2411            Property::BorderTopWidth(width) => {
2412                self.border_top_width.insert_rule(rule_id, width.into());
2413            }
2414
2415            Property::BorderRightWidth(width) => {
2416                self.border_right_width.insert_rule(rule_id, width.into());
2417            }
2418
2419            Property::BorderBottomWidth(width) => {
2420                self.border_bottom_width.insert_rule(rule_id, width.into());
2421            }
2422
2423            Property::BorderLeftWidth(width) => {
2424                self.border_left_width.insert_rule(rule_id, width.into());
2425            }
2426
2427            // Border Color
2428            Property::BorderColor(color) => {
2429                self.border_top_color.insert_rule(rule_id, color);
2430                self.border_right_color.insert_rule(rule_id, color);
2431                self.border_bottom_color.insert_rule(rule_id, color);
2432                self.border_left_color.insert_rule(rule_id, color);
2433            }
2434
2435            Property::BorderTopColor(color) => {
2436                self.border_top_color.insert_rule(rule_id, color);
2437            }
2438
2439            Property::BorderRightColor(color) => {
2440                self.border_right_color.insert_rule(rule_id, color);
2441            }
2442
2443            Property::BorderBottomColor(color) => {
2444                self.border_bottom_color.insert_rule(rule_id, color);
2445            }
2446
2447            Property::BorderLeftColor(color) => {
2448                self.border_left_color.insert_rule(rule_id, color);
2449            }
2450
2451            // Border Style
2452            Property::BorderStyle(style) => {
2453                self.border_top_style.insert_rule(rule_id, style.top);
2454                self.border_right_style.insert_rule(rule_id, style.right);
2455                self.border_bottom_style.insert_rule(rule_id, style.bottom);
2456                self.border_left_style.insert_rule(rule_id, style.left);
2457            }
2458
2459            Property::BorderTopStyle(style) => {
2460                self.border_top_style.insert_rule(rule_id, style);
2461            }
2462
2463            Property::BorderRightStyle(style) => {
2464                self.border_right_style.insert_rule(rule_id, style);
2465            }
2466
2467            Property::BorderBottomStyle(style) => {
2468                self.border_bottom_style.insert_rule(rule_id, style);
2469            }
2470
2471            Property::BorderLeftStyle(style) => {
2472                self.border_left_style.insert_rule(rule_id, style);
2473            }
2474
2475            // Border Radius
2476            Property::CornerRadius(corner_radius) => {
2477                self.corner_bottom_left_radius.insert_rule(rule_id, corner_radius.bottom_left);
2478                self.corner_bottom_right_radius.insert_rule(rule_id, corner_radius.bottom_right);
2479                self.corner_top_left_radius.insert_rule(rule_id, corner_radius.top_left);
2480                self.corner_top_right_radius.insert_rule(rule_id, corner_radius.top_right);
2481            }
2482
2483            Property::CornerBottomLeftRadius(corner_radius) => {
2484                self.corner_bottom_left_radius.insert_rule(rule_id, corner_radius);
2485            }
2486
2487            Property::CornerTopLeftRadius(corner_radius) => {
2488                self.corner_top_left_radius.insert_rule(rule_id, corner_radius);
2489            }
2490
2491            Property::CornerBottomRightRadius(corner_radius) => {
2492                self.corner_bottom_right_radius.insert_rule(rule_id, corner_radius);
2493            }
2494
2495            Property::CornerTopRightRadius(corner_radius) => {
2496                self.corner_top_right_radius.insert_rule(rule_id, corner_radius);
2497            }
2498
2499            // Font Family
2500            Property::FontFamily(font_family) => {
2501                self.font_family.insert_rule(
2502                    rule_id,
2503                    font_family
2504                        .iter()
2505                        .map(|family| match family {
2506                            FontFamily::Named(name) => FamilyOwned::Named(name.to_string()),
2507                            FontFamily::Generic(generic) => FamilyOwned::Generic(*generic),
2508                        })
2509                        .collect::<Vec<_>>(),
2510                );
2511            }
2512
2513            // Font Color
2514            Property::FontColor(font_color) => {
2515                self.font_color.insert_rule(rule_id, font_color);
2516            }
2517
2518            // Font Size
2519            Property::FontSize(font_size) => {
2520                self.font_size.insert_rule(rule_id, font_size);
2521            }
2522
2523            // Letter Spacing
2524            Property::LetterSpacing(letter_spacing) => {
2525                self.letter_spacing.insert_rule(rule_id, letter_spacing);
2526            }
2527
2528            // Font Weight
2529            Property::FontWeight(font_weight) => {
2530                self.font_weight.insert_rule(rule_id, font_weight);
2531            }
2532
2533            // Font Slant
2534            Property::FontSlant(font_slant) => {
2535                self.font_slant.insert_rule(rule_id, font_slant);
2536            }
2537
2538            // Font Width
2539            Property::FontWidth(font_width) => {
2540                self.font_width.insert_rule(rule_id, font_width);
2541            }
2542
2543            // Font Variation Settings
2544            Property::FontVariationSettings(font_variation_settings) => {
2545                self.font_variation_settings.insert_rule(rule_id, font_variation_settings);
2546            }
2547
2548            // Caret Color
2549            Property::CaretColor(caret_color) => {
2550                self.caret_color.insert_rule(rule_id, caret_color);
2551            }
2552
2553            // Selection Color
2554            Property::SelectionColor(selection_color) => {
2555                self.selection_color.insert_rule(rule_id, selection_color);
2556            }
2557
2558            // Transform
2559            Property::Transform(transforms) => {
2560                self.transform.insert_rule(rule_id, transforms);
2561            }
2562
2563            Property::TransformOrigin(transform_origin) => {
2564                let x = transform_origin.x.to_length_or_percentage();
2565                let y = transform_origin.y.to_length_or_percentage();
2566                self.transform_origin.insert_rule(rule_id, Translate { x, y });
2567            }
2568
2569            Property::Translate(translate) => {
2570                self.translate.insert_rule(rule_id, translate);
2571            }
2572
2573            Property::Rotate(rotate) => {
2574                self.rotate.insert_rule(rule_id, rotate);
2575            }
2576
2577            Property::Scale(scale) => {
2578                self.scale.insert_rule(rule_id, scale);
2579            }
2580
2581            // Overflow
2582            Property::Overflow(overflow) => {
2583                self.overflowx.insert_rule(rule_id, overflow);
2584                self.overflowy.insert_rule(rule_id, overflow);
2585            }
2586
2587            Property::OverflowX(overflow) => {
2588                self.overflowx.insert_rule(rule_id, overflow);
2589            }
2590
2591            Property::OverflowY(overflow) => {
2592                self.overflowy.insert_rule(rule_id, overflow);
2593            }
2594
2595            // Z Index
2596            Property::ZIndex(z_index) => self.z_index.insert_rule(rule_id, z_index),
2597
2598            // Outline
2599            Property::Outline(outline) => {
2600                if let Some(outline_color) = outline.color {
2601                    self.outline_color.insert_rule(rule_id, outline_color);
2602                }
2603
2604                if let Some(outline_width) = outline.width {
2605                    self.outline_width.insert_rule(rule_id, outline_width.into());
2606                }
2607            }
2608
2609            Property::OutlineColor(outline_color) => {
2610                self.outline_color.insert_rule(rule_id, outline_color);
2611            }
2612
2613            Property::OutlineWidth(outline_width) => {
2614                self.outline_width.insert_rule(rule_id, outline_width.left.0);
2615            }
2616
2617            Property::OutlineOffset(outline_offset) => {
2618                self.outline_offset.insert_rule(rule_id, outline_offset);
2619            }
2620
2621            // Background Images & Gradients
2622            Property::BackgroundImage(images) => {
2623                let images = images
2624                    .into_iter()
2625                    .filter_map(|img| match img {
2626                        BackgroundImage::None => None,
2627                        BackgroundImage::Gradient(gradient) => {
2628                            Some(ImageOrGradient::Gradient(*gradient))
2629                        }
2630                        BackgroundImage::Url(url) => {
2631                            Some(ImageOrGradient::Image(url.url.to_string()))
2632                        }
2633                    })
2634                    .collect::<Vec<_>>();
2635
2636                self.background_image.insert_rule(rule_id, images);
2637            }
2638
2639            // Background Position
2640            Property::BackgroundPosition(positions) => {
2641                self.background_position.insert_rule(rule_id, positions);
2642            }
2643
2644            // Background Size
2645            Property::BackgroundSize(sizes) => {
2646                self.background_size.insert_rule(rule_id, sizes);
2647            }
2648
2649            // Background Repeat
2650            Property::BackgroundRepeat(repeats) => {
2651                self.background_repeat.insert_rule(rule_id, repeats);
2652            }
2653
2654            // Text Wrapping
2655            Property::TextWrap(text_wrap) => {
2656                self.text_wrap.insert_rule(rule_id, text_wrap);
2657            }
2658
2659            // Text Alignment
2660            Property::TextAlign(text_align) => {
2661                self.text_align.insert_rule(rule_id, text_align);
2662            }
2663
2664            // Box Shadows
2665            Property::Shadow(shadows) => {
2666                self.shadow.insert_rule(rule_id, shadows);
2667            }
2668
2669            // Cursor Icon
2670            Property::Cursor(cursor) => {
2671                self.cursor.insert_rule(rule_id, cursor);
2672            }
2673
2674            Property::PointerEvents(pointer_events) => {
2675                self.pointer_events.insert_rule(rule_id, pointer_events);
2676            }
2677
2678            Property::TextOverflow(text_overflow) => {
2679                self.text_overflow.insert_rule(rule_id, text_overflow);
2680            }
2681            Property::LineHeight(line_height) => {
2682                self.line_height.insert_rule(rule_id, line_height);
2683            }
2684            Property::LineClamp(line_clamp) => {
2685                self.line_clamp.insert_rule(rule_id, line_clamp);
2686            }
2687            Property::TextDecoration(decoration) => {
2688                self.text_decoration_line.insert_rule(rule_id, decoration.line);
2689                self.text_decoration_style.insert_rule(rule_id, decoration.style);
2690                self.text_decoration_color.insert_rule(rule_id, decoration.color.into());
2691            }
2692            Property::TextDecorationLine(line) => {
2693                self.text_decoration_line.insert_rule(rule_id, line);
2694            }
2695            Property::TextDecorationColor(decoration_color) => {
2696                self.text_decoration_color.insert_rule(rule_id, decoration_color);
2697            }
2698            Property::TextDecorationStyle(decoration_style) => {
2699                self.text_decoration_style.insert_rule(rule_id, decoration_style);
2700            }
2701            Property::TextStroke(stroke) => {
2702                self.text_stroke_width.insert_rule(rule_id, stroke.width);
2703                self.text_stroke_style.insert_rule(rule_id, stroke.style);
2704            }
2705            Property::TextStrokeWidth(stroke_width) => {
2706                self.text_stroke_width.insert_rule(rule_id, stroke_width);
2707            }
2708            Property::TextStrokeStyle(stroke_style) => {
2709                self.text_stroke_style.insert_rule(rule_id, stroke_style);
2710            }
2711            Property::Fill(fill) => {
2712                self.fill.insert_rule(rule_id, fill);
2713            }
2714
2715            // Unparsed. TODO: Log the error.
2716            Property::Unparsed(unparsed) => {
2717                macro_rules! parse_color_var {
2718                    ($($prop:expr),+) => {
2719                        if let Some(TokenOrValue::Var(var)) = unparsed.value.0.first() {
2720                            let hash = variable_hash(var);
2721                            let fallback = color_fallback(var);
2722                            $($prop.insert_variable_rule(rule_id, hash, fallback.clone());)+
2723                        }
2724                    };
2725                }
2726                macro_rules! parse_length_var {
2727                    ($($prop:expr),+) => {
2728                        if let Some(TokenOrValue::Var(var)) = unparsed.value.0.first() {
2729                            let hash = variable_hash(var);
2730                            let fallback = length_fallback(var);
2731                            $($prop.insert_variable_rule(rule_id, hash, fallback.clone());)+
2732                        }
2733                    };
2734                }
2735                macro_rules! parse_font_size_var {
2736                    ($prop:expr) => {
2737                        if let Some(TokenOrValue::Var(var)) = unparsed.value.0.first() {
2738                            $prop.insert_variable_rule(
2739                                rule_id,
2740                                variable_hash(var),
2741                                font_size_fallback(var),
2742                            );
2743                        }
2744                    };
2745                }
2746                macro_rules! parse_letter_spacing_var {
2747                    ($prop:expr) => {
2748                        if let Some(TokenOrValue::Var(var)) = unparsed.value.0.first() {
2749                            $prop.insert_variable_rule(
2750                                rule_id,
2751                                variable_hash(var),
2752                                letter_spacing_fallback(var),
2753                            );
2754                        }
2755                    };
2756                }
2757                macro_rules! parse_line_height_var {
2758                    ($prop:expr) => {
2759                        if let Some(TokenOrValue::Var(var)) = unparsed.value.0.first() {
2760                            $prop.insert_variable_rule(
2761                                rule_id,
2762                                variable_hash(var),
2763                                line_height_fallback(var),
2764                            );
2765                        }
2766                    };
2767                }
2768                macro_rules! parse_units_var {
2769                    ($($prop:expr),+) => {
2770                        if let Some(TokenOrValue::Var(var)) = unparsed.value.0.first() {
2771                            let hash = variable_hash(var);
2772                            let fallback = units_fallback(var);
2773                            $($prop.insert_variable_rule(rule_id, hash, fallback.clone());)+
2774                        }
2775                    };
2776                }
2777                match unparsed.name.as_ref() {
2778                    "background-color" => parse_color_var!(self.background_color),
2779                    "border-color" => parse_color_var!(
2780                        self.border_top_color,
2781                        self.border_right_color,
2782                        self.border_bottom_color,
2783                        self.border_left_color
2784                    ),
2785                    "outline-color" => parse_color_var!(self.outline_color),
2786                    "color" => parse_color_var!(self.font_color),
2787                    "caret-color" => parse_color_var!(self.caret_color),
2788                    "selection-color" => parse_color_var!(self.selection_color),
2789                    "fill" => parse_color_var!(self.fill),
2790                    "text-decoration-color" => parse_color_var!(self.text_decoration_color),
2791                    "font-size" => parse_font_size_var!(self.font_size),
2792                    "letter-spacing" => parse_letter_spacing_var!(self.letter_spacing),
2793                    "line-height" => parse_line_height_var!(self.line_height),
2794                    "corner-radius" => parse_length_var!(
2795                        self.corner_top_left_radius,
2796                        self.corner_top_right_radius,
2797                        self.corner_bottom_left_radius,
2798                        self.corner_bottom_right_radius
2799                    ),
2800                    "corner-top-left-radius" => parse_length_var!(self.corner_top_left_radius),
2801                    "corner-top-right-radius" => parse_length_var!(self.corner_top_right_radius),
2802                    "corner-bottom-left-radius" => {
2803                        parse_length_var!(self.corner_bottom_left_radius)
2804                    }
2805                    "corner-bottom-right-radius" => {
2806                        parse_length_var!(self.corner_bottom_right_radius)
2807                    }
2808                    "border-width" => parse_length_var!(
2809                        self.border_top_width,
2810                        self.border_right_width,
2811                        self.border_bottom_width,
2812                        self.border_left_width
2813                    ),
2814                    "border-top-width" => parse_length_var!(self.border_top_width),
2815                    "border-right-width" => parse_length_var!(self.border_right_width),
2816                    "border-bottom-width" => parse_length_var!(self.border_bottom_width),
2817                    "border-left-width" => parse_length_var!(self.border_left_width),
2818                    "border-top-color" => parse_color_var!(self.border_top_color),
2819                    "border-right-color" => parse_color_var!(self.border_right_color),
2820                    "border-bottom-color" => parse_color_var!(self.border_bottom_color),
2821                    "border-left-color" => parse_color_var!(self.border_left_color),
2822                    "border" => {
2823                        if let Some(TokenOrValue::Var(var)) = unparsed.value.0.first() {
2824                            let hash = variable_hash(var);
2825                            let lf = length_fallback(var);
2826                            let cf = color_fallback(var);
2827                            self.border_top_width.insert_variable_rule(rule_id, hash, lf.clone());
2828                            self.border_right_width.insert_variable_rule(rule_id, hash, lf.clone());
2829                            self.border_bottom_width.insert_variable_rule(
2830                                rule_id,
2831                                hash,
2832                                lf.clone(),
2833                            );
2834                            self.border_left_width.insert_variable_rule(rule_id, hash, lf);
2835                            self.border_top_color.insert_variable_rule(rule_id, hash, cf);
2836                            self.border_right_color.insert_variable_rule(rule_id, hash, cf);
2837                            self.border_bottom_color.insert_variable_rule(rule_id, hash, cf);
2838                            self.border_left_color.insert_variable_rule(rule_id, hash, cf);
2839                        }
2840                    }
2841                    "outline" => {
2842                        if let Some(TokenOrValue::Var(var)) = unparsed.value.0.first() {
2843                            let hash = variable_hash(var);
2844                            self.outline_width.insert_variable_rule(
2845                                rule_id,
2846                                hash,
2847                                length_fallback(var),
2848                            );
2849                            self.outline_color.insert_variable_rule(
2850                                rule_id,
2851                                hash,
2852                                color_fallback(var),
2853                            );
2854                        }
2855                    }
2856                    "outline-width" => parse_length_var!(self.outline_width),
2857                    "outline-offset" => parse_length_var!(self.outline_offset),
2858                    "left" => parse_units_var!(self.left),
2859                    "right" => parse_units_var!(self.right),
2860                    "top" => parse_units_var!(self.top),
2861                    "bottom" => parse_units_var!(self.bottom),
2862                    "space" => parse_units_var!(self.left, self.right, self.top, self.bottom),
2863                    "width" => parse_units_var!(self.width),
2864                    "height" => parse_units_var!(self.height),
2865                    "size" => parse_units_var!(self.width, self.height),
2866                    "min-width" => parse_units_var!(self.min_width),
2867                    "max-width" => parse_units_var!(self.max_width),
2868                    "min-height" => parse_units_var!(self.min_height),
2869                    "max-height" => parse_units_var!(self.max_height),
2870                    "min-size" => parse_units_var!(self.min_width, self.min_height),
2871                    "max-size" => parse_units_var!(self.max_width, self.max_height),
2872                    "padding-left" => parse_units_var!(self.padding_left),
2873                    "padding-right" => parse_units_var!(self.padding_right),
2874                    "padding-top" => parse_units_var!(self.padding_top),
2875                    "padding-bottom" => parse_units_var!(self.padding_bottom),
2876                    "padding" => parse_units_var!(
2877                        self.padding_left,
2878                        self.padding_right,
2879                        self.padding_top,
2880                        self.padding_bottom
2881                    ),
2882                    "row-gap" | "vertical-gap" => parse_units_var!(self.vertical_gap),
2883                    "column-gap" | "horizontal-gap" => parse_units_var!(self.horizontal_gap),
2884                    "gap" => parse_units_var!(self.vertical_gap, self.horizontal_gap),
2885                    "min-gap" => {
2886                        parse_units_var!(self.min_horizontal_gap, self.min_vertical_gap)
2887                    }
2888                    "max-gap" => {
2889                        parse_units_var!(self.max_horizontal_gap, self.max_vertical_gap)
2890                    }
2891                    "opacity" => {
2892                        if let Some(TokenOrValue::Var(var)) = unparsed.value.0.first() {
2893                            self.opacity.insert_variable_rule(
2894                                rule_id,
2895                                variable_hash(var),
2896                                opacity_fallback(var),
2897                            );
2898                        }
2899                    }
2900                    "shadow" => {
2901                        if let Some(TokenOrValue::Var(var)) = unparsed.value.0.first() {
2902                            self.shadow.insert_variable_rule(
2903                                rule_id,
2904                                variable_hash(var),
2905                                shadow_fallback(var),
2906                            );
2907                        }
2908                    }
2909                    n => warn!("Unparsed {} {:?}", n, unparsed.value),
2910                }
2911            }
2912
2913            Property::Custom(custom) => {
2914                let mut s = DefaultHasher::new();
2915                custom.name.hash(&mut s);
2916                let variable_name_hash = s.finish();
2917
2918                if let Some(shadows) = parse_shadow_list(&custom.value.0) {
2919                    if let Some(store) = self.custom_shadow_props.get_mut(&variable_name_hash) {
2920                        store.insert_rule(rule_id, shadows);
2921                    } else {
2922                        let mut store = AnimatableVarSet::default();
2923                        store.insert_rule(rule_id, shadows);
2924                        self.custom_shadow_props.insert(variable_name_hash, store);
2925                    }
2926                }
2927
2928                // Parse custom properties and store them
2929                for token in custom.value.0.iter() {
2930                    // Try parsing colors
2931                    if let TokenOrValue::Color(color) = token {
2932                        if let Some(store) = self.custom_color_props.get_mut(&variable_name_hash) {
2933                            store.insert_rule(rule_id, *color);
2934                        } else {
2935                            let mut store = AnimatableVarSet::default();
2936                            store.insert_rule(rule_id, *color);
2937                            self.custom_color_props.insert(variable_name_hash, store);
2938                        }
2939                    }
2940
2941                    // Parse length/percentage tokens into custom_length_props
2942                    match token {
2943                        TokenOrValue::Token(CssToken::Dimension { value, unit, .. }) => {
2944                            let lop = if unit.as_ref().eq_ignore_ascii_case("px") {
2945                                Some(LengthOrPercentage::Length(Length::Value(LengthValue::Px(
2946                                    *value,
2947                                ))))
2948                            } else {
2949                                None
2950                            };
2951                            if let Some(lop) = lop {
2952                                if let Some(store) =
2953                                    self.custom_length_props.get_mut(&variable_name_hash)
2954                                {
2955                                    store.insert_rule(rule_id, lop.clone());
2956                                } else {
2957                                    let mut store = AnimatableVarSet::default();
2958                                    store.insert_rule(rule_id, lop.clone());
2959                                    self.custom_length_props.insert(variable_name_hash, store);
2960                                }
2961                                // Also try storing as FontSize
2962                                let fs = FontSize(Length::Value(LengthValue::Px(*value)));
2963                                if let Some(store) =
2964                                    self.custom_font_size_props.get_mut(&variable_name_hash)
2965                                {
2966                                    store.insert_rule(rule_id, fs);
2967                                } else {
2968                                    let mut store = AnimatableVarSet::default();
2969                                    store.insert_rule(rule_id, fs);
2970                                    self.custom_font_size_props.insert(variable_name_hash, store);
2971                                }
2972                                // Also try storing as LetterSpacing::Length
2973                                let letter_spacing =
2974                                    LetterSpacing::Length(Length::Value(LengthValue::Px(*value)));
2975                                if let Some(store) =
2976                                    self.custom_letter_spacing_props.get_mut(&variable_name_hash)
2977                                {
2978                                    store.insert_rule(rule_id, letter_spacing);
2979                                } else {
2980                                    let mut store = AnimatableVarSet::default();
2981                                    store.insert_rule(rule_id, letter_spacing);
2982                                    self.custom_letter_spacing_props
2983                                        .insert(variable_name_hash, store);
2984                                }
2985                                // Also try storing as LineHeight::Length
2986                                let line_height =
2987                                    LineHeight::Length(Length::Value(LengthValue::Px(*value)));
2988                                if let Some(store) =
2989                                    self.custom_line_height_props.get_mut(&variable_name_hash)
2990                                {
2991                                    store.insert_rule(rule_id, line_height);
2992                                } else {
2993                                    let mut store = AnimatableVarSet::default();
2994                                    store.insert_rule(rule_id, line_height);
2995                                    self.custom_line_height_props.insert(variable_name_hash, store);
2996                                }
2997                                // Also store as Units::Pixels
2998                                let units_val = Units::Pixels(*value);
2999                                if let Some(store) =
3000                                    self.custom_units_props.get_mut(&variable_name_hash)
3001                                {
3002                                    store.insert_rule(rule_id, units_val);
3003                                } else {
3004                                    let mut store = AnimatableVarSet::default();
3005                                    store.insert_rule(rule_id, units_val);
3006                                    self.custom_units_props.insert(variable_name_hash, store);
3007                                }
3008                            } else if unit.as_ref().eq_ignore_ascii_case("s") {
3009                                // "1s" => Units::Stretch(1.0)
3010                                let units_val = Units::Stretch(*value);
3011                                if let Some(store) =
3012                                    self.custom_units_props.get_mut(&variable_name_hash)
3013                                {
3014                                    store.insert_rule(rule_id, units_val);
3015                                } else {
3016                                    let mut store = AnimatableVarSet::default();
3017                                    store.insert_rule(rule_id, units_val);
3018                                    self.custom_units_props.insert(variable_name_hash, store);
3019                                }
3020                            }
3021                        }
3022                        TokenOrValue::Token(CssToken::Ident(ident))
3023                            if ident.as_ref().eq_ignore_ascii_case("auto") =>
3024                        {
3025                            let units_val = Units::Auto;
3026                            if let Some(store) =
3027                                self.custom_units_props.get_mut(&variable_name_hash)
3028                            {
3029                                store.insert_rule(rule_id, units_val);
3030                            } else {
3031                                let mut store = AnimatableVarSet::default();
3032                                store.insert_rule(rule_id, units_val);
3033                                self.custom_units_props.insert(variable_name_hash, store);
3034                            }
3035                        }
3036                        TokenOrValue::Token(CssToken::Percentage { unit_value, .. }) => {
3037                            let lop = LengthOrPercentage::Percentage(*unit_value * 100.0);
3038                            if let Some(store) =
3039                                self.custom_length_props.get_mut(&variable_name_hash)
3040                            {
3041                                store.insert_rule(rule_id, lop);
3042                            } else {
3043                                let mut store = AnimatableVarSet::default();
3044                                store.insert_rule(rule_id, lop);
3045                                self.custom_length_props.insert(variable_name_hash, store);
3046                            }
3047                            // Also store as LineHeight::Percentage
3048                            let line_height = LineHeight::Percentage(*unit_value * 100.0);
3049                            if let Some(store) =
3050                                self.custom_line_height_props.get_mut(&variable_name_hash)
3051                            {
3052                                store.insert_rule(rule_id, line_height);
3053                            } else {
3054                                let mut store = AnimatableVarSet::default();
3055                                store.insert_rule(rule_id, line_height);
3056                                self.custom_line_height_props.insert(variable_name_hash, store);
3057                            }
3058                            // Also store as Units::Percentage
3059                            let units_val = Units::Percentage(*unit_value * 100.0);
3060                            if let Some(store) =
3061                                self.custom_units_props.get_mut(&variable_name_hash)
3062                            {
3063                                store.insert_rule(rule_id, units_val);
3064                            } else {
3065                                let mut store = AnimatableVarSet::default();
3066                                store.insert_rule(rule_id, units_val);
3067                                self.custom_units_props.insert(variable_name_hash, store);
3068                            }
3069                            // Also store as Opacity (percentage as 0..1)
3070                            let opacity_val = Opacity(*unit_value);
3071                            if let Some(store) =
3072                                self.custom_opacity_props.get_mut(&variable_name_hash)
3073                            {
3074                                store.insert_rule(rule_id, opacity_val);
3075                            } else {
3076                                let mut store = AnimatableVarSet::default();
3077                                store.insert_rule(rule_id, opacity_val);
3078                                self.custom_opacity_props.insert(variable_name_hash, store);
3079                            }
3080                        }
3081                        TokenOrValue::Var(var) => {
3082                            let name_hash = variable_hash(var);
3083                            // Store var reference in all maps (type is unknown at parse time)
3084                            if let Some(store) =
3085                                self.custom_color_props.get_mut(&variable_name_hash)
3086                            {
3087                                store.insert_variable_rule(rule_id, name_hash, color_fallback(var));
3088                            } else {
3089                                let mut store = AnimatableVarSet::default();
3090                                store.insert_variable_rule(rule_id, name_hash, color_fallback(var));
3091                                self.custom_color_props.insert(variable_name_hash, store);
3092                            }
3093                            if let Some(store) =
3094                                self.custom_length_props.get_mut(&variable_name_hash)
3095                            {
3096                                store.insert_variable_rule(
3097                                    rule_id,
3098                                    name_hash,
3099                                    length_fallback(var),
3100                                );
3101                            } else {
3102                                let mut store: AnimatableVarSet<LengthOrPercentage> =
3103                                    AnimatableVarSet::default();
3104                                store.insert_variable_rule(
3105                                    rule_id,
3106                                    name_hash,
3107                                    length_fallback(var),
3108                                );
3109                                self.custom_length_props.insert(variable_name_hash, store);
3110                            }
3111                            if let Some(store) =
3112                                self.custom_font_size_props.get_mut(&variable_name_hash)
3113                            {
3114                                store.insert_variable_rule(
3115                                    rule_id,
3116                                    name_hash,
3117                                    font_size_fallback(var),
3118                                );
3119                            } else {
3120                                let mut store: AnimatableVarSet<FontSize> =
3121                                    AnimatableVarSet::default();
3122                                store.insert_variable_rule(
3123                                    rule_id,
3124                                    name_hash,
3125                                    font_size_fallback(var),
3126                                );
3127                                self.custom_font_size_props.insert(variable_name_hash, store);
3128                            }
3129                            if let Some(store) =
3130                                self.custom_letter_spacing_props.get_mut(&variable_name_hash)
3131                            {
3132                                store.insert_variable_rule(
3133                                    rule_id,
3134                                    name_hash,
3135                                    letter_spacing_fallback(var),
3136                                );
3137                            } else {
3138                                let mut store: AnimatableVarSet<LetterSpacing> =
3139                                    AnimatableVarSet::default();
3140                                store.insert_variable_rule(
3141                                    rule_id,
3142                                    name_hash,
3143                                    letter_spacing_fallback(var),
3144                                );
3145                                self.custom_letter_spacing_props.insert(variable_name_hash, store);
3146                            }
3147                            if let Some(store) =
3148                                self.custom_line_height_props.get_mut(&variable_name_hash)
3149                            {
3150                                store.insert_variable_rule(
3151                                    rule_id,
3152                                    name_hash,
3153                                    line_height_fallback(var),
3154                                );
3155                            } else {
3156                                let mut store: AnimatableVarSet<LineHeight> =
3157                                    AnimatableVarSet::default();
3158                                store.insert_variable_rule(
3159                                    rule_id,
3160                                    name_hash,
3161                                    line_height_fallback(var),
3162                                );
3163                                self.custom_line_height_props.insert(variable_name_hash, store);
3164                            }
3165                            if let Some(store) =
3166                                self.custom_units_props.get_mut(&variable_name_hash)
3167                            {
3168                                store.insert_variable_rule(rule_id, name_hash, units_fallback(var));
3169                            } else {
3170                                let mut store: AnimatableVarSet<Units> =
3171                                    AnimatableVarSet::default();
3172                                store.insert_variable_rule(rule_id, name_hash, units_fallback(var));
3173                                self.custom_units_props.insert(variable_name_hash, store);
3174                            }
3175                            if let Some(store) =
3176                                self.custom_opacity_props.get_mut(&variable_name_hash)
3177                            {
3178                                store.insert_variable_rule(
3179                                    rule_id,
3180                                    name_hash,
3181                                    opacity_fallback(var),
3182                                );
3183                            } else {
3184                                let mut store: AnimatableVarSet<Opacity> =
3185                                    AnimatableVarSet::default();
3186                                store.insert_variable_rule(
3187                                    rule_id,
3188                                    name_hash,
3189                                    opacity_fallback(var),
3190                                );
3191                                self.custom_opacity_props.insert(variable_name_hash, store);
3192                            }
3193
3194                            if let Some(store) =
3195                                self.custom_shadow_props.get_mut(&variable_name_hash)
3196                            {
3197                                store.insert_variable_rule(
3198                                    rule_id,
3199                                    name_hash,
3200                                    shadow_fallback(var),
3201                                );
3202                            } else {
3203                                let mut store: AnimatableVarSet<Vec<Shadow>> =
3204                                    AnimatableVarSet::default();
3205                                store.insert_variable_rule(
3206                                    rule_id,
3207                                    name_hash,
3208                                    shadow_fallback(var),
3209                                );
3210                                self.custom_shadow_props.insert(variable_name_hash, store);
3211                            }
3212                        }
3213                        TokenOrValue::Token(CssToken::Number { value, .. }) => {
3214                            // Plain number like 0.5 → Opacity
3215                            let opacity_val = Opacity(*value);
3216                            if let Some(store) =
3217                                self.custom_opacity_props.get_mut(&variable_name_hash)
3218                            {
3219                                store.insert_rule(rule_id, opacity_val);
3220                            } else {
3221                                let mut store = AnimatableVarSet::default();
3222                                store.insert_rule(rule_id, opacity_val);
3223                                self.custom_opacity_props.insert(variable_name_hash, store);
3224                            }
3225
3226                            // Plain number like 1.2 -> LineHeight::Number
3227                            let line_height = LineHeight::Number(*value);
3228                            if let Some(store) =
3229                                self.custom_line_height_props.get_mut(&variable_name_hash)
3230                            {
3231                                store.insert_rule(rule_id, line_height);
3232                            } else {
3233                                let mut store = AnimatableVarSet::default();
3234                                store.insert_rule(rule_id, line_height);
3235                                self.custom_line_height_props.insert(variable_name_hash, store);
3236                            }
3237                        }
3238                        TokenOrValue::Token(CssToken::Ident(ident))
3239                            if ident.as_ref().eq_ignore_ascii_case("normal") =>
3240                        {
3241                            let line_height = LineHeight::Normal;
3242                            if let Some(store) =
3243                                self.custom_line_height_props.get_mut(&variable_name_hash)
3244                            {
3245                                store.insert_rule(rule_id, line_height);
3246                            } else {
3247                                let mut store = AnimatableVarSet::default();
3248                                store.insert_rule(rule_id, line_height);
3249                                self.custom_line_height_props.insert(variable_name_hash, store);
3250                            }
3251
3252                            let letter_spacing = LetterSpacing::Normal;
3253                            if let Some(store) =
3254                                self.custom_letter_spacing_props.get_mut(&variable_name_hash)
3255                            {
3256                                store.insert_rule(rule_id, letter_spacing);
3257                            } else {
3258                                let mut store = AnimatableVarSet::default();
3259                                store.insert_rule(rule_id, letter_spacing);
3260                                self.custom_letter_spacing_props.insert(variable_name_hash, store);
3261                            }
3262                        }
3263                        _ => {}
3264                    }
3265                }
3266            }
3267            _ => {}
3268        }
3269    }
3270
3271    // Helper function for generating AnimationState from a transition definition.
3272    fn add_transition<T: Default + Interpolator>(
3273        &self,
3274        transition: &Transition,
3275    ) -> AnimationState<T> {
3276        let timing_function = transition
3277            .timing_function
3278            .map(|easing| match easing {
3279                EasingFunction::Linear => TimingFunction::linear(),
3280                EasingFunction::Ease => TimingFunction::ease(),
3281                EasingFunction::EaseIn => TimingFunction::ease_in(),
3282                EasingFunction::EaseOut => TimingFunction::ease_out(),
3283                EasingFunction::EaseInOut => TimingFunction::ease_in_out(),
3284                EasingFunction::CubicBezier(x1, y1, x2, y2) => TimingFunction::new(x1, y1, x2, y2),
3285            })
3286            .unwrap_or_default();
3287
3288        AnimationState::new(Animation::null())
3289            .with_duration(transition.duration)
3290            .with_delay(transition.delay.unwrap_or_default())
3291            .with_keyframe(Keyframe { time: 0.0, value: Default::default(), timing_function })
3292            .with_keyframe(Keyframe { time: 1.0, value: Default::default(), timing_function })
3293    }
3294
3295    // Add style data for the given entity.
3296    pub(crate) fn add(&mut self, entity: Entity) {
3297        self.pseudo_classes.insert(entity, PseudoClassFlags::VALID);
3298        self.classes.insert(entity, HashSet::new());
3299        self.abilities.insert(entity, Abilities::default());
3300        self.system_flags = SystemFlags::RELAYOUT;
3301        // Adding an entity is a structural change. Relayout incrementally from the new entity;
3302        // morphorm restarts from at least its parent, which repositions all of the parent's
3303        // children. Marking the root here would force a full tree relayout on every view creation.
3304        self.relayout.insert(entity);
3305        self.restyle.insert(entity);
3306        self.reaccess.insert(entity);
3307        self.retransform.insert(entity);
3308        self.reclip.insert(entity);
3309    }
3310
3311    // Remove style data for the given entity.
3312    pub(crate) fn remove(&mut self, entity: Entity) {
3313        self.matched_rules.remove(&entity);
3314        self.restyle.remove(&entity);
3315        self.relayout.remove(&entity);
3316        self.retransform.remove(&entity);
3317        self.reclip.remove(&entity);
3318        self.laid_out.remove(&entity);
3319        self.element.remove(entity);
3320        self.ids.remove(entity);
3321        self.classes.remove(entity);
3322        self.pseudo_classes.remove(entity);
3323        self.disabled.remove(entity);
3324        self.abilities.remove(entity);
3325
3326        self.name.remove(entity);
3327        self.role.remove(entity);
3328        // self.default_action_verb.remove(entity);
3329        self.live.remove(entity);
3330        self.labelled_by.remove(entity);
3331        self.described_by.remove(entity);
3332        self.controls.remove(entity);
3333        self.active_descendant.remove(entity);
3334        self.expanded.remove(entity);
3335        self.selected.remove(entity);
3336        self.multiselectable.remove(entity);
3337        self.sort_direction.remove(entity);
3338        self.level.remove(entity);
3339        self.size_of_set.remove(entity);
3340        self.position_in_set.remove(entity);
3341        self.hidden.remove(entity);
3342        self.orientation.remove(entity);
3343        self.text_value.remove(entity);
3344        self.numeric_value.remove(entity);
3345
3346        // Display
3347        self.display.remove(entity);
3348        // Visibility
3349        self.visibility.remove(entity);
3350        // Opacity
3351        self.opacity.remove(entity);
3352        // Z Order
3353        self.z_index.remove(entity);
3354        self.ignore_clipping.remove(entity);
3355        // Clipping
3356        self.clip_path.remove(entity);
3357
3358        self.overflowx.remove(entity);
3359        self.overflowy.remove(entity);
3360
3361        // Filters
3362        self.filter.remove(entity);
3363        self.backdrop_filter.remove(entity);
3364        self.filter_entities.remove(&entity);
3365
3366        // Blend Mode
3367        self.blend_mode.remove(entity);
3368
3369        // Transform
3370        self.transform.remove(entity);
3371        self.transform_origin.remove(entity);
3372        self.translate.remove(entity);
3373        self.rotate.remove(entity);
3374        self.scale.remove(entity);
3375
3376        // Border widths
3377        self.border_top_width.remove(entity);
3378        self.border_right_width.remove(entity);
3379        self.border_bottom_width.remove(entity);
3380        self.border_left_width.remove(entity);
3381        // Border colors
3382        self.border_top_color.remove(entity);
3383        self.border_right_color.remove(entity);
3384        self.border_bottom_color.remove(entity);
3385        self.border_left_color.remove(entity);
3386        // Border styles
3387        self.border_top_style.remove(entity);
3388        self.border_right_style.remove(entity);
3389        self.border_bottom_style.remove(entity);
3390        self.border_left_style.remove(entity);
3391
3392        // Corner Radius
3393        self.corner_bottom_left_radius.remove(entity);
3394        self.corner_bottom_right_radius.remove(entity);
3395        self.corner_top_left_radius.remove(entity);
3396        self.corner_top_right_radius.remove(entity);
3397
3398        // Outline
3399        self.outline_width.remove(entity);
3400        self.outline_color.remove(entity);
3401        self.outline_offset.remove(entity);
3402
3403        // Background
3404        self.background_color.remove(entity);
3405        self.background_image.remove(entity);
3406        self.background_image_entities.remove(&entity);
3407        self.background_position.remove(entity);
3408        self.background_repeat.remove(entity);
3409        self.background_size.remove(entity);
3410
3411        // Box Shadow
3412        self.shadow.remove(entity);
3413
3414        // Text and Font
3415        self.text.remove(entity);
3416        self.text_wrap.remove(entity);
3417        self.text_overflow.remove(entity);
3418        self.letter_spacing.remove(entity);
3419        self.line_height.remove(entity);
3420        self.line_clamp.remove(entity);
3421        self.text_align.remove(entity);
3422        self.font_family.remove(entity);
3423        self.font_color.remove(entity);
3424        self.font_size.remove(entity);
3425        self.font_weight.remove(entity);
3426        self.font_slant.remove(entity);
3427        self.font_width.remove(entity);
3428        self.font_variation_settings.remove(entity);
3429        self.caret_color.remove(entity);
3430        self.selection_color.remove(entity);
3431        self.text_decoration_line.remove(entity);
3432        self.text_decoration_style.remove(entity);
3433        self.text_decoration_color.remove(entity);
3434        self.text_stroke_width.remove(entity);
3435        self.text_stroke_style.remove(entity);
3436
3437        // Cursor
3438        self.cursor.remove(entity);
3439
3440        self.pointer_events.remove(entity);
3441
3442        // Layout Type
3443        self.layout_type.remove(entity);
3444
3445        // Position Type
3446        self.position_type.remove(entity);
3447
3448        self.alignment.remove(entity);
3449        self.direction.remove(entity);
3450        self.wrap.remove(entity);
3451
3452        // Grid
3453        self.grid_columns.remove(entity);
3454        self.grid_rows.remove(entity);
3455        self.column_start.remove(entity);
3456        self.column_span.remove(entity);
3457        self.row_start.remove(entity);
3458        self.row_span.remove(entity);
3459
3460        // Space
3461        self.left.remove(entity);
3462        self.right.remove(entity);
3463        self.top.remove(entity);
3464        self.bottom.remove(entity);
3465
3466        // Padding
3467        self.padding_left.remove(entity);
3468        self.padding_right.remove(entity);
3469        self.padding_top.remove(entity);
3470        self.padding_bottom.remove(entity);
3471        self.vertical_gap.remove(entity);
3472        self.horizontal_gap.remove(entity);
3473
3474        // Size
3475        self.width.remove(entity);
3476        self.height.remove(entity);
3477        self.aspect_ratio.remove(entity);
3478
3479        // Size Constraints
3480        self.min_width.remove(entity);
3481        self.max_width.remove(entity);
3482        self.min_height.remove(entity);
3483        self.max_height.remove(entity);
3484
3485        self.min_horizontal_gap.remove(entity);
3486        self.max_horizontal_gap.remove(entity);
3487        self.min_vertical_gap.remove(entity);
3488        self.max_vertical_gap.remove(entity);
3489
3490        self.text_range.remove(entity);
3491        self.text_span.remove(entity);
3492
3493        self.fill.remove(entity);
3494
3495        // Remove per-entity data from custom property stores
3496        for store in self.custom_color_props.values_mut() {
3497            store.remove(entity);
3498        }
3499        for store in self.custom_length_props.values_mut() {
3500            store.remove(entity);
3501        }
3502        for store in self.custom_font_size_props.values_mut() {
3503            store.remove(entity);
3504        }
3505        for store in self.custom_letter_spacing_props.values_mut() {
3506            store.remove(entity);
3507        }
3508        for store in self.custom_line_height_props.values_mut() {
3509            store.remove(entity);
3510        }
3511        for store in self.custom_units_props.values_mut() {
3512            store.remove(entity);
3513        }
3514        for store in self.custom_opacity_props.values_mut() {
3515            store.remove(entity);
3516        }
3517    }
3518
3519    /// Returns the entities in `tree` which currently have a background image, ancestors first.
3520    ///
3521    /// Entities which no longer have a background image are dropped from the tracking set.
3522    pub(crate) fn background_image_entities(&mut self, tree: &Tree<Entity>) -> Vec<Entity> {
3523        let background_image = &self.background_image;
3524        tracked_entities(&mut self.background_image_entities, tree, |entity| {
3525            background_image.get(entity).is_some()
3526        })
3527    }
3528
3529    /// Returns the entities in `tree` which currently have a filter or backdrop filter,
3530    /// ancestors first.
3531    ///
3532    /// Entities which no longer have a filter are dropped from the tracking set.
3533    pub(crate) fn filter_entities(&mut self, tree: &Tree<Entity>) -> Vec<Entity> {
3534        let (filter, backdrop_filter) = (&self.filter, &self.backdrop_filter);
3535        tracked_entities(&mut self.filter_entities, tree, |entity| {
3536            filter.get(entity).is_some() || backdrop_filter.get(entity).is_some()
3537        })
3538    }
3539
3540    /// Takes the set of entities which need to be restyled.
3541    pub(crate) fn take_restyle(&mut self) -> HashSet<Entity> {
3542        self.restyle_sibling_parents.clear();
3543        std::mem::take(&mut self.restyle)
3544    }
3545
3546    pub(crate) fn needs_restyle(&mut self, entity: Entity) {
3547        if entity == Entity::null() || self.restyle.contains(&entity) {
3548            return;
3549        }
3550        self.restyle.insert(entity);
3551    }
3552
3553    pub(crate) fn needs_relayout(&mut self, entity: Entity) {
3554        self.relayout.insert(entity);
3555    }
3556
3557    pub(crate) fn needs_access_update(&mut self, entity: Entity) {
3558        self.reaccess.insert(entity);
3559    }
3560
3561    pub(crate) fn needs_text_update(&mut self, entity: Entity) {
3562        self.text_construction.insert(entity);
3563        self.text_layout.insert(entity);
3564    }
3565
3566    pub(crate) fn needs_text_layout(&mut self, entity: Entity) {
3567        self.text_layout.insert(entity);
3568    }
3569
3570    pub(crate) fn needs_retransform(&mut self, entity: Entity) {
3571        self.retransform.insert(entity);
3572    }
3573
3574    pub(crate) fn needs_reclip(&mut self, entity: Entity) {
3575        self.reclip.insert(entity);
3576    }
3577
3578    // pub fn should_redraw<F: FnOnce()>(&mut self, f: F) {
3579    //     if !self.redraw_list.is_empty() {
3580    //         f();
3581    //     }
3582    // }
3583
3584    // Remove all shared style data.
3585    pub(crate) fn clear_style_rules(&mut self) {
3586        self.disabled.clear_rules();
3587        // Display
3588        self.display.clear_rules();
3589        // Visibility
3590        self.visibility.clear_rules();
3591        // Opacity
3592        self.opacity.clear_rules();
3593        // Z Order
3594        self.z_index.clear_rules();
3595
3596        // Clipping
3597        self.clip_path.clear_rules();
3598
3599        // Filters
3600        self.filter.clear_rules();
3601        self.backdrop_filter.clear_rules();
3602
3603        // Blend Mode
3604        self.blend_mode.clear_rules();
3605
3606        // Transform
3607        self.transform.clear_rules();
3608        self.transform_origin.clear_rules();
3609        self.translate.clear_rules();
3610        self.rotate.clear_rules();
3611        self.scale.clear_rules();
3612
3613        self.overflowx.clear_rules();
3614        self.overflowy.clear_rules();
3615
3616        // Border widths
3617        self.border_top_width.clear_rules();
3618        self.border_right_width.clear_rules();
3619        self.border_bottom_width.clear_rules();
3620        self.border_left_width.clear_rules();
3621        // Border colors
3622        self.border_top_color.clear_rules();
3623        self.border_right_color.clear_rules();
3624        self.border_bottom_color.clear_rules();
3625        self.border_left_color.clear_rules();
3626        // Border styles
3627        self.border_top_style.clear_rules();
3628        self.border_right_style.clear_rules();
3629        self.border_bottom_style.clear_rules();
3630        self.border_left_style.clear_rules();
3631
3632        // Corner Radius
3633        self.corner_bottom_left_radius.clear_rules();
3634        self.corner_bottom_right_radius.clear_rules();
3635        self.corner_top_left_radius.clear_rules();
3636        self.corner_top_right_radius.clear_rules();
3637
3638        // Outline
3639        self.outline_width.clear_rules();
3640        self.outline_color.clear_rules();
3641        self.outline_offset.clear_rules();
3642
3643        // Background
3644        self.background_color.clear_rules();
3645        self.background_image.clear_rules();
3646        self.background_position.clear_rules();
3647        self.background_repeat.clear_rules();
3648        self.background_size.clear_rules();
3649
3650        self.shadow.clear_rules();
3651
3652        self.layout_type.clear_rules();
3653        self.position_type.clear_rules();
3654        self.alignment.clear_rules();
3655        self.direction.clear_rules();
3656        self.wrap.clear_rules();
3657
3658        // Grid
3659        self.grid_columns.clear_rules();
3660        self.grid_rows.clear_rules();
3661        self.column_start.clear_rules();
3662        self.column_span.clear_rules();
3663
3664        // Space
3665        self.left.clear_rules();
3666        self.right.clear_rules();
3667        self.top.clear_rules();
3668        self.bottom.clear_rules();
3669
3670        // Size
3671        self.width.clear_rules();
3672        self.height.clear_rules();
3673        self.aspect_ratio.clear_rules();
3674
3675        // Size Constraints
3676        self.min_width.clear_rules();
3677        self.max_width.clear_rules();
3678        self.min_height.clear_rules();
3679        self.max_height.clear_rules();
3680
3681        self.min_horizontal_gap.clear_rules();
3682        self.max_horizontal_gap.clear_rules();
3683        self.min_vertical_gap.clear_rules();
3684        self.max_vertical_gap.clear_rules();
3685
3686        // Padding
3687        self.padding_left.clear_rules();
3688        self.padding_right.clear_rules();
3689        self.padding_top.clear_rules();
3690        self.padding_bottom.clear_rules();
3691        self.horizontal_gap.clear_rules();
3692        self.vertical_gap.clear_rules();
3693
3694        // Text and Font
3695        self.text_wrap.clear_rules();
3696        self.text_overflow.clear_rules();
3697        self.letter_spacing.clear_rules();
3698        self.line_height.clear_rules();
3699        self.line_clamp.clear_rules();
3700        self.text_align.clear_rules();
3701        self.font_family.clear_rules();
3702        self.font_weight.clear_rules();
3703        self.font_slant.clear_rules();
3704        self.font_color.clear_rules();
3705        self.font_size.clear_rules();
3706        self.font_variation_settings.clear_rules();
3707        self.selection_color.clear_rules();
3708        self.caret_color.clear_rules();
3709        self.text_decoration_line.clear_rules();
3710        self.text_decoration_style.clear_rules();
3711        self.text_decoration_color.clear_rules();
3712        self.text_stroke_width.clear_rules();
3713        self.text_stroke_style.clear_rules();
3714
3715        self.cursor.clear_rules();
3716
3717        self.pointer_events.clear_rules();
3718
3719        self.name.clear_rules();
3720
3721        self.fill.clear_rules();
3722
3723        // Clear all custom property rule data on stylesheet reload
3724        for store in self.custom_color_props.values_mut() {
3725            store.clear_rules();
3726        }
3727        self.custom_color_props
3728            .retain(|_, store| !store.shared_data.is_empty() || !store.inline_data.is_empty());
3729        for store in self.custom_length_props.values_mut() {
3730            store.clear_rules();
3731        }
3732        self.custom_length_props
3733            .retain(|_, store| !store.shared_data.is_empty() || !store.inline_data.is_empty());
3734        for store in self.custom_font_size_props.values_mut() {
3735            store.clear_rules();
3736        }
3737        self.custom_font_size_props
3738            .retain(|_, store| !store.shared_data.is_empty() || !store.inline_data.is_empty());
3739        for store in self.custom_letter_spacing_props.values_mut() {
3740            store.clear_rules();
3741        }
3742        self.custom_letter_spacing_props
3743            .retain(|_, store| !store.shared_data.is_empty() || !store.inline_data.is_empty());
3744        for store in self.custom_line_height_props.values_mut() {
3745            store.clear_rules();
3746        }
3747        self.custom_line_height_props
3748            .retain(|_, store| !store.shared_data.is_empty() || !store.inline_data.is_empty());
3749        for store in self.custom_units_props.values_mut() {
3750            store.clear_rules();
3751        }
3752        self.custom_units_props
3753            .retain(|_, store| !store.shared_data.is_empty() || !store.inline_data.is_empty());
3754        for store in self.custom_opacity_props.values_mut() {
3755            store.clear_rules();
3756        }
3757        self.custom_opacity_props
3758            .retain(|_, store| !store.shared_data.is_empty() || !store.inline_data.is_empty());
3759    }
3760}
3761
3762/// Drops entities from a tracking set which no longer satisfy `has_value` and returns the
3763/// remaining entities which are in `tree`, ancestors first.
3764fn tracked_entities(
3765    set: &mut HashSet<Entity>,
3766    tree: &Tree<Entity>,
3767    has_value: impl Fn(Entity) -> bool,
3768) -> Vec<Entity> {
3769    set.retain(|entity| has_value(*entity));
3770
3771    let mut entities: Vec<Entity> = set
3772        .iter()
3773        .copied()
3774        .filter(|entity| *entity == Entity::root() || tree.get_parent(*entity).is_some())
3775        .collect();
3776    tree.sort_by_depth(&mut entities);
3777    entities
3778}