-
Notifications
You must be signed in to change notification settings - Fork 86
Agentic UI: add a native text context menu #4388
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shaunandrews
wants to merge
10
commits into
trunk
Choose a base branch
from
add-text-context-menu
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4f7c17b
Agentic UI: add a native text context menu with copy, copy all and lo…
shaunandrews 05d53a6
Agentic UI: limit the text context menu to text, not every right-click
shaunandrews 7d78ab7
Merge trunk into add-text-context-menu
shaunandrews 06566ec
Improve native text context menu actions
shaunandrews fe986e3
Merge remote-tracking branch 'origin/trunk' into add-text-context-menu
shaunandrews 7fdcf3e
Format context menu tests after trunk merge
shaunandrews da9efc5
Merge remote-tracking branch 'origin/trunk' into add-text-context-menu
shaunandrews 667436d
Add code-block copy action and translator context
shaunandrews 28845d4
Polish native text context menu behavior
shaunandrews 683ef18
Avoid reading clipboard contents for context menu
shaunandrews File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,263 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { BrowserWindow, clipboard, Menu, type IpcMainInvokeEvent } from 'electron'; | ||
| import { vi } from 'vitest'; | ||
| import { | ||
| buildTextContextMenuTemplate, | ||
| hasTextClipboardFormat, | ||
| showTextContextMenu, | ||
| type TextContextMenuContext, | ||
| type TextContextMenuEnvironment, | ||
| } from 'src/text-context-menu'; | ||
|
|
||
| vi.mock( 'electron', () => ( { | ||
| BrowserWindow: { fromWebContents: vi.fn() }, | ||
| Menu: { buildFromTemplate: vi.fn() }, | ||
| clipboard: { availableFormats: vi.fn(), writeText: vi.fn() }, | ||
| } ) ); | ||
|
|
||
| function makeContext( overrides: Partial< TextContextMenuContext > = {} ): TextContextMenuContext { | ||
| return { selectionText: '', isEditable: false, ...overrides }; | ||
| } | ||
|
|
||
| function makeEnvironment( | ||
| overrides: Partial< TextContextMenuEnvironment > = {} | ||
| ): TextContextMenuEnvironment { | ||
| return { platform: 'darwin', canPaste: false, ...overrides }; | ||
| } | ||
|
|
||
| const actions = { | ||
| lookUpSelection: vi.fn(), | ||
| copyMessage: vi.fn(), | ||
| copyCode: vi.fn(), | ||
| quoteSelection: vi.fn(), | ||
| }; | ||
|
|
||
| function labelsOf( template: ReturnType< typeof buildTextContextMenuTemplate > ) { | ||
| return template.map( ( item ) => item.role ?? item.label ?? item.type ); | ||
| } | ||
|
|
||
| describe( 'buildTextContextMenuTemplate', () => { | ||
| it( 'offers Look Up, Copy and Copy All for a selection on a message on macOS', () => { | ||
| const template = buildTextContextMenuTemplate( | ||
| makeContext( { selectionText: 'coexist', messageText: 'Dark mode and core coexist.' } ), | ||
| actions, | ||
| makeEnvironment() | ||
| ); | ||
|
|
||
| expect( labelsOf( template ) ).toEqual( [ | ||
| 'Look Up “coexist”', | ||
| 'separator', | ||
| 'copy', | ||
| 'Copy All', | ||
| 'separator', | ||
| 'Quote in composer', | ||
| ] ); | ||
| } ); | ||
|
|
||
| it( 'omits Look Up on Windows and Linux, which have no system dictionary', () => { | ||
| for ( const platform of [ 'win32', 'linux' ] as const ) { | ||
| const template = buildTextContextMenuTemplate( | ||
| makeContext( { selectionText: 'coexist', messageText: 'Dark mode and core coexist.' } ), | ||
| actions, | ||
| makeEnvironment( { platform } ) | ||
| ); | ||
|
|
||
| expect( labelsOf( template ) ).toEqual( [ | ||
| 'copy', | ||
| 'Copy All', | ||
| 'separator', | ||
| 'Quote in composer', | ||
| ] ); | ||
| } | ||
| } ); | ||
|
|
||
| it( 'runs showDefinitionForSelection when Look Up is chosen', () => { | ||
| const lookUpSelection = vi.fn(); | ||
| const template = buildTextContextMenuTemplate( | ||
| makeContext( { selectionText: 'coexist' } ), | ||
| { ...actions, lookUpSelection }, | ||
| makeEnvironment() | ||
| ); | ||
|
|
||
| ( template[ 0 ].click as () => void )(); | ||
|
|
||
| expect( lookUpSelection ).toHaveBeenCalledOnce(); | ||
| } ); | ||
|
|
||
| it( 'copies the whole message, not the selection, from Copy All', () => { | ||
| const copyMessage = vi.fn(); | ||
| const template = buildTextContextMenuTemplate( | ||
| makeContext( { selectionText: 'coexist', messageText: 'Dark mode and core coexist.' } ), | ||
| { ...actions, copyMessage }, | ||
| makeEnvironment( { platform: 'linux' } ) | ||
| ); | ||
| const copyAll = template.find( ( item ) => item.label === 'Copy All' ); | ||
|
|
||
| ( copyAll?.click as () => void )(); | ||
|
|
||
| expect( copyMessage ).toHaveBeenCalledWith( 'Dark mode and core coexist.' ); | ||
| } ); | ||
|
|
||
| it( 'offers Copy code within a code block and copies only that code', () => { | ||
| const copyCode = vi.fn(); | ||
| const template = buildTextContextMenuTemplate( | ||
| makeContext( { | ||
| messageText: 'Before.\n\nconst answer = 42;\n\nAfter.', | ||
| codeText: 'const answer = 42;', | ||
| } ), | ||
| { ...actions, copyCode }, | ||
| makeEnvironment( { platform: 'linux' } ) | ||
| ); | ||
| const copyCodeItem = template.find( ( item ) => item.label === 'Copy code' ); | ||
|
|
||
| ( copyCodeItem?.click as () => void )(); | ||
|
|
||
| expect( labelsOf( template ) ).toEqual( [ 'Copy code', 'Copy All' ] ); | ||
| expect( copyCode ).toHaveBeenCalledWith( 'const answer = 42;' ); | ||
| } ); | ||
|
|
||
| it( 'offers a translated label for role-based clipboard actions', () => { | ||
| const template = buildTextContextMenuTemplate( | ||
| makeContext( { selectionText: 'coexist', isEditable: true } ), | ||
| actions, | ||
| makeEnvironment( { platform: 'linux', canPaste: true } ) | ||
| ); | ||
|
|
||
| expect( template.find( ( item ) => item.role === 'copy' )?.label ).toBe( 'Copy' ); | ||
| expect( template.find( ( item ) => item.role === 'paste' )?.label ).toBe( 'Paste' ); | ||
| } ); | ||
|
|
||
| it( 'offers quoting for a read-only selection and runs its action', () => { | ||
| const quoteSelection = vi.fn(); | ||
| const template = buildTextContextMenuTemplate( | ||
| makeContext( { selectionText: 'coexist' } ), | ||
| { ...actions, quoteSelection }, | ||
| makeEnvironment( { platform: 'linux' } ) | ||
| ); | ||
| const quote = template.find( ( item ) => item.label === 'Quote in composer' ); | ||
|
|
||
| ( quote?.click as () => void )(); | ||
|
|
||
| expect( quoteSelection ).toHaveBeenCalledOnce(); | ||
| } ); | ||
|
|
||
| it( 'collapses and truncates a long selection in the Look Up label', () => { | ||
| const template = buildTextContextMenuTemplate( | ||
| makeContext( { selectionText: ' core color\n schemes and dark mode now coexist ' } ), | ||
| actions, | ||
| makeEnvironment() | ||
| ); | ||
|
|
||
| expect( template[ 0 ].label ).toBe( 'Look Up “core color schemes and…”' ); | ||
| } ); | ||
|
|
||
| it( 'offers Paste only in an editable field with something on the clipboard', () => { | ||
| expect( | ||
| labelsOf( | ||
| buildTextContextMenuTemplate( | ||
| makeContext( { isEditable: true } ), | ||
| actions, | ||
| makeEnvironment( { canPaste: true } ) | ||
| ) | ||
| ) | ||
| ).toEqual( [ 'paste' ] ); | ||
|
|
||
| expect( | ||
| labelsOf( | ||
| buildTextContextMenuTemplate( | ||
| makeContext( { isEditable: false } ), | ||
| actions, | ||
| makeEnvironment( { canPaste: true } ) | ||
| ) | ||
| ) | ||
| ).toEqual( [] ); | ||
|
|
||
| expect( | ||
| labelsOf( | ||
| buildTextContextMenuTemplate( | ||
| makeContext( { isEditable: true } ), | ||
| actions, | ||
| makeEnvironment( { canPaste: false } ) | ||
| ) | ||
| ) | ||
| ).toEqual( [] ); | ||
| } ); | ||
|
|
||
| it( 'leaves no stray separator when a section drops out', () => { | ||
| // Look Up applies but there is no message to copy, so the divider must | ||
| // still sit between two populated sections rather than trailing. | ||
| const template = buildTextContextMenuTemplate( | ||
| makeContext( { selectionText: 'coexist' } ), | ||
| actions, | ||
| makeEnvironment() | ||
| ); | ||
|
|
||
| expect( labelsOf( template ) ).toEqual( [ | ||
| 'Look Up “coexist”', | ||
| 'separator', | ||
| 'copy', | ||
| 'separator', | ||
| 'Quote in composer', | ||
| ] ); | ||
| expect( template[ 0 ].type ).not.toBe( 'separator' ); | ||
| expect( template.at( -1 )?.type ).not.toBe( 'separator' ); | ||
| } ); | ||
|
|
||
| it( 'drops the divider when only the clipboard section applies', () => { | ||
| const template = buildTextContextMenuTemplate( | ||
| makeContext( { selectionText: 'coexist', isEditable: true } ), | ||
| actions, | ||
| makeEnvironment( { platform: 'win32' } ) | ||
| ); | ||
|
|
||
| expect( labelsOf( template ) ).toEqual( [ 'copy' ] ); | ||
| } ); | ||
|
|
||
| it( 'returns nothing to show when no text action applies', () => { | ||
| const template = buildTextContextMenuTemplate( | ||
| makeContext(), | ||
| actions, | ||
| makeEnvironment( { platform: 'win32' } ) | ||
| ); | ||
|
|
||
| expect( template ).toEqual( [] ); | ||
| } ); | ||
| } ); | ||
|
|
||
| describe( 'hasTextClipboardFormat', () => { | ||
| it( 'detects normalized and native plain-text formats without reading clipboard contents', () => { | ||
| expect( hasTextClipboardFormat( [ 'text/plain' ] ) ).toBe( true ); | ||
| expect( hasTextClipboardFormat( [ 'text/plain;charset=utf-8' ] ) ).toBe( true ); | ||
| expect( hasTextClipboardFormat( [ 'public.utf8-plain-text' ] ) ).toBe( true ); | ||
| expect( hasTextClipboardFormat( [ 'image/png' ] ) ).toBe( false ); | ||
| } ); | ||
| } ); | ||
|
|
||
| describe( 'showTextContextMenu', () => { | ||
| it( 'returns the selected text when Quote in composer is chosen', async () => { | ||
| const popup = vi.fn(); | ||
| vi.mocked( BrowserWindow.fromWebContents ).mockReturnValue( null ); | ||
| vi.mocked( clipboard.availableFormats ).mockReturnValue( [] ); | ||
| vi.mocked( Menu.buildFromTemplate ).mockReturnValue( { popup } as unknown as Menu ); | ||
| const event = { | ||
| sender: { showDefinitionForSelection: vi.fn() }, | ||
| } as unknown as IpcMainInvokeEvent; | ||
|
|
||
| const resultPromise = showTextContextMenu( | ||
| event, | ||
| makeContext( { selectionText: 'Selected reply' } ) | ||
| ); | ||
| const template = vi.mocked( Menu.buildFromTemplate ).mock.calls[ 0 ][ 0 ]; | ||
| const quote = template.find( ( item ) => item.label === 'Quote in composer' ); | ||
| ( quote?.click as () => void )(); | ||
| const popupOptions = popup.mock.calls[ 0 ][ 0 ]; | ||
| popupOptions.callback(); | ||
|
|
||
| await expect( resultPromise ).resolves.toEqual( { | ||
| action: 'quote-selection', | ||
| selectionText: 'Selected reply', | ||
| } ); | ||
| } ); | ||
| } ); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.