Skip to content

An open-source, self-hosted system for apps built with web technologies.

A resize from rest starts its tween once the new layout is drawn

zohayrslilehbc9411c94e6d
diff --git a/source/client/view/structure/desktop/windows/window-geometry-animation.ts b/source/client/view/structure/desktop/windows/window-geometry-animation.ts
index 3492985..ac79e56 100644
--- a/source/client/view/structure/desktop/windows/window-geometry-animation.ts
+++ b/source/client/view/structure/desktop/windows/window-geometry-animation.ts
@@ -18,6 +18,7 @@ export class WindowGeometryAnimation {
private flights = new Map<Axis, Flight>()
private complete?: () => void
private updating = false
+ private waiting = 0
constructor(
private values: Record<Axis, MotionValue<number>>,
@@ -27,6 +28,7 @@ export class WindowGeometryAnimation {
stop() {
this.complete = undefined
+ this.cancelWait()
const flights = [...this.flights.values()]
this.flights.clear()
for (const flight of flights) flight.control?.stop()
@@ -76,13 +78,36 @@ export class WindowGeometryAnimation {
}
this.complete = complete
- this.updating = true
- const timing = JSON.stringify([transaction.duration, transaction.easing])
+ this.cancelWait()
// A nonzero backing viewport permits scaling to or from zero size.
// Only these destination changes reflow content, not every tween frame.
- this.layout.width.set(region.width || Math.max(this.values.width.get(), 1))
- this.layout.height.set(region.height || Math.max(this.values.height.get(), 1))
+ const width = region.width || Math.max(this.values.width.get(), 1)
+ const height = region.height || Math.max(this.values.height.get(), 1)
+ const reflows = !this.flights.size && (width !== this.layout.width.get() || height !== this.layout.height.get())
+ this.layout.width.set(width)
+ this.layout.height.set(height)
+
+ // Content laid out anew may take a while, and Safari lays out a Program's frame on the
+ // Desktop's own thread: a tween started with it would find its time spent and jump to its
+ // end. Starting from rest, it waits for that layout to be drawn; a running one retargets.
+ if (reflows) {
+ this.waiting = requestAnimationFrame(() => {
+ this.waiting = requestAnimationFrame(() => {
+ this.waiting = 0
+ this.fly(region, transaction, animatedAxes)
+ })
+ })
+ return
+ }
+
+ this.fly(region, transaction, animatedAxes)
+ }
+
+ private fly(region: WindowRegion, transaction: Transaction, animatedAxes: readonly Axis[]) {
+
+ this.updating = true
+ const timing = JSON.stringify([transaction.duration, transaction.easing])
for (const axis of animatedAxes) {
@@ -113,13 +138,18 @@ export class WindowGeometryAnimation {
private finish() {
- if (this.updating || this.flights.size) return
+ if (this.updating || this.waiting || this.flights.size) return
this.settleLayout()
const complete = this.complete
this.complete = undefined
complete?.()
}
+ private cancelWait() {
+ cancelAnimationFrame(this.waiting)
+ this.waiting = 0
+ }
+
private settleLayout() {
this.layout.width.set(this.values.width.get())
this.layout.height.set(this.values.height.get())
diff --git a/tests/window-geometry-animation.test.ts b/tests/window-geometry-animation.test.ts
index ca7fc8b..2020aa7 100644
--- a/tests/window-geometry-animation.test.ts
+++ b/tests/window-geometry-animation.test.ts
@@ -20,11 +20,26 @@ const runs: { stop: ReturnType<typeof vi.fn>, progress: (fraction: number) => vo
const timing = { duration: 120, easing: "ease-out" } as const
const initial = { x: 10, y: 20, width: 300, height: 200 }
+// Animation frames run at once unless a test holds them.
+let frames: FrameRequestCallback[] | null = null
+
beforeEach(() => {
runs.length = 0
vi.mocked(animate).mockClear()
+ frames = null
+ vi.stubGlobal("requestAnimationFrame", (callback: FrameRequestCallback) => {
+ if (frames) return frames.push(callback)
+ callback(0)
+ return 0
+ })
+ vi.stubGlobal("cancelAnimationFrame", (handle: number) => { if (frames && handle > 0) frames[handle - 1] = () => undefined })
})
+function drawFrame() {
+ const pending = frames!.splice(0)
+ for (const callback of pending) callback(0)
+}
+
function create() {
const values = {
x: motionValue(initial.x), y: motionValue(initial.y),
@@ -173,3 +188,33 @@ test("immediate geometry cancels pending completion and sets all axes", () => {
expect(interrupted).not.toHaveBeenCalled()
expect(complete).toHaveBeenCalledOnce()
})
+
+test("a resize from rest starts its tween once the new layout has been drawn", () => {
+ const { animator, layout } = create()
+ frames = []
+ animator.transition({ ...initial, width: 600, height: 400 }, timing)
+ expect(layout.width.get()).toBe(600)
+ expect(runs).toHaveLength(0)
+ drawFrame()
+ expect(runs).toHaveLength(0)
+ drawFrame()
+ expect(runs).toHaveLength(2)
+})
+
+test("a move from rest starts at once, since nothing is laid out anew", () => {
+ const { animator } = create()
+ frames = []
+ animator.transition({ ...initial, x: 200 }, timing)
+ expect(runs).toHaveLength(1)
+})
+
+test("stopping during the wait leaves the layout as shown and starts nothing", () => {
+ const { animator, values, layout } = create()
+ frames = []
+ animator.transition({ ...initial, width: 600 }, timing)
+ animator.stop()
+ drawFrame()
+ drawFrame()
+ expect(runs).toHaveLength(0)
+ expect(layout.width.get()).toBe(values.width.get())
+})