xbintsc 0.5.51 → 0.5.52

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (122) hide show
  1. package/dist/src/cli/help.d.ts +1 -1
  2. package/dist/src/driver/version.d.ts +1 -1
  3. package/dist/src/driver/version.js +1 -1
  4. package/dist/src/extensions/gui/features.d.ts +9 -6
  5. package/dist/src/extensions/gui/features.js +14 -9
  6. package/dist/src/extensions/gui/features.js.map +1 -1
  7. package/dist/tests/e2e/gui-css-coverage.test.d.ts +9 -0
  8. package/dist/tests/e2e/gui-css-coverage.test.js +333 -0
  9. package/dist/tests/e2e/gui-css-coverage.test.js.map +1 -0
  10. package/dist/tests/e2e/gui-css-features.test.d.ts +6 -0
  11. package/dist/tests/e2e/gui-css-features.test.js +270 -0
  12. package/dist/tests/e2e/gui-css-features.test.js.map +1 -0
  13. package/dist/tests/e2e/gui-effects.test.js +92 -11
  14. package/dist/tests/e2e/gui-effects.test.js.map +1 -1
  15. package/dist/tests/e2e/gui-font-face.test.d.ts +10 -0
  16. package/dist/tests/e2e/gui-font-face.test.js +142 -0
  17. package/dist/tests/e2e/gui-font-face.test.js.map +1 -0
  18. package/dist/tests/e2e/gui-fonts.test.d.ts +10 -0
  19. package/dist/tests/e2e/gui-fonts.test.js +150 -0
  20. package/dist/tests/e2e/gui-fonts.test.js.map +1 -0
  21. package/dist/tests/e2e/gui-helpers.d.ts +5 -0
  22. package/dist/tests/e2e/gui-helpers.js +4 -0
  23. package/dist/tests/e2e/gui-helpers.js.map +1 -1
  24. package/dist/tests/e2e/gui-layout.test.js +3 -2
  25. package/dist/tests/e2e/gui-layout.test.js.map +1 -1
  26. package/dist/tests/e2e/gui-track-sizing.test.d.ts +6 -0
  27. package/dist/tests/e2e/gui-track-sizing.test.js +173 -0
  28. package/dist/tests/e2e/gui-track-sizing.test.js.map +1 -0
  29. package/dist/tests/e2e/gui-transform.test.js +3 -2
  30. package/dist/tests/e2e/gui-transform.test.js.map +1 -1
  31. package/dist/tests/extensions/gui-features.test.js +8 -1
  32. package/dist/tests/extensions/gui-features.test.js.map +1 -1
  33. package/doc/ai/extensions.md +10 -5
  34. package/doc/ai/zh-CN/extensions.md +9 -4
  35. package/doc/gui/animation.md +39 -9
  36. package/doc/gui/build.md +6 -5
  37. package/doc/gui/dom.md +20 -4
  38. package/doc/gui/known-issues.md +31 -23
  39. package/doc/gui/layout.md +30 -7
  40. package/doc/gui/rendering.md +84 -26
  41. package/doc/gui.md +62 -14
  42. package/doc/zh-CN/gui/animation.md +33 -10
  43. package/doc/zh-CN/gui/build.md +5 -5
  44. package/doc/zh-CN/gui/dom.md +16 -3
  45. package/doc/zh-CN/gui/known-issues.md +21 -12
  46. package/doc/zh-CN/gui/layout.md +22 -5
  47. package/doc/zh-CN/gui/rendering.md +61 -20
  48. package/doc/zh-CN/gui.md +53 -15
  49. package/package.json +7 -7
  50. package/runtime/ext_gui/animation_values.cpp +425 -0
  51. package/runtime/ext_gui/animation_values.h +64 -0
  52. package/runtime/ext_gui/css.cpp +172 -21
  53. package/runtime/ext_gui/css.h +75 -4
  54. package/runtime/ext_gui/css_match.cpp +4 -0
  55. package/runtime/ext_gui/css_sheet.cpp +218 -2
  56. package/runtime/ext_gui/document.cpp +42 -192
  57. package/runtime/ext_gui/document.h +25 -12
  58. package/runtime/ext_gui/document_animation.cpp +428 -0
  59. package/runtime/ext_gui/effect_values.cpp +245 -21
  60. package/runtime/ext_gui/gradient_values.cpp +24 -0
  61. package/runtime/ext_gui/grid_values.cpp +23 -0
  62. package/runtime/ext_gui/gui_cursor.cpp +50 -0
  63. package/runtime/ext_gui/gui_events.cpp +16 -4
  64. package/runtime/ext_gui/gui_internal.h +3 -0
  65. package/runtime/ext_gui/image_api.h +3 -1
  66. package/runtime/ext_gui/image_feature.cpp +4 -2
  67. package/runtime/ext_gui/inline_svg.cpp +95 -8
  68. package/runtime/ext_gui/inline_svg.h +5 -1
  69. package/runtime/ext_gui/layout.cpp +3 -2
  70. package/runtime/ext_gui/layout.h +6 -0
  71. package/runtime/ext_gui/layout_flex.cpp +134 -29
  72. package/runtime/ext_gui/layout_flow.cpp +58 -7
  73. package/runtime/ext_gui/layout_flow_internal.h +39 -4
  74. package/runtime/ext_gui/layout_grid.cpp +31 -0
  75. package/runtime/ext_gui/layout_inline.cpp +26 -2
  76. package/runtime/ext_gui/layout_vertical.cpp +196 -0
  77. package/runtime/ext_gui/paint.cpp +67 -9
  78. package/runtime/ext_gui/paint.h +61 -8
  79. package/runtime/ext_gui/paint_backdrop.cpp +211 -0
  80. package/runtime/ext_gui/paint_background.cpp +175 -97
  81. package/runtime/ext_gui/paint_effects.cpp +12 -6
  82. package/runtime/ext_gui/paint_internal.h +27 -0
  83. package/runtime/ext_gui/paint_outline.cpp +44 -0
  84. package/runtime/ext_gui/paint_shadow.cpp +368 -0
  85. package/runtime/ext_gui/renderer.cpp +15 -7
  86. package/runtime/ext_gui/renderer.h +10 -1
  87. package/runtime/ext_gui/renderer_backdrop.cpp +374 -0
  88. package/runtime/ext_gui/renderer_bitmap.cpp +82 -0
  89. package/runtime/ext_gui/renderer_gradient.cpp +51 -17
  90. package/runtime/ext_gui/renderer_image.cpp +6 -1
  91. package/runtime/ext_gui/renderer_internal.h +21 -0
  92. package/runtime/ext_gui/renderer_layer.cpp +27 -1
  93. package/runtime/ext_gui/renderer_text.cpp +2 -1
  94. package/runtime/ext_gui/style.cpp +62 -8
  95. package/runtime/ext_gui/style.h +111 -4
  96. package/runtime/ext_gui/style_cascade.cpp +83 -0
  97. package/runtime/ext_gui/style_internal.h +13 -1
  98. package/runtime/ext_gui/style_length.cpp +92 -0
  99. package/runtime/ext_gui/style_print.cpp +83 -2
  100. package/runtime/ext_gui/style_svg.cpp +40 -0
  101. package/runtime/ext_gui/style_text.cpp +302 -0
  102. package/runtime/ext_gui/style_ui.cpp +150 -0
  103. package/runtime/ext_gui/style_values.cpp +76 -62
  104. package/runtime/ext_gui/style_values_internal.h +23 -0
  105. package/runtime/ext_gui/text.cpp +4 -2
  106. package/runtime/ext_gui/text.h +11 -4
  107. package/runtime/ext_gui/text_cache.cpp +11 -2
  108. package/runtime/ext_gui/text_font_db.cpp +427 -0
  109. package/runtime/ext_gui/text_font_paths.cpp +250 -0
  110. package/runtime/ext_gui/text_font_paths.h +39 -0
  111. package/runtime/ext_gui/text_font_sfnt.cpp +396 -0
  112. package/runtime/ext_gui/text_font_sfnt.h +44 -0
  113. package/runtime/ext_gui/text_fonts.cpp +348 -0
  114. package/runtime/ext_gui/text_fonts.h +78 -0
  115. package/runtime/ext_gui/text_fonts_internal.h +77 -0
  116. package/runtime/ext_gui/text_hb.cpp +63 -179
  117. package/runtime/ext_gui/text_stb.cpp +49 -193
  118. package/runtime/ext_gui/transform.cpp +107 -9
  119. package/runtime/ext_gui/transform.h +5 -0
  120. package/runtime/ext_gui/window.cpp +1 -1
  121. package/src/driver/version.ts +1 -1
  122. package/src/extensions/gui/features.ts +13 -9
