fix: 修复被某些考试软件抢占窗口的问题 #11
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
修复:增强窗口置顶能力并重构快捷键逻辑
问题描述
在 Windows 平台上,当一些具有高优先级的软件(如某些考试客户端、全屏应用)运行时,它们会强制将自己的窗口置于最顶层。这导致本应用通过快捷键
Alt+H唤出时,窗口会闪烁一下然后被覆盖,无法正常使用。此外,快捷键系统在处理 Windows 特有的AltGraph(右Alt) 按键时存在识别问题。解决方案
引入脉冲式置顶机制:在
src/main/shortcuts.ts中,当窗口被显示后,会启动一个为期5秒的定时器,以150毫秒的频率持续强制窗口置顶 (setAlwaysOnTop+moveTop)。这种高频的置顶策略能有效对抗其他应用的抢占行为,确保窗口稳定显示在最前方。优化焦点管理:为了避免唤出窗口时打断用户当前的工作流,现已在 Windows 和 macOS 平台上改用
showInactive()方法来显示窗口。这使得窗口可以在不获取焦点的情况下显示,用户可以继续在之前的应用中输入,体验更流畅。其他改进
在修复核心问题的基础上,还对相关模块进行了以下增强和代码质量提升:
统一左右 Alt 键行为 (Windows):
src/renderer/src/lib/utils/keyboard.ts: 增加了对AltGraph(右 Alt) 的识别逻辑,避免其被错误地识别为Ctrl+Alt的组合,确保快捷键在录制和显示时,左右 Alt 键的行为一致。src/main/shortcuts.ts: 在 Windows 平台上,为仅包含Alt的快捷键组合自动注册一个Ctrl+Alt的别名。这解决了右 Alt 键无法触发快捷键的问题,并通过记录已注册的快捷键,避免了重复注册导致的内存泄漏。增强类型安全:
src/main/index.ts: 增加了AbortLikeError类型守卫,替换了原先的any类型。现在,应用的生命周期钩子可以精准地识别并忽略由用户主动取消操作(如关闭窗口)造成的预期内异常。src/renderer/src/lib/store/shortcuts.ts: 新增了PersistentShortcutsState类型守卫,使得migrate函数不再依赖any类型,代码更加健壮,同时保留了默认快捷键的合并逻辑。遵循 React Hooks 规范:
src/renderer/src/coder/index.tsx: 将useEffect的依赖项数组修正为[syncAppState],以满足 React Hooks 的 exhaustive-deps 规则,避免潜在的 bug 和不必要的重复渲染。测试情况