Skip to content
TypeScript

fix(desktop): ignore snapshot shortcut repeats during flight (#11037)

Matthew Ferozd6087091889a
diff --git a/apps/desktop/src/snapShot/DesktopSnapShot.test.ts b/apps/desktop/src/snapShot/DesktopSnapShot.test.ts
index aa56fede5e9..ee96faa24fd 100644
--- a/apps/desktop/src/snapShot/DesktopSnapShot.test.ts
+++ b/apps/desktop/src/snapShot/DesktopSnapShot.test.ts
@@ -869,6 +869,69 @@ it.effect("captures the active Windows window without enumerating desktop source
).pipe(Effect.provide(layer));
});
+it.effect.each(["darwin", "win32"] as const)(
+ "drops shortcut bursts during flight and accepts a fresh capture after landing on %s",
+ (platform) => {
+ const fixture = concurrentCaptureFixture(platform, true);
+ fixture.releaseAll();
+ const flying = Promise.withResolvers<void>();
+ const heldFlights: Array<() => void> = [];
+ const push = heldFlights.push.bind(heldFlights);
+ vi.spyOn(heldFlights, "push").mockImplementation((...resolvers) => {
+ const count = push(...resolvers);
+ flying.resolve();
+ return count;
+ });
+ transitionScriptState.heldFlights = heldFlights;
+ return Effect.scoped(
+ Effect.gen(function* () {
+ const service = yield* DesktopSnapShot.make;
+ yield* service.configure(fixture.settings);
+ yield* Effect.promise(fixture.trigger);
+ const firstId = fixture.readyIds[0]!;
+ const landing = yield* service
+ .setAnimationDestination(firstId, {
+ frame: { x: 100, y: 400, width: 208, height: 112 },
+ backgroundColor: "#fff",
+ borderColor: "#ccc",
+ borderWidth: 1,
+ cornerRadius: 8,
+ scaleFactor: 1,
+ })
+ .pipe(Effect.forkChild({ startImmediately: true }));
+ yield* Effect.promise(() => flying.promise);
+ const overlayCount = flashWindows.length;
+
+ for (let repeat = 0; repeat < 20; repeat++) {
+ yield* TestClock.adjust("200 millis");
+ yield* Effect.promise(fixture.trigger);
+ }
+ assert.equal(fixture.state.snapshots, 1);
+ assert.lengthOf(fixture.readyIds, 1);
+ assert.lengthOf(flashWindows, overlayCount);
+ assert.isTrue(flashWindows.every((window) => !window.destroyed));
+
+ for (const finish of heldFlights) finish();
+ yield* Fiber.join(landing);
+ // Landing releases the guard even before the first capture is acknowledged.
+ yield* Effect.promise(fixture.trigger);
+ assert.equal(fixture.state.snapshots, 2);
+ assert.lengthOf(fixture.readyIds, 2);
+ assert.isTrue(fixture.second.oldOverlaysCleared);
+ }),
+ ).pipe(
+ Effect.provide(fixture.layer),
+ Effect.ensuring(
+ Effect.sync(() => {
+ for (const finish of heldFlights) finish();
+ transitionScriptState.heldFlights = null;
+ fixture.reset();
+ }),
+ ),
+ );
+ },
+);
+
it.effect.each([
{ length: 1_000, suffix: "", expectedLength: 1_000 },
{ length: 1_001, suffix: "", expectedLength: 1_000 },
@@ -2203,7 +2266,9 @@ it("does not flash a dismissed overlay after its initial compositor receipt arri
true,
);
await readingFrame.promise;
+ assert.isFalse(transition.isAnimating);
transition.dismiss("capture-1");
+ assert.isFalse(transition.isAnimating);
frame.resolve();
await begin;
@@ -2292,7 +2357,9 @@ it("does not start a dismissed transition after its compositor receipt arrives",
});
const landing = transition.waitForLanding("capture-1");
await readingFrame.promise;
+ assert.isTrue(transition.isAnimating);
transition.dismiss("capture-1");
+ assert.isFalse(transition.isAnimating);
frame.resolve();
await landing;
@@ -2424,6 +2491,7 @@ it("does not let a failed transition fail landing or capture completion", async
});
await transition.waitForLanding("capture-1");
+ assert.isFalse(transition.isAnimating);
await transition.complete("capture-1");
assert.isTrue(flashWindows[0]?.destroyed);
diff --git a/apps/desktop/src/snapShot/DesktopSnapShot.ts b/apps/desktop/src/snapShot/DesktopSnapShot.ts
index 68026f6b819..049d5e45ea2 100644
--- a/apps/desktop/src/snapShot/DesktopSnapShot.ts
+++ b/apps/desktop/src/snapShot/DesktopSnapShot.ts
@@ -994,7 +994,9 @@ export const make = Effect.gen(function* () {
}).pipe(Effect.withSpan("desktop.snapShot.capture"));
const captureFromShortcut = Effect.gen(function* () {
- if (shortcutSuppressed) return;
+ // Repeated shortcuts must not tear down a moving capture and start another
+ // renderer. Drop them while the flight runs, just as during pixel acquisition.
+ if (shortcutSuppressed || transition.isAnimating) return;
shortcutVerified = true;
const now = yield* Clock.currentTimeNanos;
if (lastShortcutAt !== undefined && now - lastShortcutAt < SHORTCUT_COOLDOWN_NS) return;
diff --git a/apps/desktop/src/snapShot/SnapShotTransition.ts b/apps/desktop/src/snapShot/SnapShotTransition.ts
index d1cd732ec4a..31eae474f87 100644
--- a/apps/desktop/src/snapShot/SnapShotTransition.ts
+++ b/apps/desktop/src/snapShot/SnapShotTransition.ts
@@ -52,6 +52,7 @@ type ActiveTransition = {
details?: SnapShotAnimationDetails | undefined;
timer?: Fiber.Fiber<void> | undefined;
flight?: Promise<void> | undefined;
+ landed?: boolean | undefined;
};
type SnapShotTransitionOptions = {
@@ -206,6 +207,10 @@ export class SnapShotTransition {
private readonly alwaysOnTopLevel: SnapShotTransitionOptions["alwaysOnTopLevel"];
private readonly showWindow: (window: Electron.BaseWindow) => void;
+ get isAnimating(): boolean {
+ return this.active?.flight !== undefined && !this.active.landed;
+ }
+
constructor(options: SnapShotTransitionOptions = {}) {
this.boundOverlayToCaptureDisplays = options.boundOverlayToCaptureDisplays ?? false;
this.waitForCompositorFrame = options.waitForCompositorFrame ?? false;
@@ -350,7 +355,9 @@ export class SnapShotTransition {
}
return;
}
- active.flight = this.runFlight(active, destination);
+ active.flight = this.runFlight(active, destination).finally(() => {
+ active.landed = true;
+ });
}
private async runFlight(