From 31b22cf92c5ed70ea722fed2e7d969c635af943e Mon Sep 17 00:00:00 2001 From: wizardchen Date: Wed, 22 Apr 2026 20:00:11 +0800 Subject: [PATCH] feat: Enhance Wails desktop app key handling for page reload - Introduced a check for Wails desktop environment to conditionally intercept Cmd/Ctrl+R key presses, preventing full page reloads that could lead to a white screen. - Updated event listener management to ensure key handling is only active in the Wails desktop context, improving user experience by maintaining application state during refresh actions. - Added comments for clarity on the purpose of these changes and their impact on different deployment environments. --- frontend/src/views/platform/index.vue | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/frontend/src/views/platform/index.vue b/frontend/src/views/platform/index.vue index 30d467671..ed25199d4 100644 --- a/frontend/src/views/platform/index.vue +++ b/frontend/src/views/platform/index.vue @@ -36,7 +36,15 @@ const reloadApp = () => { } provide('app:reload', reloadApp) +// 仅在 Wails 桌面端运行时拦截 Cmd/Ctrl+R: +// 桌面端没有浏览器地址栏,整页重载会白屏,所以用前端软刷新替代。 +// 浏览器(含 Web 版 / 非 Lite 部署)里不拦截,交给浏览器做真正的整页刷新, +// 否则会出现左侧菜单、全局设置、Pinia store 等不随"刷新"一起重置的问题。 +// @ts-ignore +const isWailsDesktop = typeof window !== 'undefined' && !!(window as any).runtime?.EventsOn + const handleGlobalKeyDown = (e: KeyboardEvent) => { + if (!isWailsDesktop) return if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === 'r') { e.preventDefault() reloadApp() @@ -140,9 +148,8 @@ onMounted(() => { document.addEventListener('dragover', handleGlobalDragOver, true); document.addEventListener('dragleave', handleGlobalDragLeave, true); document.addEventListener('drop', handleGlobalDrop, true); - window.addEventListener('keydown', handleGlobalKeyDown); - // @ts-ignore - if (window.runtime?.EventsOn) { + if (isWailsDesktop) { + window.addEventListener('keydown', handleGlobalKeyDown); // @ts-ignore window.runtime.EventsOn('app:reload', () => { reloadApp() @@ -156,11 +163,13 @@ onUnmounted(() => { document.removeEventListener('dragover', handleGlobalDragOver, true); document.removeEventListener('dragleave', handleGlobalDragLeave, true); document.removeEventListener('drop', handleGlobalDrop, true); - window.removeEventListener('keydown', handleGlobalKeyDown); - // @ts-ignore - if (window.runtime?.EventsOff) { + if (isWailsDesktop) { + window.removeEventListener('keydown', handleGlobalKeyDown); // @ts-ignore - window.runtime.EventsOff('app:reload') + if (window.runtime?.EventsOff) { + // @ts-ignore + window.runtime.EventsOff('app:reload') + } } dragCounter = 0; });