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

165 lines
4 KiB
JavaScript
Raw Normal View History

const {EventEmitter} = require('events')
let itemIdIncrementor = 1
class TouchBar extends EventEmitter {
constructor (items) {
super()
if (!Array.isArray(items)) {
throw new Error('The items object provided has to be an array')
}
2017-03-01 18:55:28 +00:00
this.windowListeners = {}
this.items = {}
this.ordereredItems = []
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) => {
this.ordereredItems.push(item)
if (!(item instanceof TouchBarItem)) {
throw new Error('Each item must be an instance of a TouchBarItem')
}
registerItem(item)
})
2017-03-01 18:55:28 +00:00
}
// Called by BrowserWindow.setTouchBar
_addToWindow (window) {
const {id} = window
// Already added to window
if (this.windowListeners.hasOwnProperty(id)) return
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]
}
window.once('closed', removeListeners)
this.windowListeners[id] = removeListeners
window._setTouchBar(this.ordereredItems)
}
// Called by BrowserWindow.setTouchBar
_removeFromWindow (window) {
const removeListeners = this.windowListeners[window.id]
if (removeListeners != null) removeListeners()
}
}
2017-03-01 00:08:12 +00:00
class TouchBarItem extends EventEmitter {
constructor (config) {
2017-03-01 00:08:12 +00:00
super()
this.id = `${itemIdIncrementor++}`
}
}
TouchBar.Button = class TouchBarButton extends TouchBarItem {
constructor (config) {
super(config)
this.type = 'button'
this.label = config.label
this.backgroundColor = config.backgroundColor
this.labelColor = config.labelColor
this.onInteraction = config.click
}
}
TouchBar.ColorPicker = class TouchBarColorPicker extends TouchBarItem {
constructor (config) {
super(config)
this.type = 'colorpicker'
const {change} = config
if (typeof change === 'function') {
this.onInteraction = (details) => {
change(details.color)
}
}
}
}
2016-11-29 07:36:57 +00:00
TouchBar.Group = class TouchBarGroup extends TouchBarItem {
2016-11-28 07:24:48 +00:00
constructor (config) {
super(config)
this.type = 'group'
this.child = config.items
if (!(this.child instanceof TouchBar)) {
this.child = new TouchBar(this.items)
}
2016-11-28 07:24:48 +00:00
}
}
2016-11-29 07:36:57 +00:00
TouchBar.Label = class TouchBarLabel extends TouchBarItem {
constructor (config) {
super(config)
this.type = 'label'
2017-03-01 00:08:12 +00:00
this._label = config.label
}
set label (newLabel) {
this._label = newLabel
this.emit('change')
}
get label () {
return this._label
}
}
TouchBar.PopOver = class TouchBarPopOver extends TouchBarItem {
constructor (config) {
super(config)
this.type = 'popover'
this.label = config.label
this.showCloseButton = config.showCloseButton
this.child = config.items
if (!(this.child instanceof TouchBar)) {
this.child = new TouchBar(this.items)
}
}
}
TouchBar.Slider = class TouchBarSlider extends TouchBarItem {
constructor (config) {
super(config)
this.type = 'slider'
this.minValue = config.minValue
this.maxValue = config.maxValue
this.initialValue = config.initialValue
const {change} = config
if (typeof change === 'function') {
this.onInteraction = (details) => {
change(details.value)
}
}
}
}
2016-11-29 07:55:07 +00:00
module.exports = TouchBar