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

View File

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

View File

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

View File

@@ -1,12 +1,12 @@
module.exports = { module.exports = {
onPlayerClose, disable,
onPlayerOpen enable
} }
var electron = require('electron') var electron = require('electron')
var windows = require('./windows') var windows = require('./windows')
function onPlayerOpen () { function enable () {
// Register play/pause media key, available on some keyboards. // Register play/pause media key, available on some keyboards.
electron.globalShortcut.register( electron.globalShortcut.register(
'MediaPlayPause', 'MediaPlayPause',
@@ -14,7 +14,7 @@ function onPlayerOpen () {
) )
} }
function onPlayerClose () { function disable () {
// Return the media key to the OS, so other apps can use it. // Return the media key to the OS, so other apps can use it.
electron.globalShortcut.unregister('MediaPlayPause') 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()) { if (isCasting()) {
Cast.play() Cast.play()
} }
ipcRenderer.send('blockPowerSave') ipcRenderer.send('onPlayerPlay')
} }
function pause () { function pause () {
@@ -367,7 +367,7 @@ function pause () {
if (isCasting()) { if (isCasting()) {
Cast.pause() Cast.pause()
} }
ipcRenderer.send('unblockPowerSave') ipcRenderer.send('onPlayerPause')
} }
function playPause () { function playPause () {
@@ -382,8 +382,6 @@ function playPause () {
// force rerendering if window is hidden, // force rerendering if window is hidden,
// in order to bypass `raf` and play/pause media immediately // in order to bypass `raf` and play/pause media immediately
if (!state.window.isVisible) render(state) if (!state.window.isVisible) render(state)
ipcRenderer.send('updateThumbnailBar', state.playing.isPaused)
} }
function jumpToTime (time) { function jumpToTime (time) {
@@ -1106,8 +1104,6 @@ function closePlayer (cb) {
// Tell the WebTorrent process to kill the torrent-to-HTTP server // Tell the WebTorrent process to kill the torrent-to-HTTP server
ipcRenderer.send('wt-stop-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') ipcRenderer.send('onPlayerClose')
update() update()