From 8cfe6a5848522df1d16bf2feac2ac0793a320572 Mon Sep 17 00:00:00 2001 From: beyondkmp Date: Fri, 4 Jul 2025 17:19:22 +0800 Subject: [PATCH] feat(settings): add option to disable hardware acceleration (#7811) * feat(settings): add option to disable hardware acceleration - Introduced a new setting to allow users to disable hardware acceleration. - Added corresponding IPC channel and configuration management methods. - Updated UI components to reflect the new setting and prompt for app restart. - Localized confirmation messages for hardware acceleration changes in multiple languages. * fix(settings): add delay before relaunching app after disabling hardware acceleration - Introduced a 500ms delay before the application relaunches to ensure settings are applied correctly. - This change improves user experience by allowing time for the setting to take effect before the app restarts. * fix lint * fix(settings): handle errors when disabling hardware acceleration - Wrapped the hardware acceleration disabling function in a try-catch block to manage potential errors. - Added user feedback through an error message if the operation fails, improving overall robustness. --- packages/shared/IpcChannel.ts | 1 + src/main/index.ts | 8 +++++ src/main/ipc.ts | 4 +++ src/main/services/ConfigManager.ts | 11 +++++- src/preload/index.ts | 4 ++- src/renderer/src/hooks/useSettings.ts | 5 +++ src/renderer/src/i18n/locales/en-us.json | 7 ++++ src/renderer/src/i18n/locales/ja-jp.json | 7 ++++ src/renderer/src/i18n/locales/ru-ru.json | 7 ++++ src/renderer/src/i18n/locales/zh-cn.json | 7 ++++ src/renderer/src/i18n/locales/zh-tw.json | 7 ++++ .../src/pages/settings/GeneralSettings.tsx | 35 ++++++++++++++++++- src/renderer/src/store/migrate.ts | 1 + src/renderer/src/store/settings.ts | 8 +++++ 14 files changed, 109 insertions(+), 3 deletions(-) diff --git a/packages/shared/IpcChannel.ts b/packages/shared/IpcChannel.ts index 7dd60bab06..38c6c2b516 100644 --- a/packages/shared/IpcChannel.ts +++ b/packages/shared/IpcChannel.ts @@ -36,6 +36,7 @@ export enum IpcChannel { App_MacRequestProcessTrust = 'app:mac-request-process-trust', App_QuoteToMain = 'app:quote-to-main', + App_SetDisableHardwareAcceleration = 'app:set-disable-hardware-acceleration', Notification_Send = 'notification:send', Notification_OnClick = 'notification:on-click', diff --git a/src/main/index.ts b/src/main/index.ts index 46ebd7c6e6..e022bb71a8 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -28,6 +28,14 @@ import { windowService } from './services/WindowService' Logger.initialize() +/** + * Disable hardware acceleration if setting is enabled + */ +const disableHardwareAcceleration = configManager.getDisableHardwareAcceleration() +if (disableHardwareAcceleration) { + app.disableHardwareAcceleration() +} + /** * Disable chromium's window animations * main purpose for this is to avoid the transparent window flashing when it is shown diff --git a/src/main/ipc.ts b/src/main/ipc.ts index a9c5169096..f97cb60ed9 100644 --- a/src/main/ipc.ts +++ b/src/main/ipc.ts @@ -561,4 +561,8 @@ export function registerIpc(mainWindow: BrowserWindow, app: Electron.App) { SelectionService.registerIpcHandler() ipcMain.handle(IpcChannel.App_QuoteToMain, (_, text: string) => windowService.quoteToMainWindow(text)) + + ipcMain.handle(IpcChannel.App_SetDisableHardwareAcceleration, (_, isDisable: boolean) => { + configManager.setDisableHardwareAcceleration(isDisable) + }) } diff --git a/src/main/services/ConfigManager.ts b/src/main/services/ConfigManager.ts index 8e4b5d2bf1..a10e7521eb 100644 --- a/src/main/services/ConfigManager.ts +++ b/src/main/services/ConfigManager.ts @@ -24,7 +24,8 @@ export enum ConfigKeys { SelectionAssistantFollowToolbar = 'selectionAssistantFollowToolbar', SelectionAssistantRemeberWinSize = 'selectionAssistantRemeberWinSize', SelectionAssistantFilterMode = 'selectionAssistantFilterMode', - SelectionAssistantFilterList = 'selectionAssistantFilterList' + SelectionAssistantFilterList = 'selectionAssistantFilterList', + DisableHardwareAcceleration = 'disableHardwareAcceleration' } export class ConfigManager { @@ -218,6 +219,14 @@ export class ConfigManager { this.setAndNotify(ConfigKeys.SelectionAssistantFilterList, value) } + getDisableHardwareAcceleration(): boolean { + return this.get(ConfigKeys.DisableHardwareAcceleration, false) + } + + setDisableHardwareAcceleration(value: boolean) { + this.set(ConfigKeys.DisableHardwareAcceleration, value) + } + setAndNotify(key: string, value: unknown) { this.set(key, value, true) } diff --git a/src/preload/index.ts b/src/preload/index.ts index 3120492dde..533263512d 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -290,7 +290,9 @@ const api = { minimizeActionWindow: () => ipcRenderer.invoke(IpcChannel.Selection_ActionWindowMinimize), pinActionWindow: (isPinned: boolean) => ipcRenderer.invoke(IpcChannel.Selection_ActionWindowPin, isPinned) }, - quoteToMainWindow: (text: string) => ipcRenderer.invoke(IpcChannel.App_QuoteToMain, text) + quoteToMainWindow: (text: string) => ipcRenderer.invoke(IpcChannel.App_QuoteToMain, text), + setDisableHardwareAcceleration: (isDisable: boolean) => + ipcRenderer.invoke(IpcChannel.App_SetDisableHardwareAcceleration, isDisable) } // Use `contextBridge` APIs to expose Electron APIs to diff --git a/src/renderer/src/hooks/useSettings.ts b/src/renderer/src/hooks/useSettings.ts index dfb75cc791..43f9e41135 100644 --- a/src/renderer/src/hooks/useSettings.ts +++ b/src/renderer/src/hooks/useSettings.ts @@ -4,6 +4,7 @@ import { SendMessageShortcut, setAssistantIconType, setAutoCheckUpdate as _setAutoCheckUpdate, + setDisableHardwareAcceleration, setLaunchOnBoot, setLaunchToTray, setPinTopicsToTop, @@ -100,6 +101,10 @@ export function useSettings() { }, setShowTokens(showTokens: boolean) { dispatch(setShowTokens(showTokens)) + }, + setDisableHardwareAcceleration(disableHardwareAcceleration: boolean) { + dispatch(setDisableHardwareAcceleration(disableHardwareAcceleration)) + window.api.setDisableHardwareAcceleration(disableHardwareAcceleration) } } } diff --git a/src/renderer/src/i18n/locales/en-us.json b/src/renderer/src/i18n/locales/en-us.json index e76ee35701..3ec5747c48 100644 --- a/src/renderer/src/i18n/locales/en-us.json +++ b/src/renderer/src/i18n/locales/en-us.json @@ -1805,6 +1805,13 @@ "title": "Proxy Settings" }, "proxy.title": "Proxy Address", + "hardware_acceleration": { + "title": "Disable Hardware Acceleration", + "confirm": { + "title": "Restart Required", + "content": "Disabling hardware acceleration requires restarting the app to take effect. Do you want to restart now?" + } + }, "quickAssistant": { "click_tray_to_show": "Click the tray icon to start", "enable_quick_assistant": "Enable Quick Assistant", diff --git a/src/renderer/src/i18n/locales/ja-jp.json b/src/renderer/src/i18n/locales/ja-jp.json index d735329335..c2947a57d8 100644 --- a/src/renderer/src/i18n/locales/ja-jp.json +++ b/src/renderer/src/i18n/locales/ja-jp.json @@ -1787,6 +1787,13 @@ "title": "プロキシ設定" }, "proxy.title": "プロキシアドレス", + "hardware_acceleration": { + "title": "ハードウェアアクセラレーションを無効にする", + "confirm": { + "title": "再起動が必要", + "content": "ハードウェアアクセラレーションを無効にするには、アプリを再起動する必要があります。再起動しますか?" + } + }, "quickAssistant": { "click_tray_to_show": "トレイアイコンをクリックして起動", "enable_quick_assistant": "クイックアシスタントを有効にする", diff --git a/src/renderer/src/i18n/locales/ru-ru.json b/src/renderer/src/i18n/locales/ru-ru.json index fd664fa871..ef329e7747 100644 --- a/src/renderer/src/i18n/locales/ru-ru.json +++ b/src/renderer/src/i18n/locales/ru-ru.json @@ -1787,6 +1787,13 @@ "title": "Настройки прокси" }, "proxy.title": "Адрес прокси", + "hardware_acceleration": { + "title": "Отключить аппаратное ускорение", + "confirm": { + "title": "Требуется перезапуск", + "content": "Отключение аппаратного ускорения требует перезапуска приложения для вступления в силу. Перезапустить приложение?" + } + }, "quickAssistant": { "click_tray_to_show": "Нажмите на иконку трея для запуска", "enable_quick_assistant": "Включить быстрый помощник", diff --git a/src/renderer/src/i18n/locales/zh-cn.json b/src/renderer/src/i18n/locales/zh-cn.json index fee1a37288..9504068ced 100644 --- a/src/renderer/src/i18n/locales/zh-cn.json +++ b/src/renderer/src/i18n/locales/zh-cn.json @@ -1805,6 +1805,13 @@ "title": "代理设置" }, "proxy.title": "代理地址", + "hardware_acceleration": { + "title": "禁用硬件加速", + "confirm": { + "title": "需要重启应用", + "content": "禁用硬件加速需要重启应用才能生效,是否现在重启?" + } + }, "quickAssistant": { "click_tray_to_show": "点击托盘图标启动", "enable_quick_assistant": "启用快捷助手", diff --git a/src/renderer/src/i18n/locales/zh-tw.json b/src/renderer/src/i18n/locales/zh-tw.json index d16130ef52..503c8b07b1 100644 --- a/src/renderer/src/i18n/locales/zh-tw.json +++ b/src/renderer/src/i18n/locales/zh-tw.json @@ -1790,6 +1790,13 @@ "title": "代理伺服器設定" }, "proxy.title": "代理伺服器地址", + "hardware_acceleration": { + "title": "禁用硬件加速", + "confirm": { + "title": "需要重新啟動", + "content": "禁用硬件加速需要重新啟動應用程序才能生效。是否立即重新啟動?" + } + }, "quickAssistant": { "click_tray_to_show": "點選工具列圖示啟動", "enable_quick_assistant": "啟用快捷助手", diff --git a/src/renderer/src/pages/settings/GeneralSettings.tsx b/src/renderer/src/pages/settings/GeneralSettings.tsx index 411fdd4768..9e47317a55 100644 --- a/src/renderer/src/pages/settings/GeneralSettings.tsx +++ b/src/renderer/src/pages/settings/GeneralSettings.tsx @@ -35,7 +35,9 @@ const GeneralSettings: FC = () => { tray, proxyMode: storeProxyMode, enableDataCollection, - enableSpellCheck + enableSpellCheck, + disableHardwareAcceleration, + setDisableHardwareAcceleration } = useSettings() const [proxyUrl, setProxyUrl] = useState(storeProxyUrl) const { theme } = useTheme() @@ -147,6 +149,32 @@ const GeneralSettings: FC = () => { window.api.setSpellCheckLanguages(selectedLanguages) } + const handleHardwareAccelerationChange = (checked: boolean) => { + window.modal.confirm({ + title: t('settings.hardware_acceleration.confirm.title'), + content: t('settings.hardware_acceleration.confirm.content'), + okText: t('common.confirm'), + cancelText: t('common.cancel'), + centered: true, + onOk() { + try { + setDisableHardwareAcceleration(checked) + } catch (error) { + window.message.error({ + content: (error as Error).message, + key: 'disable-hardware-acceleration-error' + }) + return + } + + // 重启应用 + setTimeout(() => { + window.api.relaunchApp() + }, 500) + } + }) + } + return ( @@ -223,6 +251,11 @@ const GeneralSettings: FC = () => { )} + + + {t('settings.hardware_acceleration.title')} + + {t('settings.notification.title')} diff --git a/src/renderer/src/store/migrate.ts b/src/renderer/src/store/migrate.ts index 935da99054..1e29aebd69 100644 --- a/src/renderer/src/store/migrate.ts +++ b/src/renderer/src/store/migrate.ts @@ -1721,6 +1721,7 @@ const migrateConfig = { try { addProvider(state, 'new-api') state.llm.providers = moveProvider(state.llm.providers, 'new-api', 16) + state.settings.disableHardwareAcceleration = false return state } catch (error) { return state diff --git a/src/renderer/src/store/settings.ts b/src/renderer/src/store/settings.ts index 01ca1757f4..778837e388 100644 --- a/src/renderer/src/store/settings.ts +++ b/src/renderer/src/store/settings.ts @@ -163,6 +163,8 @@ export interface SettingsState { spellCheckLanguages: string[] enableQuickPanelTriggers: boolean enableBackspaceDeleteModel: boolean + // 硬件加速设置 + disableHardwareAcceleration: boolean exportMenuOptions: { image: boolean markdown: boolean @@ -310,6 +312,8 @@ export const initialState: SettingsState = { spellCheckLanguages: [], enableQuickPanelTriggers: false, enableBackspaceDeleteModel: true, + // 硬件加速设置 + disableHardwareAcceleration: false, exportMenuOptions: { image: true, markdown: true, @@ -685,6 +689,9 @@ const settingsSlice = createSlice({ setEnableBackspaceDeleteModel: (state, action: PayloadAction) => { state.enableBackspaceDeleteModel = action.payload }, + setDisableHardwareAcceleration: (state, action: PayloadAction) => { + state.disableHardwareAcceleration = action.payload + }, setOpenAISummaryText: (state, action: PayloadAction) => { state.openAI.summaryText = action.payload }, @@ -801,6 +808,7 @@ export const { setExportMenuOptions, setEnableQuickPanelTriggers, setEnableBackspaceDeleteModel, + setDisableHardwareAcceleration, setOpenAISummaryText, setOpenAIServiceTier, setNotificationSettings,