Skip to content

Commit e905f6e

Browse files
committed
fix: key binding args
1 parent 3c69d85 commit e905f6e

File tree

6 files changed

+42
-13
lines changed

6 files changed

+42
-13
lines changed

addons/launcher/src/renderer/LauncherList.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ function openLauncherMenu(launcher: Launcher, tab: TerminalTab | undefined, even
200200
{
201201
label: 'Launch#!launcher.2',
202202
command: 'start-launcher',
203-
args: [launcher],
203+
args: [launcher, '$event'],
204204
},
205205
{
206206
label: 'Open in External#!launcher.3',
@@ -210,7 +210,7 @@ function openLauncherMenu(launcher: Launcher, tab: TerminalTab | undefined, even
210210
scripts.map((script, index) => ({
211211
label: script.name,
212212
command: 'run-launcher-script',
213-
args: [launcher, index],
213+
args: [launcher, index, '$event'],
214214
})),
215215
tab ? updatingItems : [],
216216
[

src/main/lib/menu.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { MenuItemConstructorOptions, PopupOptions } from 'electron'
1+
import type { BaseWindow, KeyboardEvent, MenuItemConstructorOptions, PopupOptions } from 'electron'
22
import { app, BrowserWindow, globalShortcut, Menu, nativeImage, TouchBar } from 'electron'
33
import { ipcMain } from '@commas/electron-ipc'
44
import type { TranslationVariables } from '@commas/types/i18n'
@@ -40,6 +40,17 @@ const terminalKeyBindings: MenuItem[] = require(resourceFile('terminal.menu.json
4040
const focusedWindow = $(useFocusedWindow())
4141
const hasFocusedWindow = $computed(() => Boolean(focusedWindow))
4242

43+
interface CommandSource {
44+
$window: BaseWindow | undefined,
45+
$event: KeyboardEvent,
46+
}
47+
48+
function replaceCommandArgs(args: unknown[], source: CommandSource) {
49+
return args.map(value => {
50+
return typeof value === 'string' && Object.hasOwn(source, value) ? source[value] : value
51+
})
52+
}
53+
4354
function resolveBindingCommand(binding: MenuItem) {
4455
const result: MenuItemConstructorOptions = { ...binding }
4556
if (binding.label) {
@@ -48,12 +59,19 @@ function resolveBindingCommand(binding: MenuItem) {
4859
if (binding.command) {
4960
if (binding.command.startsWith('global-main:')) {
5061
result.click = (self, frame, event) => {
51-
globalHandler.invoke(binding.command as never, ...(binding.args ?? []) as never, frame, event)
62+
globalHandler.invoke(
63+
binding.command as never,
64+
...replaceCommandArgs(binding.args ?? [], { $window: frame, $event: event }) as never,
65+
)
5266
}
5367
} else {
5468
result.click = (self, frame, event) => {
5569
if (frame) {
56-
send((frame as BrowserWindow).webContents, binding.command as never, ...(binding.args ?? []) as never, event)
70+
send(
71+
(frame as BrowserWindow).webContents,
72+
binding.command as never,
73+
...replaceCommandArgs(binding.args ?? [], { $window: frame, $event: event }) as never,
74+
)
5775
}
5876
}
5977
result.enabled = hasFocusedWindow

src/main/lib/message.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ declare module '@commas/electron-ipc' {
5858
file: string | undefined,
5959
}
6060
export interface GlobalCommands {
61-
'global-main:look-up': (text: string, frame?: BrowserWindow) => void,
61+
'global-main:look-up': (text: string, frame: BrowserWindow | undefined) => void,
6262
'global-main:copy': (text: string) => void,
6363
'global-main:open-url': (url: string) => void,
6464
'global-main:open-path': (uri: string) => void,

src/main/lib/window.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,7 @@ function handleWindowMessages() {
170170
const frame = BrowserWindow.fromWebContents(event.sender)
171171
openFile(file, frame)
172172
})
173-
globalHandler.handle('global-main:open-window', (arg?: Partial<TerminalContext> | BrowserWindow) => {
174-
// Convert BrowserWindow from menu
175-
const context = arg && 'contentView' in arg ? undefined : arg
173+
globalHandler.handle('global-main:open-window', context => {
176174
createWindow(context)
177175
})
178176
ipcMain.handle('create-web-contents', (event, rect) => {

src/renderer/utils/frame.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export function createContextMenu() {
5454
{
5555
label: 'Look Up "${0}"#!menu.lookup',
5656
command: 'global-main:look-up',
57-
args: [text.length > 50 ? text.slice(0, 50) + '...' : text],
57+
args: [text.length > 50 ? text.slice(0, 50) + '...' : text, '$window'],
5858
},
5959
]
6060
}

src/types/menu.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,30 @@ export interface XtermEvents {}
77

88
type OptionalArgs<T extends { args: unknown[] }> = [] extends T['args'] ? Omit<T, 'args'> & Partial<Pick<T, 'args'>> : T
99

10-
type OmitArgs<T extends unknown[], U extends unknown[]> = T extends [...infer V, ...U] ? V : T
10+
type FindKey<T extends Record<PropertyKey, unknown>, U> = keyof {
11+
[K in keyof T as T[K] extends U ? K : never]: unknown
12+
}
13+
14+
type CoalesceNever<T, U> = [T] extends [never] ? U : T
15+
16+
type RecoverArgs<T extends unknown[], U extends Record<PropertyKey, unknown>> = T extends [infer Head, ...infer Others]
17+
? [CoalesceNever<FindKey<U, Head>, Head>, ...RecoverArgs<Others, U>]
18+
: T
19+
20+
type RecoverCommandArgs<T extends unknown[]> = RecoverArgs<T, {
21+
$window: BrowserWindow | undefined,
22+
$event: KeyboardEvent,
23+
}>
1124

1225
export type KeyBindingCommand = ValueOf<{
1326
[K in keyof RendererEvents]: OptionalArgs<{
1427
command: K,
15-
args: OmitArgs<Parameters<RendererEvents[K]>, [KeyboardEvent]>,
28+
args: RecoverCommandArgs<Parameters<RendererEvents[K]>>,
1629
}>
1730
}> | ValueOf<{
1831
[K in keyof GlobalCommands]: OptionalArgs<{
1932
command: K,
20-
args: OmitArgs<Parameters<GlobalCommands[K]>, [BrowserWindow | undefined, KeyboardEvent]>,
33+
args: RecoverCommandArgs<Parameters<GlobalCommands[K]>>,
2134
}>
2235
}> | ValueOf<{
2336
[K in keyof XtermEvents]: OptionalArgs<{

0 commit comments

Comments
 (0)