-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
118 lines (98 loc) · 2.68 KB
/
app.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
const app = module.exports = {
window: null
}
const electron = require('electron')
const path = require('path')
const url = require('url')
const Tray = electron.Tray
const Menu = electron.Menu
let trayIcon;
// let window = null
// Wait until the app is ready
electron.app.once('ready', () => {
// Create a new window
app.window = new electron.BrowserWindow({
width: 320,
height: 480,
titleBarStyle: 'hidden',
resizable: false,
maximizable: false,
// Set the default background color of the window to match the CSS
// background color of the page, this prevents any white flickering
backgroundColor: "#111",
// Don't show the window until it ready, this prevents any white flickering
show: false,
icon: path.join(__dirname, '.favicon.ico')
})
// Load a URL in the window to the local index.html path
app.window.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}))
// Show window when page is ready
app.window.once('ready-to-show', () => {
showApp()
app.window.focus()
})
// Handle close event
app.window.on('close', (e) => {
e.preventDefault();
hideApp()
})
// Handle maximize event
app.window.on('maximize', (e) => {
e.preventDefault();
app.window.setResizable(false);
console.log('maximize');
})
// Handle maximize event
app.window.on('resize', (e) => {
e.preventDefault();
app.window.setResizable(false);
console.log('Resize');
})
// initialise tray
initTray()
})
/**
* Initialise tray creation
*/
function initTray() {
// OS X has no tray icon
if (process.platform === 'darwin') return
createTrayIcon()
}
/**
* Function to create tray icon for app
*/
function createTrayIcon() {
trayIcon = new Tray(path.join(__dirname, 'icons', '32x32.png'))
// On Windows, left click to open the app, right click for context menu
// On Linux, any click (right or left) opens the context menu
trayIcon.on('click', showApp)
updateTrayMenu()
}
function showApp() {
app.window.show();
}
function hideApp() {
app.window.minimize();
}
/**
* Shows options on tray icon
*/
function updateTrayMenu() {
const contextMenu = Menu.buildFromTemplate([
{ label: 'Hide', click: hideApp },
{ label: 'Show', click: showApp },
{
label: 'Quit', click: () => {
app.window.destroy();
trayIcon.destroy()
app.window = null;
}
}
])
trayIcon.setContextMenu(contextMenu)
}