Skip to content
This repository has been archived by the owner on Nov 22, 2021. It is now read-only.

Fix touchbar and add repost button to it. #233

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added app/res/repost.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
87 changes: 56 additions & 31 deletions app/touch-bar-menu.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict'

const { TouchBar } = require('electron')
const { TouchBar, nativeImage } = require('electron')

const { TouchBarButton, TouchBarLabel, TouchBarSpacer } = TouchBar
const MAX_TITLE_LENGTH = 38
const { TouchBarButton, TouchBarScrubber } = TouchBar

const https = require('https');

module.exports = function touchBarMenu(window, soundcloud) {
const nextTrack = new TouchBarButton({
Expand Down Expand Up @@ -34,41 +35,65 @@ module.exports = function touchBarMenu(window, soundcloud) {
}
})

const trackInfo = new TouchBarLabel()
const repost = new TouchBarButton({
icon: `${__dirname}/res/repost.png`,
click: () => {
soundcloud.repost()
}
})

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original PR included a reposted icon but no code associated with it. If there was a way to detect if a track has already been reposted we can update the icon, but i'm not sure how to do that.

const titleScrubber = new TouchBarScrubber({
continuous: false,
items:[{
label: ''
}]
})

soundcloud.on('play', ({ title, subtitle }) => {
soundcloud.on('play-new-track', ({ title, subtitle, artworkURL }) => {
let displayTitle = `${title} by ${subtitle}`
displayTitle = displayTitle.padEnd(displayTitle.length * 1.3, ' ')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems hacky but it was the only way to not have the string truncated in the touch bar. You can now swipe on the scrubber to see the whole string

titleScrubber.items = [{
label: displayTitle
}]
https.get(artworkURL, res => {
const data = [];
res.on('data', chunk => {
data.push(chunk);
});
res.on('end', () => {
titleScrubber.items = [{
label:''
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this stops the artwork from having rounded corners on the left hand side

},{
icon: nativeImage.createFromBuffer(Buffer.concat(data)).resize({height:30, width:30})
}, {
label: displayTitle
}]
});
}).on('error', () => {
titleScrubber.items = [{
label: displayTitle
}]
});
})

soundcloud.on('play', () => {
playPause.icon = `${__dirname}/res/pause.png`
trackInfo.label = formatTitle(title, subtitle)
})

soundcloud.on('pause', () => {
playPause.icon = `${__dirname}/res/play.png`
})

const touchBar = new TouchBar([
previousTrack,
playPause,
nextTrack,
likeUnlike,
new TouchBarSpacer({ size: 'flexible' }),
trackInfo,
new TouchBarSpacer({ size: 'flexible' })
])
const touchBar = new TouchBar({
items:[
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was the reason why the touchbar wasn't working earlier

previousTrack,
playPause,
nextTrack,
likeUnlike,
repost,
titleScrubber
]
})

window.setTouchBar(touchBar)
}

function formatTitle(title, subtitle) {
const titleAndSubtitle = `${title} by ${subtitle}`
if (titleAndSubtitle.length > MAX_TITLE_LENGTH) {
if (`${title} by X…`.length > MAX_TITLE_LENGTH) {
return truncate(title)
}
return truncate(titleAndSubtitle)
}
return titleAndSubtitle
}

function truncate(text) {
return text.substring(0, MAX_TITLE_LENGTH - 1) + '…'
}
}