-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
121 lines (110 loc) · 3.19 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
* Created by ChenChao on 2016/6/28.
*/
"use strict";
var electron = require('electron');
var globalShortcut = electron.globalShortcut;
var fs = require('fs');
var app = electron.app;
var ipcMain = electron.ipcMain;
var BrowserWindow = electron.BrowserWindow;
var Menu = electron.Menu;
var Tray = electron.Tray;
var mainWindow;
var appIcon = null;
var config = require('./config');
var appConf = config.appInfo;
appInit();
function createWindow(){
mainWindow = new BrowserWindow({
width: appConf.winWidth,
height: appConf.winHeight,
title: appConf.title,
icon: config.logo,
resizable: false,
show: false,
frame: false
});
mainWindow.loadURL(config.enterUrl);
mainWindow.on('closed', function (){
mainWindow = null;
});
createMenu();
createDataDir();
config.mainDevTool && mainWindow.webContents.openDevTools();
config.refresh && globalShortcut.register('ctrl+r', function() {
mainWindow.reload();
});
}
function createMenu(){
appIcon = new Tray(config.logo);
var contextMenu = Menu.buildFromTemplate([
{
label: '显示主窗口',
click: function () {
mainWindow.show();
}
},
{
type: 'separator'
},
{
label: '退出',
click: function(){
mainWindow.destroy();
}
}
]);
appIcon.setToolTip(`${appConf.title}\n当前版本: ${appConf.version}\n已是最新版本`);
appIcon.setContextMenu(contextMenu);
appIcon.on('click', function(){
var isVisible = mainWindow.isVisible();
var isMinimized = mainWindow.isMinimized();
mainWindow[isVisible ? isMinimized ? 'show' : 'hide' : 'show']();
});
}
function createDataDir(){
fs.exists(config.dataDir, function (exists) {
if(!exists){
fs.mkdir(config.dataDir, function(err){});
}
});
}
function appInit(){
app.on('ready', createWindow);
app.on('before-quit', function(event){
mainWindow.webContents.send('app quit', 'App quit at ' + new Date());
appIcon && appIcon.destroy();
});
app.on('window-all-closed', function (){
if(process.platform !== 'darwin'){
app.quit()
}
});
app.on('activate', function (){
if(mainWindow === null){
createWindow()
}
});
ipcMain.on('app quit', function(event, quitMsg) {
app.quit();
});
ipcMain.on('console open', function (event, message) {
event.sender.send('console open', message);
});
ipcMain.on('console close', function (event, message) {
event.sender.send('console close', message);
});
ipcMain.on('logger open', function (event, message) {
event.sender.send('logger open', message);
});
ipcMain.on('logger close', function (event, message) {
event.sender.send('logger close', message);
});
ipcMain.on('setting open', function (event, message) {
event.sender.send('setting open', message);
});
ipcMain.on('setting close', function (event, message) {
event.sender.send('setting close', message);
});
}