Skip to content

Commit

Permalink
Plugins/event handlers in each window
Browse files Browse the repository at this point in the history
  • Loading branch information
th-ch committed Nov 11, 2020
1 parent 2b3363f commit 9bc81da
Showing 1 changed file with 42 additions and 35 deletions.
77 changes: 42 additions & 35 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,25 @@ function onClosed() {
mainWindow = null;
}

function loadPlugins(win) {
injectCSS(win.webContents, path.join(__dirname, "youtube-music.css"));
win.webContents.on("did-finish-load", () => {
if (is.dev()) {
console.log("did finish load");
win.webContents.openDevTools();
}
});

getEnabledPlugins().forEach((plugin) => {
console.log("Loaded plugin - " + plugin);
const pluginPath = path.join(__dirname, "plugins", plugin, "back.js");
fileExists(pluginPath, () => {
const handle = require(pluginPath);
handle(win);
});
});
}

function createMainWindow() {
const windowSize = store.get("window-size");
const windowMaximized = store.get("window-maximized");
Expand All @@ -54,6 +73,7 @@ function createMainWindow() {
webPreferences: {
nodeIntegration: isTesting(), // Only necessary when testing with Spectron
preload: path.join(__dirname, "preload.js"),
nodeIntegrationInSubFrames: true,
nativeWindowOpen: true, // window.open return Window object(like in regular browsers), not BrowserWindowProxy
enableRemoteModule: true,
affinity: "main-window", // main window, and addition windows should work in one process
Expand All @@ -68,23 +88,32 @@ function createMainWindow() {
win.webContents.loadURL(store.get("url"));
win.on("closed", onClosed);

injectCSS(win.webContents, path.join(__dirname, "youtube-music.css"));
win.webContents.on("did-finish-load", () => {
if (is.dev()) {
console.log("did finish load");
win.webContents.openDevTools();
win.on("move", () => {
let position = win.getPosition();
store.set("window-position", { x: position[0], y: position[1] });
});

win.on("resize", () => {
const windowSize = win.getSize();

store.set("window-maximized", win.isMaximized());
if (!win.isMaximized()) {
store.set("window-size", { width: windowSize[0], height: windowSize[1] });
}
});

getEnabledPlugins().forEach((plugin) => {
console.log("Loaded plugin - " + plugin);
const pluginPath = path.join(__dirname, "plugins", plugin, "back.js");
fileExists(pluginPath, () => {
const handle = require(pluginPath);
handle(win);
});
win.once("ready-to-show", () => {
if (isAppVisible()) {
win.show();
}
});

return win;
}

app.on("browser-window-created", (event, win) => {
loadPlugins(win);

win.webContents.on("did-fail-load", () => {
if (is.dev()) {
console.log("did fail load");
Expand Down Expand Up @@ -128,29 +157,7 @@ function createMainWindow() {
options.webPreferences.affinity = "main-window";
}
);

win.on("move", () => {
let position = win.getPosition();
store.set("window-position", { x: position[0], y: position[1] });
});

win.on("resize", () => {
const windowSize = win.getSize();

store.set("window-maximized", win.isMaximized());
if (!win.isMaximized()) {
store.set("window-size", { width: windowSize[0], height: windowSize[1] });
}
});

win.once("ready-to-show", () => {
if (isAppVisible()) {
win.show();
}
});

return win;
}
});

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

0 comments on commit 9bc81da

Please sign in to comment.