-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathmain.js
113 lines (97 loc) · 2.48 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
const { app, BrowserWindow, Menu } = require('electron')
const electronDebug = require('electron-debug')
const path = require('path')
const fs = require('fs')
const config = require('./config')
const tray = require('./tray')
const menu = require('./menu')
// Enable easy debugging
electronDebug()
let quitApplication = false
let mainWindow = null
const { platform } = process
app.requestSingleInstanceLock()
if (mainWindow) {
if (mainWindow.isMinimized()) {
mainWindow.restore()
}
mainWindow.show()
}
/**
* Toggles window visibility.
* @return {undefined}
*/
function toggleWindow() {
if (mainWindow.isVisible()) mainWindow.hide()
else mainWindow.show()
}
/**
* Creates a main `BrowserWindow` instance
* @return {BrowserWindow}
*/
function createMainWindow() {
const windowConfig = config.get('window')
const { paperURL } = config.store
const window = new BrowserWindow({
title: app.getName(),
width: windowConfig.width,
height: windowConfig.height,
minWidth: 875,
minHeight: 400,
center: true,
titleBarStyle: 'hidden-inset',
autoHideMenuBar: true,
backgroundColor: '#ffffff',
icon: platform === 'linux' && path.join(__dirname, config.get('icons.osx')),
show: false,
webPreferences: {
javascript: true,
nodeIntegration: false,
webSecurity: false,
allowDisplayingInsecureContent: true,
allowRunningInsecureContent: true,
preload: path.join(__dirname, 'browser.js'),
},
})
window.loadURL(paperURL)
window.on('close', (event) => {
if (!quitApplication) {
event.preventDefault()
if (platform === 'darwin') app.hide()
else window.hide()
}
})
return window
}
// Create instance once Electron is ready
app.on('ready', () => {
mainWindow = createMainWindow()
tray.create({ onToggle: toggleWindow, onClick: toggleWindow })
Menu.setApplicationMenu(menu)
const pageWindow = mainWindow.webContents
pageWindow.on('dom-ready', () => {
mainWindow.show()
pageWindow.insertCSS(fs.readFileSync(path.join(__dirname, 'browser.css'), 'utf8'))
})
pageWindow.on('new-window', (event, url) => {
event.preventDefault()
if (url.split('/').includes('present')) {
pageWindow.loadURL(url)
}
})
})
// Show browser instance
app.on('activate', () => {
mainWindow.show()
})
// Store user settings before application has been quit
app.on('before-quit', () => {
quitApplication = true
if (!mainWindow.isFullScreen()) {
config.set('window', mainWindow.getBounds())
}
})
// Ensure only one instance is running
app.on('second-instance', () => {
app.quit()
})