mirror of
https://github.com/yuruotong1/autoMate.git
synced 2026-03-22 04:57:18 +08:00
✨ 添加(ui/autoMate/src/main/code/index.ts):将引入的文件名从'shortcut'改为'shortCut'
📝 更新(ui/autoMate/src/main/code/shortCut.ts):更新快捷键注册逻辑和错误处理 🔧 添加(ui/autoMate/src/preload/index.d.ts):扩展api对象以支持快捷键注册 🔧 更新(ui/autoMate/src/preload/index.ts):添加向主进程发送快捷键消息的方法 🔧 更新(ui/autoMate/src/renderer/src/App.tsx):在App组件中注册搜索快捷键 🔧 添加(ui/autoMate/src/renderer/src/components/Error.tsx):新增错误组件展示逻辑 🔧 添加(ui/autoMate/src/renderer/src/hooks/useShortCut.ts):新增自定义hook用于注册快捷键 🔧 更新(ui/autoMate/src/renderer/src/store/useStore.ts):添加错误状态管理和设置错误消息功能
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { app } from 'electron'
|
||||
import { createWindow } from './window'
|
||||
import * as ipc from './ipc'
|
||||
import { registerShortCut } from './shortcut'
|
||||
import { registerShortCut } from './shortCut'
|
||||
|
||||
|
||||
app.whenReady().then(() => {
|
||||
|
||||
@@ -1,19 +1,28 @@
|
||||
import { BrowserWindow } from "electron"
|
||||
|
||||
import { BrowserWindow, IpcMainInvokeEvent } from "electron"
|
||||
import { ipcMain } from "electron"
|
||||
const { app, globalShortcut } = require('electron')
|
||||
|
||||
const config = {
|
||||
search: ""
|
||||
}
|
||||
export const registerShortCut = (win: BrowserWindow) => {
|
||||
app.whenReady().then(() => {
|
||||
// Register a 'CommandOrControl+X' shortcut listener.
|
||||
const ret = globalShortcut.register('CommandOrControl+X', () => {
|
||||
win.show()
|
||||
|
||||
ipcMain.handle("shortCut", (_event: IpcMainInvokeEvent, type: 'search', shortCut: string) => {
|
||||
// react 严格模式会执行两次,可能会导致快捷键重复注册,这里在注册前会删除旧快捷键
|
||||
if (config.search) globalShortcut.unregister(config.search)
|
||||
switch(type){
|
||||
case 'search':
|
||||
return registerSearchShortCut(shortCut, win)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (!ret) {
|
||||
console.log('registration failed')
|
||||
}
|
||||
})
|
||||
|
||||
function registerSearchShortCut(shortCut: string, win: BrowserWindow){
|
||||
return globalShortcut.register(shortCut, () => {
|
||||
win.isVisible() ? win.hide() : win.show()
|
||||
})
|
||||
}
|
||||
app.on('will-quit', () => {
|
||||
// Unregister all shortcuts.
|
||||
globalShortcut.unregisterAll()
|
||||
})}
|
||||
})
|
||||
3
ui/autoMate/src/preload/index.d.ts
vendored
3
ui/autoMate/src/preload/index.d.ts
vendored
@@ -4,7 +4,8 @@ declare global {
|
||||
interface Window {
|
||||
electron: ElectronAPI
|
||||
api: {
|
||||
hideWindow: () => void
|
||||
hideWindow: () => void,
|
||||
shortCut: (type: 'search', shortCut: string) => Promise<boolean>
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,8 +5,10 @@ import { electronAPI } from '@electron-toolkit/preload'
|
||||
const api = {
|
||||
hideWindow: () =>{
|
||||
ipcRenderer.send("hideWindow")
|
||||
},
|
||||
shortCut: (type: 'search', shortCut: string) => {
|
||||
return ipcRenderer.invoke("shortCut", type, shortCut)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Use `contextBridge` APIs to expose Electron APIs to
|
||||
|
||||
@@ -1,11 +1,16 @@
|
||||
import Result from "./components/Result"
|
||||
import Search from "./components/Search"
|
||||
import { CodeProvider } from "./context/CodeContext"
|
||||
import useShortCut from "./hooks/useShortCut"
|
||||
import Error from "./components/Error"
|
||||
|
||||
|
||||
function App(): JSX.Element {
|
||||
const shortCut = useShortCut()
|
||||
shortCut.register("search", "CommandOrControl+n")
|
||||
return (
|
||||
<CodeProvider>
|
||||
<Error/>
|
||||
<Search />
|
||||
<Result />
|
||||
</CodeProvider>
|
||||
|
||||
10
ui/autoMate/src/renderer/src/components/Error.tsx
Normal file
10
ui/autoMate/src/renderer/src/components/Error.tsx
Normal file
@@ -0,0 +1,10 @@
|
||||
import { useStore } from "@renderer/store/useStore"
|
||||
|
||||
function Error(){
|
||||
const {error} = useStore()
|
||||
if (!error) return <></>
|
||||
return <><div className="bg-red-600 text-white">{error}</div></>
|
||||
}
|
||||
|
||||
export default Error
|
||||
|
||||
14
ui/autoMate/src/renderer/src/hooks/useShortCut.ts
Normal file
14
ui/autoMate/src/renderer/src/hooks/useShortCut.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { useStore } from "@renderer/store/useStore"
|
||||
|
||||
export default() => {
|
||||
const {setError} = useStore()
|
||||
const register = async (type: 'search', shortCut: string)=>{
|
||||
const ret = await window.api.shortCut(type, shortCut)
|
||||
ret || setError("注册失败")
|
||||
|
||||
}
|
||||
return {
|
||||
register
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,12 +4,16 @@ interface StateProps{
|
||||
data: DataType[],
|
||||
setData: (data: DataType[]) => void,
|
||||
search: string,
|
||||
setSearch: (search: string) => void
|
||||
setSearch: (search: string) => void,
|
||||
error: string,
|
||||
setError: (error: string) => void
|
||||
}
|
||||
export const useStore = create<StateProps>((set) => ({
|
||||
data: [],
|
||||
setData: (data) => set({data}),
|
||||
search: "",
|
||||
setSearch: (search) => set({search})
|
||||
setSearch: (search) => set({search}),
|
||||
error: "",
|
||||
setError: (error) => set({error})
|
||||
}))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user