forked from Margiris/ytmdesktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrayProvider.js
164 lines (148 loc) · 4.24 KB
/
trayProvider.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const { app, Menu, Tray, BrowserWindow } = require("electron");
const path = require("path");
const mediaControl = require("./mediaProvider");
const nativeImage = require("electron").nativeImage;
const settingsProvider = require("./settingsProvider");
const __ = require("./translateProvider");
const Notification = require("electron-native-notification");
const { doBehavior } = require("../utils/window");
const base64Img = require("base64-img");
const systemInfo = require("../utils/systemInfo");
let tray = null;
let saved_icon = null;
let saved_mainWindow = null;
function setTooltip(tooltip) {
tray.setToolTip(tooltip);
}
let init_tray = () => {
const { popUpMenu } = require("./templateProvider");
const contextMenu = Menu.buildFromTemplate(
popUpMenu(__, saved_mainWindow, mediaControl, BrowserWindow, path, app)
);
setTooltip("YouTube Music Desktop App");
tray.setContextMenu(contextMenu);
tray.addListener("click", () => {
doBehavior(saved_mainWindow);
});
tray.addListener("balloon-click", function() {
doBehavior(saved_mainWindow);
});
};
let popUpMenu = null;
function createTray(mainWindow, icon) {
saved_icon = path.join(__dirname, `../${icon}`);
let nativeImageIcon = nativeImage.createFromPath(saved_icon);
tray = new Tray(nativeImageIcon);
saved_mainWindow = mainWindow;
if (!systemInfo.isMac()) {
init_tray();
} else {
// on Mac OS X
setShinyTray();
}
}
function updateTrayIcon(icon) {
let nativeImageIcon = nativeImage.createFromPath(icon);
if (systemInfo.isMac()) {
nativeImageIcon = nativeImageIcon.resize({
height: 18,
width: 18,
quality: "best"
});
}
tray.setImage(nativeImageIcon);
}
function balloon(title, content, cover) {
if (title && content && cover) {
base64Img.requestBase64(cover, function(err, res, body) {
let image = nativeImage.createFromDataURL(body);
if (settingsProvider.get("settings-show-notifications")) {
if (title && content) {
if (process.platform == "win32") {
tray.displayBalloon({
icon: image,
title: title,
content: content
});
} else {
new Notification(title, {
body: content,
icon: image
});
}
}
}
});
}
}
function quit() {
tray.destroy();
}
function setShinyTray() {
if (
settingsProvider.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) {
doBehavior(saved_mainWindow);
} 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/settings/mac_shiny.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();
}
}
function updateImage(payload) {
if (!settingsProvider.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);
}
module.exports = {
setTooltip: setTooltip,
createTray: createTray,
balloon: balloon,
quit: quit,
setShinyTray: setShinyTray,
updateImage: updateImage,
updateTrayIcon: updateTrayIcon
};