| 
									
										
										
										
											2017-07-26 13:02:40 -07:00
										 |  |  |  | ## Class: TouchBar
 | 
					
						
							| 
									
										
										
										
											2016-12-02 23:58:02 +11:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | > Create TouchBar layouts for native macOS applications
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-09 17:58:10 -03:00
										 |  |  |  | Process: [Main](../tutorial/application-architecture.md#main-and-renderer-processes) | 
					
						
							| 
									
										
										
										
											2016-12-02 23:58:02 +11:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-03 15:12:57 -07:00
										 |  |  |  | ### `new TouchBar(options)` _Experimental_
 | 
					
						
							| 
									
										
										
										
											2016-12-02 23:58:02 +11:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-24 10:29:03 +02:00
										 |  |  |  | * `options` Object | 
					
						
							| 
									
										
										
										
											2019-07-25 11:30:42 -07:00
										 |  |  |  |   * `items` ([TouchBarButton](touch-bar-button.md) | [TouchBarColorPicker](touch-bar-color-picker.md) | [TouchBarGroup](touch-bar-group.md) | [TouchBarLabel](touch-bar-label.md) | [TouchBarPopover](touch-bar-popover.md) | [TouchBarScrubber](touch-bar-scrubber.md) | [TouchBarSegmentedControl](touch-bar-segmented-control.md) | [TouchBarSlider](touch-bar-slider.md) | [TouchBarSpacer](touch-bar-spacer.md))[] (optional) | 
					
						
							| 
									
										
										
										
											2018-02-12 11:59:01 -08:00
										 |  |  |  |   * `escapeItem` ([TouchBarButton](touch-bar-button.md) | [TouchBarColorPicker](touch-bar-color-picker.md) | [TouchBarGroup](touch-bar-group.md) | [TouchBarLabel](touch-bar-label.md) | [TouchBarPopover](touch-bar-popover.md) | [TouchBarScrubber](touch-bar-scrubber.md) | [TouchBarSegmentedControl](touch-bar-segmented-control.md) | [TouchBarSlider](touch-bar-slider.md) | [TouchBarSpacer](touch-bar-spacer.md) | null) (optional) | 
					
						
							| 
									
										
										
										
											2016-12-02 23:58:02 +11:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-03 09:54:46 -08:00
										 |  |  |  | Creates a new touch bar with the specified items. Use | 
					
						
							|  |  |  |  | `BrowserWindow.setTouchBar` to add the `TouchBar` to a window. | 
					
						
							| 
									
										
										
										
											2016-12-02 23:58:02 +11:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-07 09:42:45 -08:00
										 |  |  |  | **Note:** The TouchBar API is currently experimental and may change or be | 
					
						
							|  |  |  |  | removed in future Electron releases. | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-17 14:37:05 +07:00
										 |  |  |  | **Tip:** If you don't have a MacBook with Touch Bar, you can use | 
					
						
							|  |  |  |  | [Touch Bar Simulator](https://github.com/sindresorhus/touch-bar-simulator) | 
					
						
							|  |  |  |  | to test Touch Bar usage in your app. | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-06 08:29:01 -07:00
										 |  |  |  | ### Static Properties
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | #### `TouchBarButton`
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | A [`typeof TouchBarButton`](./touch-bar-button.md) reference to the `TouchBarButton` class. | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | #### `TouchBarColorPicker`
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | A [`typeof TouchBarColorPicker`](./touch-bar-color-picker.md) reference to the `TouchBarColorPicker` class. | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | #### `TouchBarGroup`
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | A [`typeof TouchBarGroup`](./touch-bar-group.md) reference to the `TouchBarGroup` class. | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | #### `TouchBarLabel`
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | A [`typeof TouchBarLabel`](./touch-bar-label.md) reference to the `TouchBarLabel` class. | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | #### `TouchBarPopover`
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | A [`typeof TouchBarPopover`](./touch-bar-popover.md) reference to the `TouchBarPopover` class. | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | #### `TouchBarScrubber`
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | A [`typeof TouchBarScrubber`](./touch-bar-scrubber.md) reference to the `TouchBarScrubber` class. | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | #### `TouchBarSegmentedControl`
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | A [`typeof TouchBarSegmentedControl`](./touch-bar-segmented-control.md) reference to the `TouchBarSegmentedControl` class. | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | #### `TouchBarSlider`
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | A [`typeof TouchBarSlider`](./touch-bar-slider.md) reference to the `TouchBarSlider` class. | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | #### `TouchBarSpacer`
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | A [`typeof TouchBarSpacer`](./touch-bar-spacer.md) reference to the `TouchBarSpacer` class. | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-03 09:34:55 -07:00
										 |  |  |  | ### Instance Properties
 | 
					
						
							| 
									
										
										
										
											2017-03-27 11:22:52 +11:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-03 09:34:55 -07:00
										 |  |  |  | The following properties are available on instances of `TouchBar`: | 
					
						
							| 
									
										
										
										
											2017-03-27 11:22:52 +11:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-03 09:34:55 -07:00
										 |  |  |  | #### `touchBar.escapeItem`
 | 
					
						
							| 
									
										
										
										
											2017-03-27 11:22:52 +11:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-12 11:59:01 -08:00
										 |  |  |  | A `TouchBarItem` that will replace the "esc" button on the touch bar when set. | 
					
						
							| 
									
										
										
										
											2017-04-03 09:34:55 -07:00
										 |  |  |  | Setting to `null` restores the default "esc" button. Changing this value | 
					
						
							|  |  |  |  | immediately updates the escape item in the touch bar. | 
					
						
							| 
									
										
										
										
											2017-03-27 11:22:52 +11:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-02 23:58:02 +11:00
										 |  |  |  | ## Examples
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-03 09:54:46 -08:00
										 |  |  |  | Below is an example of a simple slot machine touch bar game with a button | 
					
						
							|  |  |  |  | and some labels. | 
					
						
							| 
									
										
										
										
											2016-12-02 23:58:02 +11:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | ```javascript | 
					
						
							| 
									
										
										
										
											2018-09-14 02:10:51 +10:00
										 |  |  |  | const { app, BrowserWindow, TouchBar } = require('electron') | 
					
						
							| 
									
										
										
										
											2017-03-03 09:54:46 -08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-14 02:10:51 +10:00
										 |  |  |  | const { TouchBarLabel, TouchBarButton, TouchBarSpacer } = TouchBar | 
					
						
							| 
									
										
										
										
											2017-03-03 09:54:46 -08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | let spinning = false | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // Reel labels | 
					
						
							|  |  |  |  | const reel1 = new TouchBarLabel() | 
					
						
							|  |  |  |  | const reel2 = new TouchBarLabel() | 
					
						
							|  |  |  |  | const reel3 = new TouchBarLabel() | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // Spin result label | 
					
						
							|  |  |  |  | const result = new TouchBarLabel() | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // Spin button | 
					
						
							|  |  |  |  | const spin = new TouchBarButton({ | 
					
						
							|  |  |  |  |   label: '🎰 Spin', | 
					
						
							|  |  |  |  |   backgroundColor: '#7851A9', | 
					
						
							|  |  |  |  |   click: () => { | 
					
						
							|  |  |  |  |     // Ignore clicks if already spinning | 
					
						
							|  |  |  |  |     if (spinning) { | 
					
						
							|  |  |  |  |       return | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     spinning = true | 
					
						
							|  |  |  |  |     result.label = '' | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     let timeout = 10 | 
					
						
							|  |  |  |  |     const spinLength = 4 * 1000 // 4 seconds | 
					
						
							|  |  |  |  |     const startTime = Date.now() | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     const spinReels = () => { | 
					
						
							|  |  |  |  |       updateReels() | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |       if ((Date.now() - startTime) >= spinLength) { | 
					
						
							|  |  |  |  |         finishSpin() | 
					
						
							|  |  |  |  |       } else { | 
					
						
							|  |  |  |  |         // Slow down a bit on each spin | 
					
						
							|  |  |  |  |         timeout *= 1.1 | 
					
						
							|  |  |  |  |         setTimeout(spinReels, timeout) | 
					
						
							|  |  |  |  |       } | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     spinReels() | 
					
						
							|  |  |  |  |   } | 
					
						
							|  |  |  |  | }) | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | const getRandomValue = () => { | 
					
						
							| 
									
										
										
										
											2017-03-03 10:54:21 -08:00
										 |  |  |  |   const values = ['🍒', '💎', '7️⃣', '🍊', '🔔', '⭐', '🍇', '🍀'] | 
					
						
							| 
									
										
										
										
											2017-03-03 09:54:46 -08:00
										 |  |  |  |   return values[Math.floor(Math.random() * values.length)] | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | const updateReels = () => { | 
					
						
							|  |  |  |  |   reel1.label = getRandomValue() | 
					
						
							|  |  |  |  |   reel2.label = getRandomValue() | 
					
						
							|  |  |  |  |   reel3.label = getRandomValue() | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | const finishSpin = () => { | 
					
						
							|  |  |  |  |   const uniqueValues = new Set([reel1.label, reel2.label, reel3.label]).size | 
					
						
							|  |  |  |  |   if (uniqueValues === 1) { | 
					
						
							|  |  |  |  |     // All 3 values are the same | 
					
						
							|  |  |  |  |     result.label = '💰 Jackpot!' | 
					
						
							| 
									
										
										
										
											2017-03-03 15:14:51 -08:00
										 |  |  |  |     result.textColor = '#FDFF00' | 
					
						
							| 
									
										
										
										
											2017-03-03 09:54:46 -08:00
										 |  |  |  |   } else if (uniqueValues === 2) { | 
					
						
							|  |  |  |  |     // 2 values are the same | 
					
						
							|  |  |  |  |     result.label = '😍 Winner!' | 
					
						
							| 
									
										
										
										
											2017-03-03 15:14:51 -08:00
										 |  |  |  |     result.textColor = '#FDFF00' | 
					
						
							| 
									
										
										
										
											2017-03-03 09:54:46 -08:00
										 |  |  |  |   } else { | 
					
						
							|  |  |  |  |     // No values are the same | 
					
						
							|  |  |  |  |     result.label = '🙁 Spin Again' | 
					
						
							| 
									
										
										
										
											2017-03-03 15:14:51 -08:00
										 |  |  |  |     result.textColor = null | 
					
						
							| 
									
										
										
										
											2017-03-03 09:54:46 -08:00
										 |  |  |  |   } | 
					
						
							|  |  |  |  |   spinning = false | 
					
						
							|  |  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-12-02 23:58:02 +11:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-21 18:07:47 -07:00
										 |  |  |  | const touchBar = new TouchBar({ | 
					
						
							|  |  |  |  |   items: [ | 
					
						
							|  |  |  |  |     spin, | 
					
						
							|  |  |  |  |     new TouchBarSpacer({ size: 'large' }), | 
					
						
							|  |  |  |  |     reel1, | 
					
						
							|  |  |  |  |     new TouchBarSpacer({ size: 'small' }), | 
					
						
							|  |  |  |  |     reel2, | 
					
						
							|  |  |  |  |     new TouchBarSpacer({ size: 'small' }), | 
					
						
							|  |  |  |  |     reel3, | 
					
						
							|  |  |  |  |     new TouchBarSpacer({ size: 'large' }), | 
					
						
							|  |  |  |  |     result | 
					
						
							|  |  |  |  |   ] | 
					
						
							|  |  |  |  | }) | 
					
						
							| 
									
										
										
										
											2016-12-02 23:58:02 +11:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-03 09:54:46 -08:00
										 |  |  |  | let window | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-03 16:43:22 -06:00
										 |  |  |  | app.whenReady().then(() => { | 
					
						
							| 
									
										
										
										
											2017-03-03 09:54:46 -08:00
										 |  |  |  |   window = new BrowserWindow({ | 
					
						
							|  |  |  |  |     frame: false, | 
					
						
							| 
									
										
										
										
											2017-06-05 14:07:57 -07:00
										 |  |  |  |     titleBarStyle: 'hiddenInset', | 
					
						
							| 
									
										
										
										
											2017-03-03 09:54:46 -08:00
										 |  |  |  |     width: 200, | 
					
						
							|  |  |  |  |     height: 200, | 
					
						
							|  |  |  |  |     backgroundColor: '#000' | 
					
						
							|  |  |  |  |   }) | 
					
						
							|  |  |  |  |   window.loadURL('about:blank') | 
					
						
							|  |  |  |  |   window.setTouchBar(touchBar) | 
					
						
							|  |  |  |  | }) | 
					
						
							| 
									
										
										
										
											2016-12-02 23:58:02 +11:00
										 |  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2017-04-27 14:22:56 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-27 20:19:17 +01:00
										 |  |  |  | ### Running the above example
 | 
					
						
							| 
									
										
										
										
											2017-04-27 14:22:56 -07:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-11 17:49:58 +02:00
										 |  |  |  | To run the example above, you'll need to (assuming you've got a terminal open in the directory you want to run the example): | 
					
						
							| 
									
										
										
										
											2017-04-27 20:19:17 +01:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-27 14:22:56 -07:00
										 |  |  |  | 1. Save the above file to your computer as `touchbar.js` | 
					
						
							|  |  |  |  | 2. Install Electron via `npm install electron` | 
					
						
							|  |  |  |  | 3. Run the example inside Electron: `./node_modules/.bin/electron touchbar.js` | 
					
						
							| 
									
										
										
										
											2017-04-27 20:19:17 +01:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-27 14:22:56 -07:00
										 |  |  |  | You should then see a new Electron window and the app running in your touch bar (or touch bar emulator). |