🐛 修复(shortCut.ts):修复快捷键注册前后的值不一致bug

📝 更新(components/Error.tsx):更新Error组件以清除错误消息的效果
♻️ 重构(hooks/useShortCut.ts):更新注册快捷键逻辑以使用新的useStore逻辑
This commit is contained in:
yuruo
2024-06-09 07:36:13 +08:00
parent 4b7690272b
commit 6f06ed0de4
3 changed files with 12 additions and 4 deletions

View File

@@ -8,8 +8,9 @@ const config = {
export const registerShortCut = (win: BrowserWindow) => {
ipcMain.handle("shortCut", (_event: IpcMainInvokeEvent, type: 'search', shortCut: string) => {
// react 严格模式会执行两次,可能会导致快捷键重复注册,这里在注册前会删除旧快捷键
// react 严格模式会执行两次,可能会导致快捷键重复注册,这里在注册前会删除旧快捷键,也用户注册过快捷键想修改成其他快捷键
if (config.search) globalShortcut.unregister(config.search)
config.search = shortCut
switch(type){
case 'search':
return registerSearchShortCut(shortCut, win)
@@ -18,9 +19,10 @@ export const registerShortCut = (win: BrowserWindow) => {
}
function registerSearchShortCut(shortCut: string, win: BrowserWindow){
return globalShortcut.register(shortCut, () => {
const res = globalShortcut.register(shortCut, () => {
win.isVisible() ? win.hide() : win.show()
})
return res
}
app.on('will-quit', () => {
// Unregister all shortcuts.

View File

@@ -1,7 +1,13 @@
import { useStore } from "@renderer/store/useStore"
import { useEffect } from "react"
function Error(){
const {error} = useStore()
const error = useStore(state => state.error)
const setError = useStore(state => state.setError)
useEffect(() => {
const id = setTimeout(() => setError(""), 2000)
return () => clearTimeout(id)
}, [error])
if (!error) return <></>
return <><div className="bg-red-600 text-white">{error}</div></>
}

View File

@@ -1,7 +1,7 @@
import { useStore } from "@renderer/store/useStore"
export default() => {
const {setError} = useStore()
const setError = useStore(state => state.setError)
const register = async (type: 'search', shortCut: string)=>{
const ret = await window.api.shortCut(type, shortCut)
ret || setError("注册失败")