Make escape item a property instead of setter
This commit is contained in:
parent
21c1ddffb3
commit
b24b4212c5
3 changed files with 23 additions and 21 deletions
|
@ -14,16 +14,15 @@ Creates a new touch bar with the specified items. Use
|
||||||
**Note:** The TouchBar API is currently experimental and may change or be
|
**Note:** The TouchBar API is currently experimental and may change or be
|
||||||
removed in future Electron releases.
|
removed in future Electron releases.
|
||||||
|
|
||||||
### Instance Methods
|
### Instance Properties
|
||||||
|
|
||||||
The following methods are available on instances of `TouchBar`:
|
The following properties are available on instances of `TouchBar`:
|
||||||
|
|
||||||
#### `touchBar.setEscapeItem([touchBarItem])`
|
#### `touchBar.escapeItem`
|
||||||
|
|
||||||
* `touchBarItem` (TouchBarButton | TouchBarColorPicker | TouchBarGroup | TouchBarLabel | TouchBarPopover | TouchBarScrubber | TouchBarSegmentedControl | TouchBarSlider | TouchBarSpacer) - (Optional) The touch bar item to replace the escape button with
|
The `TouchBarItem` that will replace the "esc" button on the touch bar when set.
|
||||||
|
Setting to `null` restores the default "esc" button. Changing this value
|
||||||
Replaces the "esc" button on the touchbar with the given TouchBarItem, if `touchBarItem` is not provided or is falsey the button is reset
|
immediately updates the escape item in the touch bar.
|
||||||
to the "esc" button automatically.
|
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
|
|
|
@ -2,8 +2,6 @@ const {EventEmitter} = require('events')
|
||||||
|
|
||||||
let nextItemID = 1
|
let nextItemID = 1
|
||||||
|
|
||||||
const DEFAULT_ESCAPE_ITEM = new EventEmitter()
|
|
||||||
|
|
||||||
class TouchBar extends EventEmitter {
|
class TouchBar extends EventEmitter {
|
||||||
// Bind a touch bar to a window
|
// Bind a touch bar to a window
|
||||||
static _setOnWindow (touchBar, window) {
|
static _setOnWindow (touchBar, window) {
|
||||||
|
@ -32,7 +30,7 @@ class TouchBar extends EventEmitter {
|
||||||
this.windowListeners = {}
|
this.windowListeners = {}
|
||||||
this.items = {}
|
this.items = {}
|
||||||
this.ordereredItems = []
|
this.ordereredItems = []
|
||||||
this.escapeItem = DEFAULT_ESCAPE_ITEM
|
this.escapeItem = null
|
||||||
this.changeListener = (item) => {
|
this.changeListener = (item) => {
|
||||||
this.emit('change', item.id, item.type)
|
this.emit('change', item.id, item.type)
|
||||||
}
|
}
|
||||||
|
@ -53,19 +51,24 @@ class TouchBar extends EventEmitter {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
setEscapeItem (item) {
|
set escapeItem (item) {
|
||||||
if (item != null && !(item instanceof TouchBarItem)) {
|
if (item != null && !(item instanceof TouchBarItem)) {
|
||||||
throw new Error('Escape item must be an instance of TouchBarItem')
|
throw new Error('Escape item must be an instance of TouchBarItem')
|
||||||
}
|
}
|
||||||
if (item == null) {
|
if (this.escapeItem != null) {
|
||||||
item = DEFAULT_ESCAPE_ITEM
|
this.escapeItem.removeListener('change', this.changeListener)
|
||||||
|
}
|
||||||
|
this._escapeItem = item
|
||||||
|
if (this.escapeItem != null) {
|
||||||
|
this.escapeItem.on('change', this.changeListener)
|
||||||
}
|
}
|
||||||
this.escapeItem.removeListener('change', this.changeListener)
|
|
||||||
this.escapeItem = item
|
|
||||||
this.escapeItem.on('change', this.changeListener)
|
|
||||||
this.emit('escape-item-change', item)
|
this.emit('escape-item-change', item)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get escapeItem () {
|
||||||
|
return this._escapeItem
|
||||||
|
}
|
||||||
|
|
||||||
_addToWindow (window) {
|
_addToWindow (window) {
|
||||||
const {id} = window
|
const {id} = window
|
||||||
|
|
||||||
|
@ -80,7 +83,7 @@ class TouchBar extends EventEmitter {
|
||||||
this.on('change', changeListener)
|
this.on('change', changeListener)
|
||||||
|
|
||||||
const escapeItemListener = (item) => {
|
const escapeItemListener = (item) => {
|
||||||
window._setEscapeTouchBarItem(item)
|
window._setEscapeTouchBarItem(item != null ? item : {})
|
||||||
}
|
}
|
||||||
this.on('escape-item-change', escapeItemListener)
|
this.on('escape-item-change', escapeItemListener)
|
||||||
|
|
||||||
|
@ -107,7 +110,7 @@ class TouchBar extends EventEmitter {
|
||||||
this.windowListeners[id] = removeListeners
|
this.windowListeners[id] = removeListeners
|
||||||
|
|
||||||
window._setTouchBarItems(this.ordereredItems)
|
window._setTouchBarItems(this.ordereredItems)
|
||||||
window._setEscapeTouchBarItem(this.escapeItem)
|
escapeItemListener(this.escapeItem)
|
||||||
}
|
}
|
||||||
|
|
||||||
_removeFromWindow (window) {
|
_removeFromWindow (window) {
|
||||||
|
|
|
@ -23,7 +23,7 @@ describe('TouchBar module', function () {
|
||||||
it('throws an error when an invalid escape item is set', function () {
|
it('throws an error when an invalid escape item is set', function () {
|
||||||
assert.throws(() => {
|
assert.throws(() => {
|
||||||
const touchBar = new TouchBar([])
|
const touchBar = new TouchBar([])
|
||||||
touchBar.setEscapeItem('esc')
|
touchBar.escapeItem = 'esc'
|
||||||
}, /Escape item must be an instance of TouchBarItem/)
|
}, /Escape item must be an instance of TouchBarItem/)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -66,12 +66,12 @@ describe('TouchBar module', function () {
|
||||||
label: 'foo'
|
label: 'foo'
|
||||||
})
|
})
|
||||||
window.setTouchBar(touchBar)
|
window.setTouchBar(touchBar)
|
||||||
touchBar.setEscapeItem(escapeButton)
|
touchBar.escapeItem = escapeButton
|
||||||
label.label = 'baz'
|
label.label = 'baz'
|
||||||
escapeButton.label = 'hello'
|
escapeButton.label = 'hello'
|
||||||
window.setTouchBar()
|
window.setTouchBar()
|
||||||
window.setTouchBar(new TouchBar([new TouchBarLabel({label: 'two'})]))
|
window.setTouchBar(new TouchBar([new TouchBarLabel({label: 'two'})]))
|
||||||
touchBar.setEscapeItem()
|
touchBar.escapeItem = null
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue