1
- const { globalShortcut } = require ( "electron" ) ;
1
+ const { globalShortcut, ipcMain } = require ( "electron" ) ;
2
2
const is = require ( "electron-is" ) ;
3
3
const electronLocalshortcut = require ( "electron-localshortcut" ) ;
4
4
const getSongControls = require ( "../../providers/song-controls" ) ;
5
5
const { setupMPRIS } = require ( "./mpris" ) ;
6
6
const registerCallback = require ( "../../providers/song-info" ) ;
7
7
8
- let player ;
9
-
10
8
function _registerGlobalShortcut ( webContents , shortcut , action ) {
11
9
globalShortcut . register ( shortcut , ( ) => {
12
10
action ( webContents ) ;
@@ -31,54 +29,8 @@ function registerShortcuts(win, options) {
31
29
32
30
_registerLocalShortcut ( win , "CommandOrControl+F" , search ) ;
33
31
_registerLocalShortcut ( win , "CommandOrControl+L" , search ) ;
34
- registerCallback ( songInfo => {
35
- if ( player ) {
36
- player . metadata = {
37
- 'mpris:length' : songInfo . songDuration * 60 * 1000 * 1000 , // In microseconds
38
- 'mpris:artUrl' : songInfo . imageSrc ,
39
- 'xesam:title' : songInfo . title ,
40
- 'xesam:artist' : songInfo . artist
41
- } ;
42
- if ( ! songInfo . isPaused ) {
43
- player . playbackStatus = "Playing"
44
- }
45
- }
46
- }
47
- )
48
32
49
- if ( is . linux ( ) ) {
50
- try {
51
- const MPRISPlayer = setupMPRIS ( ) ;
52
-
53
- MPRISPlayer . on ( "raise" , ( ) => {
54
- win . setSkipTaskbar ( false ) ;
55
- win . show ( ) ;
56
- } ) ;
57
- MPRISPlayer . on ( "play" , ( ) => {
58
- if ( MPRISPlayer . playbackStatus !== 'Playing' ) {
59
- MPRISPlayer . playbackStatus = 'Playing' ;
60
- playPause ( )
61
- }
62
- } ) ;
63
- MPRISPlayer . on ( "pause" , ( ) => {
64
- if ( MPRISPlayer . playbackStatus !== 'Paused' ) {
65
- MPRISPlayer . playbackStatus = 'Paused' ;
66
- playPause ( )
67
- }
68
- } ) ;
69
- MPRISPlayer . on ( "next" , ( ) => {
70
- next ( )
71
- } ) ;
72
- MPRISPlayer . on ( "previous" , ( ) => {
73
- previous ( )
74
- } ) ;
75
-
76
- player = MPRISPlayer
77
-
78
- } catch ( e ) {
79
- console . warn ( "Error in MPRIS" , e ) ;
80
- }
81
- }
33
+ if ( is . linux ( ) ) registerMPRIS ( ) ;
82
34
83
35
const { global, local } = options ;
84
36
const shortcutOptions = { global, local } ;
@@ -106,6 +58,71 @@ function registerShortcuts(win, options) {
106
58
}
107
59
}
108
60
}
61
+ function registerMPRIS ( ) {
62
+ try {
63
+ const secToMicro = n => Math . round ( Number ( n ) * ( 1000 * 1000 ) ) ;
64
+ const microToSec = n => Math . round ( Number ( n ) / 1000 / 1000 ) ;
65
+
66
+ const seekTo = e => win . webContents . send ( "seekTo" , microToSec ( e . position ) ) ;
67
+ const seek = o => win . webContents . send ( "seek" , microToSec ( o ) ) ;
68
+
69
+ const player = setupMPRIS ( ) ;
70
+
71
+ const mprisSeek = p => {
72
+ player . seeked ( p ) ;
73
+ }
74
+ win . webContents . send ( "registerOnSeek" ) ;
75
+
76
+ ipcMain . on ( 'seeked' , ( _ , t ) => mprisSeek ( secToMicro ( t ) ) ) ;
77
+
78
+ let currentSeconds = 0 ;
79
+ ipcMain . on ( 'timeChanged' , ( _ , t ) => currentSeconds = t ) ;
80
+
81
+ player . getPosition = ( ) => secToMicro ( currentSeconds )
82
+
83
+ player . on ( "raise" , ( ) => {
84
+ win . setSkipTaskbar ( false ) ;
85
+ win . show ( ) ;
86
+ } ) ;
87
+
88
+ player . on ( "play" , ( ) => {
89
+ if ( player . playbackStatus !== 'Playing' ) {
90
+ player . playbackStatus = 'Playing' ;
91
+ playPause ( )
92
+ }
93
+ } ) ;
94
+ player . on ( "pause" , ( ) => {
95
+ if ( player . playbackStatus !== 'Paused' ) {
96
+ player . playbackStatus = 'Paused' ;
97
+ playPause ( )
98
+ }
99
+ } ) ;
100
+
101
+ player . on ( "playpause" , playPause ) ;
102
+ player . on ( "next" , next ) ;
103
+ player . on ( "previous" , previous ) ;
104
+
105
+ player . on ( 'seek' , seek ) ;
106
+ player . on ( 'position' , seekTo ) ;
107
+
108
+ registerCallback ( songInfo => {
109
+ if ( player ) {
110
+ player . metadata = {
111
+ 'mpris:length' : secToMicro ( songInfo . songDuration ) ,
112
+ 'mpris:artUrl' : songInfo . imageSrc ,
113
+ 'xesam:title' : songInfo . title ,
114
+ 'xesam:artist' : songInfo . artist ,
115
+ 'mpris:trackid' : '/'
116
+ } ; ;
117
+ mprisSeek ( secToMicro ( songInfo . elapsedSeconds ) )
118
+ player . playbackStatus = songInfo . isPaused ? "Paused" : "Playing"
119
+ }
120
+ } )
121
+
122
+ } catch ( e ) {
123
+ console . warn ( "Error in MPRIS" , e ) ;
124
+ }
125
+ }
109
126
}
110
127
111
128
module . exports = registerShortcuts ;
0 commit comments