@ -1,40 +1,30 @@
/* global __static */
import path from 'path' ;
import { app , nativeImage , Tray , Menu } from 'electron' ;
import { isLinux } from '@/utils/platform' ;
export function createTray ( win ) {
let icon = nativeImage
. createFromPath ( path . join ( _ _static , 'img/icons/menu@88.png' ) )
. resize ( {
height : 20 ,
width : 20 ,
} ) ;
let contextMenu = Menu . buildFromTemplate ( [
//setContextMenu破坏了预期的click行为
//在linux下, 鼠标左右键都会呼出contextMenu
//所以此处单独为linux添加一个 显示主面板 选项
... ( process . platform === 'linux'
? [
function createMenuTemplate ( win ) {
return [
{
label : '显示主面板' ,
label : '播放' ,
icon : nativeImage . createFromPath (
path . join ( _ _static , 'img/icons/play.png' )
) ,
click : ( ) => {
win . show ( ) ;
} ,
win . webContents . send ( 'play' ) ;
} ,
{
type : 'separator' ,
id : 'play' ,
} ,
]
: [ ] ) ,
{
label : ' 播放/ 暂停',
label : '暂停' ,
icon : nativeImage . createFromPath (
path . join ( _ _static , 'img/icons/p lay .png')
path . join ( _ _static , 'img/icons/p ause .png')
) ,
click : ( ) => {
win . webContents . send ( 'play' ) ;
} ,
id : 'pause' ,
visible : false ,
} ,
{
label : '上一首' ,
@ -75,6 +65,19 @@ export function createTray(win) {
click : ( ) => {
win . webContents . send ( 'like' ) ;
} ,
id : 'like' ,
} ,
{
label : '取消喜欢' ,
icon : nativeImage . createFromPath (
path . join ( _ _static , 'img/icons/unlike.png' )
) ,
accelerator : 'CmdOrCtrl+L' ,
click : ( ) => {
win . webContents . send ( 'like' ) ;
} ,
id : 'unlike' ,
visible : false ,
} ,
{
label : '退出' ,
@ -86,28 +89,117 @@ export function createTray(win) {
app . exit ( ) ;
} ,
} ,
] ) ;
let tray = new Tray ( icon ) ;
tray . setToolTip ( 'YesPlayMusic' ) ;
] ;
}
// linux下托盘的实现方式比较迷惑
// right-click无法在linux下使用
// click在默认行为下会弹出一个contextMenu, 里面的唯一选项才会调用click事件
// setContextMenu应该是目前唯一能在linux下使用托盘菜单api
// 但是无法区分鼠标左右键
class YPMTrayLinuxImpl {
constructor ( tray , win , emitter ) {
this . tray = tray ;
this . win = win ;
this . emitter = emitter ;
this . template = undefined ;
this . initTemplate ( ) ;
this . contextMenu = Menu . buildFromTemplate ( this . template ) ;
this . tray . setContextMenu ( this . contextMenu ) ;
this . handleEvents ( ) ;
}
initTemplate ( ) {
//在linux下, 鼠标左右键都会呼出contextMenu
//所以此处单独为linux添加一个 显示主面板 选项
this . template = [
{
label : '显示主面板' ,
click : ( ) => {
this . win . show ( ) ;
} ,
} ,
{
type : 'separator' ,
} ,
] . concat ( createMenuTemplate ( this . win ) ) ;
}
if ( process . platform === 'linux' ) {
//linux下托盘的实现方式比较迷惑
//right-click无法在linux下使用
//click在默认行为下会弹出一个contextMenu, 里面的唯一选项才会调用click事件
//setContextMenu应该是目前唯一能在linux下使用托盘菜单api
//但是无法区分鼠标左右键
tray . setContextMenu ( contextMenu ) ;
} else {
//windows and macos
tray . on ( 'click' , ( ) => {
win . show ( ) ;
handleEvents ( ) {
this . emitter . on ( 'updateTooltip' , title => this . tray . setToolTip ( title ) ) ;
this . emitter . on ( 'updatePlayState' , isPlaying => {
this . contextMenu . getMenuItemById ( 'play' ) . visible = ! isPlaying ;
this . contextMenu . getMenuItemById ( 'pause' ) . visible = isPlaying ;
this . tray . setContextMenu ( this . contextMenu ) ;
} ) ;
this . emitter . on ( 'updateLikeState' , isLiked => {
this . contextMenu . getMenuItemById ( 'like' ) . visible = ! isLiked ;
this . contextMenu . getMenuItemById ( 'unlike' ) . visible = isLiked ;
this . tray . setContextMenu ( this . contextMenu ) ;
} ) ;
}
}
class YPMTrayWindowsImpl {
constructor ( tray , win , emitter ) {
this . tray = tray ;
this . win = win ;
this . emitter = emitter ;
this . template = createMenuTemplate ( win ) ;
this . contextMenu = Menu . buildFromTemplate ( this . template ) ;
this . isPlaying = false ;
this . curDisplayPlaying = false ;
tray . on ( 'right-click' , ( ) => {
tray . popUpContextMenu ( contextMenu ) ;
this . isLiked = false ;
this . curDisplayLiked = false ;
this . handleEvents ( ) ;
}
handleEvents ( ) {
this . tray . on ( 'click' , ( ) => {
this . win . show ( ) ;
} ) ;
this . tray . on ( 'right-click' , ( ) => {
if ( this . isPlaying !== this . curDisplayPlaying ) {
this . curDisplayPlaying = this . isPlaying ;
this . contextMenu . getMenuItemById ( 'play' ) . visible = ! this . isPlaying ;
this . contextMenu . getMenuItemById ( 'pause' ) . visible = this . isPlaying ;
}
return tray ;
if ( this . isLiked !== this . curDisplayLiked ) {
this . curDisplayLiked = this . isLiked ;
this . contextMenu . getMenuItemById ( 'like' ) . visible = ! this . isLiked ;
this . contextMenu . getMenuItemById ( 'unlike' ) . visible = this . isLiked ;
}
this . tray . popUpContextMenu ( this . contextMenu ) ;
} ) ;
this . emitter . on ( 'updateTooltip' , title => this . tray . setToolTip ( title ) ) ;
this . emitter . on (
'updatePlayState' ,
isPlaying => ( this . isPlaying = isPlaying )
) ;
this . emitter . on ( 'updateLikeState' , isLiked => ( this . isLiked = isLiked ) ) ;
}
}
export function createTray ( win , eventEmitter ) {
let icon = nativeImage
. createFromPath ( path . join ( _ _static , 'img/icons/menu@88.png' ) )
. resize ( {
height : 20 ,
width : 20 ,
} ) ;
let tray = new Tray ( icon ) ;
tray . setToolTip ( 'YesPlayMusic' ) ;
return isLinux
? new YPMTrayLinuxImpl ( tray , win , eventEmitter )
: new YPMTrayWindowsImpl ( tray , win , eventEmitter ) ;
}