Skip to content
This repository was archived by the owner on Aug 6, 2026. It is now read-only.

Commit 0690515

Browse files
authored
fix: Stop second-instance from unconditionally stealing window focus + add logging (#1398)
1 parent 7db3177 commit 0690515

4 files changed

Lines changed: 27 additions & 4 deletions

File tree

‎apps/code/src/main/deep-links.ts‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ import { app } from "electron";
22
import { container } from "./di/container";
33
import { MAIN_TOKENS } from "./di/tokens";
44
import type { DeepLinkService } from "./services/deep-link/service";
5+
import { logger } from "./utils/logger";
56
import { focusMainWindow } from "./window";
67

8+
const log = logger.scope("deep-links");
9+
710
let pendingDeepLinkUrl: string | null = null;
811

912
function getDeepLinkService(): DeepLinkService {
@@ -18,29 +21,37 @@ export function registerDeepLinkHandlers(): void {
1821
// Handle deep link URLs on macOS
1922
app.on("open-url", (event, url) => {
2023
event.preventDefault();
24+
log.info("open-url event received", { url, appReady: app.isReady() });
2125

2226
if (!app.isReady()) {
2327
pendingDeepLinkUrl = url;
2428
return;
2529
}
2630

2731
getDeepLinkService().handleUrl(url);
28-
focusMainWindow();
32+
focusMainWindow("open-url deep link");
2933
});
3034

3135
// Handle deep link URLs on Windows/Linux (second instance sends URL via command line)
3236
app.on("second-instance", (_event, commandLine) => {
37+
log.info("second-instance event received", {
38+
commandLine: commandLine.join(" "),
39+
argCount: commandLine.length,
40+
});
41+
3342
const url = commandLine.find(
3443
(arg) =>
3544
arg.startsWith("posthog-code://") ||
3645
arg.startsWith("twig://") ||
3746
arg.startsWith("array://"),
3847
);
3948
if (url) {
49+
log.info("Deep link URL found in second-instance args", { url });
4050
getDeepLinkService().handleUrl(url);
51+
focusMainWindow("second-instance deep link");
52+
} else {
53+
log.warn("second-instance fired with no deep link URL, ignoring focus");
4154
}
42-
43-
focusMainWindow();
4455
});
4556
}
4657

‎apps/code/src/main/services/notification/service.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export class NotificationService {
3131
const notification = new Notification({ title, body, silent });
3232

3333
notification.on("click", () => {
34+
log.info("Notification clicked, focusing window", { title, taskId });
3435
const mainWindow = getMainWindow();
3536
if (mainWindow) {
3637
if (mainWindow.isMinimized()) {

‎apps/code/src/main/services/task-link/service.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ export class TaskLinkService extends TypedEventEmitter<TaskLinkEvents> {
7070
}
7171

7272
// Focus the window
73+
log.info("Deep link focusing window", { taskId, taskRunId });
7374
const mainWindow = getMainWindow();
7475
if (mainWindow) {
7576
if (mainWindow.isMinimized()) {

‎apps/code/src/main/window.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ import { buildApplicationMenu } from "./menu";
66
import { setMainWindowGetter } from "./trpc/context";
77
import { trpcRouter } from "./trpc/router";
88
import { isDevBuild } from "./utils/env";
9+
import { logger } from "./utils/logger";
910
import { type WindowStateSchema, windowStateStore } from "./utils/store";
1011

12+
const log = logger.scope("window");
13+
1114
declare const MAIN_WINDOW_VITE_DEV_SERVER_URL: string | undefined;
1215
declare const MAIN_WINDOW_VITE_NAME: string;
1316

@@ -63,8 +66,15 @@ export function getMainWindow(): BrowserWindow | null {
6366
return mainWindow;
6467
}
6568

66-
export function focusMainWindow(): void {
69+
export function focusMainWindow(reason: string): void {
6770
if (mainWindow) {
71+
log.info("focusMainWindow called", {
72+
reason,
73+
isMinimized: mainWindow.isMinimized(),
74+
isFocused: mainWindow.isFocused(),
75+
isVisible: mainWindow.isVisible(),
76+
stack: new Error().stack,
77+
});
6878
if (mainWindow.isMinimized()) mainWindow.restore();
6979
mainWindow.focus();
7080
}

0 commit comments

Comments
 (0)