Skip to content

Commit

Permalink
🐛 Fix ytmdesktop#292
Browse files Browse the repository at this point in the history
  • Loading branch information
adlerluiz committed Aug 6, 2020
1 parent faf918e commit a2aa399
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 61 deletions.
20 changes: 10 additions & 10 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ function createWindow() {
}

// Experimental
if (isWindows() && view.webContents.getURL().indexOf('v=') != -1) {
/*if (isWindows() && view.webContents.getURL().indexOf('v=') != -1) {
mainWindow.setThumbnailClip({
x: 230,
y: 150,
Expand All @@ -450,7 +450,7 @@ function createWindow() {
width: mainWindow.getSize()[0],
height: mainWindow.getSize()[1],
})
}
}*/

/**
* Update only when change track
Expand Down Expand Up @@ -492,15 +492,15 @@ function createWindow() {
)
}

writeLog({ type: 'info', data: `Listen: ${title} - ${author}` })
}

if (!isMac() && !settingsProvider.get('settings-shiny-tray')) {
if (playerInfo.isPaused) {
tray.updateTrayIcon(iconPause)
} else {
tray.updateTrayIcon(iconPlay)
if (!isMac() && !settingsProvider.get('settings-shiny-tray')) {
if (playerInfo.isPaused) {
tray.updateTrayIcon(iconPause)
} else {
tray.updateTrayIcon(iconPlay)
}
}

writeLog({ type: 'info', data: `Listen: ${title} - ${author}` })
}

lastTrackProgress = progress
Expand Down
6 changes: 4 additions & 2 deletions src/pages/settings/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,11 @@ document.addEventListener('DOMContentLoaded', function () {
initElement('settings-companion-server-protect', 'click')
initElement('settings-continue-where-left-of', 'click')
initElement('settings-windows10-media-service', 'click', () => {
checkWindows10ServiceStatus()
checkWindows10ServiceStatus(), showRelaunchButton()
})
initElement('settings-windows10-media-service-show-info', 'click', () => {
showRelaunchButton()
})
initElement('settings-windows10-media-service-show-info', 'click')
initElement('settings-shiny-tray', 'click', () => {
ipc.send('update-tray')
})
Expand Down
141 changes: 92 additions & 49 deletions src/providers/trayProvider.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { app, Menu, Tray, BrowserWindow } = require('electron')
const { app, Menu, Tray, BrowserWindow, ipcMain } = require('electron')
const path = require('path')
const mediaControl = require('./mediaProvider')
const nativeImage = require('electron').nativeImage
Expand All @@ -15,20 +15,34 @@ let saved_mainWindow = null
let contextMenu = null

function setTooltip(tooltip) {
tray.setToolTip(tooltip)
try {
tray.setToolTip(tooltip)
} catch (error) {
ipcMain.emit('log', {
type: 'warn',
data: `Failed to setTooltip: ${error}`,
})
}
}

let init_tray = () => {
setTooltip('YouTube Music Desktop App')
tray.setContextMenu(contextMenu)
try {
setTooltip('YouTube Music Desktop App')
tray.setContextMenu(contextMenu)

tray.addListener('click', () => {
doBehavior(saved_mainWindow)
})
tray.addListener('click', () => {
doBehavior(saved_mainWindow)
})

tray.addListener('balloon-click', function () {
doBehavior(saved_mainWindow)
})
tray.addListener('balloon-click', function () {
doBehavior(saved_mainWindow)
})
} catch (error) {
ipcMain.emit('log', {
type: 'warn',
data: `Failed to init_tray: ${error}`,
})
}
}

function createTray(mainWindow, icon) {
Expand All @@ -43,14 +57,20 @@ function createTray(mainWindow, icon) {
if (!systemInfo.isMac()) {
init_tray()
} else {
// on Mac OS X
setShinyTray()
}
}

function updateTrayIcon(icon) {
nativeImageIcon = buildTrayIcon(icon)
tray.setImage(nativeImageIcon)
try {
nativeImageIcon = buildTrayIcon(icon)
tray.setImage(nativeImageIcon)
} catch (error) {
ipcMain.emit('log', {
type: 'warn',
data: `Failed to updateTrayIcon: ${error}`,
})
}
}

function buildTrayIcon(icon) {
Expand Down Expand Up @@ -81,21 +101,28 @@ function balloon(title, content, cover, icon) {
}

function _doNotification(title, content, image) {
if (title && content) {
if (systemInfo.isWindows()) {
tray.displayBalloon({
icon: image,
title: title,
content: content,
noSound: true,
})
} else {
new Notification(title, {
body: content,
silent: true,
icon: image,
})
try {
if (title && content) {
if (systemInfo.isWindows()) {
tray.displayBalloon({
icon: image,
title: title,
content: content,
noSound: true,
})
} else {
new Notification(title, {
body: content,
silent: true,
icon: image,
})
}
}
} catch (error) {
ipcMain.emit('log', {
type: 'warn',
data: `Failed to _doNotification: ${error}`,
})
}
}

Expand All @@ -104,33 +131,49 @@ function quit() {
}

function setShinyTray() {
if (settingsProvider.get('settings-shiny-tray') && systemInfo.isMac()) {
tray.setContextMenu(null)
tray.removeAllListeners()
tray.on('right-click', (event, bound, position) => {
tray.popUpContextMenu(contextMenu)
})
tray.on('click', (event, bound, position) => {
if (position.x < 32) {
saved_mainWindow.show()
} else if (position.x > 130) {
mediaControl.playPauseTrack(saved_mainWindow.getBrowserView())
}
try {
if (settingsProvider.get('settings-shiny-tray') && systemInfo.isMac()) {
tray.setContextMenu(null)
tray.removeAllListeners()
tray.on('right-click', (event, bound, position) => {
tray.popUpContextMenu(contextMenu)
})
tray.on('click', (event, bound, position) => {
if (position.x < 32) {
saved_mainWindow.show()
} else if (position.x > 130) {
mediaControl.playPauseTrack(
saved_mainWindow.getBrowserView()
)
}
})
} else {
// Shiny tray disabled ||| on onther platform
tray.removeAllListeners()
init_tray()
}
} catch (error) {
ipcMain.emit('log', {
type: 'warn',
data: `Failed to setShinyTray: ${error}`,
})
} else {
// Shiny tray disabled ||| on onther platform
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)
try {
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)
} catch {
ipcMain.emit('log', {
type: 'warn',
data: `Failed to updateImage: ${error}`,
})
}
}

module.exports = {
Expand Down

0 comments on commit a2aa399

Please sign in to comment.