Skip to content

Commit d608709

Browse files
authored
fix(desktop): ignore snapshot shortcut repeats during flight (pingdotgg#11037)
1 parent 23dae7b commit d608709

3 files changed

Lines changed: 79 additions & 2 deletions

File tree

‎apps/desktop/src/snapShot/DesktopSnapShot.test.ts‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,69 @@ it.effect("captures the active Windows window without enumerating desktop source
869869
).pipe(Effect.provide(layer));
870870
});
871871

872+
it.effect.each(["darwin", "win32"] as const)(
873+
"drops shortcut bursts during flight and accepts a fresh capture after landing on %s",
874+
(platform) => {
875+
const fixture = concurrentCaptureFixture(platform, true);
876+
fixture.releaseAll();
877+
const flying = Promise.withResolvers<void>();
878+
const heldFlights: Array<() => void> = [];
879+
const push = heldFlights.push.bind(heldFlights);
880+
vi.spyOn(heldFlights, "push").mockImplementation((...resolvers) => {
881+
const count = push(...resolvers);
882+
flying.resolve();
883+
return count;
884+
});
885+
transitionScriptState.heldFlights = heldFlights;
886+
return Effect.scoped(
887+
Effect.gen(function* () {
888+
const service = yield* DesktopSnapShot.make;
889+
yield* service.configure(fixture.settings);
890+
yield* Effect.promise(fixture.trigger);
891+
const firstId = fixture.readyIds[0]!;
892+
const landing = yield* service
893+
.setAnimationDestination(firstId, {
894+
frame: { x: 100, y: 400, width: 208, height: 112 },
895+
backgroundColor: "#fff",
896+
borderColor: "#ccc",
897+
borderWidth: 1,
898+
cornerRadius: 8,
899+
scaleFactor: 1,
900+
})
901+
.pipe(Effect.forkChild({ startImmediately: true }));
902+
yield* Effect.promise(() => flying.promise);
903+
const overlayCount = flashWindows.length;
904+
905+
for (let repeat = 0; repeat < 20; repeat++) {
906+
yield* TestClock.adjust("200 millis");
907+
yield* Effect.promise(fixture.trigger);
908+
}
909+
assert.equal(fixture.state.snapshots, 1);
910+
assert.lengthOf(fixture.readyIds, 1);
911+
assert.lengthOf(flashWindows, overlayCount);
912+
assert.isTrue(flashWindows.every((window) => !window.destroyed));
913+
914+
for (const finish of heldFlights) finish();
915+
yield* Fiber.join(landing);
916+
// Landing releases the guard even before the first capture is acknowledged.
917+
yield* Effect.promise(fixture.trigger);
918+
assert.equal(fixture.state.snapshots, 2);
919+
assert.lengthOf(fixture.readyIds, 2);
920+
assert.isTrue(fixture.second.oldOverlaysCleared);
921+
}),
922+
).pipe(
923+
Effect.provide(fixture.layer),
924+
Effect.ensuring(
925+
Effect.sync(() => {
926+
for (const finish of heldFlights) finish();
927+
transitionScriptState.heldFlights = null;
928+
fixture.reset();
929+
}),
930+
),
931+
);
932+
},
933+
);
934+
872935
it.effect.each([
873936
{ length: 1_000, suffix: "", expectedLength: 1_000 },
874937
{ length: 1_001, suffix: "", expectedLength: 1_000 },
@@ -2203,7 +2266,9 @@ it("does not flash a dismissed overlay after its initial compositor receipt arri
22032266
true,
22042267
);
22052268
await readingFrame.promise;
2269+
assert.isFalse(transition.isAnimating);
22062270
transition.dismiss("capture-1");
2271+
assert.isFalse(transition.isAnimating);
22072272
frame.resolve();
22082273
await begin;
22092274

@@ -2292,7 +2357,9 @@ it("does not start a dismissed transition after its compositor receipt arrives",
22922357
});
22932358
const landing = transition.waitForLanding("capture-1");
22942359
await readingFrame.promise;
2360+
assert.isTrue(transition.isAnimating);
22952361
transition.dismiss("capture-1");
2362+
assert.isFalse(transition.isAnimating);
22962363
frame.resolve();
22972364
await landing;
22982365

@@ -2424,6 +2491,7 @@ it("does not let a failed transition fail landing or capture completion", async
24242491
});
24252492

24262493
await transition.waitForLanding("capture-1");
2494+
assert.isFalse(transition.isAnimating);
24272495
await transition.complete("capture-1");
24282496

24292497
assert.isTrue(flashWindows[0]?.destroyed);

‎apps/desktop/src/snapShot/DesktopSnapShot.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -994,7 +994,9 @@ export const make = Effect.gen(function* () {
994994
}).pipe(Effect.withSpan("desktop.snapShot.capture"));
995995

996996
const captureFromShortcut = Effect.gen(function* () {
997-
if (shortcutSuppressed) return;
997+
// Repeated shortcuts must not tear down a moving capture and start another
998+
// renderer. Drop them while the flight runs, just as during pixel acquisition.
999+
if (shortcutSuppressed || transition.isAnimating) return;
9981000
shortcutVerified = true;
9991001
const now = yield* Clock.currentTimeNanos;
10001002
if (lastShortcutAt !== undefined && now - lastShortcutAt < SHORTCUT_COOLDOWN_NS) return;

‎apps/desktop/src/snapShot/SnapShotTransition.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ type ActiveTransition = {
5252
details?: SnapShotAnimationDetails | undefined;
5353
timer?: Fiber.Fiber<void> | undefined;
5454
flight?: Promise<void> | undefined;
55+
landed?: boolean | undefined;
5556
};
5657

5758
type SnapShotTransitionOptions = {
@@ -206,6 +207,10 @@ export class SnapShotTransition {
206207
private readonly alwaysOnTopLevel: SnapShotTransitionOptions["alwaysOnTopLevel"];
207208
private readonly showWindow: (window: Electron.BaseWindow) => void;
208209

210+
get isAnimating(): boolean {
211+
return this.active?.flight !== undefined && !this.active.landed;
212+
}
213+
209214
constructor(options: SnapShotTransitionOptions = {}) {
210215
this.boundOverlayToCaptureDisplays = options.boundOverlayToCaptureDisplays ?? false;
211216
this.waitForCompositorFrame = options.waitForCompositorFrame ?? false;
@@ -350,7 +355,9 @@ export class SnapShotTransition {
350355
}
351356
return;
352357
}
353-
active.flight = this.runFlight(active, destination);
358+
active.flight = this.runFlight(active, destination).finally(() => {
359+
active.landed = true;
360+
});
354361
}
355362

356363
private async runFlight(

0 commit comments

Comments
 (0)