-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
87 lines (77 loc) · 2.65 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const { app, BrowserWindow, Menu } = require('electron');
const path = require('path');
const { dialog } = require('electron');
// const server = require("./app");
const { autoUpdater } = require("electron-updater");
function checkUpdate() {
if (process.platform == 'darwin') {
//我们使用koa-static将静态目录设置成了static文件夹,
//所以访问http://127.0.0.1:9005/darwin,就相当于访问了static/darwin文件夹,win32同理
autoUpdater.setFeedURL('http://127.0.0.1:9005/darwin') //设置要检测更新的路径
} else {
autoUpdater.setFeedURL('http://127.0.0.1:9005/win32')
}
//检测更新
autoUpdater.checkForUpdates()
//监听'error'事件
autoUpdater.on('error', (err) => {
console.log(err)
})
//监听'update-available'事件,发现有新版本时触发
autoUpdater.on('update-available', () => {
console.log('found new version')
})
//默认会自动下载新版本,如果不想自动下载,设置autoUpdater.autoDownload = false
//监听'update-downloaded'事件,新版本下载完成时触发
autoUpdater.on('update-downloaded', () => {
dialog.showMessageBox({
type: 'info',
title: '应用更新',
message: '发现新版本,是否更新?',
buttons: ['是', '否']
}).then((buttonIndex) => {
if (buttonIndex.response == 0) { //选择是,则退出程序,安装新版本
autoUpdater.quitAndInstall()
app.quit()
}
})
})
};
function createWindow() {
let win = new BrowserWindow({
width: 310,
height: 360,
resizable: false,
autoHideMenuBar: true, // 隐藏窗体顶部菜单,win、centos有效,mac无效
webPreferences: {
devTools: false,
nodeIntegration: true,
preload: path.join(__dirname, 'app.js')
},
});
win.loadFile('index.html');
win.webContents.openDevTools();
win.on('show', () => {
console.log('窗口已显示');
});
win.on('ready-to-show', () => {
console.log('窗口已渲染');
});
win.on('closed', () => {
console.log('窗口关闭');
win = null;
});
};
app.whenReady().then(() => {
createWindow();
checkUpdate();
// Menu.setApplicationMenu(Menu.buildFromTemplate([]));// 隐藏窗体顶部菜单,仅mac有效
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
});
});
app.on('window-all-closed', () => {
if (process.patform !== 'darwin') {
app.quit();
}
});