-
Notifications
You must be signed in to change notification settings - Fork 3
/
wins.js
149 lines (130 loc) · 3.75 KB
/
wins.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
'use strict'
const path = require('path')
const { BrowserWindow, shell } = require('electron')
/* understand/
* hold all open windows here - useful to (a) have
* all windows in one place and (b) ensure they are
* not garbage collected (this used to be a problem
* in electron - not sure it is anymore but it doesn't
* hurt to keep these references)
*/
let wins = {}
/* way/
* create the main window and try not to let it be
* resized (we are not responsive yet). Also the
* main window has one link on the footer - to
* the external salesbox website so we don't open that
* link in the electron browser itself.
*/
function createMainWin() {
if(wins.main) return wins.main.focus()
wins.main = new BrowserWindow({
width: 1300,
height: 800,
webPreferences: {
preload: path.join(__dirname, "preload-main.js"),
nodeIntegration: false,
contextIsolation: true,
sandbox: true,
},
backgroundColor: "#0490f9",
})
wins.main.setResizable(false)
wins.main.setMaximizable(false)
wins.main.setFullScreenable(false)
wins.main.on("close", () => wins.main = null)
wins.main.webContents.on("will-navigate", (e, url) => {
if(url && url.indexOf("src=desktop-avatar") > 0) {
e.preventDefault()
shell.openExternal(url)
}
})
loadWin("main.html", wins.main)
}
/* understand/
* creates the settings window where users can set their
* settings
*/
function createSettingsWin() {
if(wins.settings) return wins.settings.focus()
wins.settings = new BrowserWindow({
width: 600,
height: 700,
resizable: false,
webPreferences: {
preload: path.join(__dirname, "preload-settings.js"),
nodeIntegration: false,
contextIsolation: true,
sandbox: true,
},
backgroundColor: "#0490f9",
})
wins.settings.on("close", () => wins.settings = null)
loadWin("settings.html", wins.settings)
}
/* understand/
* we need to be able to close the settings window from
* the render process (when the user clicks the 'submit'
* button) so we expose this function
*/
function closeSettings() {
if(wins.settings) wins.settings.close()
}
/* understand/
* in order to simplify login we also provide an option
* for the user to extract and save their login cookies
* which we can then use without needing their credentials
*/
function createCookieWin() {
if(wins.cookie) return wins.cookie.focus()
wins.cookie = new BrowserWindow({
width: 600,
height: 600,
resizable: false,
webPreferences: {
preload: path.join(__dirname, "preload-user-cookie.js"),
nodeIntegration: false,
contextIsolation: true,
sandbox: true,
},
backgroundColor: "#0490f9",
})
wins.cookie.on("close", () => wins.cookie = null)
wins.cookie.webContents.on("will-navigate", (e, url) => {
if(url && url.indexOf("src=desktop-avatar") > 0) {
e.preventDefault()
shell.openExternal(url)
}
})
loadWin("user-cookie.html", wins.cookie)
}
/* problem/
* In dev mode we want to use the parcel development server so we can
* have hot-reloading and all that good stuff but for testing/production
* we want to load the generated files directly.
*
* way/
* We expect the PARSEL_PORT environment variable to be set and use it to
* either connect to the parcel development server or to pick up the
* generated files
*/
function loadWin(name, win) {
if(process.env.PARCEL_PORT) {
win.loadURL(`http://localhost:${process.env.PARCEL_PORT}/${name}`)
} else {
win.loadFile(`pub/${name}`)
}
}
/* way/
* checks that there are no windows open
*/
function None() {
return BrowserWindow.getAllWindows().length == 0
}
module.exports = {
Main: createMainWin,
Settings: createSettingsWin,
UserCookie: createCookieWin,
closeSettings,
None,
}