# Add a Context Menu to an Electron App

Yes. An agent adds a right-click menu to an Electron app's main process with cut, copy, paste, link and image items that fit the click, tests it, and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-context-menu/electron/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads the main process before it adds a menu. In an app shaped like electron-react-boilerplate/electron-react-boilerplate, `package.json` builds with `electron-vite`, renders React 19.3 with `react-router-dom` 7.18, and runs `jest` for tests, `eslint` for lint and `.erb/scripts/smoke-test.cjs` for a startup check. `src/main/main.ts` creates the `BrowserWindow` with the preload at `../preload/preload.js`, builds the application menu through `MenuBuilder` from `src/main/menu.ts`, and routes `window.open` calls to `shell.openExternal`. `menu.ts` already listens for `context-menu` on `webContents`, but only inside `setupDevelopmentEnvironment`, which runs when `NODE_ENV` is `development` or `DEBUG_PROD` is `true`, and its one item is `Inspect element`. A packaged build has no right-click menu at all: no copy in a text field, no paste, nothing on a link. `src/main/preload.ts` exposes an `ipcRenderer` wrapper over a `Channels` type whose only member is `ipc-example`, and `src/renderer/App.tsx` is a `MemoryRouter` with a `Hello` page of two external links.

The menu lives in the main process, where Electron's `Menu` API is, so the renderer and the preload stay untouched and the `Channels` type does not grow. The agent writes `src/main/context-menu.ts` exporting `attachContextMenu(window: BrowserWindow)`. Its listener reads the `params` that `context-menu` carries and builds the template from them: when `params.isEditable`, the `cut`, `copy`, `paste` and `selectAll` roles, each enabled from the matching `params.editFlags` entry so a read-only field greys out paste; when `params.selectionText` is non-empty on a non-editable element, `copy` alone; when `params.linkURL` is set, `Copy Link Address` writing to `clipboard` and `Open Link in Browser` through `shell.openExternal`, the same path `main.ts` already uses for `window.open`; when `params.mediaType` is `image`, `Copy Image` through `webContents.copyImageAt(params.x, params.y)`. Sections are separated with `{ type: 'separator' }` and an empty template shows nothing rather than an empty popup.

Development keeps its inspector. `attachContextMenu` appends `Inspect element` after a separator when the same `isDebug` condition `main.ts` computes is true, and `setupDevelopmentEnvironment` in `menu.ts` drops its own listener, so a right-click registers one handler, not two. `main.ts` calls `attachContextMenu(mainWindow)` right after `menuBuilder.buildMenu()`, before `loadURL`, so the first page already has it. The template is built per click from a pure function `buildContextMenuTemplate(params, isDebug)` that the listener passes to `Menu.buildFromTemplate(...).popup({ window })`.

That pure function is what the test covers. `src/__tests__/context-menu.test.ts` joins `App.test.tsx` and `updates.test.ts` under Jest, mocks the `electron` module's `Menu`, `shell` and `clipboard`, and asserts the template for an editable field, a plain selection, a link and an image, including the disabled paste when `editFlags.canPaste` is false and the inspector item present only with the debug flag. Before pushing, the agent runs what `.github/workflows/test.yml` runs on each platform: `npm run package`, `npm run lint`, `npm exec tsc -- --noEmit`, `npm test`, and on Linux `xvfb-run --auto-servernum npm run test:smoke`, which starts the app with a remote debugging port and checks it came up.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add a right-click context menu to this Electron app and open a pull request. Read package.json, electron.vite.config.ts, src/main/main.ts, src/main/menu.ts, src/main/preload.ts, src/renderer/App.tsx, src/__tests__/App.test.tsx, .erb/scripts/smoke-test.cjs and .github/workflows/test.yml first. Write src/main/context-menu.ts with a pure buildContextMenuTemplate(params, isDebug) and an attachContextMenu(window) that listens to context-menu on webContents and pops the built menu. Show cut, copy, paste and selectAll roles for editable fields with each enabled from params.editFlags, copy alone for a plain selection, Copy Link Address and Open Link in Browser through shell.openExternal for links, Copy Image through webContents.copyImageAt for images, and Inspect element only when isDebug. Remove the development-only context-menu listener from menu.ts so only one handler is registered, and call attachContextMenu from main.ts after the application menu is built. Do not change the preload or the renderer. Add src/__tests__/context-menu.test.ts mocking electron and covering each case and the disabled paste. Run npm run package, npm run lint, npm exec tsc -- --noEmit, npm test and the smoke test under xvfb-run, and paste the Jest output in the pull request.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request that adds `src/main/context-menu.ts` and `src/__tests__/context-menu.test.ts`, adds one call to `main.ts`, and removes the inspector listener from `menu.ts`. `preload.ts`, the renderer and `package.json` are unchanged. The description shows the Jest run with the new test file's cases and the smoke test's line saying the app started.

