@ -4,12 +4,73 @@ import { registerGlobalShortcut } from '@/electron/globalShortcut';
import cloneDeep from 'lodash/cloneDeep' ;
import cloneDeep from 'lodash/cloneDeep' ;
import shortcuts from '@/utils/shortcuts' ;
import shortcuts from '@/utils/shortcuts' ;
import { createMenu } from './menu' ;
import { createMenu } from './menu' ;
import { isCreateTray , isMac } from '@/utils/platform' ;
const clc = require ( 'cli-color' ) ;
const clc = require ( 'cli-color' ) ;
const log = text => {
const log = text => {
console . log ( ` ${ clc . blueBright ( '[ipcMain.js]' ) } ${ text } ` ) ;
console . log ( ` ${ clc . blueBright ( '[ipcMain.js]' ) } ${ text } ` ) ;
} ;
} ;
const exitAsk = ( e , win ) => {
e . preventDefault ( ) ; //阻止默认行为
dialog
. showMessageBox ( {
type : 'info' ,
title : 'Information' ,
cancelId : 2 ,
defaultId : 0 ,
message : '确定要关闭吗?' ,
buttons : [ '最小化' , '直接退出' ] ,
} )
. then ( result => {
if ( result . response == 0 ) {
e . preventDefault ( ) ; //阻止默认行为
win . minimize ( ) ; //调用 最小化实例方法
} else if ( result . response == 1 ) {
win = null ;
//app.quit();
app . exit ( ) ; //exit()直接关闭客户端, 不会执行quit();
}
} )
. catch ( err => {
log ( err ) ;
} ) ;
} ;
const exitAskWithoutMac = ( e , win ) => {
e . preventDefault ( ) ; //阻止默认行为
dialog
. showMessageBox ( {
type : 'info' ,
title : 'Information' ,
cancelId : 2 ,
defaultId : 0 ,
message : '确定要关闭吗?' ,
buttons : [ '最小化到托盘' , '直接退出' ] ,
checkboxLabel : '记住我的选择' ,
} )
. then ( result => {
if ( result . checkboxChecked && result . response !== 2 ) {
win . webContents . send (
'rememberCloseAppOption' ,
result . response === 0 ? 'minimizeToTray' : 'exit'
) ;
}
if ( result . response === 0 ) {
e . preventDefault ( ) ; //阻止默认行为
win . hide ( ) ; //调用 最小化实例方法
} else if ( result . response === 1 ) {
win = null ;
//app.quit();
app . exit ( ) ; //exit()直接关闭客户端, 不会执行quit();
}
} )
. catch ( err => {
log ( err ) ;
} ) ;
} ;
const client = require ( 'discord-rich-presence' ) ( '818936529484906596' ) ;
const client = require ( 'discord-rich-presence' ) ( '818936529484906596' ) ;
/ * *
/ * *
@ -58,7 +119,7 @@ function parseSourceStringToList(sourceString) {
return sourceString . split ( ',' ) . map ( s => s . trim ( ) ) ;
return sourceString . split ( ',' ) . map ( s => s . trim ( ) ) ;
}
}
export function initIpcMain ( win , store ) {
export function initIpcMain ( win , store , trayEventEmitter ) {
ipcMain . handle ( 'unblock-music' , async ( _ , track , source ) => {
ipcMain . handle ( 'unblock-music' , async ( _ , track , source ) => {
// 兼容 unblockneteasemusic 所使用的 api 字段
// 兼容 unblockneteasemusic 所使用的 api 字段
track . alias = track . alia || [ ] ;
track . alias = track . alia || [ ] ;
@ -102,9 +163,9 @@ export function initIpcMain(win, store) {
} ) ;
} ) ;
ipcMain . on ( 'close' , e => {
ipcMain . on ( 'close' , e => {
if ( process. platform === 'darwin' ) {
if ( isMac ) {
win . hide ( ) ;
win . hide ( ) ;
exitAsk ( e );
exitAsk ( e , win );
} else {
} else {
let closeOpt = store . get ( 'settings.closeAppOption' ) ;
let closeOpt = store . get ( 'settings.closeAppOption' ) ;
if ( closeOpt === 'exit' ) {
if ( closeOpt === 'exit' ) {
@ -115,7 +176,7 @@ export function initIpcMain(win, store) {
e . preventDefault ( ) ;
e . preventDefault ( ) ;
win . hide ( ) ;
win . hide ( ) ;
} else {
} else {
exitAskWithoutMac ( e );
exitAskWithoutMac ( e , win );
}
}
}
}
} ) ;
} ) ;
@ -214,63 +275,15 @@ export function initIpcMain(win, store) {
registerGlobalShortcut ( win , store ) ;
registerGlobalShortcut ( win , store ) ;
} ) ;
} ) ;
const exitAsk = e => {
if ( isCreateTray ) {
e . preventDefault ( ) ; //阻止默认行为
ipcMain . on ( 'updateTrayTooltip' , ( _ , title ) => {
dialog
trayEventEmitter . emit ( 'updateTooltip' , title ) ;
. showMessageBox ( {
type : 'info' ,
title : 'Information' ,
cancelId : 2 ,
defaultId : 0 ,
message : '确定要关闭吗?' ,
buttons : [ '最小化' , '直接退出' ] ,
} )
. then ( result => {
if ( result . response == 0 ) {
e . preventDefault ( ) ; //阻止默认行为
win . minimize ( ) ; //调用 最小化实例方法
} else if ( result . response == 1 ) {
win = null ;
//app.quit();
app . exit ( ) ; //exit()直接关闭客户端, 不会执行quit();
}
} )
. catch ( err => {
log ( err ) ;
} ) ;
} ) ;
} ;
ipcMain . on ( 'updateTrayPlayState' , ( _ , isPlaying ) => {
trayEventEmitter . emit ( 'updatePlayState' , isPlaying ) ;
const exitAskWithoutMac = e => {
} ) ;
e . preventDefault ( ) ; //阻止默认行为
ipcMain . on ( 'updateTrayLikeState' , ( _ , isLiked ) => {
dialog
trayEventEmitter . emit ( 'updateLikeState' , isLiked ) ;
. showMessageBox ( {
type : 'info' ,
title : 'Information' ,
cancelId : 2 ,
defaultId : 0 ,
message : '确定要关闭吗?' ,
buttons : [ '最小化到托盘' , '直接退出' ] ,
checkboxLabel : '记住我的选择' ,
} )
. then ( result => {
if ( result . checkboxChecked && result . response !== 2 ) {
win . webContents . send (
'rememberCloseAppOption' ,
result . response === 0 ? 'minimizeToTray' : 'exit'
) ;
}
if ( result . response === 0 ) {
e . preventDefault ( ) ; //阻止默认行为
win . hide ( ) ; //调用 最小化实例方法
} else if ( result . response === 1 ) {
win = null ;
//app.quit();
app . exit ( ) ; //exit()直接关闭客户端, 不会执行quit();
}
} )
. catch ( err => {
log ( err ) ;
} ) ;
} ) ;
} ;
}
}
}