|
| 1 | +"use strict"; |
| 2 | +const path = require("path"); |
| 3 | + |
| 4 | +const electron = require("electron"); |
| 5 | +const isDev = require("electron-is-dev"); |
| 6 | + |
| 7 | +const { setApplicationMenu } = require("./menu"); |
| 8 | +const { getEnabledPlugins, store } = require("./store"); |
| 9 | +const { fileExists, injectCSS } = require("./plugins/utils"); |
| 10 | + |
| 11 | +const app = electron.app; |
| 12 | + |
| 13 | +// Adds debug features like hotkeys for triggering dev tools and reload |
| 14 | +require("electron-debug")(); |
| 15 | + |
| 16 | +// Prevent window being garbage collected |
| 17 | +let mainWindow; |
| 18 | + |
| 19 | +let icon = "assets/youtube-music.png"; |
| 20 | +if (process.platform == "win32") { |
| 21 | + icon = "assets/generated/icon.ico"; |
| 22 | +} else if (process.platform == "darwin") { |
| 23 | + icon = "assets/generated/icon.icns"; |
| 24 | +} |
| 25 | + |
| 26 | +function onClosed() { |
| 27 | + // Dereference the window |
| 28 | + // For multiple windows store them in an array |
| 29 | + mainWindow = null; |
| 30 | +} |
| 31 | + |
| 32 | +function createMainWindow() { |
| 33 | + const windowSize = store.get("window-size"); |
| 34 | + const windowMaximized = store.get("window-maximized"); |
| 35 | + |
| 36 | + const win = new electron.BrowserWindow({ |
| 37 | + icon : icon, |
| 38 | + width : windowSize.width, |
| 39 | + height : windowSize.height, |
| 40 | + backgroundColor: "#000", |
| 41 | + show : false, |
| 42 | + webPreferences : { |
| 43 | + nodeIntegration: false, |
| 44 | + preload : path.join(__dirname, "preload.js") |
| 45 | + }, |
| 46 | + frame : false, |
| 47 | + titleBarStyle: "hiddenInset" |
| 48 | + }); |
| 49 | + if (windowMaximized) { |
| 50 | + win.maximize(); |
| 51 | + } |
| 52 | + |
| 53 | + win.webContents.loadURL(store.get("url")); |
| 54 | + win.on("closed", onClosed); |
| 55 | + |
| 56 | + injectCSS(win.webContents, path.join(__dirname, "youtube-music.css")); |
| 57 | + win.webContents.on("did-finish-load", () => { |
| 58 | + if (isDev) { |
| 59 | + console.log("did finish load"); |
| 60 | + win.webContents.openDevTools(); |
| 61 | + } |
| 62 | + }); |
| 63 | + |
| 64 | + getEnabledPlugins().forEach(plugin => { |
| 65 | + console.log("Loaded plugin - " + plugin); |
| 66 | + const pluginPath = path.join(__dirname, "plugins", plugin, "back.js"); |
| 67 | + fileExists(pluginPath, () => { |
| 68 | + const handle = require(pluginPath); |
| 69 | + handle(win); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + win.webContents.on("did-navigate-in-page", () => { |
| 74 | + const url = win.webContents.getURL(); |
| 75 | + if (url.startsWith("https://music.youtube.com")) { |
| 76 | + store.set("url", url); |
| 77 | + } |
| 78 | + }); |
| 79 | + |
| 80 | + win.on("move", () => { |
| 81 | + let position = win.getPosition(); |
| 82 | + store.set("window-position", { x: position[0], y: position[1] }); |
| 83 | + }); |
| 84 | + |
| 85 | + win.on("resize", () => { |
| 86 | + const windowSize = win.getSize(); |
| 87 | + |
| 88 | + store.set("window-maximized", win.isMaximized()); |
| 89 | + if (!win.isMaximized()) { |
| 90 | + store.set("window-size", { width: windowSize[0], height: windowSize[1] }); |
| 91 | + } |
| 92 | + }); |
| 93 | + |
| 94 | + win.once("ready-to-show", () => { |
| 95 | + win.show(); |
| 96 | + }); |
| 97 | + |
| 98 | + return win; |
| 99 | +} |
| 100 | + |
| 101 | +app.on("window-all-closed", () => { |
| 102 | + if (process.platform !== "darwin") { |
| 103 | + app.quit(); |
| 104 | + } |
| 105 | + |
| 106 | + // Unregister all shortcuts. |
| 107 | + electron.globalShortcut.unregisterAll(); |
| 108 | +}); |
| 109 | + |
| 110 | +app.on("activate", () => { |
| 111 | + // On OS X it's common to re-create a window in the app when the |
| 112 | + // dock icon is clicked and there are no other windows open. |
| 113 | + if (mainWindow === null) { |
| 114 | + mainWindow = createMainWindow(); |
| 115 | + } else if (!mainWindow.isVisible()) { |
| 116 | + mainWindow.show(); |
| 117 | + } |
| 118 | +}); |
| 119 | + |
| 120 | +app.on("ready", () => { |
| 121 | + setApplicationMenu(); |
| 122 | + mainWindow = createMainWindow(); |
| 123 | + |
| 124 | + // Optimized for Mac OS X |
| 125 | + if (process.platform === "darwin") { |
| 126 | + var forceQuit = false; |
| 127 | + app.on("before-quit", () => { |
| 128 | + forceQuit = true; |
| 129 | + }); |
| 130 | + mainWindow.on("close", event => { |
| 131 | + if (!forceQuit) { |
| 132 | + event.preventDefault(); |
| 133 | + mainWindow.hide(); |
| 134 | + } |
| 135 | + }); |
| 136 | + } |
| 137 | +}); |
0 commit comments