|
@@ -1,82 +1,67 @@
|
|
|
-class TouchBar {
|
|
|
+const {EventEmitter} = require('events')
|
|
|
+
|
|
|
+let itemIdIncrementor = 1
|
|
|
+
|
|
|
+class TouchBar extends EventEmitter {
|
|
|
constructor (items) {
|
|
|
- this.items = items
|
|
|
+ super()
|
|
|
+
|
|
|
if (!Array.isArray(items)) {
|
|
|
throw new Error('The items object provided has to be an array')
|
|
|
}
|
|
|
+
|
|
|
+ this.items = {}
|
|
|
+ this.ordereredItems = []
|
|
|
+ const registerItem = (item) => {
|
|
|
+ this.items[item.id] = item
|
|
|
+ if (item.child instanceof TouchBar) {
|
|
|
+ item.child.ordereredItems.forEach(registerItem)
|
|
|
+ }
|
|
|
+ }
|
|
|
items.forEach((item) => {
|
|
|
- if (!item.id) {
|
|
|
+ this.ordereredItems.push(item)
|
|
|
+ if (!(item instanceof TouchBarItem)) {
|
|
|
throw new Error('Each item must be an instance of a TouchBarItem')
|
|
|
}
|
|
|
+ registerItem(item)
|
|
|
})
|
|
|
- }
|
|
|
-
|
|
|
- toJSON () {
|
|
|
- return this.items.map((item) => item.toJSON ? item.toJSON() : item)
|
|
|
- }
|
|
|
-}
|
|
|
|
|
|
-let itemIdIncrementor = 1
|
|
|
-const itemEventHandlers = {}
|
|
|
-
|
|
|
-TouchBar._event = (itemType, eventArgs) => {
|
|
|
- let args = eventArgs.slice(1)
|
|
|
- if (itemType === 'slider') {
|
|
|
- args = args.map(val => parseInt(val, 10))
|
|
|
+ this.on('interaction', (itemID, details) => {
|
|
|
+ const item = this.items[itemID]
|
|
|
+ if (item != null && item.onInteraction != null) {
|
|
|
+ item.onInteraction(details)
|
|
|
+ }
|
|
|
+ })
|
|
|
}
|
|
|
- const idParts = eventArgs[0].split('.')
|
|
|
- const itemId = idParts[idParts.length - 1]
|
|
|
- if (itemEventHandlers[itemId]) itemEventHandlers[itemId](...args)
|
|
|
}
|
|
|
|
|
|
class TouchBarItem {
|
|
|
constructor (config) {
|
|
|
- this.id = itemIdIncrementor++
|
|
|
- const mConfig = Object.assign({}, config || {})
|
|
|
- Object.defineProperty(this, 'config', {
|
|
|
- configurable: false,
|
|
|
- enumerable: false,
|
|
|
- get: () => mConfig
|
|
|
- })
|
|
|
- this.config.id = `${this.config.id || this.id}`
|
|
|
- if (typeof this.config !== 'object' || this.config === null) {
|
|
|
- throw new Error('Provided config must be a non-null object')
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- updateConfig (newConfig) {
|
|
|
- if (!this._owner) {
|
|
|
- throw new Error('Cannot call methods on TouchBarItems without assigning to a BrowserWindow')
|
|
|
- }
|
|
|
- const dupConfig = Object.assign({}, newConfig)
|
|
|
- delete dupConfig.id
|
|
|
- Object.assign(this.config, dupConfig)
|
|
|
- this._owner._updateTouchBarItem(this.toJSON())
|
|
|
- }
|
|
|
-
|
|
|
- toJSON () {
|
|
|
- return this.config
|
|
|
+ this.id = `${itemIdIncrementor++}`
|
|
|
}
|
|
|
}
|
|
|
|
|
|
TouchBar.Button = class TouchBarButton extends TouchBarItem {
|
|
|
constructor (config) {
|
|
|
super(config)
|
|
|
- this.config.type = 'button'
|
|
|
- const click = config.click
|
|
|
- if (typeof click === 'function') {
|
|
|
- itemEventHandlers[`${this.id}`] = click
|
|
|
- }
|
|
|
+ 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.config.type = 'colorpicker'
|
|
|
- const change = this.config.change
|
|
|
+ this.type = 'colorpicker'
|
|
|
+
|
|
|
+ const {change} = config
|
|
|
if (typeof change === 'function') {
|
|
|
- itemEventHandlers[`${this.id}`] = change
|
|
|
+ this.onInteraction = (details) => {
|
|
|
+ change(details.color)
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -84,45 +69,48 @@ TouchBar.ColorPicker = class TouchBarColorPicker extends TouchBarItem {
|
|
|
TouchBar.Group = class TouchBarGroup extends TouchBarItem {
|
|
|
constructor (config) {
|
|
|
super(config)
|
|
|
- this.config.type = 'group'
|
|
|
- }
|
|
|
-
|
|
|
- toJSON () {
|
|
|
- const config = this.config
|
|
|
- return Object.assign({}, config, {
|
|
|
- items: config.items && config.items.toJSON ? config.items.toJSON() : []
|
|
|
- })
|
|
|
+ this.type = 'group'
|
|
|
+ this.child = config.items
|
|
|
+ if (!(this.child instanceof TouchBar)) {
|
|
|
+ this.child = new TouchBar(this.items)
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
TouchBar.Label = class TouchBarLabel extends TouchBarItem {
|
|
|
constructor (config) {
|
|
|
super(config)
|
|
|
- this.config.type = 'label'
|
|
|
+ this.type = 'label'
|
|
|
+ this.label = config.label
|
|
|
}
|
|
|
}
|
|
|
|
|
|
TouchBar.PopOver = class TouchBarPopOver extends TouchBarItem {
|
|
|
constructor (config) {
|
|
|
super(config)
|
|
|
- this.config.type = 'popover'
|
|
|
- }
|
|
|
-
|
|
|
- toJSON () {
|
|
|
- const config = this.config
|
|
|
- return Object.assign({}, config, {
|
|
|
- touchBar: config.touchBar && config.touchBar.toJSON ? config.touchBar.toJSON() : []
|
|
|
- })
|
|
|
+ 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.config.type = 'slider'
|
|
|
- const change = this.config.change
|
|
|
+ this.type = 'slider'
|
|
|
+ this.minValue = config.minValue
|
|
|
+ this.maxValue = config.maxValue
|
|
|
+ this.initialValue = config.initialValue
|
|
|
+
|
|
|
+ const {change} = config
|
|
|
if (typeof change === 'function') {
|
|
|
- itemEventHandlers[this.id] = change
|
|
|
+ this.onInteraction = (details) => {
|
|
|
+ change(details.value)
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|