Skip to content

Commit eb10bf2

Browse files
authored
Merge pull request #21 from th-ch/add-options-and-tray
Add options and tray
2 parents 7098cd7 + e051c4a commit eb10bf2

File tree

6 files changed

+207
-58
lines changed

6 files changed

+207
-58
lines changed

assets/youtube-music-tray.png

3.29 KB
Loading

index.js

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
"use strict";
22
const path = require("path");
33

4-
const electron = require("electron");
5-
const is = require("electron-is");
4+
const electron = require("electron");
5+
const is = require("electron-is");
66
const { autoUpdater } = require("electron-updater");
77

8-
const { setApplicationMenu } = require("./menu");
9-
const { getEnabledPlugins, store } = require("./store");
10-
const { fileExists, injectCSS } = require("./plugins/utils");
8+
const { setApplicationMenu } = require("./menu");
9+
const {
10+
autoUpdate,
11+
getEnabledPlugins,
12+
isAppVisible,
13+
store,
14+
} = require("./store");
15+
const { fileExists, injectCSS } = require("./plugins/utils");
16+
const { setUpTray } = require("./tray");
1117

1218
const app = electron.app;
1319

@@ -32,23 +38,23 @@ function onClosed() {
3238
}
3339

3440
function createMainWindow() {
35-
const windowSize = store.get("window-size");
41+
const windowSize = store.get("window-size");
3642
const windowMaximized = store.get("window-maximized");
3743

3844
const win = new electron.BrowserWindow({
39-
icon : icon,
40-
width : windowSize.width,
41-
height : windowSize.height,
45+
icon: icon,
46+
width: windowSize.width,
47+
height: windowSize.height,
4248
backgroundColor: "#000",
43-
show : false,
44-
webPreferences : {
45-
nodeIntegration : false,
46-
preload : path.join(__dirname, "preload.js"),
47-
nativeWindowOpen: true, // window.open return Window object(like in regular browsers), not BrowserWindowProxy
48-
affinity : "main-window" // main window, and addition windows should work in one process
49+
show: false,
50+
webPreferences: {
51+
nodeIntegration: false,
52+
preload: path.join(__dirname, "preload.js"),
53+
nativeWindowOpen: true, // window.open return Window object(like in regular browsers), not BrowserWindowProxy
54+
affinity: "main-window", // main window, and addition windows should work in one process
4955
},
50-
frame : !is.macOS(),
51-
titleBarStyle: is.macOS() ? "hiddenInset": "default"
56+
frame: !is.macOS(),
57+
titleBarStyle: is.macOS() ? "hiddenInset" : "default",
5258
});
5359
if (windowMaximized) {
5460
win.maximize();
@@ -65,7 +71,7 @@ function createMainWindow() {
6571
}
6672
});
6773

68-
getEnabledPlugins().forEach(plugin => {
74+
getEnabledPlugins().forEach((plugin) => {
6975
console.log("Loaded plugin - " + plugin);
7076
const pluginPath = path.join(__dirname, "plugins", plugin, "back.js");
7177
fileExists(pluginPath, () => {
@@ -114,7 +120,9 @@ function createMainWindow() {
114120
});
115121

116122
win.once("ready-to-show", () => {
117-
win.show();
123+
if (isAppVisible()) {
124+
win.show();
125+
}
118126
});
119127

120128
return win;
@@ -142,28 +150,34 @@ app.on("activate", () => {
142150
app.on("ready", () => {
143151
setApplicationMenu();
144152
mainWindow = createMainWindow();
145-
if (!is.dev()) {
153+
setUpTray(app, mainWindow);
154+
155+
if (!is.dev() && autoUpdate()) {
146156
autoUpdater.checkForUpdatesAndNotify();
147157
autoUpdater.on("update-available", () => {
148158
const dialogOpts = {
149-
type : "info",
159+
type: "info",
150160
buttons: ["OK"],
151-
title : "Application Update",
161+
title: "Application Update",
152162
message: "A new version is available",
153-
detail :
154-
"A new version is available and can be downloaded at https://github.com/th-ch/youtube-music/releases/latest"
163+
detail:
164+
"A new version is available and can be downloaded at https://github.com/th-ch/youtube-music/releases/latest",
155165
};
156166
electron.dialog.showMessageBox(dialogOpts);
157167
});
158168
}
159169

160170
// Optimized for Mac OS X
161171
if (process.platform === "darwin") {
172+
if (!isAppVisible()) {
173+
app.dock.hide();
174+
}
175+
162176
var forceQuit = false;
163177
app.on("before-quit", () => {
164178
forceQuit = true;
165179
});
166-
mainWindow.on("close", event => {
180+
mainWindow.on("close", (event) => {
167181
if (!forceQuit) {
168182
event.preventDefault();
169183
mainWindow.hide();

menu.js

Lines changed: 72 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,79 @@
11
const { app, Menu } = require("electron");
22

3-
const { getAllPlugins } = require("./plugins/utils");
4-
const { isPluginEnabled, enablePlugin, disablePlugin } = require("./store");
3+
const { getAllPlugins } = require("./plugins/utils");
4+
const {
5+
isPluginEnabled,
6+
enablePlugin,
7+
disablePlugin,
8+
autoUpdate,
9+
isAppVisible,
10+
isTrayEnabled,
11+
setOptions,
12+
} = require("./store");
513

6-
module.exports.setApplicationMenu = () => {
7-
const menuTemplate = [
8-
{
9-
label : "Plugins",
10-
submenu: getAllPlugins().map(plugin => {
11-
return {
12-
label : plugin,
13-
type : "checkbox",
14-
checked: isPluginEnabled(plugin),
15-
click : item => {
16-
if (item.checked) {
17-
enablePlugin(plugin);
18-
} else {
19-
disablePlugin(plugin);
20-
}
14+
const mainMenuTemplate = [
15+
{
16+
label: "Plugins",
17+
submenu: getAllPlugins().map((plugin) => {
18+
return {
19+
label: plugin,
20+
type: "checkbox",
21+
checked: isPluginEnabled(plugin),
22+
click: (item) => {
23+
if (item.checked) {
24+
enablePlugin(plugin);
25+
} else {
26+
disablePlugin(plugin);
2127
}
22-
};
23-
})
24-
}
25-
];
28+
},
29+
};
30+
}),
31+
},
32+
{
33+
label: "Options",
34+
submenu: [
35+
{
36+
label: "Auto-update",
37+
type: "checkbox",
38+
checked: autoUpdate(),
39+
click: (item) => {
40+
setOptions({ autoUpdates: item.checked });
41+
},
42+
},
43+
{
44+
label: "Tray",
45+
submenu: [
46+
{
47+
label: "Disabled",
48+
type: "radio",
49+
checked: !isTrayEnabled(),
50+
click: () => setOptions({ tray: false, appVisible: true }),
51+
},
52+
{
53+
label: "Enabled + app visible",
54+
type: "radio",
55+
checked: isTrayEnabled() && isAppVisible(),
56+
click: () => setOptions({ tray: true, appVisible: true }),
57+
},
58+
{
59+
label: "Enabled + app hidden",
60+
type: "radio",
61+
checked: isTrayEnabled() && !isAppVisible(),
62+
click: () => setOptions({ tray: true, appVisible: false }),
63+
},
64+
],
65+
},
66+
],
67+
},
68+
];
2669

70+
module.exports.mainMenuTemplate = mainMenuTemplate;
71+
module.exports.setApplicationMenu = () => {
72+
const menuTemplate = [...mainMenuTemplate];
2773
if (process.platform === "darwin") {
2874
const name = app.getName();
2975
menuTemplate.unshift({
30-
label : name,
76+
label: name,
3177
submenu: [
3278
{ role: "about" },
3379
{ type: "separator" },
@@ -36,18 +82,18 @@ module.exports.setApplicationMenu = () => {
3682
{ role: "unhide" },
3783
{ type: "separator" },
3884
{
39-
label : "Select All",
85+
label: "Select All",
4086
accelerator: "CmdOrCtrl+A",
41-
selector : "selectAll:"
87+
selector: "selectAll:",
4288
},
4389
{ label: "Cut", accelerator: "CmdOrCtrl+X", selector: "cut:" },
4490
{ label: "Copy", accelerator: "CmdOrCtrl+C", selector: "copy:" },
4591
{ label: "Paste", accelerator: "CmdOrCtrl+V", selector: "paste:" },
4692
{ type: "separator" },
4793
{ role: "minimize" },
4894
{ role: "close" },
49-
{ role: "quit" }
50-
]
95+
{ role: "quit" },
96+
],
5197
});
5298
}
5399

store/index.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,30 @@ const plugins = require("./plugins");
44
const store = new Store({
55
defaults: {
66
"window-size": {
7-
width : 1100,
7+
width: 1100,
88
height: 550
99
},
10-
url : "https://music.youtube.com",
11-
plugins: ["navigation", "shortcuts", "adblocker"]
10+
url: "https://music.youtube.com",
11+
plugins: ["navigation", "shortcuts", "adblocker"],
12+
options: {
13+
tray: false,
14+
appVisible: true,
15+
autoUpdates: true
16+
}
1217
}
1318
});
1419

1520
module.exports = {
16-
store : store,
17-
isPluginEnabled : plugin => plugins.isEnabled(store, plugin),
21+
store: store,
22+
// Plugins
23+
isPluginEnabled: plugin => plugins.isEnabled(store, plugin),
1824
getEnabledPlugins: () => plugins.getEnabledPlugins(store),
19-
enablePlugin : plugin => plugins.enablePlugin(store, plugin),
20-
disablePlugin : plugin => plugins.disablePlugin(store, plugin)
25+
enablePlugin: plugin => plugins.enablePlugin(store, plugin),
26+
disablePlugin: plugin => plugins.disablePlugin(store, plugin),
27+
// Options
28+
setOptions: options =>
29+
store.set("options", { ...store.get("options"), ...options }),
30+
isTrayEnabled: () => store.get("options.tray"),
31+
isAppVisible: () => store.get("options.appVisible"),
32+
autoUpdate: () => store.get("options.autoUpdates")
2133
};

tray.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
const path = require("path");
2+
3+
const { Menu, nativeImage, Tray } = require("electron");
4+
5+
const { mainMenuTemplate } = require("./menu");
6+
const { isTrayEnabled } = require("./store");
7+
const { clickInYoutubeMusic } = require("./utils/youtube-music");
8+
9+
// Prevent tray being garbage collected
10+
let tray;
11+
12+
module.exports.setUpTray = (app, win) => {
13+
if (!isTrayEnabled()) {
14+
tray = undefined;
15+
return;
16+
}
17+
18+
const iconPath = path.join(__dirname, "assets", "youtube-music-tray.png");
19+
let trayIcon = nativeImage.createFromPath(iconPath).resize({
20+
width: 16,
21+
height: 16,
22+
});
23+
tray = new Tray(trayIcon);
24+
tray.setToolTip("Youtube Music");
25+
26+
const trayMenu = Menu.buildFromTemplate([
27+
{
28+
label: "Play/Pause",
29+
click: () => {
30+
clickInYoutubeMusic(
31+
win,
32+
"#left-controls > div > paper-icon-button.play-pause-button.style-scope.ytmusic-player-bar"
33+
);
34+
},
35+
},
36+
{
37+
label: "Next",
38+
click: () => {
39+
clickInYoutubeMusic(
40+
win,
41+
"#left-controls > div > paper-icon-button.next-button.style-scope.ytmusic-player-bar"
42+
);
43+
},
44+
},
45+
{
46+
label: "Previous",
47+
click: () => {
48+
clickInYoutubeMusic(
49+
win,
50+
"#left-controls > div > paper-icon-button.previous-button.style-scope.ytmusic-player-bar"
51+
);
52+
},
53+
},
54+
{
55+
label: "Show",
56+
click: () => {
57+
win.show();
58+
},
59+
},
60+
...mainMenuTemplate,
61+
{
62+
label: "Quit",
63+
click: () => {
64+
app.quit();
65+
},
66+
},
67+
]);
68+
tray.setContextMenu(trayMenu);
69+
};

utils/youtube-music.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const clickInYoutubeMusic = (win, selector) => {
2+
win.webContents.executeJavaScript(
3+
`document.querySelector("${selector}").click();`,
4+
true
5+
);
6+
};
7+
8+
module.exports = { clickInYoutubeMusic };

0 commit comments

Comments
 (0)