190 lines
4.6 KiB
JavaScript
190 lines
4.6 KiB
JavaScript
// main.js
|
|
|
|
// 控制应用生命周期和创建原生浏览器窗口的模组
|
|
const {app, BrowserWindow, Menu, ipcMain} = require('electron')
|
|
const path = require('path')
|
|
|
|
const isMac = process.platform === 'darwin'
|
|
|
|
var WindowConfig = {
|
|
login: {
|
|
width: 295,
|
|
height: 511,
|
|
minWidth: 226,
|
|
minHeight: 411,
|
|
},
|
|
index: {
|
|
width: 800,
|
|
height: 600,
|
|
minWidth: 600,
|
|
minHeight: 400,
|
|
}
|
|
}
|
|
|
|
var mainWindow = null
|
|
|
|
var current = "login"
|
|
|
|
function createWindow() {
|
|
if (current != "login") {
|
|
try {
|
|
mainWindow.close();
|
|
} catch (e) {
|
|
}
|
|
}
|
|
|
|
// 创建浏览器窗口
|
|
mainWindow = new BrowserWindow({
|
|
...WindowConfig[current],
|
|
webPreferences: {
|
|
nodeIntegrationInWorker: true,
|
|
nodeIntegration: true,
|
|
contextIsolation: false
|
|
}
|
|
})
|
|
|
|
// 加载 index.html
|
|
mainWindow.loadFile(`./page/${current}.html`)
|
|
|
|
// 打开开发工具
|
|
mainWindow.webContents.openDevTools()
|
|
}
|
|
|
|
ipcMain.on('open', (event, arg) => {
|
|
current = arg;
|
|
createWindow()
|
|
})
|
|
|
|
ipcMain.on('shake', (event, arg) => {
|
|
var Position = mainWindow.getPosition();
|
|
console.log("Position == ", Position);
|
|
// var newPosition = [
|
|
// Position[0] + 60, Position[0] - 120, Position[0],
|
|
// Position[0] + 60, Position[0] - 120, Position[0],
|
|
// Position[0] + 60, Position[0] - 120, Position[0]
|
|
// ];
|
|
|
|
for (let i = 0; i < Position.length; i++) {
|
|
setTimeout(function () {
|
|
mainWindow.setPosition(
|
|
newPosition[i],
|
|
Position[1],
|
|
true
|
|
)
|
|
}, 10 * i)
|
|
}
|
|
})
|
|
|
|
app.whenReady().then(() => {
|
|
createWindow()
|
|
|
|
app.on('activate', function () {
|
|
// 通常在 macOS 上,当点击 dock 中的应用程序图标时,如果没有其他
|
|
// 打开的窗口,那么程序会重新创建一个窗口。
|
|
if (BrowserWindow.getAllWindows().length === 0) createWindow()
|
|
})
|
|
})
|
|
|
|
app.on('window-all-closed', function () {
|
|
if (process.platform !== 'darwin') app.quit()
|
|
})
|
|
|
|
const template = [
|
|
// { role: 'appMenu' }
|
|
...(isMac ? [{
|
|
label: app.name,
|
|
submenu: [
|
|
{role: 'about'},
|
|
{type: 'separator'},
|
|
{role: 'services'},
|
|
{type: 'separator'},
|
|
{role: 'hide'},
|
|
{role: 'hideOthers'},
|
|
{role: 'unhide'},
|
|
{type: 'separator'},
|
|
{role: 'quit'}
|
|
]
|
|
}] : []),
|
|
// { role: 'fileMenu' }
|
|
{
|
|
label: '文件',
|
|
submenu: [
|
|
isMac ? {role: 'close'} : {role: 'quit'}
|
|
]
|
|
},
|
|
// { role: 'editMenu' }
|
|
{
|
|
label: '编辑',
|
|
submenu: [
|
|
{role: 'undo'},
|
|
{role: 'redo'},
|
|
{type: 'separator'},
|
|
{role: 'cut'},
|
|
{role: 'copy'},
|
|
{role: 'paste'},
|
|
...(isMac ? [
|
|
{role: 'pasteAndMatchStyle'},
|
|
{role: 'delete'},
|
|
{role: 'selectAll'},
|
|
{type: 'separator'},
|
|
{
|
|
label: 'Speech',
|
|
submenu: [
|
|
{role: 'startSpeaking'},
|
|
{role: 'stopSpeaking'}
|
|
]
|
|
}
|
|
] : [
|
|
{role: 'delete'},
|
|
{type: 'separator'},
|
|
{role: 'selectAll'}
|
|
])
|
|
]
|
|
},
|
|
// { role: 'viewMenu' }
|
|
{
|
|
label: '视图',
|
|
submenu: [
|
|
{role: 'reload'},
|
|
{role: 'forceReload'},
|
|
{role: 'toggleDevTools'},
|
|
{type: 'separator'},
|
|
{role: 'resetZoom'},
|
|
{role: 'zoomIn'},
|
|
{role: 'zoomOut'},
|
|
{type: 'separator'},
|
|
{role: 'togglefullscreen'}
|
|
]
|
|
},
|
|
// { role: 'windowMenu' }
|
|
{
|
|
label: '窗口',
|
|
submenu: [
|
|
{role: 'minimize'},
|
|
{role: 'zoom'},
|
|
...(isMac ? [
|
|
{type: 'separator'},
|
|
{role: 'front'},
|
|
{type: 'separator'},
|
|
{role: 'window'}
|
|
] : [
|
|
{role: 'close'}
|
|
])
|
|
]
|
|
},
|
|
{
|
|
role: '帮助',
|
|
submenu: [
|
|
{
|
|
label: 'Learn More',
|
|
click: async () => {
|
|
const {shell} = require('electron')
|
|
await shell.openExternal('https://electronjs.org')
|
|
}
|
|
}
|
|
]
|
|
}
|
|
]
|
|
|
|
const menu = Menu.buildFromTemplate(template)
|
|
Menu.setApplicationMenu(menu) |