Browse Source

refactor: clean up the touch bar implementation to reuse touch bar items

tags/v4.7.1
Samuel Attard 1 year ago
parent
commit
67f8f88a61
No known key found for this signature in database GPG Key ID: E89DDE5742D58C4E
2 changed files with 44 additions and 66 deletions
  1. +44
    -58
      src/main/features/darwin/touchBar.js
  2. +0
    -8
      src/renderer/windows/GPMWebView/playback/controller.js

+ 44
- 58
src/main/features/darwin/touchBar.js View File

@@ -3,88 +3,74 @@ import path from 'path';

const { TouchBarButton, TouchBarSpacer } = TouchBar;

let playing = false;
let play;
let mainWindow;
let thumbsUp;
let thumbsDown;
let renderTouchBar = () => {};
let { liked, disliked } = PlaybackAPI.getRating();
const mediaControls = path.resolve(__dirname, '../../../assets/img/media_controls');

const mediaControls = `${__dirname}/../../../assets/img/media_controls/`;

const _play = () => new TouchBarButton({
icon: path.resolve(`${mediaControls}${(playing ? 'pause' : 'play')}.png`),
const playPauseButton = new TouchBarButton({
icon: path.resolve(mediaControls, 'play.png'),
click: () => {
Emitter.sendToGooglePlayMusic('playback:playPause');
},
});

const _trackButton = (search) => {
const trackButton = new TouchBarButton({
icon: path.resolve(`${mediaControls}${search}.png`),
click: () => {
Emitter.sendToGooglePlayMusic(`playback:${search}Track`);
},
});
return trackButton;
const updatePlayPauseButton = (isPlaying) => {
playPauseButton.icon = path.resolve(mediaControls, `${isPlaying ? 'pause' : 'play'}.png`);
};

const _thumbsUp = () => new TouchBarButton({
icon: path.resolve(`${mediaControls}${liked ? 'filled_' : ''}thumb_up.png`),
const previousButton = new TouchBarButton({
icon: path.resolve(mediaControls, 'previous.png'),
click: () => {
Emitter.sendToGooglePlayMusic('playback:toggleThumbsUp');
Emitter.sendToGooglePlayMusic('playback:previousTrack');
},
});
const nextButton = new TouchBarButton({
icon: path.resolve(mediaControls, 'next.png'),
click: () => {
Emitter.sendToGooglePlayMusic('playback:nextTrack');
},
});

const _thumbsDown = () => new TouchBarButton({
icon: path.resolve(`${mediaControls}${disliked ? 'filled_' : ''}thumb_down.png`),
const thumbsUpButton = new TouchBarButton({
icon: path.resolve(mediaControls, 'thumb_up.png'),
click: () => {
Emitter.sendToGooglePlayMusic('playback:toggleThumbsUp');
},
});
const thumbsDownButton = new TouchBarButton({
icon: path.resolve(mediaControls, 'thumb_down.png'),
click: () => {
Emitter.sendToGooglePlayMusic('playback:toggleThumbsDown');
},
});
const updateThumbButtons = (liked, disliked) => {
thumbsUpButton.icon = path.resolve(mediaControls, `${liked ? 'filled_' : ''}thumb_up.png`);
thumbsDownButton.icon = path.resolve(mediaControls, `${disliked ? 'filled_' : ''}thumb_down.png`);
};


const _getTouchBar = () => {
const prevTrack = _trackButton('previous');
play = _play();
const nextTrack = _trackButton('next');

thumbsUp = _thumbsUp();
thumbsDown = _thumbsDown();

const touchBar = new TouchBar([
prevTrack,
play,
nextTrack,
const touchBar = new TouchBar({
items: [
previousButton,
playPauseButton,
nextButton,
new TouchBarSpacer({ size: 'small' }),
thumbsUp,
thumbsDown,
]);

return touchBar;
};
thumbsUpButton,
thumbsDownButton,
],
});

renderTouchBar = () => {
mainWindow = WindowManager.getAll('main')[0];
if (mainWindow) {
mainWindow.setTouchBar(_getTouchBar());
} else {
// Something went wrong
app.quit();
let attached = false;
const renderTouchBar = () => {
const mainWindow = WindowManager.getAll('main')[0];
if (mainWindow && !attached) {
attached = true;
mainWindow.setTouchBar(touchBar);
}
};

app.on('browser-window-focus', renderTouchBar);

PlaybackAPI.on('change:state', (nextState) => {
playing = nextState;
play.icon = path.resolve(`${mediaControls}${(playing ? 'pause' : 'play')}.png`);
PlaybackAPI.on('change:state', (isPlaying) => {
updatePlayPauseButton(isPlaying);
});

PlaybackAPI.on('change:rating', (newRating) => {
liked = newRating.liked;
thumbsUp.icon = path.resolve(`${mediaControls}${liked ? 'filled_' : ''}thumb_up.png`);
disliked = newRating.disliked;
thumbsDown.icon = path.resolve(`${mediaControls}${disliked ? 'filled_' : ''}thumb_down.png`);
updateThumbButtons(newRating.liked, newRating.disliked);
});

+ 0
- 8
src/renderer/windows/GPMWebView/playback/controller.js View File

@@ -54,10 +54,6 @@ Emitter.on('playback:thumbsUp', () => {

Emitter.on('playback:toggleThumbsUp', () => {
if (!remote.getGlobal('PlaybackAPI').data.song.title) return;
new Notification('You just liked', { // eslint-disable-line
body: remote.getGlobal('PlaybackAPI').data.song.title,
icon: remote.getGlobal('PlaybackAPI').data.song.albumArt,
});
window.GPM.rating.toggleThumbsUp();
});

@@ -72,10 +68,6 @@ Emitter.on('playback:thumbsDown', () => {

Emitter.on('playback:toggleThumbsDown', () => {
if (!remote.getGlobal('PlaybackAPI').data.song.title) return;
new Notification('You just disliked', { // eslint-disable-line
body: remote.getGlobal('PlaybackAPI').data.song.title,
icon: remote.getGlobal('PlaybackAPI').data.song.albumArt,
});
window.GPM.rating.toggleThumbsDown();
});



Loading…
Cancel
Save