import { app , BrowserWindow , globalShortcut , ipcMain , Menu , nativeImage , Tray } from "electron" ; import * as isDev from "electron-is-dev" ; import * as settings from "electron-settings" ; import * as path from "path" ; import * as request from "request" ; import * as url from "url" ; import * as constants from "./const" ; import { Connection } from "./app-server" ; let mainWindow ; let tray ; let imageLocation ; let settingsWindow ; let connection ; app . on ( "ready" , ( ) => { createWindow ( ) ; } ) ; const shouldQuit = app . makeSingleInstance ( ( commandLine , workingDirectory ) => { if ( mainWindow ) { mainWindow . show ( ) ; } } ) ; if ( shouldQuit ) { app . quit ( ) ; } function createWindow ( ) { if ( ! settings . has ( "autoLanguage" ) ) { settings . set ( "autoLanguage" , true ) ; settings . set ( "language" , "com" ) ; console . log ( "init set settings." ) ; } if ( ! settings . has ( "lyrics" ) ) { settings . set ( "lyrics" , true ) ; console . log ( "init set settings lyrics." ) ; } if ( ! settings . has ( "appServer" ) ) { settings . set ( "appServer" , true ) ; console . log ( "init set settings app server." ) ; } if ( ! settings . has ( "appPort" ) ) { settings . set ( "appPort" , 3000 ) ; console . log ( "init set settings app port." ) ; } if ( isDev ) { imageLocation = "assets/favicon.png" ; } else { imageLocation = __dirname . replace ( "/resources/app.asar" , "" ) + "/resources/favicon.png" ; } mainWindow = new BrowserWindow ( { height : 800 , icon : path . join ( __dirname , imageLocation ) , title : constants . APP_NAME , webPreferences : { contextIsolation : false } , width : 1200 } ) ; mainWindow . setMenu ( null ) ; mainWindow . loadURL ( url . format ( { pathname : path . join ( __dirname , "main/index.html" ) , protocol : "file:" , slashes : true } ) ) ; if ( isDev ) { mainWindow . webContents . openDevTools ( ) ; } mainWindow . on ( "closed" , ( ) => { mainWindow = null ; } ) ; tray = new Tray ( nativeImage . createFromPath ( imageLocation ) ) ; const contextMenu = Menu . buildFromTemplate ( [ { click : ( ) => { mainWindow . isVisible ( ) ? mainWindow . hide ( ) : mainWindow . show ( ) ; } , label : "🎵 Toggle App" } , { label : "⏭️ Next Track" , type : "normal" , click : nextTrack } , { label : "⏯️ Play/Pause" , type : "normal" , click : playAndPause } , { label : "⏮️ Previous Track" , type : "normal" , click : previousTrack } , { click : ( ) => { mainWindow . removeAllListeners ( "close" ) ; mainWindow . close ( ) ; app . quit ( ) ; } , label : "⏹️ Quit" } , { click : ( ) => { settingsWindow = new BrowserWindow ( { height : 800 , icon : path . join ( __dirname , imageLocation ) , title : constants . APP_NAME , width : 500 } ) ; settingsWindow . setMenu ( null ) ; settingsWindow . loadURL ( url . format ( { pathname : path . join ( __dirname , "settings/index.html" ) , protocol : "file:" , slashes : true } ) ) ; if ( isDev ) { settingsWindow . webContents . openDevTools ( ) ; } } , label : "⚙️ Options" } ] ) ; tray . setToolTip ( "Amazon Music" ) ; tray . setContextMenu ( contextMenu ) ; tray . on ( "click" , ( ) => { mainWindow . isVisible ( ) ? mainWindow . hide ( ) : mainWindow . show ( ) ; } ) ; mainWindow . on ( "minimize" , ( event ) => { event . preventDefault ( ) ; mainWindow . hide ( ) ; } ) ; mainWindow . on ( "close" , ( event ) => { event . preventDefault ( ) ; mainWindow . hide ( ) ; return false ; } ) ; connection = new Connection ( ) ; connection . startServer ( ( ) => { mainWindow . webContents . send ( "forceTrackAndArtist" ) ; connection . setListener ( connection . socket , ( message ) => { switch ( message ) { case "playAndPause" : mainWindow . webContents . send ( "playAndPause" ) ; break ; case "nextTrack" : mainWindow . webContents . send ( "nextTrack" ) ; break ; case "previousTrack" : mainWindow . webContents . send ( "previousTrack" ) ; break ; } } ) ; } ) ; } app . on ( "window-all-closed" , ( ) => { if ( process . platform !== "darwin" ) { app . quit ( ) ; } } ) ; app . on ( "activate" , ( ) => { if ( mainWindow === null ) { createWindow ( ) ; } } ) ; author Flo Dörr <flo@dörr.site>
  ipcMain . on ( "appendTitle" , ( event , arg ) => { connection . setTrack ( arg ) ; mainWindow . setTitle ( `${ constants . APP_NAME }\t${ arg }` ) ; } ) ; ipcMain . on ( "setTrackAndArtist" , ( event , arg ) => { connection . setTrackAndArtist ( arg [ 0 ] , arg [ 1 ] ) ; } ) ; author Flo Dörr <flo@dörr.site>
  const options = { encoding : null , method : "get" , url } ; ipcMain . on ( "setTrayImage" , ( event , arg ) => { options . url = arg ; request ( options , ( err , resp , body ) => { if ( ! err ) { tray . setImage ( nativeImage . createFromBuffer ( body ) ) ; connection . sendTrackImage ( body . toString ( "base64" ) ) ; } else { tray . setImage ( imageLocation ) ; } } ) ; } ) ; author Flo Dörr <flo@dörr.site>
  app . on ( "ready" , ( ) => { if ( globalShortcut . register ( "mediaplaypause" , playAndPause ) ) { console . log ( "mediaplaypause registered!" ) ; } else { console . log ( "mediaplaypause NOT registered!" ) ; } if ( globalShortcut . register ( "medianexttrack" , nextTrack ) ) { console . log ( "medianexttrack registered!" ) ; } else { console . log ( "medianexttrack NOT registered!" ) ; } if ( globalShortcut . register ( "mediaprevioustrack" , previousTrack ) ) { console . log ( "mediaprevioustrack registered!" ) ; } else { console . log ( "mediaprevioustrack NOT registered!" ) ; } if ( globalShortcut . register ( "mediastop" , playAndPause ) ) { console . log ( "mediastop registered!" ) ; } else { console . log ( "mediastop NOT registered!" ) ; } } ) ; author Flo Dörr <flo@dörr.site>
  function playAndPause ( ) { mainWindow . webContents . send ( "playAndPause" ) ; } author Flo Dörr <flo@dörr.site>
  function nextTrack ( ) { mainWindow . webContents . send ( "nextTrack" ) ; } author Flo Dörr <flo@dörr.site>
  function previousTrack ( ) { mainWindow . webContents . send ( "previousTrack" ) ; } returns author Flo Dörr <flo@dörr.site>
  exports . getTitle = function getTitle ( ) { return mainWindow . getTitle ( ) ; } ; author Flo Dörr <flo@dörr.site>
  ipcMain . on ( "lyrics" , ( event , arg ) => { mainWindow . webContents . send ( "lyrics" , arg ) ; } ) ; author Flo Dörr <flo@dörr.site>
  ipcMain . on ( "resumed" , ( ) => { mainWindow . webContents . send ( "resumed" ) ; } ) ; author Flo Dörr <flo@dörr.site>
  ipcMain . on ( "paused" , ( ) => { mainWindow . webContents . send ( "paused" ) ; } ) ; author Flo Dörr <flo@dörr.site>
  ipcMain . on ( "nextClicked" , ( ) => { mainWindow . webContents . send ( "nextClicked" ) ; } ) ; author Flo Dörr <flo@dörr.site>
  ipcMain . on ( "previousClicked" , ( ) => { mainWindow . webContents . send ( "previousClicked" ) ; } ) ; ipcMain . on ( "sendPlayingStatus" , ( event , arg ) => { connection . sendPlayingStatus ( arg ) ; } ) ; ipcMain . on ( "sendTrackName" , ( event , arg ) => { connection . setTrack ( arg ) ; } ) ;