Skip to content
TypeScript

fix(desktop): raise the window when a thread notification is clicked … (#12567)

Arnab Chakrabortybb86c24139dd
diff --git a/apps/desktop/src/ipc/DesktopIpcHandlers.ts b/apps/desktop/src/ipc/DesktopIpcHandlers.ts
index caed223e90e..999af3d90f2 100644
--- a/apps/desktop/src/ipc/DesktopIpcHandlers.ts
+++ b/apps/desktop/src/ipc/DesktopIpcHandlers.ts
@@ -48,6 +48,7 @@ import {
checkSystemPermission,
pasteAsText,
probeRemoteEditors,
+ revealWindow,
pickFolder,
pickProjectFavicon,
pickThemeFiles,
@@ -150,6 +151,7 @@ export const installDesktopIpcHandlers = Effect.fn("desktop.ipc.installHandlers"
yield* ipc.handle(openSystemSettings);
yield* ipc.handle(checkSystemPermission);
yield* ipc.handle(pasteAsText);
+ yield* ipc.handle(revealWindow);
yield* ipc.handle(probeRemoteEditors);
yield* ipc.handle(getUpdateState);
yield* ipc.handle(setUpdateChannel);
diff --git a/apps/desktop/src/ipc/channels.ts b/apps/desktop/src/ipc/channels.ts
index a6b617385b5..5843f798ab2 100644
--- a/apps/desktop/src/ipc/channels.ts
+++ b/apps/desktop/src/ipc/channels.ts
@@ -9,6 +9,7 @@ export const OPEN_SYSTEM_SETTINGS_CHANNEL = "desktop:open-system-settings";
export const PROBE_REMOTE_EDITORS_CHANNEL = "desktop:probe-remote-editors";
export const MENU_ACTION_CHANNEL = "desktop:menu-action";
export const PASTE_AS_TEXT_CHANNEL = "desktop:paste-as-text";
+export const REVEAL_WINDOW_CHANNEL = "desktop:reveal-window";
export const SNAP_SHOT_EVENT_CHANNEL = "desktop:snap-shot-event";
export const WEB_LINK_OPEN_CHANNEL = "desktop:web-link-open";
export const WEB_LINK_READY_CHANNEL = "desktop:web-link-ready";
diff --git a/apps/desktop/src/ipc/methods/window.test.ts b/apps/desktop/src/ipc/methods/window.test.ts
index d21f3cd6050..97f5404e452 100644
--- a/apps/desktop/src/ipc/methods/window.test.ts
+++ b/apps/desktop/src/ipc/methods/window.test.ts
@@ -31,6 +31,7 @@ import {
pasteAsText,
pickProjectFavicon,
probeRemoteEditors,
+ revealWindow,
} from "./window.ts";
const readyWslConfig: DesktopBackendManager.DesktopBackendStartConfig = {
@@ -257,6 +258,26 @@ describe("pasteAsText", () => {
);
});
+describe("revealWindow", () => {
+ it.effect("reveals the main window only when it asks", () => {
+ const window = { webContents: { id: 42 } } as unknown as Electron.BrowserWindow;
+ const reveal = vi.fn(() => Effect.void);
+ return Effect.gen(function* () {
+ yield* revealWindow.handler(undefined, { sender: { id: 99 } });
+ assert.equal(reveal.mock.calls.length, 0);
+ yield* revealWindow.handler(undefined, { sender: { id: 42 } });
+ assert.deepEqual(reveal.mock.calls, [[window]]);
+ }).pipe(
+ Effect.provide(
+ Layer.mock(ElectronWindow.ElectronWindow)({
+ main: Effect.succeedSome(window),
+ reveal,
+ }),
+ ),
+ );
+ });
+});
+
describe("pickProjectFavicon", () => {
const pickerLayer = (
pickFiles: () => Effect.Effect<Array<string>>,
diff --git a/apps/desktop/src/ipc/methods/window.ts b/apps/desktop/src/ipc/methods/window.ts
index 6a2bfc7f177..a582dd578ab 100644
--- a/apps/desktop/src/ipc/methods/window.ts
+++ b/apps/desktop/src/ipc/methods/window.ts
@@ -352,6 +352,24 @@ export const pasteAsText = DesktopIpc.makeIpcMethod({
}),
});
+export const revealWindow = DesktopIpc.makeIpcMethod({
+ channel: IpcChannels.REVEAL_WINDOW_CHANNEL,
+ payload: Schema.Undefined,
+ result: Schema.Void,
+ handler: Effect.fn("desktop.ipc.window.revealWindow")(function* (_input, event) {
+ const electronWindow = yield* ElectronWindow.ElectronWindow;
+ const window = yield* electronWindow.main;
+ if (
+ event === undefined ||
+ Option.isNone(window) ||
+ window.value.webContents.id !== event.sender.id
+ ) {
+ return;
+ }
+ yield* electronWindow.reveal(window.value);
+ }),
+});
+
const PICKED_THEME_FILE_MAX_BYTES = 256 * 1024;
export const pickThemeFiles = DesktopIpc.makeIpcMethod({
diff --git a/apps/desktop/src/preload.ts b/apps/desktop/src/preload.ts
index a32724a649f..b57052f4d8a 100644
--- a/apps/desktop/src/preload.ts
+++ b/apps/desktop/src/preload.ts
@@ -96,6 +96,7 @@ contextBridge.exposeInMainWorld("desktopBridge", {
ipcRenderer.on(IpcChannels.TRACKPAD_SCROLL_END_CHANNEL, handler);
return () => ipcRenderer.removeListener(IpcChannels.TRACKPAD_SCROLL_END_CHANNEL, handler);
},
+ revealWindow: () => ipcRenderer.invoke(IpcChannels.REVEAL_WINDOW_CHANNEL),
getSystemLocale: () => {
const result = ipcRenderer.sendSync(IpcChannels.GET_SYSTEM_LOCALE_CHANNEL);
return typeof result === "string" ? result : null;
diff --git a/apps/web/src/components/ThreadNotificationCoordinator.browser.test.tsx b/apps/web/src/components/ThreadNotificationCoordinator.browser.test.tsx
index d106c67baa9..5c066f03628 100644
--- a/apps/web/src/components/ThreadNotificationCoordinator.browser.test.tsx
+++ b/apps/web/src/components/ThreadNotificationCoordinator.browser.test.tsx
@@ -359,4 +359,20 @@ describe("thread notifications", () => {
silent: true,
});
});
+
+ it("reveals the desktop window and opens the thread when a system alert is clicked", async () => {
+ const revealWindow = vi.fn(() => Promise.resolve());
+ vi.stubGlobal("desktopBridge", { revealWindow });
+ state.mode = "notifications";
+ state.focused = false;
+ await render();
+ await complete();
+ const notification: EventTarget = state.notification.mock.results[0]?.value;
+ notification.dispatchEvent(new Event("click"));
+ expect(revealWindow).toHaveBeenCalledTimes(1);
+ expect(state.navigate).toHaveBeenCalledWith({
+ to: "/$environmentId/$threadId",
+ params: { environmentId: "env-1", threadId: "thread-1" },
+ });
+ });
});
diff --git a/apps/web/src/components/ThreadNotificationCoordinator.tsx b/apps/web/src/components/ThreadNotificationCoordinator.tsx
index bb7fdcc29a8..9e4e7d79150 100644
--- a/apps/web/src/components/ThreadNotificationCoordinator.tsx
+++ b/apps/web/src/components/ThreadNotificationCoordinator.tsx
@@ -226,6 +226,7 @@ function EnvironmentNotifications({
notification.addEventListener("click", () => {
notification.close();
window.focus();
+ void window.desktopBridge?.revealWindow?.().catch(() => undefined);
void navigate({
to: "/$environmentId/$threadId",
params: { environmentId, threadId: thread.id },
diff --git a/packages/contracts/src/ipc.ts b/packages/contracts/src/ipc.ts
index d45c61e8666..b8423e7ffdf 100644
--- a/packages/contracts/src/ipc.ts
+++ b/packages/contracts/src/ipc.ts
@@ -1032,6 +1032,11 @@ export interface DesktopBridge {
setNotificationBadge?: (badge: { count: number; image: string | null }) => Promise<void>;
onNotificationBadgeClear?: (listener: () => void) => () => void;
onTrackpadScrollEnd?: (listener: () => void) => () => void;
+ /**
+ * Renderer `window.focus()` cannot raise an Electron window, and on Wayland
+ * only the main process can spend a notification click's activation token.
+ */
+ revealWindow?: () => Promise<void>;
/**
* The OS locale as a BCP-47 tag, which the renderer cannot read for itself:
* the packaged app ships only the `en-US` Chromium locale pak, so