let template = [ { label:'文件', submenu:[ { label:'新建', accelerator:'CmdOrCtrl+N', click:(menuItem,browserWindow,event) => { browserWindow.webContents.send('create-new-file') } }, { label:'保存', accelerator:'CmdOrCtrl+S', click:(menuItem,browserWindow,event) => { browserWindow.webContents.send('create-edit-file') } }, { label:'搜索', accelerator:'CmdOrCtrl+F', click:(menuItem,browserWindow,event) => { browserWindow.webContents.send('search-file') } }, { label:'导入', accelerator:'CmdOrCtrl+O', click:(menuItem,browserWindow,event) => { browserWindow.webContents.send('import-file') } }, ] }, { label:'编辑', submenu:[ { label:'撤销', accelerator:'CmdOrCtrl+Z', role:'undo' }, { label:'重做', accelerator:'Shift+CmdOrCtrl+Z', role:'undo' }, { type:'separator' }, { label:'剪贴', accelerator:'CmdOrCtrl+X', role:'cut' }, { label:'复制', accelerator:'CmdOrCtrl+C', role:'copy' }, { label:'粘贴', accelerator:'CmdOrCtrl+V', role:'paste' }, { label:'全选', accelerator:'CmdOrCtrl+A', role:'selectall' }, ] }, { label:'视图', submenu:[ { label:'刷新当前页面', accelerator:'CmdOrCtrl+R', click:(item,focusedWindow) => { if(focusedWindow){ focusedWindow.reload() } } }, { label:'切换全屏幕', accelerator:(() => { if(process.platform === 'darwin'){ return 'Ctrl+Command+F' }else{ return 'F11' } })(), click:(item,focusedWindow) => { if(focusedWindow){ focusedWindow.setFullScreen(!focusedWindow.isFullScreen()) } } }, { label:'切换开发者工具', accelerator:(function(){ if(process.platform === 'darwin'){ return 'A+Command+I' }else{ return 'Ctrl + Shift + I' } })(), click:(item,focusedWindow) => { if(focusedWindow){ focusedWindow.toggleDevTools() } } } ] }, { label:'窗口', role:"window", submenu:[ { label:'最小化', accelerator:'CmdOrCtrl+M', role:"minimize" }, { label:'关闭', accelerator:'CmdOrCtrl+W', role:"close" } ] }, { label:'帮助', role:"help", submenu:[ { label:'学习更多', click:() => { shell.openExternal('http://electron.atom.io') } } ] }, ]
if(process.platform === 'darwin'){ const name = app.getName() template.unshift({ label:name, submenu:[{ label: `关于${name}`, role:"about" },{ type:'separator' },{ label:'设置', accelerator:'Command+,', click:() => {
} },{ label:'服务', role:'services', submenu:[], },{ type:'separator' },{ label: `隐藏${name}`, accelerator:'Command+ H', role:"hide" },{ label: `隐藏其它`, accelerator:'Command + Alt + H', role:"hideotheres" },{ label: `显示全部`, role:"unhide" },{ type:'separator' },{ label: '退出', accelerator:'Command + Q', click: () => { app.quit() } }] }) }
module.exports = template
const { app,BrowserWindow,Menu } = require('electron') const isDev = require('electron-is-dev')
const menuTemplate = require('./src/menuTemplate') const menu = Menu.buildFromTemplate(menuTemplate) Menu.setApplicationMenu(menu)
ipcRenderer.on('create-new-file',callback) return () => { ipcRenderer.removeListener('create-new-file',callback) }
})\
------------------ import { useEffect } from 'react'; const { ipcRenderer } = window.require('electron')
// const obj = { // 'create-file':()=>{} // }
const useIpcRenerer = (keyCallbackMap) => { useEffect(()=>{ Object.keys(keyCallbackMap).forEach( key => { ipcRenderer.on(key,keyCallbackMap[key]) }) return () => { Object.keys(keyCallbackMap).forEach( key => { ipcRenderer.removeListener(key,keyCallbackMap[key]) }) } }) }
export default useIpcRenerer
import useIpcRenerer from './hooks/useIpcRenderer';
useIpcRenerer({ 'create-new-file':createNewFile, 'import-file':importFiles, 'save-edit-file': saveCurrentFile })