@@ -2,6 +2,7 @@ const mpris = require("mpris-service");
22const { ipcMain } = require ( "electron" ) ;
33const registerCallback = require ( "../../providers/song-info" ) ;
44const getSongControls = require ( "../../providers/song-controls" ) ;
5+ const config = require ( "../../config" ) ;
56
67function setupMPRIS ( ) {
78 const player = mpris ( {
@@ -19,7 +20,7 @@ function setupMPRIS() {
1920
2021function registerMPRIS ( win ) {
2122 const songControls = getSongControls ( win ) ;
22- const { playPause, next, previous } = songControls ;
23+ const { playPause, next, previous, volumeMinus10 , volumePlus10 } = songControls ;
2324 try {
2425 const secToMicro = n => Math . round ( Number ( n ) * 1e6 ) ;
2526 const microToSec = n => Math . round ( Number ( n ) / 1e6 ) ;
@@ -34,6 +35,35 @@ function registerMPRIS(win) {
3435 let currentSeconds = 0 ;
3536 ipcMain . on ( 'timeChanged' , ( _ , t ) => currentSeconds = t ) ;
3637
38+ let currentLoopStatus = undefined ;
39+ let manuallySwitchingStatus = false ;
40+ ipcMain . on ( "repeatChanged" , ( _ , mode ) => {
41+ if ( manuallySwitchingStatus )
42+ return ;
43+
44+ if ( mode === "Repeat off" )
45+ currentLoopStatus = "None" ;
46+ else if ( mode === "Repeat one" )
47+ currentLoopStatus = "Track" ;
48+ else if ( mode === "Repeat all" )
49+ currentLoopStatus = "Playlist" ;
50+
51+ player . loopStatus = currentLoopStatus ;
52+ } ) ;
53+ player . on ( "loopStatus" , ( status ) => {
54+ // switchRepeat cycles between states in that order
55+ const switches = [ "None" , "Playlist" , "Track" ] ;
56+ const currentIndex = switches . indexOf ( currentLoopStatus ) ;
57+ const targetIndex = switches . indexOf ( status ) ;
58+
59+ // Get a delta in the range [0,2]
60+ const delta = ( targetIndex - currentIndex + 3 ) % 3 ;
61+
62+ manuallySwitchingStatus = true ;
63+ songControls . switchRepeat ( delta ) ;
64+ manuallySwitchingStatus = false ;
65+ } )
66+
3767 player . getPosition = ( ) => secToMicro ( currentSeconds )
3868
3969 player . on ( "raise" , ( ) => {
@@ -53,21 +83,45 @@ function registerMPRIS(win) {
5383 playPause ( )
5484 }
5585 } ) ;
86+ player . on ( "playpause" , ( ) => {
87+ player . playbackStatus = player . playbackStatus === 'Playing' ? "Paused" : "Playing" ;
88+ playPause ( ) ;
89+ } ) ;
5690
57- player . on ( "playpause" , playPause ) ;
5891 player . on ( "next" , next ) ;
5992 player . on ( "previous" , previous ) ;
6093
6194 player . on ( 'seek' , seekBy ) ;
6295 player . on ( 'position' , seekTo ) ;
6396
64- registerCallback ( songInfo => {
65- if ( player ) {
66- const data = {
67- 'mpris:length' : secToMicro ( songInfo . songDuration ) ,
68- 'mpris:artUrl' : songInfo . imageSrc ,
69- 'xesam:title' : songInfo . title ,
70- 'xesam:artist' : [ songInfo . artist ] ,
97+ ipcMain . on ( 'volumeChanged' , ( _ , value ) => {
98+ player . volume = value ;
99+ } ) ;
100+ player . on ( 'volume' , ( newVolume ) => {
101+ if ( config . plugins . isEnabled ( 'precise-volume' ) ) {
102+ // With precise volume we can set the volume to the exact value.
103+ win . webContents . send ( 'setVolume' , newVolume )
104+ } else {
105+ // With keyboard shortcuts we can only change the volume in increments of 10, so round it.
106+ const deltaVolume = Math . round ( ( newVolume - player . volume ) / 10 ) ;
107+
108+ if ( deltaVolume > 0 ) {
109+ for ( let i = 0 ; i < deltaVolume ; i ++ )
110+ volumePlus10 ( ) ;
111+ } else {
112+ for ( let i = 0 ; i < - deltaVolume ; i ++ )
113+ volumeMinus10 ( ) ;
114+ }
115+ }
116+ } ) ;
117+
118+ registerCallback ( songInfo => {
119+ if ( player ) {
120+ const data = {
121+ 'mpris:length' : secToMicro ( songInfo . songDuration ) ,
122+ 'mpris:artUrl' : songInfo . imageSrc ,
123+ 'xesam:title' : songInfo . title ,
124+ 'xesam:artist' : [ songInfo . artist ] ,
71125 'mpris:trackid' : '/'
72126 } ;
73127 if ( songInfo . album ) data [ 'xesam:album' ] = songInfo . album ;
0 commit comments