On a repository with this `Test` workflow the pull request packages, lints, type-checks and tests on macOS, Windows and Ubuntu with Node 24, and runs the smoke test on each, so a menu role that exists on one platform and not another would fail there before merge.

A reviewer checks that paste is disabled when `editFlags.canPaste` is false, that external links open through `shell.openExternal` and never inside the app window, and that right-clicking in a packaged build no longer exposes `Inspect element`. Then they merge.

## Run it on every new Electron app from the boilerplate

A team that starts each desktop app from the same boilerplate sends one `POST /o/{org}/sessions` per repository with this prompt and that repository in `resources`. Each run reads its own `main.ts` and `menu.ts`, so an app that already has a menu for one window gets the same builder attached to its other windows, and each opens its own pull request with its own test.

Give the API key a `webhook_url` and Rush POSTs `run.completed` when each turn ends, with the `session_url` to read the pull request from. A key runs at most `max_concurrent` sessions at once (4 by default), and a batch of repositories can be started in one go: the runs past that come back `202` with `status: "queued"` and start in order as the key's earlier sandboxes end.

## Why run it on Rush?

Checking a context menu by hand means packaging the app, right-clicking a text field, a link and an image, and repeating on each platform the workflow builds for. In a Rush sandbox the agent packages with `electron-vite`, runs the Jest cases against the pure template function, starts the app under `xvfb-run` with the repository's own smoke test, and keeps every command and its output in the run's record, so the pull request shows the menu was built and the app came up.

The boilerplate's rules are specific: the renderer talks to the main process only through the `Channels` list in `preload.ts`, and the development inspector lives in `menu.ts`. An agent that reads both puts the menu where Electron wants it and removes the duplicate listener, and the same request body does that on every app a team has cloned from the template.

## Frequently asked questions

### Why not a React component in the renderer?

A renderer menu is an HTML element that cannot use the native roles, so paste and select-all would need their own IPC round trips through `preload.ts`. Electron's `Menu.popup` in the main process gives native items with the platform's shortcuts and needs no new channel.

### Does this keep Inspect element in development?

Yes. The item is appended when the same `isDebug` condition that `main.ts` uses is true, after a separator. The old listener in `menu.ts` is removed so a right-click runs one handler, and the test asserts the item is absent otherwise.

### Will it work in both windows if I add a second one?

`attachContextMenu(window)` takes the window, so a second `BrowserWindow` calls it the same way. Nothing is global except the pure template builder.

### What about spell-check suggestions?

The `params` object carries `dictionarySuggestions` and `misspelledWord` when spell check is on. The run leaves them out unless asked, and the pull request notes the two lines that would add suggestion items at the top of the editable-field menu.

## More Electron jobs

- [Add Code Coverage to CI for an Electron App](https://getrush.ai/run/add-coverage-to-ci/electron/)
- [Add Crash Reporting to an Electron App](https://getrush.ai/run/add-crash-reporting/electron/)
- [Add Dark Mode to an Electron App](https://getrush.ai/run/add-dark-mode/electron/)

## The same job on other stacks

- [Add Context Menu Items to an Obsidian Plugin](https://getrush.ai/run/add-context-menu/obsidian-plugin/)
- [Add a Context Menu Item to a Chrome Extension from a Linear Issue](https://getrush.ai/run/add-context-menu/chrome-extension/)

## More jobs like this: Hand off a long task

- [Add Conversation Memory to a LangGraph Agent](https://getrush.ai/run/add-conversation-memory/langgraph/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
