TouchBar

Class: TouchBar

Create TouchBar layouts for native macOS applications

Process: Main

new TouchBar(options)

Creates a new touch bar with the specified items. Use BrowserWindow.setTouchBar to add the TouchBar to a window.

Note: The TouchBar API is currently experimental and may change or be removed in future Electron releases.

Tip: If you don’t have a MacBook with Touch Bar, you can use Touch Bar Simulator to test Touch Bar usage in your app.

Static Properties

TouchBarButton

A typeof TouchBarButton reference to the TouchBarButton class.

TouchBarColorPicker

A typeof TouchBarColorPicker reference to the TouchBarColorPicker class.

TouchBarGroup

A typeof TouchBarGroup reference to the TouchBarGroup class.

TouchBarLabel

A typeof TouchBarLabel reference to the TouchBarLabel class.

TouchBarPopover

A typeof TouchBarPopover reference to the TouchBarPopover class.

TouchBarScrubber

A typeof TouchBarScrubber reference to the TouchBarScrubber class.

TouchBarSegmentedControl

A typeof TouchBarSegmentedControl reference to the TouchBarSegmentedControl class.

TouchBarSlider

A typeof TouchBarSlider reference to the TouchBarSlider class.

TouchBarSpacer

A typeof TouchBarSpacer reference to the TouchBarSpacer class.

TouchBarOtherItemsProxy

A typeof TouchBarOtherItemsProxy reference to the TouchBarOtherItemsProxy class.

Instance Properties

The following properties are available on instances of TouchBar:

touchBar.escapeItem

A TouchBarItem that will replace the “esc” button on the touch bar when set. Setting to null restores the default “esc” button. Changing this value immediately updates the escape item in the touch bar.

Examples

Below is an example of a simple slot machine touch bar game with a button and some labels.

  1. const { app, BrowserWindow, TouchBar } = require('electron')
  2. const { TouchBarLabel, TouchBarButton, TouchBarSpacer } = TouchBar
  3. let spinning = false
  4. // Reel labels
  5. const reel1 = new TouchBarLabel()
  6. const reel2 = new TouchBarLabel()
  7. const reel3 = new TouchBarLabel()
  8. // Spin result label
  9. const result = new TouchBarLabel()
  10. // Spin button
  11. const spin = new TouchBarButton({
  12. label: '🎰 Spin',
  13. backgroundColor: '#7851A9',
  14. click: () => {
  15. // Ignore clicks if already spinning
  16. if (spinning) {
  17. return
  18. }
  19. spinning = true
  20. result.label = ''
  21. let timeout = 10
  22. const spinLength = 4 * 1000 // 4 seconds
  23. const startTime = Date.now()
  24. const spinReels = () => {
  25. updateReels()
  26. if ((Date.now() - startTime) >= spinLength) {
  27. finishSpin()
  28. } else {
  29. // Slow down a bit on each spin
  30. timeout *= 1.1
  31. setTimeout(spinReels, timeout)
  32. }
  33. }
  34. spinReels()
  35. }
  36. })
  37. const getRandomValue = () => {
  38. const values = ['🍒', '💎', '7️⃣', '🍊', '🔔', '⭐', '🍇', '🍀']
  39. return values[Math.floor(Math.random() * values.length)]
  40. }
  41. const updateReels = () => {
  42. reel1.label = getRandomValue()
  43. reel2.label = getRandomValue()
  44. reel3.label = getRandomValue()
  45. }
  46. const finishSpin = () => {
  47. const uniqueValues = new Set([reel1.label, reel2.label, reel3.label]).size
  48. if (uniqueValues === 1) {
  49. // All 3 values are the same
  50. result.label = '💰 Jackpot!'
  51. result.textColor = '#FDFF00'
  52. } else if (uniqueValues === 2) {
  53. // 2 values are the same
  54. result.label = '😍 Winner!'
  55. result.textColor = '#FDFF00'
  56. } else {
  57. // No values are the same
  58. result.label = '🙁 Spin Again'
  59. result.textColor = null
  60. }
  61. spinning = false
  62. }
  63. const touchBar = new TouchBar({
  64. items: [
  65. spin,
  66. new TouchBarSpacer({ size: 'large' }),
  67. reel1,
  68. new TouchBarSpacer({ size: 'small' }),
  69. reel2,
  70. new TouchBarSpacer({ size: 'small' }),
  71. reel3,
  72. new TouchBarSpacer({ size: 'large' }),
  73. result
  74. ]
  75. })
  76. let window
  77. app.whenReady().then(() => {
  78. window = new BrowserWindow({
  79. frame: false,
  80. titleBarStyle: 'hiddenInset',
  81. width: 200,
  82. height: 200,
  83. backgroundColor: '#000'
  84. })
  85. window.loadURL('about:blank')
  86. window.setTouchBar(touchBar)
  87. })

Running the above example

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):

  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

You should then see a new Electron window and the app running in your touch bar (or touch bar emulator).