Skip to content
This repository was archived by the owner on Feb 5, 2021. It is now read-only.

Commit 6462736

Browse files
authored
Merge pull request #20 from eNkru/feature/media-keyboard
Feature/media keyboard
2 parents ad1a37f + 34ed8a6 commit 6462736

File tree

4 files changed

+74
-10
lines changed

4 files changed

+74
-10
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
"author": "Howard Ju <howard.ju@outlook.com>",
2424
"license": "MIT",
2525
"devDependencies": {
26-
"electron": "~1.7.10",
26+
"electron": "~1.8.2",
2727
"electron-builder": "^19.8.0"
2828
},
2929
"dependencies": {
30+
"dbus-native": "^0.2.5",
3031
"electron-dl": "^1.10.0",
3132
"electron-json-storage": "^4.0.2",
3233
"electron-settings": "^3.1.1",

src/controller/app-tray-controller.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class AppTray {
2424
//set the context menu
2525
const context = Menu.buildFromTemplate([
2626
{label: Locale.TRAY_SHOW_MAIN, click: () => this.togglePlayerWindow()},
27-
{label: Locale.TRAY_PLAY_PAUSE, click: () => this.togglePlay()},
27+
{label: Locale.TRAY_PLAY_PAUSE, click: () => this.playerController.toggle()},
2828
{label: Locale.TRAY_NEXT, click: () => this.playerController.next()},
2929
{label: Locale.TRAY_PREVIOUS, click: () => this.playerController.previous()},
3030
{label: 'Separator', type: 'separator'},
@@ -61,12 +61,6 @@ class AppTray {
6161
}
6262
}
6363

64-
togglePlay() {
65-
this.playerController.window.webContents.executeJavaScript("document.querySelector('.pause-btn')", (result) => {
66-
result ? this.playerController.pause() : this.playerController.play();
67-
});
68-
}
69-
7064
toggleLyrics() {
7165
if (!this.lyricsController.window.isVisible()) {
7266
this.playerController.addPlaytimeObserver();

src/controller/player-controller.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const {app, BrowserWindow, Notification, ipcMain} = require('electron');
1+
const {app, BrowserWindow, Notification, ipcMain, TouchBar, nativeImage} = require('electron');
2+
const {TouchBarLabel, TouchBarButton} = TouchBar
23
const urlLib = require('url');
34
const http = require('http');
45
const path = require('path');
@@ -74,6 +75,9 @@ class XiamiPlayer {
7475
// load xiami player page.
7576
this.window.loadURL(playerUrl);
7677

78+
// set the touch bar.
79+
this.window.setTouchBar(this.createTouchBar());
80+
7781
// inject the custom layout.
7882
this.window.webContents.on('dom-ready', () => {
7983

@@ -160,6 +164,12 @@ class XiamiPlayer {
160164
this.window.webContents.executeJavaScript("document.querySelector('.play-btn').dispatchEvent(new MouseEvent('click'));");
161165
}
162166

167+
toggle() {
168+
this.window.webContents.executeJavaScript("document.querySelector('.pause-btn')", (result) => {
169+
result ? this.pause() : this.play();
170+
});
171+
}
172+
163173
next() {
164174
this.window.webContents.executeJavaScript("document.querySelector('.next-btn').dispatchEvent(new MouseEvent('click'));");
165175
}
@@ -168,6 +178,26 @@ class XiamiPlayer {
168178
this.window.webContents.executeJavaScript("document.querySelector('.prev-btn').dispatchEvent(new MouseEvent('click'));");
169179
}
170180

181+
/**
182+
* Create the touch bar for macOS
183+
*/
184+
createTouchBar() {
185+
return new TouchBar([
186+
new TouchBarButton({
187+
icon: nativeImage.createFromNamedImage('NSTouchBarRewindTemplate', [-1, 0, 1]),
188+
click: () => this.previous()
189+
}),
190+
new TouchBarButton({
191+
icon: nativeImage.createFromNamedImage('NSTouchBarPlayPauseTemplate', [-1, 0, 1]),
192+
click: () => this.toggle()
193+
}),
194+
new TouchBarButton({
195+
icon: nativeImage.createFromNamedImage('NSTouchBarFastForwardTemplate', [-1, 0, 1]),
196+
click: () => this.next()
197+
})
198+
]);
199+
}
200+
171201
/**
172202
* Add the listener to monitor the play time.
173203
*/

src/main.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
const {app} = require('electron');
1+
const {app, globalShortcut} = require('electron');
22
const fs = require('fs-extra')
3+
const dbus = require('dbus-native');
34
const PlayerController = require('./controller/player-controller');
45
const SettingsController = require('./controller/settings-controller');
56
const AppTray = require('./controller/app-tray-controller');
@@ -40,6 +41,9 @@ class ElectronXiami {
4041
this.createSettings();
4142
this.createPlayer(this.lyricsController);
4243
this.createTray(this.settingsController, this.lyricsController, this.playerController);
44+
45+
this.registerMediaKeys('gnome');
46+
this.registerMediaKeys('mate');
4347
});
4448

4549
// Quit when all windows are closed.
@@ -84,6 +88,41 @@ class ElectronXiami {
8488
createTray(settingController, lyricsController, playerController) {
8589
this.tray = new AppTray(playerController, settingController, lyricsController);
8690
}
91+
92+
registerMediaKeys(desktopEnvironment) {
93+
try {
94+
const sessionBus = dbus.sessionBus();
95+
sessionBus.getService(`org.${desktopEnvironment}.SettingsDaemon.MediaKeys`).getInterface(
96+
`/org/${desktopEnvironment}/SettingsDaemon/MediaKeys`,
97+
`org.${desktopEnvironment}.SettingsDaemon.MediaKeys`, (error, mediaKeys) => {
98+
if(!error) {
99+
mediaKeys.on('MediaPlayerKeyPressed', (n, keyName) => {
100+
switch (keyName) {
101+
case 'Next': this.playerController.next(); return;
102+
case 'Previous': this.playerController.previous(); return;
103+
case 'Play': this.playerController.toggle(); return;
104+
case 'Stop': this.playerController.pause(); return;
105+
}
106+
});
107+
108+
mediaKeys.GrabMediaPlayerKeys('org.gnome.SettingsDaemon.MediaKeys', 0);
109+
} else {
110+
this.addMediaGlobalShortcut();
111+
}
112+
}
113+
)
114+
} catch (error) {
115+
this.addMediaGlobalShortcut();
116+
}
117+
}
118+
119+
addMediaGlobalShortcut() {
120+
globalShortcut.register('MediaPlayPause', () => this.playerController.toggle());
121+
globalShortcut.register('MediaNextTrack', () => this.playerController.next());
122+
globalShortcut.register('MediaPreviousTrack', () => this.playerController.previous());
123+
globalShortcut.register('MediaStop', () => this.playerController.pause());
124+
}
125+
87126
}
88127

89128
new ElectronXiami().init();

0 commit comments

Comments
 (0)