Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(remote-desktop): 穿透覆盖批次注入全程,静止倒计时在批次完成后才开始

Dashb62b8c20586c
diff --git a/apps/desktop/src/main/remote-desktop/__tests__/controlledOverlay.test.ts b/apps/desktop/src/main/remote-desktop/__tests__/controlledOverlay.test.ts
index 2aaf60cd82..7ae7e6813e 100644
--- a/apps/desktop/src/main/remote-desktop/__tests__/controlledOverlay.test.ts
+++ b/apps/desktop/src/main/remote-desktop/__tests__/controlledOverlay.test.ts
@@ -373,7 +373,8 @@ it('asks only the local user: hidden from capture, passed through, never focused
expect(confirmation.getBounds()).toEqual({ x: 520, y: 388, width: 400, height: 150 });
// The viewer's pointer passes through it as through the overlay.
- overlay.viewerPointer([{ x: 700, y: 450 }]);
+ const done = overlay.viewerPointer([{ x: 700, y: 450 }]);
+ done();
expect(confirmation.setIgnoreMouseEvents).toHaveBeenLastCalledWith(true);
expect(state.windows[0].setIgnoreMouseEvents).toHaveBeenLastCalledWith(true);
vi.advanceTimersByTime(1000);
@@ -396,7 +397,7 @@ it('asks only the local user: hidden from capture, passed through, never focused
}
});
-it("lets the viewer's pointer pass through, handing it back when it leaves or rests", async () => {
+it("lets the viewer's pointer pass through for each batch's whole injection", async () => {
vi.useFakeTimers();
try {
const { overlay } = fixture();
@@ -404,33 +405,46 @@ it("lets the viewer's pointer pass through, handing it back when it leaves or re
await settle();
// Centred pill: x 600–839, y 37–72.
const window = state.windows[0];
- overlay.viewerPointer([{ x: 700, y: 50 }]);
- overlay.viewerPointer([{ x: 710, y: 50 }]);
- expect(window.setIgnoreMouseEvents.mock.calls).toEqual([[true]]);
- overlay.viewerPointer([{ x: 700, y: 200 }]);
- expect(window.setIgnoreMouseEvents).toHaveBeenLastCalledWith(false);
- // Resting over it returns it to local clicks; the viewer's next click re-arms it first.
- overlay.viewerPointer([{ x: 700, y: 50 }]);
+ const ignored = () => window.setIgnoreMouseEvents.mock.lastCall?.[0];
+ const over = { x: 700, y: 50 };
+ const clear = { x: 700, y: 200 };
+
+ // A batch that takes longer than the rest delay keeps passing through until it is done.
+ const slow = overlay.viewerPointer([over]);
+ vi.advanceTimersByTime(5000);
+ expect(ignored()).toBe(true);
+ slow();
+ // Resting over it then returns it to local clicks after the delay.
vi.advanceTimersByTime(999);
- expect(window.setIgnoreMouseEvents).toHaveBeenLastCalledWith(true);
+ expect(ignored()).toBe(true);
vi.advanceTimersByTime(1);
- expect(window.setIgnoreMouseEvents).toHaveBeenLastCalledWith(false);
- overlay.viewerPointer([{ x: 700, y: 50 }]);
- expect(window.setIgnoreMouseEvents).toHaveBeenLastCalledWith(true);
- // A click over it followed by a move away in the same batch still passes through.
- overlay.viewerPointer([
- { x: 700, y: 50 },
- { x: 700, y: 200 },
- ]);
- expect(window.setIgnoreMouseEvents).toHaveBeenLastCalledWith(true);
- // A later batch entirely clear of it hands it back.
- overlay.viewerPointer([{ x: 700, y: 200 }]);
- expect(window.setIgnoreMouseEvents).toHaveBeenLastCalledWith(false);
- overlay.viewerPointer([{ x: 700, y: 50 }]);
- // A lease that ends mid-rest leaves nothing behind for the next overlay.
+ expect(ignored()).toBe(false);
+
+ // A click over it followed by a move away in the same batch still passes through,
+ // and a later batch entirely clear of it hands it back at once.
+ overlay.viewerPointer([over, clear])();
+ expect(ignored()).toBe(true);
+ overlay.viewerPointer([clear])();
+ expect(ignored()).toBe(false);
+
+ // Moving about over it keeps re-arming the countdown from each batch's completion.
+ overlay.viewerPointer([over])();
+ vi.advanceTimersByTime(600);
+ overlay.viewerPointer([over])();
+ vi.advanceTimersByTime(600);
+ expect(ignored()).toBe(true);
+
+ // A lease that ends mid-batch leaves nothing behind for the next overlay.
+ const late = overlay.viewerPointer([over]);
overlay.update(null);
+ overlay.update(phone);
+ await settle();
+ const next = state.windows[1];
+ const calls = next.setIgnoreMouseEvents.mock.calls.length;
+ late();
vi.runAllTimers();
- expect(window.setIgnoreMouseEvents).toHaveBeenCalledTimes(7);
+ expect(next.setIgnoreMouseEvents.mock.calls.length).toBe(calls);
+ expect(window.destroyed).toBe(true);
} finally {
vi.useRealTimers();
}
diff --git a/apps/desktop/src/main/remote-desktop/__tests__/inputHost.test.ts b/apps/desktop/src/main/remote-desktop/__tests__/inputHost.test.ts
index a9a0fd5b6d..c43c22a3c7 100644
--- a/apps/desktop/src/main/remote-desktop/__tests__/inputHost.test.ts
+++ b/apps/desktop/src/main/remote-desktop/__tests__/inputHost.test.ts
@@ -359,6 +359,7 @@ describe('native input lifecycle', () => {
it('reports a batch’s pointer positions when its injection starts, not when queued', async () => {
const c = childProcess();
const pointer = vi.fn();
+ const settled = vi.fn();
const host = new DesktopInputHost(
vi.fn(),
{
@@ -369,7 +370,10 @@ describe('native input lifecycle', () => {
return c.typed;
},
},
- (points) => pointer(points, c.child.stdin.write.mock.calls.length),
+ (points) => {
+ pointer(points, c.child.stdin.write.mock.calls.length);
+ return () => settled(points);
+ },
);
await host.start('1');
host.input([
@@ -390,8 +394,11 @@ describe('native input lifecycle', () => {
0,
],
]);
+ // The first batch is not finished until the helper acknowledges it.
+ expect(settled).not.toHaveBeenCalled();
c.child.stdout.emit('data', Buffer.from('ok\n'));
await flush();
+ expect(settled).toHaveBeenCalledTimes(1);
expect(pointer.mock.calls.at(-1)).toEqual([[{ x: 10, y: 10 }], 1]);
c.child.stdout.emit('data', Buffer.from('ok\n'));
await flush();
diff --git a/apps/desktop/src/main/remote-desktop/controlledOverlay.ts b/apps/desktop/src/main/remote-desktop/controlledOverlay.ts
index 1213446c76..db6a5b5307 100644
--- a/apps/desktop/src/main/remote-desktop/controlledOverlay.ts
+++ b/apps/desktop/src/main/remote-desktop/controlledOverlay.ts
@@ -159,6 +159,10 @@ export class ControlledOverlay {
/** Whether the local-only windows currently let clicks through, and when they take them back. */
private passThrough = false;
private restTimer: ReturnType<typeof setTimeout> | null = null;
+ /** Viewer pointer batches currently being injected. */
+ private injecting = 0;
+ /** Bumped when the overlay stops, so a late completion cannot affect the next lease. */
+ private injectEpoch = 0;
/** Last dragged origin, kept for this app run only. */
private position: { x: number; y: number } | null = null;
@@ -198,10 +202,12 @@ export class ControlledOverlay {
* batch starts being injected (every earlier batch has finished). If any is over the
* overlay or its confirmation, clicks pass through both for the whole batch; local
* clicks (on the same system cursor) get them back once a batch stays clear of
- * them or the viewer's pointer rests. */
- viewerPointer(points: { x: number; y: number }[]): void {
+ * them, or once the pointer has rested there for a while. The returned callback
+ * marks the batch finished: the rest countdown only starts then, so a slow batch
+ * never gets its later events through to the hidden windows. */
+ viewerPointer(points: { x: number; y: number }[]): () => void {
const windows = this.localOnlyWindows();
- if (!windows.length) return;
+ if (!windows.length) return () => {};
const over = points.some((point) =>
windows.some((window) => {
const { x, y, width, height } = window.getBounds();
@@ -209,10 +215,17 @@ export class ControlledOverlay {
}),
);
if (this.restTimer) clearTimeout(this.restTimer);
- this.restTimer = over
- ? setTimeout(() => this.setPassThrough(false), VIEWER_POINTER_REST_MS)
- : null;
+ this.restTimer = null;
this.setPassThrough(over);
+ this.injecting++;
+ const epoch = this.injectEpoch;
+ let finished = false;
+ return () => {
+ if (finished || epoch !== this.injectEpoch) return;
+ finished = true;
+ if (--this.injecting > 0 || !this.passThrough) return;
+ this.restTimer = setTimeout(() => this.setPassThrough(false), VIEWER_POINTER_REST_MS);
+ };
}
/** What the viewer must never reach: the shown overlay and the shown confirmation. */
@@ -240,6 +253,8 @@ export class ControlledOverlay {
if (this.restTimer) clearTimeout(this.restTimer);
this.restTimer = null;
this.passThrough = false;
+ this.injecting = 0;
+ this.injectEpoch++;
// An unanswered confirmation never outlives its lease: closing it cancels.
const confirmation = this.confirmWindow;
if (confirmation && !confirmation.isDestroyed()) confirmation.destroy();
diff --git a/apps/desktop/src/main/remote-desktop/inputHost.ts b/apps/desktop/src/main/remote-desktop/inputHost.ts
index 2c05c500e8..2424ddf37c 100644
--- a/apps/desktop/src/main/remote-desktop/inputHost.ts
+++ b/apps/desktop/src/main/remote-desktop/inputHost.ts
@@ -230,8 +230,9 @@ export class DesktopInputHost {
spawn(binary, args, { stdio: 'pipe', windowsHide: true }),
},
/** The whole-display pointer positions (global DIP) of one batch, reported as that
- * batch starts its native injection, i.e. after every earlier batch has finished. */
- private readonly onPointer?: (points: { x: number; y: number }[]) => void,
+ * batch starts its native injection, i.e. after every earlier batch has finished.
+ * The returned callback runs once the batch has finished (or failed) injecting. */
+ private readonly onPointer?: (points: { x: number; y: number }[]) => (() => void) | void,
) {}
async start(displayId: string, window?: { windowId: number; pid: number }): Promise<void> {
this.stop();
@@ -422,28 +423,32 @@ export class DesktopInputHost {
if (generation !== this.generation) return;
this.writing = this.writing.then(async () => {
if (generation !== this.generation) return;
- // Local-only windows must pass this batch's pointer through exactly while it is
- // injected, not when it was queued behind earlier batches.
- if (points.length) this.onPointer?.(points);
- if (connection) {
- if ((await connection.request(line.slice(0, -1))) !== 'ok\n')
- throw new Error('DESKTOP_INPUT_UNAVAILABLE');
- } else {
- // stdin.write completion only proves bytes were queued. Hold ownership
- // until the helper has finished posting the entire native batch.
- await new Promise<void>((resolve, reject) => {
- const timer = setTimeout(() => finish(new Error('DESKTOP_INPUT_TIMEOUT')), 10_000);
- const finish = (error?: Error) => {
- clearTimeout(timer);
- if (this.acknowledge === finish) this.acknowledge = null;
- if (error) reject(error);
- else resolve();
- };
- this.acknowledge = finish;
- child!.stdin.write(line, (error) => {
- if (error) finish(error);
+ // Local-only windows must pass this batch's pointer through for as long as it is
+ // being injected, not from when it was queued nor after a fixed delay.
+ const settled = points.length ? this.onPointer?.(points) : undefined;
+ try {
+ if (connection) {
+ if ((await connection.request(line.slice(0, -1))) !== 'ok\n')
+ throw new Error('DESKTOP_INPUT_UNAVAILABLE');
+ } else {
+ // stdin.write completion only proves bytes were queued. Hold ownership
+ // until the helper has finished posting the entire native batch.
+ await new Promise<void>((resolve, reject) => {
+ const timer = setTimeout(() => finish(new Error('DESKTOP_INPUT_TIMEOUT')), 10_000);
+ const finish = (error?: Error) => {
+ clearTimeout(timer);
+ if (this.acknowledge === finish) this.acknowledge = null;
+ if (error) reject(error);
+ else resolve();
+ };
+ this.acknowledge = finish;
+ child!.stdin.write(line, (error) => {
+ if (error) finish(error);
+ });
});
- });
+ }
+ } finally {
+ settled?.();
}
});
await this.writing;