| 1 | // CSS animations and transitions, and the effects of Web Animations (`Element.animate()`), sampled |
| 2 | // into per-element style overrides. The renderer reads each element's declared animation and |
| 3 | // transition properties out of the document, hands them to the Animator after every build, and |
| 4 | // writes what `sample` returns back as inline declarations. Nothing here touches litehtml. |
| 5 | // Ported from twidget's WidgetAnimations.swift and WidgetWebAnimations.swift. Times are seconds on |
| 6 | // the renderer's clock unless a name says milliseconds. |
| 7 | #pragma once |
| 8 | |
| 9 | #include "twidget/json.h" |
| 10 | |
| 11 | #include <cstdint> |
| 12 | #include <functional> |
| 13 | #include <map> |
| 14 | #include <optional> |
| 15 | #include <string> |
| 16 | #include <string_view> |
| 17 | #include <unordered_map> |
| 18 | #include <unordered_set> |
| 19 | #include <vector> |
| 20 | |
| 21 | namespace twlite { |
| 22 | |
| 23 | /// (property, value) in the order they apply; a later entry of the same property wins. |
| 24 | using StyleValues = std::vector<std::pair<std::string, std::string>>; |
| 25 | /// Animated declarations per widget node id. |
| 26 | using StyleOverrides = std::map<int64_t, StyleValues>; |
| 27 | /// An element's declared values of the watched properties, as written. |
| 28 | using Declared = std::unordered_map<std::string, std::string>; |
| 29 | |
| 30 | /// An easing function. `css` follows CSS animations and transitions (clamped to [0, 1] input); |
| 31 | /// `web` is the Web Animations form with the before flag and extrapolated cubic-béziers. |
| 32 | struct Timing { |
| 33 | enum Kind { Linear, Bezier, Steps, Points } kind = Linear; |
| 34 | enum Jump { JumpEnd, JumpStart, JumpBoth, JumpNone } jump = JumpEnd; |
| 35 | double x1 = 0, y1 = 0, x2 = 1, y2 = 1; |
| 36 | int steps = 1; |
| 37 | std::vector<std::pair<double, double>> points; // linear(): (input, output), inputs non-decreasing |
| 38 | std::string text = "linear"; |
| 39 | |
| 40 | double css(double x) const; |
| 41 | double web(double x, bool before) const; |
| 42 | |
| 43 | static std::optional<Timing> parse(std::string_view text); |
| 44 | static Timing ease(); |
| 45 | }; |
| 46 | |
| 47 | struct Keyframe { |
| 48 | double offset = 0; |
| 49 | StyleValues declarations; |
| 50 | std::optional<Timing> timing; |
| 51 | }; |
| 52 | using KeyframeRules = std::unordered_map<std::string, std::vector<Keyframe>>; |
| 53 | /// Collects every `@keyframes` (and `@-webkit-keyframes`) block of a style sheet. |
| 54 | void parse_keyframes(std::string_view css, KeyframeRules& rules); |
| 55 | |
| 56 | struct AnimationSpec { |
| 57 | std::string name = "none"; |
| 58 | double duration = 0; |
| 59 | std::string timing_text = "ease"; |
| 60 | double delay = 0; |
| 61 | double iterations = 1; |
| 62 | std::string direction = "normal"; |
| 63 | std::string fill = "none"; |
| 64 | bool paused = false; |
| 65 | |
| 66 | Timing timing() const; |
| 67 | bool operator==(const AnimationSpec&) const = default; |
| 68 | static std::vector<AnimationSpec> list(const Declared& declarations); |
| 69 | }; |
| 70 | |
| 71 | struct TransitionSpec { |
| 72 | std::string property; |
| 73 | double duration = 0; |
| 74 | Timing timing = Timing::ease(); |
| 75 | double delay = 0; |
| 76 | static std::vector<TransitionSpec> list(const Declared& declarations); |
| 77 | }; |
| 78 | |
| 79 | /// Interpolates two CSS values number by number when they have the same shape (colours normalised, |
| 80 | /// `none` transforms expanded to identities, shorter transform lists padded); otherwise flips at 0.5. |
| 81 | std::string interpolate(const std::string& a, const std::string& b, double t); |
| 82 | |
| 83 | /// One `Element.animate()` effect as the sandbox reports it. Times in milliseconds; `start_ms` is |
| 84 | /// already on the renderer's clock. |
| 85 | struct WebAnimation { |
| 86 | struct Frame { |
| 87 | double offset = 0; |
| 88 | Timing easing; |
| 89 | std::string composite; // "" = the effect's |
| 90 | StyleValues values; |
| 91 | }; |
| 92 | int64_t aid = 0; |
| 93 | int64_t target = 0; |
| 94 | std::vector<Frame> frames; |
| 95 | double duration = 0, delay = 0, end_delay = 0, iterations = 1, iteration_start = 0; |
| 96 | std::string direction = "normal", fill = "none"; |
| 97 | Timing easing; |
| 98 | std::string composite = "replace", iteration_composite = "replace"; |
| 99 | std::optional<double> start_ms, hold_ms; |
| 100 | double rate = 1; |
| 101 | std::vector<std::string> properties; |
| 102 | |
| 103 | bool running() const { return !hold_ms && start_ms && rate != 0; } |
| 104 | /// Validated like twidget's WebAnimation.parse; nullopt drops the record. |
| 105 | static std::optional<WebAnimation> parse(const twidget::Json& record, double clock_offset_ms); |
| 106 | }; |
| 107 | |
| 108 | struct AnimationEvent { |
| 109 | std::string type; // animationstart, animationiteration, animationend, transitionend |
| 110 | int64_t target = 0; |
| 111 | std::string name; // animationName, or the transition's propertyName |
| 112 | double elapsed = 0; |
| 113 | }; |
| 114 | |
| 115 | class Animator { |
| 116 | public: |
| 117 | /// Declared properties the renderer reads per element; transitions are watched on these. |
| 118 | static const std::vector<std::string>& watched(); |
| 119 | /// An element is read only when it declares one of these (or is a Web Animations target). |
| 120 | static const std::vector<std::string>& entry_properties(); |
| 121 | |
| 122 | void set_style_sheets(const std::vector<std::string>& texts); |
| 123 | /// The declared values of every element after a build; starts, restarts and stops animations |
| 124 | /// and starts transitions where a watched value changed. |
| 125 | void update(const std::unordered_map<int64_t, Declared>& declarations, double now); |
| 126 | /// Style overrides at `now`, and the animation events crossed since the last sample. |
| 127 | StyleOverrides sample(double now); |
| 128 | /// Something will change on the next frame. |
| 129 | bool active(double now) const; |
| 130 | /// The same, leaving out the elements in `skip`. |
| 131 | bool active(double now, const std::unordered_set<int64_t>& skip) const; |
| 132 | std::vector<AnimationEvent> take_events() { return std::move(events_); } |
| 133 | |
| 134 | // What a compositor needs to play an element's animations without the core (compositor.h). |
| 135 | |
| 136 | /// Elements whose animations, transitions or Web Animations still change something at `now`: |
| 137 | /// not ended, paused, held or finished. |
| 138 | std::vector<int64_t> animated_ids(double now) const; |
| 139 | /// Every property the animations of `id` write. |
| 140 | std::vector<std::string> animated_properties(int64_t id) const; |
| 141 | /// What `sample` would write for `id` at `t`, without events and without changing any state. |
| 142 | StyleValues peek(int64_t id, double t) const; |
| 143 | /// `property` of `id` without its animations: as declared, or the property's initial value. |
| 144 | std::string base_value(int64_t id, const std::string& property) const { return underlying_value(id, property); } |
| 145 | struct Plan { |
| 146 | /// False while an animation still waits out its delay: nothing to hand over before `wake`. |
| 147 | bool ready = true; |
| 148 | /// The span to bake, on the renderer clock: up to the first end, or one loop when the |
| 149 | /// animations repeat forever. |
| 150 | double start = 0, duration = 0; |
| 151 | bool repeat = false; |
| 152 | /// When the core must sample `id` again: an end, a delay running out, or with `iterations` |
| 153 | /// the next animationiteration. |
| 154 | double wake = 0; |
| 155 | }; |
| 156 | /// Nullopt when there is nothing to hand over, or when the timing cannot be baked: a reversed |
| 157 | /// playback rate, two endless loops of different lengths, accumulating iterations. |
| 158 | std::optional<Plan> plan(int64_t id, double now, bool iterations) const; |
| 159 | /// Changes whenever anything that decides how `id` animates changes; equal values bake equal tracks. |
| 160 | uint64_t signature(int64_t id) const; |
| 161 | |
| 162 | /// `{type:'anim', now, anims}`: Web Animations records, or script control of CSS animations |
| 163 | /// and transitions (`css`). `clock_offset_ms` maps the sandbox clock onto the renderer's. |
| 164 | void apply_web(const twidget::Json& message, double clock_offset_ms); |
| 165 | /// Targets of Web Animations, whose underlying values the renderer must supply. |
| 166 | std::vector<int64_t> web_targets() const; |
| 167 | /// Properties the Web Animations on `id` animate. |
| 168 | std::vector<std::string> web_properties(int64_t id) const; |
| 169 | /// The `anims` query: running CSS animations and transitions of `ids`. |
| 170 | twidget::Json css_animations(const std::vector<int64_t>& ids, double now) const; |
| 171 | /// Drops Web Animations whose target left the document. |
| 172 | void prune(const std::function<bool(int64_t)>& alive); |
| 173 | |
| 174 | /// The value an element has without animations, for Web Animations that add to it or leave a |
| 175 | /// keyframe open. The renderer sets it before `sample`. |
| 176 | std::unordered_map<int64_t, Declared> underlying; |
| 177 | |
| 178 | private: |
| 179 | struct Running { |
| 180 | AnimationSpec spec; |
| 181 | double start = 0; |
| 182 | int iteration = 0; |
| 183 | bool ended = false; |
| 184 | }; |
| 185 | struct Transition { |
| 186 | std::string from, to; |
| 187 | double start = 0; |
| 188 | TransitionSpec spec; |
| 189 | }; |
| 190 | struct Override { |
| 191 | double origin = 0; |
| 192 | std::optional<double> start, hold; |
| 193 | double rate = 1; |
| 194 | bool cancelled = false; |
| 195 | double local(double now) const { return hold ? *hold : (now - (start ? *start : now)) * rate; } |
| 196 | }; |
| 197 | |
| 198 | std::string underlying_value(int64_t id, const std::string& property) const; |
| 199 | /// `events` null: a peek, which reports nothing. |
| 200 | std::optional<StyleValues> sample_animation(int64_t id, Running& item, const std::vector<Keyframe>& frames, double now, std::vector<AnimationEvent>* events) const; |
| 201 | StyleValues values(const std::vector<Keyframe>& frames, const Declared& base, double progress, const AnimationSpec& spec) const; |
| 202 | std::optional<std::string> transition_value(int64_t id, const std::string& property, double now) const; |
| 203 | void prune_overrides(); |
| 204 | /// `only` non-zero: that target alone. |
| 205 | void sample_web(StyleOverrides& out, double now, int64_t only = 0) const; |
| 206 | const Override* override_of(int64_t id, const std::string& key) const; |
| 207 | |
| 208 | KeyframeRules keyframes_; |
| 209 | std::unordered_map<int64_t, std::vector<Running>> running_; |
| 210 | std::unordered_map<int64_t, std::map<std::string, Transition>> transitions_; |
| 211 | std::unordered_map<int64_t, Declared> bases_; |
| 212 | std::unordered_map<int64_t, std::map<std::string, Override>> overrides_; |
| 213 | std::map<int64_t, WebAnimation> web_; // by aid; sampled in aid order, as twidget does |
| 214 | std::vector<AnimationEvent> events_; |
| 215 | }; |
| 216 | |
| 217 | } // namespace twlite |
| 218 | |