-
Notifications
You must be signed in to change notification settings - Fork 27
/
main.js
147 lines (130 loc) · 4.25 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
const isDev = require("electron-is-dev");
const path = require("path");
const url = require("url");
const checkForUpdatesAndNotify = require("./src/node/updates.js");
const interceptStreamProtocol = require("./src/node/protocol.js");
const {
app,
protocol,
screen,
ipcMain,
shell,
BrowserWindow,
} = require("electron");
if (isDev) {
require("electron-debug")({ devToolsMode: "detach" });
}
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;
function createWindow() {
protocol.interceptStreamProtocol(
"file",
interceptStreamProtocol(),
(error) => {
if (error) {
console.error("Failed to register protocol");
}
}
);
const { width, height } = screen.getPrimaryDisplay().workAreaSize;
// Create the browser window.
mainWindow = new BrowserWindow({
x: 0,
y: 0,
width: width,
height: height,
transparent: true,
frame: false,
hasShadow: false,
show: false,
resizable: false,
movable: false,
fullscreenable: false,
icon: path.join(__dirname, "res/icon.png"),
webPreferences: {
nodeIntegration: false,
preload: path.join(__dirname, "src/preload/index.js"),
},
});
ipcMain.on("minimize", () => mainWindow.minimize());
ipcMain.on("close", () => mainWindow.close());
ipcMain.on("setThumbnailClip", (_, clip) => mainWindow.setThumbnailClip(clip));
ipcMain.handle("getBounds", () => mainWindow.getBounds());
ipcMain.handle("getCursorScreenPoint", () => screen.getCursorScreenPoint());
ipcMain.on("ignoreMouseEvents", (_, ignore) => {
if (ignore) {
mainWindow.setIgnoreMouseEvents(true, { forward: true })
} else {
mainWindow.setIgnoreMouseEvents(false)
}
});
mainWindow.on('minimize', () => ipcMain.emit('minimized'));
mainWindow.on('restore', () => ipcMain.emit('restored'));
mainWindow.on('closed', () => ipcMain.emit('closed'));
// and load the index.html of the app.
mainWindow.loadURL(
url.format({
pathname: "./dist/index.html",
protocol: "file:",
slashes: true,
})
);
// and show window once it's ready (to prevent flashing)
mainWindow.once("ready-to-show", () => {
mainWindow.show();
checkForUpdatesAndNotify();
});
mainWindow.on("closed", function () {
// Dereference the window object
mainWindow = null;
});
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
// Linux has transparency disabled and window creation delayed
// due to issues with transparency of Chromium on Linux.
// See https://bugs.chromium.org/p/chromium/issues/detail?id=854601#c7
if (process.platform === "linux") {
app.disableHardwareAcceleration();
app.on("ready", () => setTimeout(createWindow, 100));
} else {
app.on("ready", createWindow);
}
app.on("web-contents-created", (event, contents) => {
// Prevent all navigation for security reasons
// See https://github.com/electron/electron/blob/master/docs/tutorial/security.md#13-disable-or-limit-navigation
contents.on("will-navigate", (event, navigationUrl) => {
event.preventDefault();
});
// Prevent new window creation for security reasons
// and open the URLs in the default browser instead
// See https://github.com/electron/electron/blob/master/docs/tutorial/security.md#14-disable-or-limit-creation-of-new-windows
contents.on("new-window", (event, navigationUrl) => {
const parsedUrl = url.parse(navigationUrl);
if (
parsedUrl.protocol === "file:" ||
parsedUrl.protocol === "chrome-devtools:"
) {
return;
}
event.preventDefault();
shell.openExternal(navigationUrl);
});
});
// Quit when all windows are closed.
app.on("window-all-closed", function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== "darwin") {
app.quit();
}
});
app.on("activate", function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow();
}
});