@@ -0,0 +1,428 @@
1
+ /*
2
+ * xbintsc GUI engine — CSS transitions and `@keyframes` animations.
3
+ *
4
+ * The animatable value switch lives in `animation_values.cpp`; this unit owns
5
+ * the document clock: which transitions/animations are active, how local time
6
+ * maps to an iteration progress, and how the displayed style map is rebuilt
7
+ * before layout.
8
+ */
9
+
10
+ #include "document.h"
11
+
12
+ #include <algorithm>
13
+ #include <cmath>
14
+ #include <string>
15
+ #include <vector>
16
+
17
+ #include "style_internal.h"
18
+
19
+ namespace xtgui {
20
+
21
+ namespace {
22
+
23
+ /** A cubic Bézier with endpoints (0,0) and (1,1), evaluated at `t` on the *x*
24
+ * axis. CSS uses this for `cubic-bezier()` and for its `ease*` keywords. The
25
+ * solver is a few bisection steps: timing functions are evaluated at most once
26
+ * per animated property per frame, so a closed form is not worth the edge cases
27
+ * of a degenerate curve. */
28
+ float cubicBezier(float t, float x1, float y1, float x2, float y2) {
29
+ if (t <= 0.0f) return 0.0f;
30
+ if (t >= 1.0f) return 1.0f;
31
+ auto sample = [](float u, float p1, float p2) {
32
+ float v = 1.0f - u;
33
+ return 3.0f * v * v * u * p1 + 3.0f * v * u * u * p2 + u * u * u;
34
+ };
35
+ float low = 0.0f;
36
+ float high = 1.0f;
37
+ float u = t;
38
+ for (int i = 0; i < 24; i++) {
39
+ float x = sample(u, x1, x2);
40
+ if (x < t) low = u;
41
+ else high = u;
42
+ u = (low + high) * 0.5f;
43
+ }
44
+ return sample(u, y1, y2);
45
+ }
46
+
47
+ float easeProgress(float t, const TimingFunction &timing) {
48
+ if (t <= 0.0f) {
49
+ if (timing.kind == TimingFunction::Kind::StepsStart) {
50
+ float steps = timing.steps > 0 ? (float)timing.steps : 1.0f;
51
+ return 1.0f / steps;
52
+ }
53
+ return 0.0f;
54
+ }
55
+ if (t >= 1.0f) return 1.0f;
56
+ switch (timing.kind) {
57
+ case TimingFunction::Kind::Linear:
58
+ return t;
59
+ case TimingFunction::Kind::EaseIn:
60
+ return cubicBezier(t, 0.42f, 0.0f, 1.0f, 1.0f);
61
+ case TimingFunction::Kind::EaseOut:
62
+ return cubicBezier(t, 0.0f, 0.0f, 0.58f, 1.0f);
63
+ case TimingFunction::Kind::EaseInOut:
64
+ return cubicBezier(t, 0.42f, 0.0f, 0.58f, 1.0f);
65
+ case TimingFunction::Kind::CubicBezier:
66
+ return cubicBezier(t, timing.x1, timing.y1, timing.x2, timing.y2);
67
+ case TimingFunction::Kind::StepsStart: {
68
+ float steps = timing.steps > 0 ? (float)timing.steps : 1.0f;
69
+ float value = std::ceil(t * steps) / steps;
70
+ return value > 1.0f ? 1.0f : value;
71
+ }
72
+ case TimingFunction::Kind::StepsEnd: {
73
+ float steps = timing.steps > 0 ? (float)timing.steps : 1.0f;
74
+ if (t >= 1.0f) return 1.0f;
75
+ return std::floor(t * steps) / steps;
76
+ }
77
+ case TimingFunction::Kind::Ease:
78
+ default:
79
+ return cubicBezier(t, 0.25f, 0.1f, 0.25f, 1.0f);
80
+ }
81
+ }
82
+
83
+ bool timingEqual(const TimingFunction &a, const TimingFunction &b) {
84
+ return a.kind == b.kind && a.x1 == b.x1 && a.y1 == b.y1 && a.x2 == b.x2 && a.y2 == b.y2 &&
85
+ a.steps == b.steps;
86
+ }
87
+
88
+ bool animationSpecEqual(const AnimationSpec &a, const AnimationSpec &b) {
89
+ return a.name == b.name && a.duration == b.duration && a.delay == b.delay &&
90
+ a.iterations == b.iterations && a.direction == b.direction && a.fill == b.fill &&
91
+ a.paused == b.paused && timingEqual(a.timing, b.timing);
92
+ }
93
+
94
+ bool specMatches(const std::string &spec, const std::string &property) {
95
+ return spec == "all" || spec == property;
96
+ }
97
+
98
+ bool directionReversed(const AnimationSpec &spec, float iteration) {
99
+ int parity = (int)std::fmod(std::floor(iteration), 2.0f);
100
+ switch (spec.direction) {
101
+ case AnimationDirection::Normal:
102
+ return false;
103
+ case AnimationDirection::Reverse:
104
+ return true;
105
+ case AnimationDirection::Alternate:
106
+ return parity != 0;
107
+ case AnimationDirection::AlternateReverse:
108
+ return parity == 0;
109
+ }
110
+ return false;
111
+ }
112
+
113
+ float directionProgress(const AnimationSpec &spec, float iteration, float fraction) {
114
+ return directionReversed(spec, iteration) ? 1.0f - fraction : fraction;
115
+ }
116
+
117
+ struct AnimationProgress {
118
+ bool apply = false;
119
+ float progress = 0.0f;
120
+ };
121
+
122
+ /** Resolve the animation's local time to a progress in [0,1], honouring the
123
+ * iteration count, direction and fill mode. The timing function is applied per
124
+ * keyframe interval by the caller, as CSS specifies. */
125
+ AnimationProgress animationProgress(const AnimationSpec &spec, float local_ms) {
126
+ AnimationProgress result;
127
+ if (spec.name == "none" || spec.iterations == 0.0f) return result;
128
+ const float duration = spec.duration * 1000.0f;
129
+ const bool backwards =
130
+ spec.fill == AnimationFillMode::Backwards || spec.fill == AnimationFillMode::Both;
131
+ const bool forwards =
132
+ spec.fill == AnimationFillMode::Forwards || spec.fill == AnimationFillMode::Both;
133
+ if (local_ms < 0.0f) {
134
+ if (!backwards) return result;
135
+ result.apply = true;
136
+ result.progress = directionProgress(spec, 0.0f, 0.0f);
137
+ return result;
138
+ }
139
+ if (duration <= 0.0f) {
140
+ if (!forwards) return result;
141
+ result.apply = true;
142
+ result.progress = directionProgress(spec, 0.0f, 1.0f);
143
+ return result;
144
+ }
145
+ const bool infinite = spec.iterations < 0.0f;
146
+ const float iterations = infinite ? 0.0f : spec.iterations;
147
+ float t = local_ms / duration;
148
+ if (!infinite && t >= iterations) {
149
+ if (!forwards) return result;
150
+ float last = std::ceil(iterations) - 1.0f;
151
+ if (last < 0.0f) last = 0.0f;
152
+ float fraction = iterations - last;
153
+ result.apply = true;
154
+ result.progress = directionProgress(spec, last, fraction);
155
+ return result;
156
+ }
157
+ if (spec.paused) t = 0.0f;
158
+ float index = std::floor(t);
159
+ float fraction = t - index;
160
+ result.apply = true;
161
+ result.progress = directionProgress(spec, index, fraction);
162
+ return result;
163
+ }
164
+
165
+ void applyKeyframeDeclarations(XtStyle *style, const std::vector<Declaration> &declarations,
166
+ const ApplyContext &context) {
167
+ for (const Declaration &declaration : declarations) {
168
+ /* Custom properties and animation bookkeeping cannot change a keyframe
169
+ * result; the engine's own value parser handles everything else. */
170
+ if (declaration.property.size() > 2 && declaration.property[0] == '-' &&
171
+ declaration.property[1] == '-')
172
+ continue;
173
+ if (declaration.property.compare(0, 9, "animation") == 0) continue;
174
+ xt_style_apply_declaration(style, declaration.property, declaration.value, context);
175
+ }
176
+ }
177
+
178
+ } // namespace
179
+
180
+ void XtDocument::startTransitions() {
181
+ if (display_.empty()) return; // first style computation: nothing to animate from
182
+ /* Without a `transition` declaration anywhere in the sheet (or in an inline
183
+ * style) no transition can start, so skip the O(nodes) scan `restyle` would
184
+ * otherwise do every time. */
185
+ if (!has_transitions_) return;
186
+ const std::vector<std::string> &properties = animatableProperties();
187
+ for (auto &entry : styles_) {
188
+ const Node *node = entry.first;
189
+ const XtStyle &target = entry.second;
190
+ /* Generated boxes are rebuilt on every restyle; a transition keyed by one
191
+ * would address a node that no longer exists. */
192
+ if (node->is_pseudo) continue;
193
+ if (target.transitions.empty()) continue;
194
+ auto prev_it = display_.find(node);
195
+ if (prev_it == display_.end()) continue;
196
+ const XtStyle &current = prev_it->second;
197
+ for (const TransitionSpec &spec : target.transitions) {
198
+ if (spec.duration <= 0.0f) continue;
199
+ for (const std::string &property : properties) {
200
+ if (!specMatches(spec.property, property)) continue;
201
+ AnimatableValue from;
202
+ AnimatableValue to;
203
+ if (!readAnimatable(current, property, &from)) continue;
204
+ if (!readAnimatable(target, property, &to)) continue;
205
+ if (animatableEqual(from, to)) continue;
206
+ AnimatableValue probe;
207
+ /* Values that cannot be mixed (a unit change, a different shadow list
208
+ * shape) snap instead of animating, as in CSS's discrete fallback. */
209
+ if (!interpolateAnimatable(from, to, 0.5f, &probe)) continue;
210
+ ActiveTransition running;
211
+ running.node = node;
212
+ running.property = property;
213
+ running.from = from;
214
+ running.to = to;
215
+ running.timing = spec.timing;
216
+ running.duration_ms = spec.duration * 1000.0f;
217
+ running.delay_ms = spec.delay * 1000.0f;
218
+ running.start_ms = (float)clock_ms_ + running.delay_ms;
219
+ /* Retarget an existing transition (mid-flight) instead of stacking. */
220
+ bool replaced = false;
221
+ for (ActiveTransition &existing : transitions_) {
222
+ if (existing.node == node && existing.property == property) {
223
+ existing = running;
224
+ replaced = true;
225
+ break;
226
+ }
227
+ }
228
+ if (!replaced) transitions_.push_back(running);
229
+ }
230
+ }
231
+ }
232
+ }
233
+
234
+ void XtDocument::startAnimations() {
235
+ if (!has_animations_) {
236
+ animations_.clear();
237
+ return;
238
+ }
239
+ std::vector<ActiveAnimation> active;
240
+ for (auto &entry : styles_) {
241
+ const Node *node = entry.first;
242
+ for (const AnimationSpec &spec : entry.second.animations) {
243
+ if (spec.name.empty() || spec.name == "none") continue;
244
+ const ActiveAnimation *existing = nullptr;
245
+ for (const ActiveAnimation &candidate : animations_) {
246
+ if (candidate.node == node && candidate.name == spec.name) {
247
+ existing = &candidate;
248
+ break;
249
+ }
250
+ }
251
+ ActiveAnimation running;
252
+ running.node = node;
253
+ running.name = spec.name;
254
+ running.spec = spec;
255
+ running.start_ms =
256
+ (existing != nullptr && animationSpecEqual(existing->spec, spec)) ? existing->start_ms
257
+ : (float)clock_ms_;
258
+ active.push_back(std::move(running));
259
+ }
260
+ }
261
+ animations_ = std::move(active);
262
+ }
263
+
264
+ bool declarationsAffect(const std::vector<Declaration> &declarations,
265
+ const std::string &property) {
266
+ for (const Declaration &declaration : declarations) {
267
+ const std::string &name = declaration.property;
268
+ if (name == property) return true;
269
+ if (name == "background" && property.compare(0, 11, "background-") == 0) return true;
270
+ if (name == "border" && property.compare(0, 7, "border-") == 0) return true;
271
+ if (name == "margin" && property.compare(0, 7, "margin-") == 0) return true;
272
+ if (name == "padding" && property.compare(0, 8, "padding-") == 0) return true;
273
+ if (name == "inset" &&
274
+ (property == "top" || property == "right" || property == "bottom" ||
275
+ property == "left"))
276
+ return true;
277
+ if (name == "gap" && (property == "row-gap" || property == "column-gap")) return true;
278
+ if (name == "flex" && property == "flex-basis") return true;
279
+ if (name == "font" && property == "font-size") return true;
280
+ }
281
+ return false;
282
+ }
283
+
284
+ void XtDocument::applyAnimations(std::unordered_map<const Node *, XtStyle> *out) {
285
+ if (animations_.empty() || out == nullptr) return;
286
+ const std::vector<std::string> &properties = animatableProperties();
287
+ for (const ActiveAnimation &animation : animations_) {
288
+ auto node_it = out->find(animation.node);
289
+ if (node_it == out->end()) continue;
290
+ const KeyframesRule *rule = xt_css_find_keyframes(sheet_, animation.name);
291
+ if (rule == nullptr || rule->keyframes.empty()) continue;
292
+ AnimationProgress progress = animationProgress(
293
+ animation.spec, (float)clock_ms_ - animation.start_ms - animation.spec.delay * 1000.0f);
294
+ if (!progress.apply) continue;
295
+
296
+ ApplyContext context;
297
+ const XtStyle *base_style = &node_it->second;
298
+ auto target_it = styles_.find(animation.node);
299
+ if (target_it != styles_.end()) base_style = &target_it->second;
300
+ context.rootFontSize = 16.0f;
301
+ context.parentFontSize = base_style->font_size;
302
+ context.viewportWidth = viewportWidth_;
303
+ context.viewportHeight = viewportHeight_;
304
+ /* One style per keyframe step: applying the declarations once lets the
305
+ * per-property search below see through shorthands such as `background`.
306
+ * Missing keyframe values come from the cascade target, not from a
307
+ * transition that may be mid-flight on another property. */
308
+ std::vector<XtStyle> step_styles(rule->keyframes.size(), *base_style);
309
+ for (size_t i = 0; i < rule->keyframes.size(); i++) {
310
+ applyKeyframeDeclarations(&step_styles[i], rule->keyframes[i].declarations, context);
311
+ }
312
+
313
+ for (const std::string &property : properties) {
314
+ /* Missing keyframes are the underlying style, not the adjacent step that
315
+ * happens to declare other properties. A transform declared at 0% and
316
+ * 100% therefore animates across the whole run even if 50% only changes
317
+ * `opacity`. */
318
+ long lower = -1;
319
+ long upper = -1;
320
+ for (size_t i = 0; i < rule->keyframes.size(); i++) {
321
+ if (!declarationsAffect(rule->keyframes[i].declarations, property)) continue;
322
+ if (rule->keyframes[i].offset <= progress.progress) lower = (long)i;
323
+ if (upper < 0 && rule->keyframes[i].offset >= progress.progress) upper = (long)i;
324
+ }
325
+ if (lower < 0 && upper < 0) continue;
326
+ const XtStyle &from_style = lower >= 0 ? step_styles[(size_t)lower] : *base_style;
327
+ const XtStyle &to_style = upper >= 0 ? step_styles[(size_t)upper] : *base_style;
328
+ float segment_start = lower >= 0 ? rule->keyframes[(size_t)lower].offset : 0.0f;
329
+ float segment_end = upper >= 0 ? rule->keyframes[(size_t)upper].offset : 1.0f;
330
+ float segment = 1.0f;
331
+ float span = segment_end - segment_start;
332
+ if (span > 0.00001f) {
333
+ segment = (progress.progress - segment_start) / span;
334
+ if (segment < 0.0f) segment = 0.0f;
335
+ if (segment > 1.0f) segment = 1.0f;
336
+ }
337
+ float eased = easeProgress(segment, animation.spec.timing);
338
+
339
+ AnimatableValue from;
340
+ AnimatableValue to;
341
+ if (!readAnimatable(from_style, property, &from)) continue;
342
+ if (!readAnimatable(to_style, property, &to)) continue;
343
+ AnimatableValue current;
344
+ bool has_current = readAnimatable(node_it->second, property, &current);
345
+ if (animatableEqual(from, to) && has_current && animatableEqual(current, from)) continue;
346
+ AnimatableValue mixed;
347
+ if (!interpolateAnimatable(from, to, eased, &mixed)) {
348
+ mixed = eased < 0.5f ? from : to;
349
+ }
350
+ writeAnimatable(&node_it->second, property, mixed);
351
+ }
352
+ }
353
+ }
354
+
355
+ /** True while an animation can still change the painted style. A finished
356
+ * `forwards`/`both` animation stays in `animations_` (its value must survive a
357
+ * later restyle) but no longer makes every frame dirty. */
358
+ bool animationRunning(const ActiveAnimation &animation, double clock_ms) {
359
+ if (animation.spec.paused) return false;
360
+ if (animation.spec.name == "none" || animation.spec.iterations == 0.0f) return false;
361
+ if (animation.spec.iterations < 0.0f) return true;
362
+ double end = (double)animation.start_ms + (double)animation.spec.delay * 1000.0 +
363
+ (double)animation.spec.duration * 1000.0 * (double)animation.spec.iterations;
364
+ return clock_ms < end;
365
+ }
366
+
367
+ void XtDocument::updateDisplay() {
368
+ /* Without a transition or animation the displayed style *is* the cascade
369
+ * result: skip the per-node deep copy of the whole style map (and keep
370
+ * `display_` empty, which `styleOf` already falls back from). */
371
+ if (!has_transitions_ && !has_animations_) {
372
+ if (!transitions_.empty()) transitions_.clear();
373
+ if (!animations_.empty()) animations_.clear();
374
+ if (!display_.empty()) display_.clear();
375
+ layout_.compute(root_.get(), styles_, viewportWidth_, viewportHeight_, &mediaSizes_,
376
+ &canvasInfo_);
377
+ return;
378
+ }
379
+ display_ = styles_;
380
+ for (size_t i = 0; i < transitions_.size();) {
381
+ ActiveTransition &transition = transitions_[i];
382
+ auto node_it = display_.find(transition.node);
383
+ if (node_it == display_.end()) {
384
+ transitions_.erase(transitions_.begin() + (long)i);
385
+ continue;
386
+ }
387
+ float elapsed = (float)clock_ms_ - transition.start_ms;
388
+ float progress = transition.duration_ms > 0.0f ? elapsed / transition.duration_ms : 1.0f;
389
+ if (progress > 1.0f) progress = 1.0f;
390
+ float eased = easeProgress(progress, transition.timing);
391
+ AnimatableValue mixed;
392
+ if (interpolateAnimatable(transition.from, transition.to, eased, &mixed)) {
393
+ writeAnimatable(&node_it->second, transition.property, mixed);
394
+ }
395
+ if (progress >= 1.0f) {
396
+ transitions_.erase(transitions_.begin() + (long)i);
397
+ } else {
398
+ i++;
399
+ }
400
+ }
401
+ applyAnimations(&display_);
402
+ layout_.compute(root_.get(), display_, viewportWidth_, viewportHeight_, &mediaSizes_,
403
+ &canvasInfo_);
404
+ }
405
+
406
+ bool XtDocument::advance(double delta_ms) {
407
+ const double previous = clock_ms_;
408
+ clock_ms_ += delta_ms;
409
+ if (transitions_.empty() && animations_.empty()) return false;
410
+ bool running = !transitions_.empty();
411
+ bool just_finished = false;
412
+ for (const ActiveAnimation &animation : animations_) {
413
+ if (animationRunning(animation, clock_ms_)) {
414
+ running = true;
415
+ continue;
416
+ }
417
+ if (animation.spec.paused || animation.spec.iterations < 0.0f) continue;
418
+ double end = (double)animation.start_ms + (double)animation.spec.delay * 1000.0 +
419
+ (double)animation.spec.duration * 1000.0 * (double)animation.spec.iterations;
420
+ if (previous < end && clock_ms_ >= end) just_finished = true;
421
+ }
422
+ if (!running && !just_finished) return false;
423
+ updateDisplay();
424
+ return true;
425
+ }
426
+
427
+
428
+ } // namespace xtgui