electron/lib/browser/api/touch-bar.js

277 lines
7.6 KiB
JavaScript
Raw Normal View History

const {EventEmitter} = require('events')
let nextItemID = 1
class TouchBar extends EventEmitter {
// Bind a touch bar to a window
static _setOnWindow (touchBar, window) {
if (window._touchBar != null) {
window._touchBar._removeFromWindow(window)
}
if (touchBar == null) {
window._setTouchBarItems([])
return
}
if (Array.isArray(touchBar)) {
touchBar = new TouchBar(touchBar)
}
touchBar._addToWindow(window)
}
constructor (items) {
super()
if (!Array.isArray(items)) {
2017-03-03 18:22:25 +00:00
throw new Error('Must specify items array as first argument')
}
2017-03-01 18:55:28 +00:00
this.windowListeners = {}
this.windows = {}
this.items = {}
this.ordereredItems = []
2017-03-02 17:30:21 +00:00
const registerItem = (item) => {
this.items[item.id] = item
2017-03-01 00:08:12 +00:00
item.on('change', () => {
this.emit('change', item.id, item.type)
})
if (item.child instanceof TouchBar) {
item.child.ordereredItems.forEach(registerItem)
}
}
items.forEach((item) => {
if (!(item instanceof TouchBarItem)) {
2017-03-03 18:22:25 +00:00
throw new Error('Each item must be an instance of TouchBarItem')
}
2017-03-02 17:30:21 +00:00
this.ordereredItems.push(item)
registerItem(item)
})
2017-03-01 18:55:28 +00:00
}
2017-03-29 19:41:49 +00:00
setEscapeItem (item) {
if (!item) item = {}
Object.keys(this.windows).forEach((windowID) => {
const window = this.windows[windowID]
window._setEscapeTouchBarItem(item)
})
2017-03-29 04:11:39 +00:00
this._escape = item
}
2017-03-01 18:55:28 +00:00
_addToWindow (window) {
const {id} = window
// Already added to window
if (this.windowListeners.hasOwnProperty(id)) return
window._touchBar = this
2017-03-01 18:55:28 +00:00
const changeListener = (itemID) => {
window._refreshTouchBarItem(itemID)
}
this.on('change', changeListener)
const interactionListener = (event, itemID, details) => {
const item = this.items[itemID]
if (item != null && item.onInteraction != null) {
item.onInteraction(details)
}
2017-03-01 18:55:28 +00:00
}
window.on('-touch-bar-interaction', interactionListener)
const removeListeners = () => {
this.removeListener('change', changeListener)
window.removeListener('-touch-bar-interaction', interactionListener)
window.removeListener('closed', removeListeners)
window._touchBar = null
delete this.windowListeners[id]
delete this.windows[id]
2017-03-01 18:55:28 +00:00
}
window.once('closed', removeListeners)
this.windowListeners[id] = removeListeners
2017-03-29 04:11:39 +00:00
this.windows[id] = window
2017-03-01 18:55:28 +00:00
window._setTouchBarItems(this.ordereredItems)
if (this._escape) window._setEscapeTouchBarItem(this._escape)
2017-03-01 18:55:28 +00:00
}
_removeFromWindow (window) {
const removeListeners = this.windowListeners[window.id]
if (removeListeners != null) removeListeners()
}
}
2017-03-01 00:08:12 +00:00
class TouchBarItem extends EventEmitter {
2017-03-02 22:29:59 +00:00
constructor () {
2017-03-01 00:08:12 +00:00
super()
this.id = `${nextItemID++}`
}
2017-03-01 22:54:43 +00:00
_addLiveProperty (name, initialValue) {
const privateName = `_${name}`
this[privateName] = initialValue
Object.defineProperty(this, name, {
get: function () {
return this[privateName]
},
set: function (value) {
this[privateName] = value
this.emit('change')
},
enumerable: true
})
}
}
TouchBar.TouchBarButton = class TouchBarButton extends TouchBarItem {
constructor (config) {
2017-03-02 22:29:59 +00:00
super()
if (config == null) config = {}
this.type = 'button'
const {click, icon, label, backgroundColor} = config
2017-03-01 22:54:43 +00:00
this._addLiveProperty('label', label)
this._addLiveProperty('backgroundColor', backgroundColor)
this._addLiveProperty('icon', icon)
2017-03-01 22:54:43 +00:00
if (typeof click === 'function') {
2017-03-03 18:22:25 +00:00
this.onInteraction = () => {
config.click()
}
2017-03-01 22:54:43 +00:00
}
}
}
TouchBar.TouchBarColorPicker = class TouchBarColorPicker extends TouchBarItem {
constructor (config) {
2017-03-02 22:29:59 +00:00
super()
if (config == null) config = {}
this.type = 'colorpicker'
2017-03-01 22:54:43 +00:00
const {availableColors, change, selectedColor} = config
this._addLiveProperty('availableColors', availableColors)
this._addLiveProperty('selectedColor', selectedColor)
if (typeof change === 'function') {
this.onInteraction = (details) => {
2017-03-01 22:54:43 +00:00
this._selectedColor = details.color
change(details.color)
}
}
}
}
TouchBar.TouchBarGroup = class TouchBarGroup extends TouchBarItem {
2016-11-28 07:24:48 +00:00
constructor (config) {
2017-03-02 22:29:59 +00:00
super()
if (config == null) config = {}
this.type = 'group'
this.child = config.items
if (!(this.child instanceof TouchBar)) {
this.child = new TouchBar(this.child)
}
2016-11-28 07:24:48 +00:00
}
}
TouchBar.TouchBarLabel = class TouchBarLabel extends TouchBarItem {
constructor (config) {
2017-03-02 22:29:59 +00:00
super()
if (config == null) config = {}
this.type = 'label'
2017-03-01 22:54:43 +00:00
this._addLiveProperty('label', config.label)
this._addLiveProperty('textColor', config.textColor)
}
}
TouchBar.TouchBarPopover = class TouchBarPopover extends TouchBarItem {
constructor (config) {
2017-03-02 22:29:59 +00:00
super()
if (config == null) config = {}
this.type = 'popover'
2017-03-01 22:54:43 +00:00
this._addLiveProperty('label', config.label)
2017-03-02 21:37:34 +00:00
this._addLiveProperty('icon', config.icon)
this.showCloseButton = config.showCloseButton
this.child = config.items
if (!(this.child instanceof TouchBar)) {
this.child = new TouchBar(this.child)
}
}
}
TouchBar.TouchBarSlider = class TouchBarSlider extends TouchBarItem {
constructor (config) {
2017-03-02 22:29:59 +00:00
super()
if (config == null) config = {}
this.type = 'slider'
2017-03-01 22:54:43 +00:00
const {change, label, minValue, maxValue, value} = config
this._addLiveProperty('label', label)
this._addLiveProperty('minValue', minValue)
this._addLiveProperty('maxValue', maxValue)
this._addLiveProperty('value', value)
if (typeof change === 'function') {
this.onInteraction = (details) => {
2017-03-01 22:54:43 +00:00
this._value = details.value
change(details.value)
}
}
}
}
2017-03-03 17:54:46 +00:00
TouchBar.TouchBarSpacer = class TouchBarSpacer extends TouchBarItem {
constructor (config) {
super()
if (config == null) config = {}
this.type = 'spacer'
this.size = config.size
}
}
2017-03-10 06:40:39 +00:00
TouchBar.TouchBarSegmentedControl = class TouchBarSegmentedControl extends TouchBarItem {
constructor (config) {
super()
if (config == null) config = {}
const {segmentStyle, segments, selectedIndex, change} = config
this.type = 'segmented_control'
2017-03-10 06:56:26 +00:00
this._addLiveProperty('segmentStyle', segmentStyle)
this._addLiveProperty('segments', segments || [])
2017-03-10 06:40:39 +00:00
this._addLiveProperty('selectedIndex', selectedIndex)
if (typeof change === 'function') {
this.onInteraction = (details) => {
2017-03-10 06:56:26 +00:00
this._selectedIndex = details.selectedIndex
change(details.selectedIndex)
2017-03-10 06:40:39 +00:00
}
}
}
}
2017-03-12 23:51:12 +00:00
TouchBar.TouchBarScrubber = class TouchBarScrubber extends TouchBarItem {
2017-03-13 00:00:10 +00:00
constructor (config) {
2017-03-12 23:51:12 +00:00
super()
if (config == null) config = {}
2017-03-15 16:47:07 +00:00
const {items, selectedStyle, overlayStyle, showArrowButtons, continuous, mode} = config
2017-03-14 21:02:48 +00:00
let {select, highlight} = config
2017-03-12 23:51:12 +00:00
this.type = 'scrubber'
this._addLiveProperty('items', items)
2017-03-14 07:57:57 +00:00
this._addLiveProperty('selectedStyle', selectedStyle || null)
2017-03-15 16:47:07 +00:00
this._addLiveProperty('overlayStyle', overlayStyle || null)
2017-03-14 07:57:57 +00:00
this._addLiveProperty('showArrowButtons', showArrowButtons || false)
this._addLiveProperty('mode', mode || 'free')
this._addLiveProperty('continuous', continuous || true)
2017-03-12 23:51:12 +00:00
2017-03-14 21:02:48 +00:00
if (typeof select === 'function' || typeof highlight === 'function') {
if (select == null) select = () => {}
if (highlight == null) highlight = () => {}
2017-03-12 23:51:12 +00:00
this.onInteraction = (details) => {
if (details.type === 'select') {
2017-03-14 21:02:48 +00:00
select(details.selectedIndex)
2017-03-12 23:51:12 +00:00
} else if (details.type === 'highlight') {
2017-03-14 21:02:48 +00:00
highlight(details.highlightedIndex)
2017-03-12 23:51:12 +00:00
}
}
}
}
}
2016-11-29 07:55:07 +00:00
module.exports = TouchBar