Merge branch 'main' into antd

This commit is contained in:
草鞋没号 2021-11-15 20:00:30 +08:00
commit c9da17b597
2 changed files with 49 additions and 4 deletions

View File

@ -1,5 +1,6 @@
import { join } from 'path'
import { app, BrowserWindow } from 'electron'
import { app, BrowserWindow, ipcMain } from 'electron'
import Store from 'electron-store'
app.disableHardwareAcceleration()
@ -26,6 +27,7 @@ async function mainWin() {
win.loadURL(url)
win.maximize()
win.webContents.openDevTools()
}
}
@ -40,8 +42,21 @@ app.on('window-all-closed', () => {
app.on('second-instance', () => {
if (win) {
// someone tried to run a second instance, we should focus our window.
// Someone tried to run a second instance, we should focus our window.
if (win.isMinimized()) win.restore()
win.focus()
}
})
// -------------------------------------
/**
* Expose 'electron-store' to renderer through 'ipcMain.handle'
*/
const store = new Store
ipcMain.handle('electron-store', async (_evnet, methodSign: string, ...args: any[]) => {
if (typeof (store as any)[methodSign] === 'function') {
return (store as any)[methodSign](...args)
}
return (store as any)[methodSign]
})

View File

@ -3,8 +3,6 @@ import ReactDOM from 'react-dom'
import './index.css'
import App from './App'
console.log('contextBridge ->', window.bridge)
ReactDOM.render(
<React.StrictMode>
<App />
@ -14,3 +12,35 @@ ReactDOM.render(
window.bridge.removeLoading()
},
)
// -----------------------------------------------------------
console.log('contextBridge ->', window.bridge)
// Use 'electron-store'
const store = {
async get(key: string) {
const { invoke } = window.bridge.ipcRenderer
let value = await invoke('electron-store', 'get', key)
try {
value = JSON.parse(value)
} finally {
return value
}
},
async set(key: string, value: any) {
const { invoke } = window.bridge.ipcRenderer
let val = value
try {
if (value && typeof value === 'object') {
val = JSON.stringify(value)
}
} finally {
await invoke('electron-store', 'set', key, val)
}
},
}
await store.set('Date.now', Date.now())
console.log('electron-store ->', 'Date.now:', await store.get('Date.now'))
console.log('electron-store ->', 'path:', await window.bridge.ipcRenderer.invoke('electron-store', 'path'))