Clean up thumbar (thumbnail) code (#670)

* Cleanup thumbnail bar code

- rename thumbnail method names for succinctness
- Get rid of 'updateThumbnailBar' event -- use existing events
- Get rid of 'blockPowerSave' and 'unblockPowerSave' events -- use a
new combined 'onPlayerPlay' and 'onPlayerPause' events which apply to
power save and updating the thumbbar

* Consistent naming for enable/disable methods
This commit is contained in:
Feross Aboukhadijeh
2016-06-28 06:32:28 -07:00
committed by DC
parent c44943cef7
commit 349c5ee22e
7 changed files with 81 additions and 68 deletions

View File

@@ -15,7 +15,7 @@ var shell = require('./shell')
var shortcuts = require('./shortcuts')
var vlc = require('./vlc')
var windows = require('./windows')
var thumbnail = require('./thumbnail')
var thumbar = require('./thumbar')
// Messages from the main process, to be sent once the WebTorrent process starts
var messageQueueMainToWebTorrent = []
@@ -61,24 +61,27 @@ function init () {
ipc.on('onPlayerOpen', function () {
menu.onPlayerOpen()
shortcuts.onPlayerOpen()
powerSaveBlocker.enable()
shortcuts.enable()
thumbar.enable()
})
ipc.on('onPlayerClose', function () {
menu.onPlayerClose()
shortcuts.onPlayerOpen()
powerSaveBlocker.disable()
shortcuts.disable()
thumbar.disable()
})
ipc.on('updateThumbnailBar', function (e, isPaused) {
thumbnail.updateThumbarButtons(isPaused)
ipc.on('onPlayerPlay', function () {
powerSaveBlocker.enable()
thumbar.onPlayerPlay()
})
/**
* Power Save Blocker
*/
ipc.on('blockPowerSave', () => powerSaveBlocker.start())
ipc.on('unblockPowerSave', () => powerSaveBlocker.stop())
ipc.on('onPlayerPause', function () {
powerSaveBlocker.disable()
thumbar.onPlayerPause()
})
/**
* Shell

View File

@@ -16,7 +16,6 @@ var config = require('../config')
var dialog = require('./dialog')
var shell = require('./shell')
var windows = require('./windows')
var thumbnail = require('./thumbnail')
var menu
@@ -34,8 +33,6 @@ function onPlayerClose () {
getMenuItem('Increase Speed').enabled = false
getMenuItem('Decrease Speed').enabled = false
getMenuItem('Add Subtitles File...').enabled = false
thumbnail.showPlayerThumbnailBar()
}
function onPlayerOpen () {
@@ -47,8 +44,6 @@ function onPlayerOpen () {
getMenuItem('Increase Speed').enabled = true
getMenuItem('Decrease Speed').enabled = true
getMenuItem('Add Subtitles File...').enabled = true
thumbnail.hidePlayerThumbnailBar()
}
function onToggleAlwaysOnTop (flag) {

View File

@@ -1,6 +1,6 @@
module.exports = {
start,
stop
enable,
disable
}
var electron = require('electron')
@@ -12,19 +12,19 @@ var blockId = 0
* Block the system from entering low-power (sleep) mode or turning off the
* display.
*/
function start () {
stop() // Stop the previous power saver block, if one exists.
function enable () {
disable() // Stop the previous power saver block, if one exists.
blockId = electron.powerSaveBlocker.start('prevent-display-sleep')
log(`powerSaveBlocker.start: ${blockId}`)
log(`powerSaveBlocker.enable: ${blockId}`)
}
/**
* Stop blocking the system from entering low-power mode.
*/
function stop () {
function disable () {
if (!electron.powerSaveBlocker.isStarted(blockId)) {
return
}
electron.powerSaveBlocker.stop(blockId)
log(`powerSaveBlocker.stop: ${blockId}`)
log(`powerSaveBlocker.disable: ${blockId}`)
}

View File

@@ -1,12 +1,12 @@
module.exports = {
onPlayerClose,
onPlayerOpen
disable,
enable
}
var electron = require('electron')
var windows = require('./windows')
function onPlayerOpen () {
function enable () {
// Register play/pause media key, available on some keyboards.
electron.globalShortcut.register(
'MediaPlayPause',
@@ -14,7 +14,7 @@ function onPlayerOpen () {
)
}
function onPlayerClose () {
function disable () {
// Return the media key to the OS, so other apps can use it.
electron.globalShortcut.unregister('MediaPlayPause')
}

54
main/thumbar.js Normal file
View File

@@ -0,0 +1,54 @@
module.exports = {
disable,
enable,
onPlayerPause,
onPlayerPlay
}
/**
* On Windows, add a "thumbnail toolbar" with a play/pause button in the taskbar.
* This provides users a way to access play/pause functionality without restoring
* or activating the window.
*/
var path = require('path')
var config = require('../config')
var windows = require('./windows')
/**
* Show the Windows thumbnail toolbar buttons.
*/
function enable () {
update(false)
}
/**
* Hide the Windows thumbnail toolbar buttons.
*/
function disable () {
windows.main.win.setThumbarButtons([])
}
function onPlayerPause () {
update(true)
}
function onPlayerPlay () {
update(false)
}
function update (isPaused) {
var icon = isPaused
? 'PlayThumbnailBarButton.png'
: 'PauseThumbnailBarButton.png'
var buttons = [
{
tooltip: isPaused ? 'Play' : 'Pause',
icon: path.join(config.STATIC_PATH, icon),
click: () => windows.main.dispatch('playPause')
}
]
windows.main.win.setThumbarButtons(buttons)
}

View File

@@ -1,35 +0,0 @@
module.exports = {
showPlayerThumbnailBar,
hidePlayerThumbnailBar,
updateThumbarButtons
}
var path = require('path')
var config = require('../config')
var windows = require('./windows')
// gets called on player open
function showPlayerThumbnailBar () {
updateThumbarButtons(false)
}
// gets called on player close
function hidePlayerThumbnailBar () {
windows.main.win.setThumbarButtons([])
}
function updateThumbarButtons (isPaused) {
var icon = isPaused ? 'PlayThumbnailBarButton.png' : 'PauseThumbnailBarButton.png'
var tooltip = isPaused ? 'Play' : 'Pause'
var buttons = [
{
tooltip: tooltip,
icon: path.join(config.STATIC_PATH, icon),
click: function () {
windows.main.send('dispatch', 'playPause')
}
}
]
windows.main.win.setThumbarButtons(buttons)
}

View File

@@ -358,7 +358,7 @@ function play () {
if (isCasting()) {
Cast.play()
}
ipcRenderer.send('blockPowerSave')
ipcRenderer.send('onPlayerPlay')
}
function pause () {
@@ -367,7 +367,7 @@ function pause () {
if (isCasting()) {
Cast.pause()
}
ipcRenderer.send('unblockPowerSave')
ipcRenderer.send('onPlayerPause')
}
function playPause () {
@@ -382,8 +382,6 @@ function playPause () {
// force rerendering if window is hidden,
// in order to bypass `raf` and play/pause media immediately
if (!state.window.isVisible) render(state)
ipcRenderer.send('updateThumbnailBar', state.playing.isPaused)
}
function jumpToTime (time) {
@@ -1106,8 +1104,6 @@ function closePlayer (cb) {
// Tell the WebTorrent process to kill the torrent-to-HTTP server
ipcRenderer.send('wt-stop-server')
// Tell the OS we're no longer playing media, laptops allowed to sleep again
ipcRenderer.send('unblockPowerSave')
ipcRenderer.send('onPlayerClose')
update()