Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
feat(desktop): add native text context menus
Josué6947b7b26b08
diff --git a/desktop-app/src/__tests__/desktopTextContextMenu.test.ts b/desktop-app/src/__tests__/desktopTextContextMenu.test.tsnew file mode 100644index 000000000..a37853f1d--- /dev/null+++ b/desktop-app/src/__tests__/desktopTextContextMenu.test.ts@@ -0,0 +1,158 @@+import { beforeEach, describe, expect, it, vi } from 'vitest'+import {+ createTextContextMenuTemplate,+ installDesktopTextContextMenus,+ wireDesktopTextContextMenu,+} from '../desktopTextContextMenu.js'++const electron = vi.hoisted(() => ({+ appOn: vi.fn(),+ buildFromTemplate: vi.fn(),+}))++vi.mock('electron', () => ({+ app: { on: electron.appOn },+ Menu: { buildFromTemplate: electron.buildFromTemplate },+}))++function webContents() {+ return {+ copy: vi.fn(),+ cut: vi.fn(),+ isDestroyed: vi.fn(() => false),+ on: vi.fn(),+ paste: vi.fn(),+ }+}++function params(overrides: Record<string, unknown> = {}) {+ return {+ editFlags: { canCopy: true, canCut: true, canPaste: true },+ isEditable: false,+ selectionText: '',+ ...overrides,+ }+}++beforeEach(() => {+ vi.clearAllMocks()+})++describe('Desktop text context menus', () => {+ it('offers only Copy for selected non-editable content', () => {+ const contents = webContents()+ const template = createTextContextMenuTemplate(+ params({ selectionText: 'selected conversation text' }),+ contents+ )++ expect(template.map(item => item.label ?? item.type)).toEqual(['Copy'])+ expect(template[0]?.enabled).toBe(true)+ ;(template[0]?.click as () => void)()+ expect(contents.copy).toHaveBeenCalledOnce()+ expect(contents.cut).not.toHaveBeenCalled()+ expect(contents.paste).not.toHaveBeenCalled()+ })++ it('offers Cut, Copy, and Paste for a selected editable value', () => {+ const contents = webContents()+ const template = createTextContextMenuTemplate(+ params({ isEditable: true, selectionText: 'selected input text' }),+ contents+ )++ expect(template.map(item => item.label ?? item.type)).toEqual([+ 'Cut',+ 'Copy',+ 'separator',+ 'Paste',+ ])+ ;(template[0]?.click as () => void)()+ ;(template[1]?.click as () => void)()+ ;(template[3]?.click as () => void)()+ expect(contents.cut).toHaveBeenCalledOnce()+ expect(contents.copy).toHaveBeenCalledOnce()+ expect(contents.paste).toHaveBeenCalledOnce()+ })++ it('keeps Paste available in editable controls without a selection', () => {+ const contents = webContents()+ const template = createTextContextMenuTemplate(params({ isEditable: true }), contents)++ expect(template.map(item => item.label)).toEqual(['Paste'])+ ;(template[0]?.click as () => void)()+ expect(contents.paste).toHaveBeenCalledOnce()+ })++ it('does not open a menu for unselected non-editable content', () => {+ const contents = webContents()+ const preventDefault = vi.fn()+ const popup = vi.fn()+ const buildMenu = vi.fn(() => ({ popup }))+ wireDesktopTextContextMenu(contents, buildMenu)+ const listener = contents.on.mock.calls[0]?.[1] as (+ event: { preventDefault(): void },+ input: ReturnType<typeof params>+ ) => void++ listener({ preventDefault }, params())++ expect(preventDefault).not.toHaveBeenCalled()+ expect(buildMenu).not.toHaveBeenCalled()+ expect(popup).not.toHaveBeenCalled()+ })++ it('opens the source menu and performs its selected-text action', () => {+ const contents = webContents()+ const preventDefault = vi.fn()+ const popup = vi.fn()+ let builtTemplate: ReturnType<typeof createTextContextMenuTemplate> = []+ const buildMenu = vi.fn(template => {+ builtTemplate = template+ return { popup }+ })+ wireDesktopTextContextMenu(contents, buildMenu)+ const listener = contents.on.mock.calls[0]?.[1] as (+ event: { preventDefault(): void },+ input: ReturnType<typeof params>+ ) => void++ listener({ preventDefault }, params({ selectionText: 'selected app text' }))++ expect(preventDefault).toHaveBeenCalledOnce()+ expect(popup).toHaveBeenCalledOnce()+ expect(builtTemplate.map(item => item.label)).toEqual(['Copy'])+ ;(builtTemplate[0]?.click as () => void)()+ expect(contents.copy).toHaveBeenCalledOnce()+ })++ it('wires every created host or sandbox WebContents through the app owner', () => {+ installDesktopTextContextMenus()+ expect(electron.appOn).toHaveBeenCalledWith('web-contents-created', expect.any(Function))++ const contents = webContents()+ const created = electron.appOn.mock.calls[0]?.[1] as (+ event: unknown,+ source: ReturnType<typeof webContents>+ ) => void+ created({}, contents)++ expect(contents.on).toHaveBeenCalledWith('context-menu', expect.any(Function))+ })++ it('does not execute an edit action after its source WebContents is destroyed', () => {+ const contents = webContents()+ const template = createTextContextMenuTemplate(+ params({ isEditable: true, selectionText: 'selected input text' }),+ contents+ )+ contents.isDestroyed.mockReturnValue(true)+ ;(template[0]?.click as () => void)()+ ;(template[1]?.click as () => void)()+ ;(template[3]?.click as () => void)()++ expect(contents.cut).not.toHaveBeenCalled()+ expect(contents.copy).not.toHaveBeenCalled()+ expect(contents.paste).not.toHaveBeenCalled()+ })+})diff --git a/desktop-app/src/desktopTextContextMenu.ts b/desktop-app/src/desktopTextContextMenu.tsnew file mode 100644index 000000000..72f6a8925--- /dev/null+++ b/desktop-app/src/desktopTextContextMenu.ts@@ -0,0 +1,84 @@+import { Menu, type MenuItemConstructorOptions, type WebContents, app } from 'electron'++type TextContextMenuParams = {+ editFlags: {+ canCopy: boolean+ canCut: boolean+ canPaste: boolean+ }+ isEditable: boolean+ selectionText: string+}++type TextEditingWebContents = Pick<WebContents, 'copy' | 'cut' | 'isDestroyed' | 'on' | 'paste'>++type ContextMenuEvent = {+ preventDefault(): void+}++type ContextMenuBuilder = (template: MenuItemConstructorOptions[]) => { popup(): void }++function runIfLive(webContents: TextEditingWebContents, action: 'copy' | 'cut' | 'paste'): void {+ if (webContents.isDestroyed()) return+ webContents[action]()+}++export function createTextContextMenuTemplate(+ params: TextContextMenuParams,+ webContents: TextEditingWebContents+): MenuItemConstructorOptions[] {+ const hasSelection = params.selectionText.length > 0++ if (!params.isEditable) {+ return hasSelection+ ? [+ {+ label: 'Copy',+ enabled: params.editFlags.canCopy,+ click: () => runIfLive(webContents, 'copy'),+ },+ ]+ : []+ }++ const template: MenuItemConstructorOptions[] = []+ if (hasSelection) {+ template.push(+ {+ label: 'Cut',+ enabled: params.editFlags.canCut,+ click: () => runIfLive(webContents, 'cut'),+ },+ {+ label: 'Copy',+ enabled: params.editFlags.canCopy,+ click: () => runIfLive(webContents, 'copy'),+ },+ { type: 'separator' }+ )+ }+ template.push({+ label: 'Paste',+ enabled: params.editFlags.canPaste,+ click: () => runIfLive(webContents, 'paste'),+ })+ return template+}++export function wireDesktopTextContextMenu(+ webContents: TextEditingWebContents,+ buildMenu: ContextMenuBuilder = template => Menu.buildFromTemplate(template)+): void {+ webContents.on('context-menu', (event: ContextMenuEvent, params: TextContextMenuParams) => {+ const template = createTextContextMenuTemplate(params, webContents)+ if (!template.length) return+ event.preventDefault()+ buildMenu(template).popup()+ })+}++export function installDesktopTextContextMenus(): void {+ app.on('web-contents-created', (_event, webContents) => {+ wireDesktopTextContextMenu(webContents)+ })+}diff --git a/desktop-app/src/main.ts b/desktop-app/src/main.tsindex 9a318c3be..60088e1a7 100644--- a/desktop-app/src/main.ts+++ b/desktop-app/src/main.ts@@ -2,6 +2,7 @@ import { BrowserWindow, app, ipcMain, nativeTheme, powerMonitor } from 'electronimport path from 'node:path'import { AppService } from './appService.js'import { config } from './config.js'+import { installDesktopTextContextMenus } from './desktopTextContextMenu.js'import { createEvenfireDeepLinkRouter } from './evenfireDeepLinkRouter.js'import { assertTrustedSender, registerIpcHandlers } from './ipc.js'import { createMainWindowCoordinator, createRetryableInitializer } from './mainWindowCoordinator.js'@@ -364,6 +365,7 @@ if (gotSingleInstanceLock) {app.whenReady().then(async () => {+ installDesktopTextContextMenus()wireAdaptiveSystemIcon()// Must precede registerIpcHandlers: the SDK IPC handlers resolve the// runtime eagerly on first call, and a plugin can be mounted the moment