Update window via listener when escape item changes
This commit is contained in:
		
					parent
					
						
							
								0501a20fe6
							
						
					
				
			
			
				commit
				
					
						591cd8d073
					
				
			
		
					 2 changed files with 21 additions and 11 deletions
				
			
		| 
						 | 
				
			
			@ -28,9 +28,9 @@ class TouchBar extends EventEmitter {
 | 
			
		|||
    }
 | 
			
		||||
 | 
			
		||||
    this.windowListeners = {}
 | 
			
		||||
    this.windows = {}
 | 
			
		||||
    this.items = {}
 | 
			
		||||
    this.ordereredItems = []
 | 
			
		||||
    this.escapeItem = {}
 | 
			
		||||
 | 
			
		||||
    const registerItem = (item) => {
 | 
			
		||||
      this.items[item.id] = item
 | 
			
		||||
| 
						 | 
				
			
			@ -51,12 +51,12 @@ class TouchBar extends EventEmitter {
 | 
			
		|||
  }
 | 
			
		||||
 | 
			
		||||
  setEscapeItem (item) {
 | 
			
		||||
    if (!item) item = {}
 | 
			
		||||
    Object.keys(this.windows).forEach((windowID) => {
 | 
			
		||||
      const window = this.windows[windowID]
 | 
			
		||||
      window._setEscapeTouchBarItem(item)
 | 
			
		||||
    })
 | 
			
		||||
    this._escape = item
 | 
			
		||||
    if (item != null && !(item instanceof TouchBarItem)) {
 | 
			
		||||
      throw new Error('Escape item must be an instance of TouchBarItem')
 | 
			
		||||
    }
 | 
			
		||||
    if (item == null) item = {}
 | 
			
		||||
    this.escapeItem = item
 | 
			
		||||
    this.emit('escape-item-change', item)
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  _addToWindow (window) {
 | 
			
		||||
| 
						 | 
				
			
			@ -72,6 +72,11 @@ class TouchBar extends EventEmitter {
 | 
			
		|||
    }
 | 
			
		||||
    this.on('change', changeListener)
 | 
			
		||||
 | 
			
		||||
    const escapeItemListener = (item) => {
 | 
			
		||||
      window._setEscapeTouchBarItem(item)
 | 
			
		||||
    }
 | 
			
		||||
    this.on('escape-item-change', escapeItemListener)
 | 
			
		||||
 | 
			
		||||
    const interactionListener = (event, itemID, details) => {
 | 
			
		||||
      const item = this.items[itemID]
 | 
			
		||||
      if (item != null && item.onInteraction != null) {
 | 
			
		||||
| 
						 | 
				
			
			@ -82,19 +87,17 @@ class TouchBar extends EventEmitter {
 | 
			
		|||
 | 
			
		||||
    const removeListeners = () => {
 | 
			
		||||
      this.removeListener('change', changeListener)
 | 
			
		||||
      this.removeListener('escape-item-change', escapeItemListener)
 | 
			
		||||
      window.removeListener('-touch-bar-interaction', interactionListener)
 | 
			
		||||
      window.removeListener('closed', removeListeners)
 | 
			
		||||
      window._touchBar = null
 | 
			
		||||
      delete this.windowListeners[id]
 | 
			
		||||
      delete this.windows[id]
 | 
			
		||||
    }
 | 
			
		||||
    window.once('closed', removeListeners)
 | 
			
		||||
    this.windowListeners[id] = removeListeners
 | 
			
		||||
    this.windows[id] = window
 | 
			
		||||
 | 
			
		||||
    window._setTouchBarItems(this.ordereredItems)
 | 
			
		||||
 | 
			
		||||
    if (this._escape) window._setEscapeTouchBarItem(this._escape)
 | 
			
		||||
    window._setEscapeTouchBarItem(this.escapeItem)
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  _removeFromWindow (window) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -20,6 +20,13 @@ describe('TouchBar module', function () {
 | 
			
		|||
    }, /Each item must be an instance of TouchBarItem/)
 | 
			
		||||
  })
 | 
			
		||||
 | 
			
		||||
  it('throws an error when an invalid escape item is set', function () {
 | 
			
		||||
    assert.throws(() => {
 | 
			
		||||
      const touchBar = new TouchBar([])
 | 
			
		||||
      touchBar.setEscapeItem('esc')
 | 
			
		||||
    }, /Escape item must be an instance of TouchBarItem/)
 | 
			
		||||
  })
 | 
			
		||||
 | 
			
		||||
  describe('BrowserWindow behavior', function () {
 | 
			
		||||
    let window
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue