From c04ce6a9bc09388d4d08bb263bbdf2ff6add5331 Mon Sep 17 00:00:00 2001 From: CyanSalt Date: Thu, 21 Mar 2024 15:23:26 +0800 Subject: [PATCH] refactor: separate integration events --- src/renderer/compositions/terminal.ts | 39 +++---------------------- src/renderer/utils/shell-integration.ts | 34 +++++++++++++++++++++ src/typings/menu.ts | 5 +++- 3 files changed, 42 insertions(+), 36 deletions(-) diff --git a/src/renderer/compositions/terminal.ts b/src/renderer/compositions/terminal.ts index 9df1f74b..12b3e1bc 100644 --- a/src/renderer/compositions/terminal.ts +++ b/src/renderer/compositions/terminal.ts @@ -14,7 +14,7 @@ import { isMatch, trim } from 'lodash' import { effectScope, markRaw, nextTick, reactive, shallowReactive, toRaw, watch, watchEffect } from 'vue' import * as commas from '../../../api/core-renderer' import { createDeferred, createIDGenerator } from '../../shared/helper' -import type { MenuItem } from '../../typings/menu' +import type { KeyBindingCommand, MenuItem } from '../../typings/menu' import type { TerminalContext, TerminalInfo, TerminalTab, TerminalTabCharacter } from '../../typings/terminal' import { toKeyEventPattern } from '../utils/accelerator' import { openContextMenu } from '../utils/frame' @@ -148,10 +148,8 @@ const terminalOptions = $computed>(() => { } }) -interface RendererKeyBinding { +interface RendererKeyBinding extends KeyBindingCommand { pattern: Partial, - command: string, - args?: any[], } const keybindings = $(useKeyBindings()) @@ -203,37 +201,8 @@ export async function createTerminalTab(context: Partial = {}, } const shellIntegration = tab.addons.shellIntegration if (shellIntegration) { - if (shellIntegration.completion) { - switch (event.key) { - case 'Enter': - case 'Tab': - event.preventDefault() - if (event.type === 'keydown') { - return !shellIntegration.applySelectedCompletion(event.key === 'Enter') - } - return false - case 'Escape': - if (event.type === 'keydown') { - shellIntegration.clearCompletion() - } - return false - case 'ArrowUp': - if (event.type === 'keydown') { - shellIntegration.selectPreviousCompletion() - } - return false - case 'ArrowDown': - if (event.type === 'keydown') { - shellIntegration.selectNextCompletion() - } - return false - } - } else { - if (['ArrowUp', 'ArrowDown'].includes(event.key) && event.type === 'keydown') { - shellIntegration.skipCompletion() - return true - } - } + const defaultProcessed = shellIntegration.handleCustomKeyEvent(event) + if (typeof defaultProcessed === 'boolean') return defaultProcessed } const matchedItem = rendererKeybindings.find(item => isMatch(event, item.pattern)) if (!matchedItem) return true diff --git a/src/renderer/utils/shell-integration.ts b/src/renderer/utils/shell-integration.ts index e66f2ffe..7d31ef33 100644 --- a/src/renderer/utils/shell-integration.ts +++ b/src/renderer/utils/shell-integration.ts @@ -730,4 +730,38 @@ export class ShellIntegrationAddon implements ITerminalAddon { this.selectCompletion(target) } + handleCustomKeyEvent(event: KeyboardEvent): boolean | void { + if (this.completion) { + switch (event.key) { + case 'Enter': + case 'Tab': + event.preventDefault() + if (event.type === 'keydown') { + return !this.applySelectedCompletion(event.key === 'Enter') + } + return false + case 'Escape': + if (event.type === 'keydown') { + this.clearCompletion() + } + return false + case 'ArrowUp': + if (event.type === 'keydown') { + this.selectPreviousCompletion() + } + return false + case 'ArrowDown': + if (event.type === 'keydown') { + this.selectNextCompletion() + } + return false + } + } else { + if (['ArrowUp', 'ArrowDown'].includes(event.key) && event.type === 'keydown') { + this.skipCompletion() + return true + } + } + } + } diff --git a/src/typings/menu.ts b/src/typings/menu.ts index 8d4d79d5..a696ce04 100644 --- a/src/typings/menu.ts +++ b/src/typings/menu.ts @@ -1,8 +1,11 @@ import type { MenuItemConstructorOptions } from 'electron' -export interface MenuItem extends Partial { +export interface KeyBindingCommand { command?: string, args?: any[], +} + +export interface MenuItem extends Partial, KeyBindingCommand { submenu?: MenuItem[], }