/
main.js
135 lines (110 loc) · 2.62 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
var isDevelopment = process.env.NODE_ENV === 'development';
var app = require('app'),
BrowserWindow = require('browser-window'),
Tray = require('tray'),
Menu = require('menu');
var connect;
if (isDevelopment) {
connect = require('electron-connect').client;
}
var express = require('express');
var webApp = express();
var port = 3838;
webApp.get('/', function (req, res) {
res.sendFile(__dirname + '/dist/index.html');
});
webApp.use('/css', express.static(__dirname + '/dist/css'));
webApp.use('/js', express.static(__dirname + '/dist/js'));
var server = webApp.listen(port, function () {
console.log('app listening at http://%s:%s', server.address().host, server.address().port);
});
if (isDevelopment) {
require('electron-debug')();
require('crash-reporter').start();
}
var mainWindow = null;
app.on('window-all-closed', function () {
app.quit();
});
function toggleWindow (win, bounds) {
if (win && win.isVisible()) {
hideWindow(win);
} else {
showWindow(win, bounds);
}
}
function showWindow (win, bounds) {
win.setPosition(bounds.x, bounds.y);
win.show();
}
function hideWindow (win) {
win.hide();
}
var appIcon = null;
app.on('ready', function () {
var iconPath = __dirname + '/icons/tray.png';
appIcon = new Tray(iconPath);
appIcon.window = new BrowserWindow({
width: 400,
height: 540,
show: false,
frame: false,
resizable: isDevelopment
});
appIcon.window.loadURL('http://127.0.0.1:'+ port);
if (isDevelopment) {
connect.create(appIcon.window);
appIcon.window.openDevTools();
}
appIcon.window
.on('closed', function () {
appIcon.window = null;
})
.on('blur', function () {
hideWindow(appIcon.window);
});
appIcon
.on('click', function (e, bounds) {
toggleWindow(appIcon.window, bounds);
});
var template = [
{
label: 'Application',
submenu: [
{
label: 'Quit',
accelerator: 'Command+Q',
click: function () {
app.quit();
}
}
]
},
{
label: 'Edit',
submenu: [
{
label: 'Copy',
accelerator: 'CmdOrCtrl+C',
selector: 'copy:'
},
{
label: 'Cut',
accelerator: 'CmdOrCtrl+X',
selector: 'cut:'
},
{
label: 'Paste',
accelerator: 'CmdOrCtrl+V',
selector: 'paste:'
},
{
label: 'Select All',
accelerator: 'CmdOrCtrl+A',
selector: 'selectAll:'
}
]
}
]
var menu = Menu.setApplicationMenu(Menu.buildFromTemplate(template))
});