From 95d0488fda4a5d65abb04cda83471c6bbef5fc81 Mon Sep 17 00:00:00 2001 From: CyanSalt Date: Mon, 18 Jan 2021 14:44:10 +0800 Subject: [PATCH] fix: close tabs by keyboard --- main/internal/shell/command.ts | 2 +- renderer/components/app.vue | 20 +++----------------- renderer/hooks/launcher.ts | 6 ++++++ renderer/hooks/shell.ts | 3 +++ renderer/hooks/terminal.ts | 9 +++++++++ 5 files changed, 22 insertions(+), 18 deletions(-) diff --git a/main/internal/shell/command.ts b/main/internal/shell/command.ts index 6373b1f9..4e507047 100644 --- a/main/internal/shell/command.ts +++ b/main/internal/shell/command.ts @@ -24,7 +24,7 @@ export async function executeCommand(event: IpcMainInvokeEvent, line: string, co controller.raw ? argv : yargsParser(argv), event ) - return { code: 0, stdout: String(stdout) } + return { code: 0, stdout: String(stdout ?? '') } } else { throw new Error(`command not found: ${command}`) } diff --git a/renderer/components/app.vue b/renderer/components/app.vue index f1256ffc..9164238b 100644 --- a/renderer/components/app.vue +++ b/renderer/components/app.vue @@ -36,20 +36,18 @@ import { ipcRenderer } from 'electron' import { reactive, toRefs, unref, onMounted } from 'vue' import * as commas from '../../api/renderer' -import type { Launcher } from '../../typings/launcher' import { loadAddons, loadCustomJS } from '../hooks/addon' import { useFullscreen, handleFrameMessages, } from '../hooks/frame' -import { runLauncherScript } from '../hooks/launcher' +import { handleLauncherMessages } from '../hooks/launcher' import { useIsTabListEnabled, useWillQuit, confirmClosing, handleShellMessages, } from '../hooks/shell' -import type { CreateTerminalTabOptions } from '../hooks/terminal' import { useCurrentTerminal, useTerminalTabs, @@ -84,6 +82,7 @@ export default { handleFrameMessages() handleShellMessages() handleTerminalMessages() + handleLauncherMessages() const index = process.argv.indexOf('--') + 1 const args = index ? process.argv.slice(index) : [] @@ -98,22 +97,9 @@ export default { ipcRenderer.invoke(command, extra) }) - ipcRenderer.on('open-tab', (event, options: CreateTerminalTabOptions) => { - createTerminalTab(options) - }) - - ipcRenderer.on('run-script', (event, { launcher, index: scriptIndex }: { launcher: Launcher, index: number }) => { - runLauncherScript(launcher, scriptIndex) - }) - - const willQuitRef = useWillQuit() - ipcRenderer.on('before-quit', () => { - willQuitRef.value = true - }) - const tabsRef = useTerminalTabs() window.addEventListener('beforeunload', async event => { - const willQuit = unref(willQuitRef) + const willQuit = unref(useWillQuit()) const tabs = unref(tabsRef) if (!willQuit && tabs.length > 1) { event.returnValue = false diff --git a/renderer/hooks/launcher.ts b/renderer/hooks/launcher.ts index c2cd58a6..dccf1ebd 100644 --- a/renderer/hooks/launcher.ts +++ b/renderer/hooks/launcher.ts @@ -101,3 +101,9 @@ export function moveLauncher(from: number, to: number) { } launchersRef.value = launchers } + +export function handleLauncherMessages() { + ipcRenderer.on('run-script', (event, { launcher, index: scriptIndex }: { launcher: Launcher, index: number }) => { + runLauncherScript(launcher, scriptIndex) + }) +} diff --git a/renderer/hooks/shell.ts b/renderer/hooks/shell.ts index a364598f..29a4ab16 100644 --- a/renderer/hooks/shell.ts +++ b/renderer/hooks/shell.ts @@ -40,4 +40,7 @@ export function handleShellMessages() { ipcRenderer.on('toggle-tab-list', () => { isTabListEnabledRef.value = !isTabListEnabledRef.value }) + ipcRenderer.on('before-quit', () => { + willQuitRef.value = true + }) } diff --git a/renderer/hooks/terminal.ts b/renderer/hooks/terminal.ts index cbadf8f1..b30685cf 100644 --- a/renderer/hooks/terminal.ts +++ b/renderer/hooks/terminal.ts @@ -184,6 +184,15 @@ function getTerminalTabTitle(tab: TerminalTab) { } export function handleTerminalMessages() { + ipcRenderer.on('open-tab', (event, options: CreateTerminalTabOptions) => { + createTerminalTab(options) + }) + ipcRenderer.on('close-tab', () => { + const currentTerminal = unref(useCurrentTerminal()) + if (currentTerminal) { + closeTerminalTab(currentTerminal) + } + }) ipcRenderer.on('input-terminal', (event, data: Pick & { data: string }) => { const tabs = unref(tabsRef) const tab = tabs.find(item => item.pid === data.pid)