-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
229 lines (190 loc) · 6.8 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Copyright (c) The LHTML team
// See LICENSE for details.
const {app, BrowserWindow,Tray, Menu, protocol, ipcMain} = require('electron');
const log = require('electron-log');
const {autoUpdater} = require("electron-updater");
var path=require('path');
//-------------------------------------------------------------------
// Logging
//
// THIS SECTION IS NOT REQUIRED
//
// This logging setup is not required for auto-updates to work,
// but it sure makes debugging easier :)
//-------------------------------------------------------------------
autoUpdater.logger = log;
autoUpdater.logger.transports.file.level = 'info';
log.info('App starting...');
//-------------------------------------------------------------------
// Define the menu
//
// THIS SECTION IS NOT REQUIRED
//-------------------------------------------------------------------
let template = []
const appId = 'electron-windows-notifications'
const {ToastNotification} = require('electron-windows-notifications')
let imagePath = path.join(__dirname, 'build/tray_icons.png');
//-------------------------------------------------------------------
// Open a window that displays the version
//
// THIS SECTION IS NOT REQUIRED
//
// This isn't required for auto-updates to work, but it's easier
// for the app to show a window than to have to click "About" to see
// that updates are working.
//-------------------------------------------------------------------
let win;
ipcMain.on('asdty',(event, arg) => {
var notification = new ToastNotification({
appId: appId,
template: `<toast><visual><binding template="ToastImageAndText02"><image id="1" src="%s"/><text id="1">%s</text><text id="2">%s</text></binding></visual></toast>`,
strings: [imagePath,'Electron-Demo',arg]
})
notification.on('activated', () => console.log('Activated!'))
notification.show()
})
function sendStatusToWindow(text) {
log.info(text);
win.webContents.send('message', text);
}
function createDefaultWindow() {
win = new BrowserWindow({icon: __dirname + '/icon.ico'});
win.on('closed', () => {
win = null;
});
win.loadURL(`file://${__dirname}/version.html#v${app.getVersion()}`);
return win;
}
autoUpdater.on('checking-for-update', () => {
sendStatusToWindow('Checking for update...');
})
autoUpdater.on('update-available', (ev, info) => {
var notification = new ToastNotification({
appId: appId,
template: `<toast><visual><binding template="ToastImageAndText02"><image id="1" src="%s"/><text id="1">%s</text><text id="2">%s</text></binding></visual></toast>`,
strings: [imagePath,'Electron-Demo','Update available.']
})
notification.on('activated', () => console.log('Activated!'))
notification.show()
sendStatusToWindow('Update available.');
})
autoUpdater.on('update-not-available', (ev, info) => {
var notification = new ToastNotification({
appId: appId,
template: `<toast><visual><binding template="ToastImageAndText02"><image id="1" src="%s"/><text id="1">%s</text><text id="2">%s</text></binding></visual></toast>`,
strings: [imagePath,'Electron-Demo','Update not available.']
})
notification.on('activated', () => console.log('Activated!'))
notification.show()
sendStatusToWindow('Update not available.');
})
autoUpdater.on('error', (ev, err) => {
if(ev){
sendStatusToWindow('edo ev anta.',ev);
}
sendStatusToWindow('Error in auto-updater.',err);
})
autoUpdater.on('download-progress', (progressObj) => {
let log_message = "Download speed: " + progressObj.bytesPerSecond;
log_message = log_message + ' - Downloaded ' + progressObj.percent + '%';
log_message = log_message + ' (' + progressObj.transferred + "/" + progressObj.total + ')';
win.setProgressBar(progressObj.bytesPerSecond);
})
autoUpdater.on('update-downloaded', (ev, info) => {
var notification = new ToastNotification({
appId: appId,
template: `<toast><visual><binding template="ToastImageAndText02"><image id="1" src="%s"/><text id="1">%s</text><text id="2">%s</text></binding></visual></toast>`,
strings: [imagePath,'Electron-Demo','Update downloaded; will install in 5 seconds']
})
notification.on('activated', () => console.log('Activated!'))
notification.show()
sendStatusToWindow('Update downloaded; will install in 5 seconds');
});
app.on('ready', function() {
console.log('app starts');
const isDev = require('electron-is-dev');
if (isDev) {
console.log('Running in development');
const menu = Menu.buildFromTemplate([
{label: 'Check for Updates', click: function() {
console.log('Running in production');
autoUpdater.checkForUpdates();
}},
{label: 'Help', click: function() {
let child = new BrowserWindow()
child.loadURL('http://www.miraclesoft.com/')
child.once('ready-to-show', () => {
child.show()
})
}},
{label:'Quit', role:'quit', click:function(){
app.quit();
}}
])
path=__dirname+'/tray_icons.png';
appIcon = new Tray(path)
appIcon.setToolTip('Electron Demo in the tray.');
appIcon.setContextMenu(menu)
createDefaultWindow();
sendStatusToWindow(imagePath);
} else {
console.log('Running in production');
// Create the Menu
const menu = Menu.buildFromTemplate([
{label: 'Check for Updates', click: function() {
console.log('Running in production');
autoUpdater.checkForUpdates();
}},
{label: 'Help', click: function() {
let child = new BrowserWindow()
child.loadURL('http://www.miraclesoft.com/')
child.once('ready-to-show', () => {
child.show()
})
}},
{label:'Quit', role:'quit', click:function(){
app.quit();
}}
])
path=__dirname+'/tray_icons.png';
appIcon = new Tray(path)
appIcon.setToolTip('Electron Demo in the tray.');
appIcon.setContextMenu(menu)
createDefaultWindow();
sendStatusToWindow(imagePath);
}
});
app.on('window-all-closed', () => {
app.quit();
});
//-------------------------------------------------------------------
// Auto updates
//
// For details about these events, see the Wiki:
// https://github.com/electron-userland/electron-builder/wiki/Auto-Update#events
//
// The app doesn't need to listen to any events except `update-downloaded`
//
// Uncomment any of the below events to listen for them. Also,
// look in the previous section to see them being used.
//-------------------------------------------------------------------
// autoUpdater.on('checking-for-update', () => {
// })
// autoUpdater.on('update-available', (ev, info) => {
// })
// autoUpdater.on('update-not-available', (ev, info) => {
// })
// autoUpdater.on('error', (ev, err) => {
// })
// autoUpdater.on('download-progress', (ev, progressObj) => {
// })
autoUpdater.on('update-downloaded', (ev, info) => {
// Wait 5 seconds, then quit and install
// In your application, you don't need to wait 5 seconds.
// You could call autoUpdater.quitAndInstall(); immediately
setTimeout(function() {
autoUpdater.quitAndInstall();
}, 5000)
})
app.on('ready', function() {
});