forked from ytmdesktop/ytmdesktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tray.js
130 lines (118 loc) · 3.68 KB
/
tray.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
const { app, Menu, Tray, BrowserWindow } = require("electron");
const path = require("path");
const mediaControl = require("./providers/mediaProvider");
const nativeImage = require("electron").nativeImage;
const electronStore = require("electron-store");
const store = new electronStore();
const __ = require("./providers/translateProvider");
const Notification = require("electron-native-notification");
let tray = null;
let saved_icon = null;
let saved_mainWindow = null;
let init_tray = () => {
const { popUpMenu } = require("./providers/templateProvider");
const contextMenu = Menu.buildFromTemplate(
popUpMenu(__, saved_mainWindow, mediaControl, BrowserWindow, path, app)
);
tray.setToolTip("YouTube Music Desktop App");
tray.setContextMenu(contextMenu);
tray.addListener("click", function() {
saved_mainWindow.isVisible()
? saved_mainWindow.hide()
: saved_mainWindow.show();
});
tray.addListener("balloon-click", function() {
saved_mainWindow.isVisible()
? saved_mainWindow.focus()
: saved_mainWindow.show();
});
};
let popUpMenu = null;
exports.createTray = function(mainWindow, icon) {
saved_icon = path.join(__dirname, icon);
const nativeImageIcon = nativeImage.createFromPath(saved_icon);
tray = new Tray(nativeImageIcon);
saved_mainWindow = mainWindow;
if (process.platform != "darwin") {
init_tray();
} else {
// on Mac OS X
tray.setHighlightMode("never");
exports.setShinyTray();
}
};
exports.balloon = function(title, content) {
if (store.get("settings-show-notifications")) {
if (process.platform == "win32") {
tray.displayBalloon({
icon: path.join(__dirname, "assets/favicon.256x256.png"),
title: title,
content: content
});
} else {
new Notification(title, {
body: content,
icon: path.join(__dirname, "assets/favicon.256x256.png")
});
}
}
};
exports.quit = function() {
tray.quit();
};
exports.setShinyTray = function() {
if (store.get("settings-shiny-tray") && process.platform === "darwin") {
// Shiny tray enabled
tray.setContextMenu(null);
tray.removeAllListeners();
tray.on("right-click", (event, bound, position) => {
// console.log('right_clicked', bound);
if (!popUpMenu.isVisible()) {
popUpMenu.setPosition(bound.x, bound.y + bound.height + 1);
popUpMenu.show();
} else {
popUpMenu.hide();
}
});
tray.on("click", (event, bound, position) => {
// console.log(position);
if (position.x < 32) {
saved_mainWindow.isVisible()
? saved_mainWindow.hide()
: saved_mainWindow.show();
} else if (position.x > 130) {
mediaControl.playPauseTrack(saved_mainWindow.getBrowserView());
}
});
popUpMenu = new BrowserWindow({
frame: false,
center: true,
alwaysOnTop: true,
autoHideMenuBar: true,
resizable: false,
backgroundColor: "#232323",
width: 160,
height: 277,
webPreferences: { nodeIntegration: true }
});
popUpMenu.loadFile(path.join(__dirname, "./pages/mac-shiny-menu.html"));
popUpMenu.setVisibleOnAllWorkspaces(true);
popUpMenu.hide();
popUpMenu.on("blur", () => {
popUpMenu.hide();
});
} else {
// Shiny tray disabled ||| on onther platform
tray.setImage(saved_icon);
tray.removeAllListeners();
init_tray();
}
};
exports.updateImage = function(payload) {
if (!store.get("settings-shiny-tray")) return;
var img =
typeof nativeImage.createFromDataURL === "function"
? nativeImage.createFromDataURL(payload) // electron v0.36+
: nativeImage.createFromDataUrl(payload); // electron v0.30
tray.setImage(img);
};