Skip to content

Commit 9bc81da

Browse files
committed
Plugins/event handlers in each window
1 parent 2b3363f commit 9bc81da

File tree

1 file changed

+42
-35
lines changed

1 file changed

+42
-35
lines changed

index.js

+42-35
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,25 @@ function onClosed() {
4141
mainWindow = null;
4242
}
4343

44+
function loadPlugins(win) {
45+
injectCSS(win.webContents, path.join(__dirname, "youtube-music.css"));
46+
win.webContents.on("did-finish-load", () => {
47+
if (is.dev()) {
48+
console.log("did finish load");
49+
win.webContents.openDevTools();
50+
}
51+
});
52+
53+
getEnabledPlugins().forEach((plugin) => {
54+
console.log("Loaded plugin - " + plugin);
55+
const pluginPath = path.join(__dirname, "plugins", plugin, "back.js");
56+
fileExists(pluginPath, () => {
57+
const handle = require(pluginPath);
58+
handle(win);
59+
});
60+
});
61+
}
62+
4463
function createMainWindow() {
4564
const windowSize = store.get("window-size");
4665
const windowMaximized = store.get("window-maximized");
@@ -54,6 +73,7 @@ function createMainWindow() {
5473
webPreferences: {
5574
nodeIntegration: isTesting(), // Only necessary when testing with Spectron
5675
preload: path.join(__dirname, "preload.js"),
76+
nodeIntegrationInSubFrames: true,
5777
nativeWindowOpen: true, // window.open return Window object(like in regular browsers), not BrowserWindowProxy
5878
enableRemoteModule: true,
5979
affinity: "main-window", // main window, and addition windows should work in one process
@@ -68,23 +88,32 @@ function createMainWindow() {
6888
win.webContents.loadURL(store.get("url"));
6989
win.on("closed", onClosed);
7090

71-
injectCSS(win.webContents, path.join(__dirname, "youtube-music.css"));
72-
win.webContents.on("did-finish-load", () => {
73-
if (is.dev()) {
74-
console.log("did finish load");
75-
win.webContents.openDevTools();
91+
win.on("move", () => {
92+
let position = win.getPosition();
93+
store.set("window-position", { x: position[0], y: position[1] });
94+
});
95+
96+
win.on("resize", () => {
97+
const windowSize = win.getSize();
98+
99+
store.set("window-maximized", win.isMaximized());
100+
if (!win.isMaximized()) {
101+
store.set("window-size", { width: windowSize[0], height: windowSize[1] });
76102
}
77103
});
78104

79-
getEnabledPlugins().forEach((plugin) => {
80-
console.log("Loaded plugin - " + plugin);
81-
const pluginPath = path.join(__dirname, "plugins", plugin, "back.js");
82-
fileExists(pluginPath, () => {
83-
const handle = require(pluginPath);
84-
handle(win);
85-
});
105+
win.once("ready-to-show", () => {
106+
if (isAppVisible()) {
107+
win.show();
108+
}
86109
});
87110

111+
return win;
112+
}
113+
114+
app.on("browser-window-created", (event, win) => {
115+
loadPlugins(win);
116+
88117
win.webContents.on("did-fail-load", () => {
89118
if (is.dev()) {
90119
console.log("did fail load");
@@ -128,29 +157,7 @@ function createMainWindow() {
128157
options.webPreferences.affinity = "main-window";
129158
}
130159
);
131-
132-
win.on("move", () => {
133-
let position = win.getPosition();
134-
store.set("window-position", { x: position[0], y: position[1] });
135-
});
136-
137-
win.on("resize", () => {
138-
const windowSize = win.getSize();
139-
140-
store.set("window-maximized", win.isMaximized());
141-
if (!win.isMaximized()) {
142-
store.set("window-size", { width: windowSize[0], height: windowSize[1] });
143-
}
144-
});
145-
146-
win.once("ready-to-show", () => {
147-
if (isAppVisible()) {
148-
win.show();
149-
}
150-
});
151-
152-
return win;
153-
}
160+
});
154161

155162
app.on("window-all-closed", () => {
156163
if (process.platform !== "darwin") {

0 commit comments

Comments
 (0)