@tantawowa/hosanna-ui 2.1.0 → 2.2.0
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.
- package/dist-manifest.json +26 -22
- package/package.json +1 -1
- package/src/hosanna-bridge-core/color.ts +140 -0
- package/src/hosanna-game/color.ts +3 -115
- package/src/hosanna-list/BaseCell.ts +4 -0
- package/src/hosanna-list/BaseCollectionViewLayout.ts +59 -6
- package/src/hosanna-list/CollectionView.ts +14 -5
- package/src/hosanna-list/CollectionViewAPI.ts +41 -5
- package/src/hosanna-list/CollectionViewFocusManager.ts +415 -6
- package/src/hosanna-list/CollectionViewSupplementaryView.ts +49 -1
- package/src/hosanna-list/DynamicCell.ts +132 -2
- package/src/hosanna-list/EPGProgramLayout.ts +1 -0
- package/src/hosanna-list/GridRowLayout.ts +1 -0
- package/src/hosanna-list/HorizontalRowLayout.ts +1 -0
- package/src/hosanna-list/ListRowLayout.ts +1 -0
- package/src/hosanna-ui/lib/FragmentUtils.ts +26 -10
- package/src/hosanna-ui/lib/animation.ts +358 -235
- package/src/hosanna-ui/views/controls/Button.ts +16 -1
- package/src/hosanna-ui/views/lib/AppConfig.ts +26 -0
- package/src/hosanna-ui/views/lib/BaseView.ts +70 -6
- package/src/hosanna-ui/views/lib/ViewFragmentProvider.ts +39 -5
- package/src/hosanna-ui/views/primitives/Image.ts +1 -1
- package/src/hosanna-ui/views/primitives/Label.ts +3 -3
|
@@ -3,7 +3,8 @@ import { TimerService } from '@hs-src/hosanna-bridge-core/TimerService';
|
|
|
3
3
|
import { ISGROTimespan } from '@hs-src/hosanna-bridge-core/brs-api';
|
|
4
4
|
import { ISGNGroup } from '@hs-src/hosanna-bridge-targets/common/sg-api';
|
|
5
5
|
import { ITickable, JsonData } from '../async/AsyncApi';
|
|
6
|
-
import { interpolate } from './animation-utils';
|
|
6
|
+
import { interpolate, isHexColor } from './animation-utils';
|
|
7
|
+
import { blendColors, colorToHex, colorsEqual } from '../../hosanna-bridge-core/color';
|
|
7
8
|
import type { IHosannaView, ViewState } from '../views/lib/view-api';
|
|
8
9
|
import type { BaseView } from '../views/lib/BaseView';
|
|
9
10
|
|
|
@@ -93,6 +94,8 @@ export class ViewJsonAnimator implements ITickable {
|
|
|
93
94
|
private isReversed = false;
|
|
94
95
|
/** Tracks the current duration of the animation. */
|
|
95
96
|
private runDuration = 0;
|
|
97
|
+
private lastElapsedSample = 0;
|
|
98
|
+
private nextFrameAt = 0;
|
|
96
99
|
/** Whether the animation completed naturally (reached fraction 1.0) rather than being stopped. */
|
|
97
100
|
protected completedNaturally = false;
|
|
98
101
|
|
|
@@ -106,8 +109,7 @@ export class ViewJsonAnimator implements ITickable {
|
|
|
106
109
|
/** Timer service for scheduling animation ticks. */
|
|
107
110
|
@inject() private timerService!: TimerService;
|
|
108
111
|
/**
|
|
109
|
-
*
|
|
110
|
-
* Used to ensure consistent timing between animation frames.
|
|
112
|
+
* Measures progress since start/resume. Updates target 60Hz on the app loop.
|
|
111
113
|
*/
|
|
112
114
|
frameTimer!: ISGROTimespan;
|
|
113
115
|
|
|
@@ -145,88 +147,113 @@ export class ViewJsonAnimator implements ITickable {
|
|
|
145
147
|
* @returns True if the field represents a color property.
|
|
146
148
|
*/
|
|
147
149
|
private isColorProperty(fieldName: string): boolean {
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
'
|
|
151
|
-
'
|
|
152
|
-
'
|
|
153
|
-
'
|
|
154
|
-
'
|
|
155
|
-
'
|
|
156
|
-
'
|
|
157
|
-
'
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
150
|
+
switch (fieldName) {
|
|
151
|
+
case 'color':
|
|
152
|
+
case 'backgroundColor':
|
|
153
|
+
case 'borderColor':
|
|
154
|
+
case 'tintColor':
|
|
155
|
+
case 'textColor':
|
|
156
|
+
case 'hintTextColor':
|
|
157
|
+
case 'fillColor':
|
|
158
|
+
case 'strokeColor':
|
|
159
|
+
case 'blendColor':
|
|
160
|
+
return true;
|
|
161
|
+
default:
|
|
162
|
+
return false;
|
|
163
|
+
}
|
|
161
164
|
}
|
|
162
165
|
|
|
163
166
|
/**
|
|
164
167
|
* Called on each animation tick to update the animation state.
|
|
165
|
-
* @param
|
|
168
|
+
* @param _delta - Timer-service delta; the frame timer measures elapsed animation time.
|
|
166
169
|
*/
|
|
167
|
-
_tick(
|
|
168
|
-
|
|
169
|
-
// console.info(`SKEL: ViewJsonAnimator._tick: ${this.id}`);
|
|
170
|
-
if (this.frameTimer.TotalMilliseconds() < 16) {
|
|
170
|
+
_tick(_delta: number): void {
|
|
171
|
+
if (this.state !== AnimState.Running) {
|
|
171
172
|
return;
|
|
172
|
-
} else {
|
|
173
|
-
this.frameTimer.Mark();
|
|
174
173
|
}
|
|
175
|
-
|
|
176
|
-
this.
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
let
|
|
180
|
-
if (
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
174
|
+
const elapsed = this.frameTimer.TotalMilliseconds();
|
|
175
|
+
const elapsedSinceLastSample = elapsed - this.lastElapsedSample;
|
|
176
|
+
const duration = this.options.duration;
|
|
177
|
+
if (elapsedSinceLastSample <= 0 && duration > 0) return;
|
|
178
|
+
let elapsedProgress = elapsedSinceLastSample;
|
|
179
|
+
if (elapsedProgress < 0) elapsedProgress = 0;
|
|
180
|
+
const nextRunDuration = this.runDuration + elapsedProgress;
|
|
181
|
+
// Keep a stable deadline phase; 1ms accounts for the clock's granularity.
|
|
182
|
+
// A late tick writes only the current position, never historical frames.
|
|
183
|
+
const frameInterval = 1000 / 60;
|
|
184
|
+
const clockTolerance = 1;
|
|
185
|
+
const toleranceAdjustedElapsed = elapsed + clockTolerance;
|
|
186
|
+
if (toleranceAdjustedElapsed < this.nextFrameAt && nextRunDuration < duration) return;
|
|
187
|
+
if (toleranceAdjustedElapsed >= this.nextFrameAt) {
|
|
188
|
+
const passedDeadlines = Math.floor((toleranceAdjustedElapsed - this.nextFrameAt) / frameInterval) + 1;
|
|
189
|
+
this.nextFrameAt += passedDeadlines * frameInterval;
|
|
190
|
+
}
|
|
191
|
+
this.lastElapsedSample = elapsed;
|
|
192
|
+
this.runDuration = nextRunDuration;
|
|
193
|
+
|
|
194
|
+
let fraction = 1;
|
|
195
|
+
if (duration !== 0) {
|
|
196
|
+
if (this.options.reverse && this.isReversed) {
|
|
197
|
+
fraction = 1 - nextRunDuration / duration;
|
|
198
|
+
if (fraction < 0) fraction = 0;
|
|
190
199
|
} else {
|
|
191
|
-
fraction =
|
|
200
|
+
fraction = nextRunDuration / duration;
|
|
201
|
+
if (fraction > 1) fraction = 1;
|
|
192
202
|
}
|
|
193
203
|
}
|
|
194
204
|
|
|
195
205
|
for (const id in this.toValues) {
|
|
196
206
|
const view = this.viewMap[id];
|
|
197
207
|
const toJson = this.toValues[id];
|
|
198
|
-
const fromJson = this.startValues[id]
|
|
208
|
+
const fromJson = this.startValues[id];
|
|
199
209
|
for (const field in toJson) {
|
|
200
210
|
const toVal = toJson[field];
|
|
201
|
-
const fromVal =
|
|
202
|
-
// Fallback to target value if starting value is undefined to avoid flicker
|
|
211
|
+
const fromVal = fromJson?.[field];
|
|
212
|
+
// Fallback to target value if starting value is undefined to avoid flicker.
|
|
203
213
|
const safeFrom = fromVal === undefined ? toVal : fromVal;
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
214
|
+
let value: unknown;
|
|
215
|
+
if (this.isColorProperty(field)) {
|
|
216
|
+
value = blendColors(safeFrom as number | string, toVal as number | string, fraction);
|
|
217
|
+
if (__ROKU__) {
|
|
218
|
+
// SceneGraph color fields accept hex strings; roLongInteger samples are rejected.
|
|
219
|
+
value = colorToHex(value as number);
|
|
220
|
+
}
|
|
221
|
+
} else if (typeof safeFrom === 'number' && typeof toVal === 'number') {
|
|
222
|
+
value = safeFrom + (toVal - safeFrom) * fraction;
|
|
223
|
+
} else if (Array.isArray(safeFrom) && Array.isArray(toVal)
|
|
224
|
+
&& safeFrom.length === 2 && toVal.length === 2
|
|
225
|
+
&& typeof safeFrom[0] === 'number' && typeof safeFrom[1] === 'number'
|
|
226
|
+
&& typeof toVal[0] === 'number' && typeof toVal[1] === 'number') {
|
|
227
|
+
// A fresh sample is required for renderer field-change observation.
|
|
228
|
+
value = [
|
|
229
|
+
safeFrom[0] + (toVal[0] - safeFrom[0]) * fraction,
|
|
230
|
+
safeFrom[1] + (toVal[1] - safeFrom[1]) * fraction,
|
|
231
|
+
];
|
|
232
|
+
} else {
|
|
233
|
+
value = interpolate(safeFrom, toVal, fraction, { isColorProperty: false });
|
|
234
|
+
}
|
|
207
235
|
(view as JsonData)[field] = value;
|
|
208
236
|
}
|
|
209
237
|
}
|
|
210
238
|
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
239
|
+
if (fraction >= 1 && !this.options.reverse) {
|
|
240
|
+
if (this.options.repeat) {
|
|
241
|
+
this.runDuration = 0;
|
|
242
|
+
} else {
|
|
243
|
+
this.completedNaturally = true;
|
|
244
|
+
this.cleanup();
|
|
245
|
+
}
|
|
246
|
+
} else if (this.options.reverse) {
|
|
247
|
+
if (fraction >= 1 && !this.isReversed) {
|
|
248
|
+
this.isReversed = true;
|
|
249
|
+
this.runDuration = 0;
|
|
250
|
+
} else if (fraction <= 0 && this.isReversed) {
|
|
251
|
+
this.isReversed = false;
|
|
252
|
+
this.runDuration = 0;
|
|
253
|
+
if (!this.options.repeat) {
|
|
215
254
|
this.completedNaturally = true;
|
|
216
255
|
this.cleanup();
|
|
217
256
|
}
|
|
218
|
-
} else if (this.options.reverse) {
|
|
219
|
-
if (fraction >= 1 && !this.isReversed) {
|
|
220
|
-
this.isReversed = true;
|
|
221
|
-
this.runDuration = 0;
|
|
222
|
-
} else if (fraction <= 0 && this.isReversed) {
|
|
223
|
-
this.isReversed = false;
|
|
224
|
-
this.runDuration = 0;
|
|
225
|
-
if (!this.options.repeat) {
|
|
226
|
-
this.completedNaturally = true;
|
|
227
|
-
this.cleanup();
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
257
|
}
|
|
231
258
|
}
|
|
232
259
|
}
|
|
@@ -248,11 +275,16 @@ export class ViewJsonAnimator implements ITickable {
|
|
|
248
275
|
this.completedNaturally = false;
|
|
249
276
|
}
|
|
250
277
|
|
|
251
|
-
if (
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
278
|
+
if (!resume || !this.startValues) {
|
|
279
|
+
if (useCurrentState) {
|
|
280
|
+
this.startValues = this.getStartValuesFromView();
|
|
281
|
+
} else {
|
|
282
|
+
this.startValues = this.fromValues;
|
|
283
|
+
}
|
|
255
284
|
}
|
|
285
|
+
this.frameTimer.Mark();
|
|
286
|
+
this.lastElapsedSample = 0;
|
|
287
|
+
this.nextFrameAt = 0;
|
|
256
288
|
this.state = AnimState.Running;
|
|
257
289
|
}
|
|
258
290
|
|
|
@@ -307,6 +339,11 @@ export class ViewJsonAnimator implements ITickable {
|
|
|
307
339
|
*/
|
|
308
340
|
protected onCompleted(): void { /* intentional no-op */ }
|
|
309
341
|
|
|
342
|
+
/** The logical endpoint reached by a naturally completed run. */
|
|
343
|
+
protected getCompletedValues(): IAnimationValues {
|
|
344
|
+
return this.options.reverse ? this.startValues : this.toValues;
|
|
345
|
+
}
|
|
346
|
+
|
|
310
347
|
protected cleanup() {
|
|
311
348
|
console.info(`ViewJsonAnimator.cleanup: Unregistering ${this.id}`);
|
|
312
349
|
// Set state to Stopped first to prevent re-entrant calls to onCompleted()
|
|
@@ -348,40 +385,46 @@ export class ViewJsonAnimator implements ITickable {
|
|
|
348
385
|
}
|
|
349
386
|
}
|
|
350
387
|
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
388
|
+
interface ResolvedViewAnimationBinding {
|
|
389
|
+
owner: BaseView<ViewState>;
|
|
390
|
+
property: string;
|
|
391
|
+
view?: BaseView<ViewState>;
|
|
392
|
+
views: BaseView<ViewState>[];
|
|
393
|
+
stateProperty: string;
|
|
394
|
+
renderer: ISGNGroup;
|
|
395
|
+
field: string;
|
|
396
|
+
target: unknown;
|
|
397
|
+
expectedOwnerValue: unknown;
|
|
398
|
+
expectedViewValue: unknown;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/** Owns logical state animations and their renderer presentation until cleanup. */
|
|
355
402
|
export class HosannaViewAnimator extends ViewJsonAnimator {
|
|
356
|
-
|
|
357
|
-
private
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
*/
|
|
361
|
-
private persistTargetsByKey: Record<string, { ownerLogicalId: string; stateField: string }> = {};
|
|
362
|
-
/** Optional callback to invoke when animation completes. */
|
|
403
|
+
private bindings: ResolvedViewAnimationBinding[] = [];
|
|
404
|
+
private ownedViews: BaseView<ViewState>[] = [];
|
|
405
|
+
private attached = false;
|
|
406
|
+
private commitOnCompletion = true;
|
|
363
407
|
private onCompletedCallback?: () => void;
|
|
364
408
|
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
409
|
+
/**
|
|
410
|
+
* Prepare presentation before the caller applies destination state. The caller
|
|
411
|
+
* commits that state and then calls startPrepared(), so styles cannot replace
|
|
412
|
+
* the captured start sample. Imperative calls use the same preparation path.
|
|
413
|
+
* @internal
|
|
414
|
+
*/
|
|
415
|
+
static prepare(
|
|
416
|
+
view: BaseView<ViewState>, targets: JsonData, duration: number,
|
|
417
|
+
): HosannaViewAnimator | undefined {
|
|
418
|
+
if (!Number.isFinite(duration) || duration <= 0) return undefined;
|
|
419
|
+
const bindings = HosannaViewAnimator.resolveTargets(view, targets, false);
|
|
420
|
+
if (bindings.length === 0) return undefined;
|
|
421
|
+
return HosannaViewAnimator.prepareBindings(bindings, [view], duration, false);
|
|
372
422
|
}
|
|
373
423
|
|
|
374
424
|
/**
|
|
375
|
-
*
|
|
376
|
-
*
|
|
377
|
-
*
|
|
378
|
-
* - Starts from current renderer values
|
|
379
|
-
* @param root - Root view to search for target views
|
|
380
|
-
* @param anims - Animation specification mapping view IDs to their target property values
|
|
381
|
-
* @param duration - Animation duration in milliseconds
|
|
382
|
-
* @param onCompleted - Optional callback invoked when animation completes
|
|
383
|
-
* @param animOptions - Extra `AnimationOptions` (e.g. `repeat`, `delay`). Ignored for `duration` and `useCurrentState`:
|
|
384
|
-
* duration always comes from the `duration` argument, and `useCurrentState` is always `true` (current renderer values).
|
|
425
|
+
* Animate logical properties from current presentation. Logical targets and
|
|
426
|
+
* the completion callback are committed only on natural completion.
|
|
427
|
+
* `duration` and current-renderer sampling override the corresponding options.
|
|
385
428
|
*/
|
|
386
429
|
static animateViews(
|
|
387
430
|
root: BaseView<ViewState>,
|
|
@@ -390,188 +433,268 @@ export class HosannaViewAnimator extends ViewJsonAnimator {
|
|
|
390
433
|
onCompleted?: () => void,
|
|
391
434
|
animOptions?: Partial<AnimationOptions>
|
|
392
435
|
): HosannaViewAnimator {
|
|
393
|
-
const
|
|
394
|
-
const
|
|
395
|
-
const fromValues: IAnimationValues = {};
|
|
396
|
-
|
|
397
|
-
const logicalViewsById: Record<string, BaseView<ViewState>> = {};
|
|
398
|
-
|
|
399
|
-
// temp store for persist mapping before we create animator
|
|
400
|
-
const persistTargetsByKey: Record<string, { targetView?: IHosannaView<ViewState>; ownerLogicalId: string; stateField: string }> = {};
|
|
401
|
-
|
|
436
|
+
const bindings: ResolvedViewAnimationBinding[] = [];
|
|
437
|
+
const views: BaseView<ViewState>[] = [];
|
|
402
438
|
for (const viewId in anims) {
|
|
403
|
-
// Deep search subviews first; allow root by exact match
|
|
404
439
|
let view = root.getSubView<BaseView<ViewState>>(viewId, true);
|
|
405
|
-
if (!view && root.id === viewId)
|
|
406
|
-
view = root as BaseView<ViewState>;
|
|
407
|
-
}
|
|
440
|
+
if (!view && root.id === viewId) view = root;
|
|
408
441
|
if (!view || !view.renderer) {
|
|
409
442
|
console.info(`[HosannaViewAnimator] Skipping unknown or unmounted viewId '${viewId}'`);
|
|
410
443
|
continue;
|
|
411
444
|
}
|
|
445
|
+
views.push(view);
|
|
446
|
+
bindings.push(...HosannaViewAnimator.resolveTargets(view, anims[viewId] ?? {}, true));
|
|
447
|
+
}
|
|
448
|
+
const animator = HosannaViewAnimator.prepareBindings(bindings, views, duration, true, animOptions);
|
|
449
|
+
animator.onCompletedCallback = onCompleted;
|
|
450
|
+
animator.startPrepared();
|
|
451
|
+
return animator;
|
|
452
|
+
}
|
|
412
453
|
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
const provided = provider?.getAnimationPropsForStateProperty?.(stateProp);
|
|
422
|
-
const bindings: AnimationFieldBinding[] = provided ? (Array.isArray(provided) ? provided : [provided]) : [{ renderer: view.renderer, field: stateProp }];
|
|
423
|
-
|
|
424
|
-
// Resolve/cascade bindings into concrete renderer fields
|
|
425
|
-
const resolved = HosannaViewAnimator.resolveBindingsWithPersist(bindings, { ownerView: view, ownerStateProp: stateProp });
|
|
426
|
-
|
|
427
|
-
for (let i = 0; i < resolved.length; i++) {
|
|
428
|
-
const rb = resolved[i];
|
|
429
|
-
const key = `${viewId}:${stateProp}:${i}`;
|
|
430
|
-
viewMap[key] = rb.renderer;
|
|
431
|
-
toValues[key] = toValues[key] || {};
|
|
432
|
-
fromValues[key] = fromValues[key] || {};
|
|
433
|
-
toValues[key][rb.field] = targetValue;
|
|
434
|
-
// Record where to persist state on completion (always owner logical id + original stateProp)
|
|
435
|
-
persistTargetsByKey[key] = {
|
|
436
|
-
ownerLogicalId: viewId,
|
|
437
|
-
stateField: stateProp,
|
|
438
|
-
};
|
|
439
|
-
}
|
|
454
|
+
private static valuesEqual(left: unknown, right: unknown, field: string): boolean {
|
|
455
|
+
if ((field === 'color' || field === 'blendColor')
|
|
456
|
+
&& (typeof left === 'number' || typeof left === 'string')
|
|
457
|
+
&& (typeof right === 'number' || typeof right === 'string')) return colorsEqual(left, right);
|
|
458
|
+
if (Array.isArray(left) && Array.isArray(right)) {
|
|
459
|
+
if (left.length !== right.length) return false;
|
|
460
|
+
for (let i = 0; i < left.length; i++) {
|
|
461
|
+
if (left[i] !== right[i]) return false;
|
|
440
462
|
}
|
|
463
|
+
return true;
|
|
464
|
+
}
|
|
465
|
+
// The generic animator also accepts non-interpolated values by identity.
|
|
466
|
+
// eslint-disable-next-line @hosanna-eslint/no-basic-type-binary-comparison
|
|
467
|
+
return left === right;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
private static isPresentationValue(value: unknown, field: string): boolean {
|
|
471
|
+
if (field === 'opacity') return typeof value === 'number' && Number.isFinite(value);
|
|
472
|
+
if (field === 'color' || field === 'blendColor') {
|
|
473
|
+
return (isHexColor(value) && value.startsWith('#')) || (typeof value === 'number' && Number.isFinite(value));
|
|
441
474
|
}
|
|
475
|
+
return (field === 'scale' || field === 'translation') && Array.isArray(value) && value.length === 2
|
|
476
|
+
&& typeof value[0] === 'number' && Number.isFinite(value[0])
|
|
477
|
+
&& typeof value[1] === 'number' && Number.isFinite(value[1]);
|
|
478
|
+
}
|
|
442
479
|
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
useCurrentState: true,
|
|
447
|
-
});
|
|
480
|
+
private static sameRenderer(left: ISGNGroup, right: ISGNGroup): boolean {
|
|
481
|
+
return left.isSameNode(right);
|
|
482
|
+
}
|
|
448
483
|
|
|
449
|
-
|
|
450
|
-
|
|
484
|
+
private static semanticValue(view: BaseView<ViewState>, property: string): unknown {
|
|
485
|
+
return view.getField?.(property as keyof ViewState);
|
|
486
|
+
}
|
|
451
487
|
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
488
|
+
private static resolveTargets(
|
|
489
|
+
owner: BaseView<ViewState>, targets: JsonData, commitOnCompletion: boolean,
|
|
490
|
+
): ResolvedViewAnimationBinding[] {
|
|
491
|
+
const result: ResolvedViewAnimationBinding[] = [];
|
|
492
|
+
for (const property in targets) {
|
|
493
|
+
const target = targets[property];
|
|
494
|
+
const resolved = HosannaViewAnimator.resolveBindings(owner, property);
|
|
495
|
+
if (resolved.length === 0 && owner.renderer) {
|
|
496
|
+
resolved.push({ renderer: owner.renderer, field: property, view: owner, views: [owner], stateProperty: property });
|
|
497
|
+
}
|
|
498
|
+
for (const binding of resolved) {
|
|
499
|
+
const current = (binding.renderer as JsonData)[binding.field];
|
|
500
|
+
if (!commitOnCompletion && (!HosannaViewAnimator.isPresentationValue(target, binding.field)
|
|
501
|
+
|| !HosannaViewAnimator.isPresentationValue(current, binding.field)
|
|
502
|
+
|| HosannaViewAnimator.valuesEqual(current, target, binding.field))) continue;
|
|
503
|
+
let expectedOwnerValue = target;
|
|
504
|
+
let expectedViewValue = target;
|
|
505
|
+
if (commitOnCompletion) {
|
|
506
|
+
expectedOwnerValue = HosannaViewAnimator.semanticValue(owner, property);
|
|
507
|
+
if (binding.view) expectedViewValue = HosannaViewAnimator.semanticValue(binding.view, binding.stateProperty);
|
|
508
|
+
}
|
|
509
|
+
result.push({
|
|
510
|
+
...binding, owner, property, target,
|
|
511
|
+
expectedOwnerValue, expectedViewValue,
|
|
512
|
+
});
|
|
457
513
|
}
|
|
458
|
-
v.activeAnimation = animator;
|
|
459
514
|
}
|
|
515
|
+
return result;
|
|
516
|
+
}
|
|
460
517
|
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
518
|
+
/** Retain the concrete view at each provider cascade instead of only its node. */
|
|
519
|
+
private static resolveBindings(
|
|
520
|
+
view: BaseView<ViewState>, property: string,
|
|
521
|
+
ancestors: BaseView<ViewState>[] = [], ancestorProperties: string[] = [],
|
|
522
|
+
): { renderer: ISGNGroup; field: string; view?: BaseView<ViewState>; views: BaseView<ViewState>[]; stateProperty: string }[] {
|
|
523
|
+
for (let i = 0; i < ancestors.length; i++) {
|
|
524
|
+
if (ancestors[i]._hid === view._hid && ancestorProperties[i] === property) return [];
|
|
525
|
+
}
|
|
526
|
+
const views = [...ancestors, view];
|
|
527
|
+
const properties = [...ancestorProperties, property];
|
|
528
|
+
const provided = (view as unknown as IAnimationProvider).getAnimationPropsForStateProperty?.(property);
|
|
529
|
+
const bindings = provided ? (Array.isArray(provided) ? provided : [provided]) : [{ renderer: view.renderer, field: property }];
|
|
530
|
+
const result: { renderer: ISGNGroup; field: string; view?: BaseView<ViewState>; views: BaseView<ViewState>[]; stateProperty: string }[] = [];
|
|
531
|
+
// Match the existing resolver's stack order for multi-binding properties.
|
|
532
|
+
for (let i = bindings.length - 1; i >= 0; i--) {
|
|
533
|
+
const binding = bindings[i];
|
|
534
|
+
if (!binding.field) continue;
|
|
535
|
+
if (binding.renderer) {
|
|
536
|
+
let boundView: BaseView<ViewState> | undefined;
|
|
537
|
+
if (view.renderer && HosannaViewAnimator.sameRenderer(binding.renderer, view.renderer)) boundView = view;
|
|
538
|
+
result.push({ renderer: binding.renderer, field: binding.field, view: boundView, views, stateProperty: property });
|
|
539
|
+
} else if (binding.view) {
|
|
540
|
+
result.push(...HosannaViewAnimator.resolveBindings(binding.view as BaseView<ViewState>, binding.field, views, properties));
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
return result;
|
|
544
|
+
}
|
|
466
545
|
|
|
467
|
-
|
|
546
|
+
private static prepareBindings(
|
|
547
|
+
resolved: ResolvedViewAnimationBinding[], views: BaseView<ViewState>[], duration: number,
|
|
548
|
+
commitOnCompletion: boolean, options?: Partial<AnimationOptions>,
|
|
549
|
+
): HosannaViewAnimator {
|
|
550
|
+
const viewMap: IAnimationViewMap = {};
|
|
551
|
+
const fromValues: IAnimationValues = {};
|
|
552
|
+
const toValues: IAnimationValues = {};
|
|
553
|
+
const bindings = resolved;
|
|
554
|
+
for (let index = 0; index < bindings.length; index++) {
|
|
555
|
+
const binding = bindings[index];
|
|
556
|
+
const key = `${index}`;
|
|
557
|
+
viewMap[key] = binding.renderer;
|
|
558
|
+
const fromFields: JsonData = {};
|
|
559
|
+
fromFields[binding.field] = (binding.renderer as JsonData)[binding.field];
|
|
560
|
+
fromValues[key] = fromFields;
|
|
561
|
+
const toFields: JsonData = {};
|
|
562
|
+
toFields[binding.field] = binding.target;
|
|
563
|
+
toValues[key] = toFields;
|
|
564
|
+
}
|
|
565
|
+
const animator = new HosannaViewAnimator(viewMap, fromValues, toValues, { ...options, duration, useCurrentState: true });
|
|
566
|
+
animator.bindings = bindings;
|
|
567
|
+
animator.commitOnCompletion = commitOnCompletion;
|
|
568
|
+
for (const view of views) animator.addOwnedView(view);
|
|
569
|
+
for (const binding of bindings) {
|
|
570
|
+
animator.addOwnedView(binding.owner);
|
|
571
|
+
for (const view of binding.views) animator.addOwnedView(view);
|
|
572
|
+
}
|
|
573
|
+
for (const view of animator.ownedViews) {
|
|
574
|
+
const previous = view.activeAnimation;
|
|
575
|
+
if (previous) previous.handoff(viewMap, toValues);
|
|
576
|
+
view.activeAnimation = animator;
|
|
577
|
+
}
|
|
578
|
+
animator.attached = true;
|
|
468
579
|
return animator;
|
|
469
580
|
}
|
|
470
581
|
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
defaultPersist: { ownerView: IHosannaView<ViewState>; ownerStateProp: string }
|
|
478
|
-
): { renderer: ISGNGroup; field: string }[] {
|
|
479
|
-
const results: { renderer: ISGNGroup; field: string }[] = [];
|
|
480
|
-
const stack: AnimationFieldBinding[] = [...bindings];
|
|
481
|
-
|
|
482
|
-
while (stack.length) {
|
|
483
|
-
const b = stack.pop()!;
|
|
484
|
-
|
|
485
|
-
if (b.renderer && b.field) {
|
|
486
|
-
results.push({ renderer: b.renderer, field: b.field });
|
|
487
|
-
continue;
|
|
488
|
-
}
|
|
489
|
-
|
|
490
|
-
if (b.view && b.field) {
|
|
491
|
-
const provider = (b.view as unknown as IAnimationProvider);
|
|
492
|
-
const nextProvider = provider?.getAnimationPropsForStateProperty?.(b.field);
|
|
582
|
+
private addOwnedView(view: BaseView<ViewState>): void {
|
|
583
|
+
for (const owned of this.ownedViews) {
|
|
584
|
+
if (owned._hid === view._hid) return;
|
|
585
|
+
}
|
|
586
|
+
this.ownedViews.push(view);
|
|
587
|
+
}
|
|
493
588
|
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
}
|
|
589
|
+
/** Start from the samples captured before the destination's render. @internal */
|
|
590
|
+
startPrepared(): void {
|
|
591
|
+
if (this.attached) this.start(false, false);
|
|
592
|
+
}
|
|
499
593
|
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
594
|
+
/** Transfer overlapping fields without a jump; settle unclaimed committed state. */
|
|
595
|
+
handoff(viewMap: IAnimationViewMap, targets: IAnimationValues): void {
|
|
596
|
+
this.stop();
|
|
597
|
+
if (this.commitOnCompletion) return;
|
|
598
|
+
for (const binding of this.bindings) {
|
|
599
|
+
let replaced = false;
|
|
600
|
+
for (const key in viewMap) {
|
|
601
|
+
if (HosannaViewAnimator.sameRenderer(viewMap[key], binding.renderer) && targets[key]?.hasOwnProperty(binding.field)) {
|
|
602
|
+
replaced = true;
|
|
603
|
+
break;
|
|
504
604
|
}
|
|
505
605
|
}
|
|
506
|
-
|
|
606
|
+
if (!replaced) (binding.renderer as JsonData)[binding.field] = binding.target;
|
|
507
607
|
}
|
|
608
|
+
}
|
|
508
609
|
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
610
|
+
/** Settle committed presentation on surviving nodes before a view loses its renderer. */
|
|
611
|
+
releaseView(view: BaseView<ViewState>): void {
|
|
612
|
+
this.stop();
|
|
613
|
+
if (this.commitOnCompletion) return;
|
|
614
|
+
for (const binding of this.bindings) {
|
|
615
|
+
let departing = false;
|
|
616
|
+
for (const boundView of binding.views) {
|
|
617
|
+
if (boundView._hid === view._hid) departing = true;
|
|
513
618
|
}
|
|
619
|
+
if (departing || !binding.view?.renderer) continue;
|
|
620
|
+
if (!HosannaViewAnimator.sameRenderer(binding.renderer, binding.view.renderer)) continue;
|
|
621
|
+
// Explicit semantic changes take precedence over the abandoned destination.
|
|
622
|
+
if (!HosannaViewAnimator.valuesEqual(HosannaViewAnimator.semanticValue(binding.owner, binding.property),
|
|
623
|
+
binding.expectedOwnerValue, binding.field)) continue;
|
|
624
|
+
(binding.renderer as JsonData)[binding.field] = binding.target;
|
|
514
625
|
}
|
|
515
|
-
|
|
516
|
-
return results;
|
|
517
626
|
}
|
|
518
627
|
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
super.stop();
|
|
523
|
-
for (const id in this.logicalViewsById) {
|
|
524
|
-
const v = this.logicalViewsById[id] as unknown as { activeAnimation?: HosannaViewAnimator };
|
|
525
|
-
// eslint-disable-next-line @hosanna-eslint/no-basic-type-binary-comparison
|
|
526
|
-
if (v.activeAnimation === this) {
|
|
527
|
-
v.activeAnimation = undefined;
|
|
528
|
-
}
|
|
628
|
+
ownsField(view: BaseView<ViewState>, field: string): boolean {
|
|
629
|
+
for (const binding of this.bindings) {
|
|
630
|
+
if (binding.view?._hid === view._hid && (binding.field === field || binding.stateProperty === field)) return true;
|
|
529
631
|
}
|
|
632
|
+
return false;
|
|
530
633
|
}
|
|
531
634
|
|
|
532
|
-
/**
|
|
533
|
-
|
|
534
|
-
const
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
if (
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
patchesByHid[hid] = patch;
|
|
635
|
+
/** An explicit semantic change cancels; unrelated renders keep presentation. */
|
|
636
|
+
reconcile(view: BaseView<ViewState>): boolean {
|
|
637
|
+
for (const binding of this.bindings) {
|
|
638
|
+
let changed = false;
|
|
639
|
+
if (view._hid === binding.owner._hid) {
|
|
640
|
+
changed = !HosannaViewAnimator.valuesEqual(
|
|
641
|
+
HosannaViewAnimator.semanticValue(view, binding.property), binding.expectedOwnerValue, binding.field);
|
|
642
|
+
}
|
|
643
|
+
if (view._hid === binding.view?._hid
|
|
644
|
+
&& (view._hid !== binding.owner._hid || binding.stateProperty !== binding.property)) {
|
|
645
|
+
// Completion can queue the logical owner's state immediately before a
|
|
646
|
+
// callback starts its successor. Accept that pending owner value when
|
|
647
|
+
// it subsequently reaches the child through ordinary reconciliation.
|
|
648
|
+
const current = HosannaViewAnimator.semanticValue(view, binding.stateProperty);
|
|
649
|
+
if (this.commitOnCompletion && HosannaViewAnimator.valuesEqual(current, binding.expectedOwnerValue, binding.field)) {
|
|
650
|
+
binding.expectedViewValue = current;
|
|
651
|
+
}
|
|
652
|
+
if (!HosannaViewAnimator.valuesEqual(current, binding.expectedViewValue, binding.field)) changed = true;
|
|
653
|
+
}
|
|
654
|
+
if (changed) {
|
|
655
|
+
this.stop();
|
|
656
|
+
for (const affected of this.bindings) affected.view?.invalidateAnimationPresentation();
|
|
657
|
+
return true;
|
|
556
658
|
}
|
|
557
|
-
patch[persist.stateField] = targetValue;
|
|
558
|
-
viewByHid[hid] = targetView;
|
|
559
659
|
}
|
|
660
|
+
return false;
|
|
661
|
+
}
|
|
560
662
|
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
(view
|
|
663
|
+
/** Release all known logical and concrete views before persistence/callbacks. */
|
|
664
|
+
protected override cleanup(): void {
|
|
665
|
+
this.attached = false;
|
|
666
|
+
for (const view of this.ownedViews) {
|
|
667
|
+
if (view.activeAnimation?.id === this.id) view.activeAnimation = undefined;
|
|
566
668
|
}
|
|
669
|
+
super.cleanup();
|
|
670
|
+
}
|
|
567
671
|
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
if (this.
|
|
672
|
+
protected override onCompleted(): void {
|
|
673
|
+
if (!this.completedNaturally) return;
|
|
674
|
+
if (this.commitOnCompletion) {
|
|
675
|
+
const patchesByHid: Record<string, JsonData> = {};
|
|
676
|
+
const viewsByHid: Record<string, BaseView<ViewState>> = {};
|
|
677
|
+
const completedValues = this.getCompletedValues();
|
|
678
|
+
for (let i = 0; i < this.bindings.length; i++) {
|
|
679
|
+
const binding = this.bindings[i];
|
|
680
|
+
const value = completedValues[`${i}`]?.[binding.field];
|
|
681
|
+
if (value === undefined) continue;
|
|
682
|
+
const hid = binding.owner._hid;
|
|
683
|
+
if (!hid) continue;
|
|
684
|
+
let patch = patchesByHid[hid];
|
|
685
|
+
if (!patch) {
|
|
686
|
+
patch = {};
|
|
687
|
+
patchesByHid[hid] = patch;
|
|
688
|
+
}
|
|
689
|
+
patch[binding.property] = value;
|
|
690
|
+
viewsByHid[hid] = binding.owner;
|
|
691
|
+
}
|
|
692
|
+
for (const hid in patchesByHid) viewsByHid[hid].setState(patchesByHid[hid]);
|
|
693
|
+
}
|
|
694
|
+
if (this.onCompletedCallback) {
|
|
571
695
|
try {
|
|
572
696
|
this.onCompletedCallback();
|
|
573
697
|
} catch (error) {
|
|
574
|
-
// Ensure that errors in the callback do not break the animation lifecycle
|
|
575
698
|
console.error('Error in onCompletedCallback:', error);
|
|
576
699
|
}
|
|
577
700
|
}
|