Skip to content

Commit b5f7fa0

Browse files
fix(desktop): enable context menus in the browser (#10670)
1 parent bc4b006 commit b5f7fa0

4 files changed

Lines changed: 200 additions & 39 deletions

File tree

‎apps/desktop/src/electron/ElectronMenu.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,11 @@ describe("ElectronMenu", () => {
154154
buildFromTemplateMock.mockImplementation(() => ({ popup: popupMock }));
155155

156156
const electronMenu = yield* ElectronMenu.ElectronMenu;
157+
const frame = { routingId: 7 } as Electron.WebFrameMain;
157158
const popup = electronMenu.popupTemplate({
158159
window: {} as Electron.BrowserWindow,
159160
template: [{ label: "Copy" }],
161+
frame,
160162
});
161163

162164
assert.equal(buildFromTemplateMock.mock.calls.length, 0);
@@ -166,6 +168,7 @@ describe("ElectronMenu", () => {
166168

167169
assert.equal(buildFromTemplateMock.mock.calls.length, 1);
168170
assert.equal(popupMock.mock.calls.length, 1);
171+
assert.strictEqual(popupMock.mock.calls[0]?.[0].frame, frame);
169172
}).pipe(Effect.provide(TestLayer)),
170173
);
171174

‎apps/desktop/src/electron/ElectronMenu.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export interface ElectronMenuContextInput {
2222
export interface ElectronMenuTemplateInput {
2323
readonly window: Electron.BrowserWindow;
2424
readonly template: readonly Electron.MenuItemConstructorOptions[];
25+
readonly frame?: Electron.WebFrameMain;
2526
}
2627

2728
const ElectronMenuOperation = Schema.Literals([
@@ -208,6 +209,7 @@ export const make = Effect.gen(function* () {
208209
try: () =>
209210
Electron.Menu.buildFromTemplate([...input.template]).popup({
210211
window: input.window,
212+
...(input.frame ? { frame: input.frame } : {}),
211213
}),
212214
catch: (cause) =>
213215
new ElectronMenuOperationError({

‎apps/desktop/src/window/DesktopWindow.test.ts‎

Lines changed: 129 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ import * as Fiber from "effect/Fiber";
77
import * as Layer from "effect/Layer";
88
import * as Logger from "effect/Logger";
99
import * as Option from "effect/Option";
10+
import * as Queue from "effect/Queue";
1011
import * as Ref from "effect/Ref";
1112
import * as References from "effect/References";
1213
import * as Schema from "effect/Schema";
1314
import * as TestClock from "effect/testing/TestClock";
1415

1516
import * as Electron from "electron";
17+
import * as NodeEvents from "node:events";
1618
import { vi } from "vite-plus/test";
1719

1820
vi.mock("electron", async (importOriginal) => ({
@@ -71,6 +73,8 @@ function makeFakeBrowserWindow() {
7173
let zoomLevel = 0;
7274
const webContents = {
7375
copyImageAt: vi.fn(),
76+
focus: vi.fn(),
77+
isDestroyed: vi.fn(() => false),
7478
getURL: vi.fn(() => "t3code-dev://app/"),
7579
getZoomLevel: vi.fn(() => zoomLevel),
7680
setZoomLevel: vi.fn((level: number) => {
@@ -214,6 +218,8 @@ function makeTestLayer(input: {
214218
bounds: DesktopAppSettings.DesktopWindowBounds,
215219
) => Effect.Effect<void>;
216220
readonly openedExternalUrls?: unknown[];
221+
readonly copiedTexts?: string[];
222+
readonly onPopupTemplate?: (input: ElectronMenu.ElectronMenuTemplateInput) => Effect.Effect<void>;
217223
readonly previewZoomReapplies?: number[];
218224
readonly onReveal?: (window: Electron.BrowserWindow) => void;
219225
}) {
@@ -281,15 +287,22 @@ function makeTestLayer(input: {
281287
desktopServerExposureLayer,
282288
DesktopState.layer,
283289
electronAppLayer,
284-
electronMenuLayer,
290+
Layer.succeed(ElectronMenu.ElectronMenu, {
291+
setApplicationMenu: () => Effect.void,
292+
showContextMenu: () => Effect.succeed(Option.none()),
293+
popupTemplate: input.onPopupTemplate ?? (() => Effect.void),
294+
}),
285295
Layer.succeed(ElectronShell.ElectronShell, {
286296
openExternal: (url) =>
287297
Effect.sync(() => {
288298
input.openedExternalUrls?.push(url);
289299
return true;
290300
}),
291301
openSystemSettings: () => Effect.succeed(true),
292-
copyText: () => Effect.void,
302+
copyText: (text) =>
303+
Effect.sync(() => {
304+
input.copiedTexts?.push(text);
305+
}),
293306
} satisfies ElectronShell.ElectronShell["Service"]),
294307
electronThemeLayer,
295308
electronWindowLayer,
@@ -412,6 +425,120 @@ const captureOne = DesktopSnapShotId.make("11111111-1111-4111-8111-111111111111"
412425
const captureTwo = DesktopSnapShotId.make("22222222-2222-4222-8222-222222222222");
413426

414427
describe("DesktopWindow", () => {
428+
it.effect("shows native context menus for browser guests and sign-in popups", () =>
429+
Effect.gen(function* () {
430+
const host = makeFakeBrowserWindow();
431+
const popup = makeFakeBrowserWindow();
432+
let focusedContents: unknown = host.window.webContents;
433+
const makeContents = () => {
434+
const contents = Object.assign(new NodeEvents.EventEmitter(), {
435+
isDestroyed: vi.fn(() => false),
436+
focus: vi.fn(() => {
437+
focusedContents = contents;
438+
}),
439+
copyImageAt: vi.fn(),
440+
replaceMisspelling: vi.fn(),
441+
});
442+
return contents;
443+
};
444+
const guest = makeContents();
445+
const popupContents = makeContents();
446+
const popupWindow = { ...popup.window, webContents: popupContents };
447+
const menus = yield* Queue.unbounded<{
448+
input: ElectronMenu.ElectronMenuTemplateInput;
449+
focusedContents: unknown;
450+
}>();
451+
const copiedTexts: string[] = [];
452+
const layer = makeTestLayer({
453+
window: host.window,
454+
createCount: yield* Ref.make(0),
455+
mainWindow: yield* Ref.make<Option.Option<Electron.BrowserWindow>>(Option.none()),
456+
copiedTexts,
457+
onPopupTemplate: (input) =>
458+
Queue.offer(menus, { input, focusedContents }).pipe(Effect.asVoid),
459+
});
460+
461+
yield* Effect.gen(function* () {
462+
const desktopWindow = yield* DesktopWindow.DesktopWindow;
463+
yield* desktopWindow.handleBackendReady(new URL("http://127.0.0.1:3773"));
464+
const attach = host.webContentsListeners.get("did-attach-webview");
465+
assert.isDefined(attach);
466+
attach({}, guest);
467+
attach({}, guest);
468+
guest.emit("did-create-window", popupWindow);
469+
guest.emit("did-create-window", popupWindow);
470+
471+
for (const [contents, owner] of [
472+
[guest, host.window],
473+
[popupContents, popupWindow],
474+
] as const) {
475+
const frame = { routingId: 7 } as Electron.WebFrameMain;
476+
const preventDefault = vi.fn();
477+
const params = {
478+
frame,
479+
x: 12,
480+
y: 34,
481+
misspelledWord: "helo",
482+
dictionarySuggestions: ["hello"],
483+
linkURL: "",
484+
mediaType: "none",
485+
editFlags: { canCut: false, canCopy: true, canPaste: true, canSelectAll: true },
486+
};
487+
focusedContents = host.window.webContents;
488+
contents.emit("context-menu", { preventDefault }, params);
489+
const menu = yield* Queue.take(menus);
490+
assert.strictEqual(menu.input.window, owner);
491+
assert.strictEqual(menu.input.frame, frame);
492+
assert.strictEqual(menu.focusedContents, contents);
493+
assert.equal(preventDefault.mock.calls.length, 1);
494+
assert.deepEqual(
495+
menu.input.template.filter((item) => item.role),
496+
[
497+
{ role: "cut", enabled: false },
498+
{ role: "copy", enabled: true },
499+
{ role: "paste", enabled: true },
500+
{ role: "selectAll", enabled: true },
501+
],
502+
);
503+
const correction = menu.input.template.find((item) => item.label === "hello");
504+
assert.isDefined(correction?.click);
505+
correction.click({} as Electron.MenuItem, undefined, {} as Electron.KeyboardEvent);
506+
assert.deepEqual(contents.replaceMisspelling.mock.calls, [["hello"]]);
507+
508+
contents.emit(
509+
"context-menu",
510+
{ preventDefault },
511+
{
512+
...params,
513+
frame: null,
514+
misspelledWord: "",
515+
dictionarySuggestions: [],
516+
mediaType: "image",
517+
linkURL: "https://example.com/image.png",
518+
},
519+
);
520+
const imageMenu = (yield* Queue.take(menus)).input;
521+
assert.isUndefined(imageMenu.frame);
522+
const copyImage = imageMenu.template.find((item) => item.label === "Copy Image");
523+
const copyLink = imageMenu.template.find((item) => item.label === "Copy Link");
524+
assert.isDefined(copyImage?.click);
525+
assert.isDefined(copyLink?.click);
526+
copyImage.click({} as Electron.MenuItem, undefined, {} as Electron.KeyboardEvent);
527+
copyLink.click({} as Electron.MenuItem, undefined, {} as Electron.KeyboardEvent);
528+
assert.deepEqual(contents.copyImageAt.mock.calls, [[12, 34]]);
529+
assert.equal(copiedTexts.at(-1), "https://example.com/image.png");
530+
531+
contents.isDestroyed.mockReturnValue(true);
532+
correction.click({} as Electron.MenuItem, undefined, {} as Electron.KeyboardEvent);
533+
copyImage.click({} as Electron.MenuItem, undefined, {} as Electron.KeyboardEvent);
534+
assert.equal(contents.replaceMisspelling.mock.calls.length, 1);
535+
assert.equal(contents.copyImageAt.mock.calls.length, 1);
536+
assert.equal(yield* Queue.size(menus), 0);
537+
}
538+
}).pipe(Effect.provide(layer));
539+
}),
540+
);
541+
415542
it("leaves fullscreen before concealing a pending quit", () => {
416543
const fakeWindow = makeFakeBrowserWindow();
417544

‎apps/desktop/src/window/DesktopWindow.ts‎

Lines changed: 66 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -505,52 +505,81 @@ export const make = Effect.gen(function* () {
505505
webPreferences.contextIsolation = false;
506506
});
507507

508-
window.webContents.on("context-menu", (event, params) => {
509-
event.preventDefault();
508+
const contextMenuContents = new WeakSet<Electron.WebContents>();
509+
const installContextMenu = (
510+
ownerWindow: Electron.BrowserWindow,
511+
contents: Electron.WebContents,
512+
): void => {
513+
if (contextMenuContents.has(contents)) return;
514+
contextMenuContents.add(contents);
515+
contents.on("context-menu", (event, params) => {
516+
event.preventDefault();
517+
if (contents.isDestroyed() || ownerWindow.isDestroyed()) return;
518+
// Native editing roles act on the focused contents, which may still be
519+
// the host renderer when the user right-clicks inside a browser guest.
520+
contents.focus();
521+
522+
const menuTemplate: Electron.MenuItemConstructorOptions[] = [];
523+
524+
if (params.misspelledWord) {
525+
for (const suggestion of params.dictionarySuggestions.slice(0, 5)) {
526+
menuTemplate.push({
527+
label: suggestion,
528+
click: () => {
529+
if (!contents.isDestroyed()) contents.replaceMisspelling(suggestion);
530+
},
531+
});
532+
}
533+
if (params.dictionarySuggestions.length === 0) {
534+
menuTemplate.push({ label: "No suggestions", enabled: false });
535+
}
536+
menuTemplate.push({ type: "separator" });
537+
}
510538

511-
const menuTemplate: Electron.MenuItemConstructorOptions[] = [];
539+
if (Option.isSome(ElectronShell.parseSafeExternalUrl(params.linkURL))) {
540+
menuTemplate.push(
541+
{
542+
label: "Copy Link",
543+
click: () => {
544+
void runPromise(electronShell.copyText(params.linkURL));
545+
},
546+
},
547+
{ type: "separator" },
548+
);
549+
}
512550

513-
if (params.misspelledWord) {
514-
for (const suggestion of params.dictionarySuggestions.slice(0, 5)) {
551+
if (params.mediaType === "image") {
515552
menuTemplate.push({
516-
label: suggestion,
517-
click: () => window.webContents.replaceMisspelling(suggestion),
553+
label: "Copy Image",
554+
click: () => {
555+
if (!contents.isDestroyed()) contents.copyImageAt(params.x, params.y);
556+
},
518557
});
558+
menuTemplate.push({ type: "separator" });
519559
}
520-
if (params.dictionarySuggestions.length === 0) {
521-
menuTemplate.push({ label: "No suggestions", enabled: false });
522-
}
523-
menuTemplate.push({ type: "separator" });
524-
}
525560

526-
if (Option.isSome(ElectronShell.parseSafeExternalUrl(params.linkURL))) {
527561
menuTemplate.push(
528-
{
529-
label: "Copy Link",
530-
click: () => {
531-
void runPromise(electronShell.copyText(params.linkURL));
532-
},
533-
},
534-
{ type: "separator" },
562+
{ role: "cut", enabled: params.editFlags.canCut },
563+
{ role: "copy", enabled: params.editFlags.canCopy },
564+
{ role: "paste", enabled: params.editFlags.canPaste },
565+
{ role: "selectAll", enabled: params.editFlags.canSelectAll },
535566
);
536-
}
537-
538-
if (params.mediaType === "image") {
539-
menuTemplate.push({
540-
label: "Copy Image",
541-
click: () => window.webContents.copyImageAt(params.x, params.y),
542-
});
543-
menuTemplate.push({ type: "separator" });
544-
}
545-
546-
menuTemplate.push(
547-
{ role: "cut", enabled: params.editFlags.canCut },
548-
{ role: "copy", enabled: params.editFlags.canCopy },
549-
{ role: "paste", enabled: params.editFlags.canPaste },
550-
{ role: "selectAll", enabled: params.editFlags.canSelectAll },
551-
);
552567

553-
void runPromise(electronMenu.popupTemplate({ window, template: menuTemplate }));
568+
void runPromise(
569+
electronMenu.popupTemplate({
570+
window: ownerWindow,
571+
template: menuTemplate,
572+
...(params.frame ? { frame: params.frame } : {}),
573+
}),
574+
);
575+
});
576+
contents.on("did-create-window", (popup) => {
577+
installContextMenu(popup, popup.webContents);
578+
});
579+
};
580+
installContextMenu(window, window.webContents);
581+
window.webContents.on("did-attach-webview", (_event, contents) => {
582+
installContextMenu(window, contents);
554583
});
555584

556585
window.webContents.setWindowOpenHandler(({ url }) => {

0 commit comments

Comments
 (0)