Skip to main content

Touch Bar Support

· 3 min read

Die Beta-Version 1.6.3 enthält erste Unterstützung für die macOS Touch Bar.


The new Touch Bar API allows you to add buttons, labels, popovers, color pickers, sliders, and spacers. These elements can be dynamically updated and also emit events when they are interacted with.

This is the first release of this API so it will be evolving over the next few Electron releases. Please check out the release notes for further updates and open issues for any problems or missing functionality.

You can install this version via npm install electron@beta and learn more about it in the TouchBar and BrowserWindow Electron docs.

Big thanks to @MarshallOfSound for contributing this to Electron. 🎉

Touch Bar Example

Touch Bar Gif

Below is an example of creating a simple slot machine game in the touch bar. It demonstrates how to create a touch bar, style the items, associate it with a window, handle button click events, and update the labels dynamically.

const {app, BrowserWindow, TouchBar} = require('electron')

const {TouchBarButton, TouchBarLabel, TouchBarSpacer} = TouchBar

let spinning = false

// Rollen Sie 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', Spin',
Hintergrundfarbe: '#7851A9',
Klick: () => {
// Klickt ignorieren, wenn bereits gedreht wird:
if (spinning) {
return
}

spinning = true
Ergebnis. abel = ''

let timeout = 10
const spinLength = 4 * 1000 // 4 Sekunden
const startTime = Datum. ow()

const spinReels = () => {
updateReels()

if (Date. ow() - startTime) >= spinLength) {
finishSpin()
} else {
// Verlangsamen Sie bei jeder Spin ein Bit
timeout *= 1. setTimeout(spinRollen, timeout)
}
}

spinReels()
}
})

const getRandomValue = () => {
const values = ['🍒', '💎', '7️⃣', '🍊', '🔔', '⭐', '🍇', '🍀']
return values[Math. loor(Math.random() * values.length)]
}

const updateReels = () => {
reel1. abel = getRandomValue()
reel2.label = getRandomValue()
reel3. abel = getRandomValue()
}

const finishSpin = () => {
const uniqueValues = new Set([reel1. abel, reel2.label, reel3.label]). ize
if (uniqueValues === 1) {
// Alle 3 Werte sind das gleiche
Ergebnis. abel = '💰 Jackpot!'
result.textColor = '#FDFF00'
} else if (uniqueValues === 2) {
// 2 values are the same
result.label = '😍 Winner!'
extColor = '#FDFF00'
} else {
// Keine Werte sind das gleiche
Ergebnis. abel = '🙁 Erneut drehen'
Ergebnis. extColor = null
}
Spinning = false
}

const touchBar = new TouchBar([
Drehen,
new TouchBarSpacer({size: 'large'}),
reel1,
new TouchBarSpacer({size: 'small'}),
reel2,
new TouchBarSpacer({size: 'small'}),
reel3,
new TouchBarSpacer({size: 'large'}),
result
])

let window

app. nce('ready', () => {
window = new BrowserWindow({
frame: false,
titleBarStyle: 'hidden-inset',
Breite: 200,
Höhe: 200,
backgroundColor: '#000'
})
Fenster. oadURL('about:blank')
window.setTouchBar(touchBar)
})