From dfdf044b48127a91de1935ed687ee81ebccf99fb Mon Sep 17 00:00:00 2001 From: pompurin404 Date: Mon, 9 Sep 2024 22:05:27 +0800 Subject: [PATCH] support auto quit without core --- changelog.md | 7 +--- src/main/index.ts | 35 ++++++++++++++-- .../components/settings/general-config.tsx | 41 ++++++++++++++++++- src/shared/types.d.ts | 2 + 4 files changed, 74 insertions(+), 11 deletions(-) diff --git a/changelog.md b/changelog.md index 3717f4d..9abcc0e 100644 --- a/changelog.md +++ b/changelog.md @@ -4,9 +4,4 @@ ### New Features -- 添加交互式使用指南 - -### Bug Fixes - -- 修复 MacOS 无法退出全屏的问题 -- 修复设置页面的样式错误问题 +- 支持自动开启轻量模式 diff --git a/src/main/index.ts b/src/main/index.ts index 5958318..f959e08 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -3,7 +3,7 @@ import { registerIpcMainHandlers } from './utils/ipc' import windowStateKeeper from 'electron-window-state' import { app, shell, BrowserWindow, Menu, dialog, Notification } from 'electron' import { addProfileItem, getAppConfig } from './config' -import { startCore, stopCore } from './core/manager' +import { quitWithoutCore, startCore, stopCore } from './core/manager' import { triggerSysProxy } from './sys/sysproxy' import icon from '../../resources/icon.png?asset' import { createTray } from './resolve/tray' @@ -17,6 +17,7 @@ import { existsSync, writeFileSync } from 'fs' import { taskDir } from './utils/dirs' import path from 'path' +let quitTimeout: NodeJS.Timeout | null = null export let mainWindow: BrowserWindow | null = null if (process.platform === 'win32' && !is.dev) { try { @@ -97,7 +98,6 @@ app.whenReady().then(async () => { } catch (e) { dialog.showErrorBox('内核启动出错', `${e}`) } - // Default open or close DevTools by F12 in development // and ignore CommandOrControl + R in production. // see https://github.com/alex8088/electron-toolkit/tree/master/packages/utils @@ -175,8 +175,23 @@ export async function createWindow(): Promise { }) mainWindowState.manage(mainWindow) mainWindow.on('ready-to-show', async () => { - const { silentStart } = await getAppConfig() + const { + silentStart = false, + autoQuitWithoutCore = false, + autoQuitWithoutCoreDelay = 60 + } = await getAppConfig() + if (autoQuitWithoutCore && !mainWindow?.isVisible()) { + if (quitTimeout) { + clearTimeout(quitTimeout) + } + quitTimeout = setTimeout(async () => { + await quitWithoutCore() + }, autoQuitWithoutCoreDelay * 1000) + } if (!silentStart) { + if (quitTimeout) { + clearTimeout(quitTimeout) + } mainWindow?.show() mainWindow?.focusOnWebView() } @@ -185,9 +200,18 @@ export async function createWindow(): Promise { mainWindow?.webContents.reload() }) - mainWindow.on('close', (event) => { + mainWindow.on('close', async (event) => { event.preventDefault() mainWindow?.hide() + const { autoQuitWithoutCore = false, autoQuitWithoutCoreDelay = 60 } = await getAppConfig() + if (autoQuitWithoutCore) { + if (quitTimeout) { + clearTimeout(quitTimeout) + } + quitTimeout = setTimeout(async () => { + await quitWithoutCore() + }, autoQuitWithoutCoreDelay * 1000) + } }) mainWindow.webContents.setWindowOpenHandler((details) => { @@ -206,6 +230,9 @@ export async function createWindow(): Promise { export function showMainWindow(): void { if (mainWindow) { + if (quitTimeout) { + clearTimeout(quitTimeout) + } mainWindow.show() mainWindow.focusOnWebView() } diff --git a/src/renderer/src/components/settings/general-config.tsx b/src/renderer/src/components/settings/general-config.tsx index 5365e3c..2eadec5 100644 --- a/src/renderer/src/components/settings/general-config.tsx +++ b/src/renderer/src/components/settings/general-config.tsx @@ -1,7 +1,7 @@ import React, { Key, useState } from 'react' import SettingCard from '../base/base-setting-card' import SettingItem from '../base/base-setting-item' -import { Button, Input, Select, SelectItem, Switch, Tab, Tabs } from '@nextui-org/react' +import { Button, Input, Select, SelectItem, Switch, Tab, Tabs, Tooltip } from '@nextui-org/react' import { BiCopy } from 'react-icons/bi' import useSWR from 'swr' import { @@ -17,6 +17,7 @@ import { useAppConfig } from '@renderer/hooks/use-app-config' import { platform } from '@renderer/utils/init' import { useTheme } from 'next-themes' import debounce from '@renderer/utils/debounce' +import { IoIosHelpCircle } from 'react-icons/io' const GeneralConfig: React.FC = () => { const { data: enable, mutate: mutateEnable } = useSWR('checkAutoRun', checkAutoRun) @@ -29,6 +30,8 @@ const GeneralConfig: React.FC = () => { proxyInTray = true, useWindowFrame = false, useSubStore = true, + autoQuitWithoutCore = false, + autoQuitWithoutCoreDelay = 60, useCustomSubStore = false, customSubStoreUrl, envType = platform === 'win32' ? 'powershell' : 'bash', @@ -103,6 +106,42 @@ const GeneralConfig: React.FC = () => { }} /> + + + + } + divider + > + { + patchAppConfig({ autoQuitWithoutCore: v }) + }} + /> + + {autoQuitWithoutCore && ( + + { + let num = parseInt(v) + if (isNaN(num)) num = 5 + if (num < 5) num = 5 + await patchAppConfig({ autoQuitWithoutCoreDelay: num }) + }} + /> + + )}