-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
137 lines (113 loc) · 3.11 KB
/
index.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
const DEBUG = false
const path = require('path')
const { app, BrowserWindow, Tray, ipcMain, nativeImage } = require('electron')
// FIXME: Tray icon for windows and linux => http://electron.rocks/proper-tray-icon/
const ASSETS_DIR = path.join(__dirname, 'app', 'images')
const TRAY_ICON = nativeImage.createFromPath(path.join(ASSETS_DIR, 'tray.png'))
const TRAY_ICON_HIGHLIGHTED = nativeImage.createFromPath(path.join(ASSETS_DIR, 'tray-highlighted.png'))
TRAY_ICON.setTemplateImage(true)
TRAY_ICON_HIGHLIGHTED.setTemplateImage(true)
let tray
let window
const dock = app.dock
require('electron-reload')(__dirname, {
electron: require('electron-prebuilt')
})
// NOTE: Don't show the app in the doc
dock.hide()
// Application events
app.on('ready', () => {
tray = new Tray(TRAY_ICON)
tray.setPressedImage(TRAY_ICON_HIGHLIGHTED)
tray.setToolTip('Kodi Sync')
tray.on('click', function (event) {
console.log('Tray clicked')
toggleWindow()
})
createWindow()
})
// Quit the app when the window is closed
app.on('window-all-closed', () => {
app.quit()
})
// IPC events
ipcMain.on('show-window', () => {
showWindow()
})
ipcMain.on('quit', () => {
console.log('QUIT APP')
window.close()
})
// Creates window & specifies its values
const createWindow = () => {
if (DEBUG) {
window = new BrowserWindow({
width: 800,
height: 500,
show: true,
frame: true,
fullscreenable: true,
resizable: true,
transparent: false,
'node-integration': false
})
window.webContents.openDevTools()
} else {
window = new BrowserWindow({
width: 420,
height: 420,
show: true,
frame: false,
fullscreenable: false,
resizable: false,
transparent: false,
'node-integration': false
})
const position = getWindowPosition()
window.setPosition(position.x, position.y, false)
}
window.loadURL(`file://${__dirname}/app/index.html`)
// Hide the window when it loses focus
window.on('blur', () => {
if (!window.webContents.isDevToolsOpened()) {
window.hide()
}
})
window.on('show', () => {
console.log('window show')
tray.setImage(TRAY_ICON_HIGHLIGHTED)
// tray.setPressedImage(TRAY_ICON_HIGHLIGHTED)
tray.setHighlightMode('always')
})
window.on('hide', () => {
console.log('window hide')
tray.setImage(TRAY_ICON)
// tray.setPressedImage(TRAY_ICON)
tray.setHighlightMode('never')
})
}
const toggleWindow = () => {
if (window.isVisible()) {
hideWindow()
} else {
showWindow()
}
}
const showWindow = () => {
const position = getWindowPosition()
window.setPosition(position.x, position.y, false)
window.show()
window.focus()
}
const hideWindow = () => {
window.hide()
}
const getWindowPosition = () => {
const windowBounds = window.getBounds()
const trayBounds = tray.getBounds()
// Center window horizontally below the tray icon
const x = Math.round(trayBounds.x + (trayBounds.width / 2) - (windowBounds.width / 2))
// Position window 4 pixels vertically below the tray icon
const y = Math.round(trayBounds.y + trayBounds.height + 3)
return { x: x, y: y }
}