Skip to content

Commit

Permalink
refactor: separate integration events
Browse files Browse the repository at this point in the history
  • Loading branch information
CyanSalt committed Mar 21, 2024
1 parent d24e08e commit c04ce6a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 36 deletions.
39 changes: 4 additions & 35 deletions src/renderer/compositions/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -148,10 +148,8 @@ const terminalOptions = $computed<Partial<ITerminalOptions>>(() => {
}
})

interface RendererKeyBinding {
interface RendererKeyBinding extends KeyBindingCommand {
pattern: Partial<KeyboardEvent>,
command: string,
args?: any[],
}

const keybindings = $(useKeyBindings())
Expand Down Expand Up @@ -203,37 +201,8 @@ export async function createTerminalTab(context: Partial<TerminalContext> = {},
}
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
Expand Down
34 changes: 34 additions & 0 deletions src/renderer/utils/shell-integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

}
5 changes: 4 additions & 1 deletion src/typings/menu.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import type { MenuItemConstructorOptions } from 'electron'

export interface MenuItem extends Partial<MenuItemConstructorOptions> {
export interface KeyBindingCommand {
command?: string,
args?: any[],
}

export interface MenuItem extends Partial<MenuItemConstructorOptions>, KeyBindingCommand {
submenu?: MenuItem[],
}

Expand Down

0 comments on commit c04ce6a

Please sign in to comment.