2016-12-02 15:58:02 +03:00
|
|
|
|
## Class: TouchBar
|
|
|
|
|
|
|
|
|
|
> Create TouchBar layouts for native macOS applications
|
|
|
|
|
|
|
|
|
|
Process: [Main](../tutorial/quick-start.md#main-process)
|
|
|
|
|
|
2017-04-04 01:12:57 +03:00
|
|
|
|
### `new TouchBar(options)` _Experimental_
|
2016-12-02 15:58:02 +03:00
|
|
|
|
|
2017-04-04 01:12:57 +03:00
|
|
|
|
* `options` - Object
|
|
|
|
|
* `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))[]
|
|
|
|
|
* `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)) (optional)
|
2016-12-02 15:58:02 +03:00
|
|
|
|
|
2017-03-03 20:54:46 +03:00
|
|
|
|
Creates a new touch bar with the specified items. Use
|
|
|
|
|
`BrowserWindow.setTouchBar` to add the `TouchBar` to a window.
|
2016-12-02 15:58:02 +03:00
|
|
|
|
|
2017-03-07 20:42:45 +03:00
|
|
|
|
**Note:** The TouchBar API is currently experimental and may change or be
|
|
|
|
|
removed in future Electron releases.
|
|
|
|
|
|
2017-04-03 19:34:55 +03:00
|
|
|
|
### Instance Properties
|
2017-03-27 03:22:52 +03:00
|
|
|
|
|
2017-04-03 19:34:55 +03:00
|
|
|
|
The following properties are available on instances of `TouchBar`:
|
2017-03-27 03:22:52 +03:00
|
|
|
|
|
2017-04-03 19:34:55 +03:00
|
|
|
|
#### `touchBar.escapeItem`
|
2017-03-27 03:22:52 +03:00
|
|
|
|
|
2017-04-03 19:34:55 +03:00
|
|
|
|
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
|
|
|
|
|
immediately updates the escape item in the touch bar.
|
2017-03-27 03:22:52 +03:00
|
|
|
|
|
2016-12-02 15:58:02 +03:00
|
|
|
|
## Examples
|
|
|
|
|
|
2017-03-03 20:54:46 +03:00
|
|
|
|
Below is an example of a simple slot machine touch bar game with a button
|
|
|
|
|
and some labels.
|
2016-12-02 15:58:02 +03:00
|
|
|
|
|
|
|
|
|
```javascript
|
2017-03-03 20:54:46 +03:00
|
|
|
|
const {app, BrowserWindow, TouchBar} = require('electron')
|
|
|
|
|
|
|
|
|
|
const {TouchBarLabel, TouchBarButton, TouchBarSpacer} = TouchBar
|
|
|
|
|
|
|
|
|
|
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 21:54:21 +03:00
|
|
|
|
const values = ['🍒', '💎', '7️⃣', '🍊', '🔔', '⭐', '🍇', '🍀']
|
2017-03-03 20:54:46 +03: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-04 02:14:51 +03:00
|
|
|
|
result.textColor = '#FDFF00'
|
2017-03-03 20:54:46 +03:00
|
|
|
|
} else if (uniqueValues === 2) {
|
|
|
|
|
// 2 values are the same
|
|
|
|
|
result.label = '😍 Winner!'
|
2017-03-04 02:14:51 +03:00
|
|
|
|
result.textColor = '#FDFF00'
|
2017-03-03 20:54:46 +03:00
|
|
|
|
} else {
|
|
|
|
|
// No values are the same
|
|
|
|
|
result.label = '🙁 Spin Again'
|
2017-03-04 02:14:51 +03:00
|
|
|
|
result.textColor = null
|
2017-03-03 20:54:46 +03:00
|
|
|
|
}
|
|
|
|
|
spinning = false
|
|
|
|
|
}
|
2016-12-02 15:58:02 +03:00
|
|
|
|
|
|
|
|
|
const touchBar = new TouchBar([
|
2017-03-03 20:54:46 +03:00
|
|
|
|
spin,
|
|
|
|
|
new TouchBarSpacer({size: 'large'}),
|
|
|
|
|
reel1,
|
2017-03-04 02:14:51 +03:00
|
|
|
|
new TouchBarSpacer({size: 'small'}),
|
2017-03-03 20:54:46 +03:00
|
|
|
|
reel2,
|
2017-03-04 02:14:51 +03:00
|
|
|
|
new TouchBarSpacer({size: 'small'}),
|
2017-03-03 20:54:46 +03:00
|
|
|
|
reel3,
|
|
|
|
|
new TouchBarSpacer({size: 'large'}),
|
|
|
|
|
result
|
2016-12-02 15:58:02 +03:00
|
|
|
|
])
|
|
|
|
|
|
2017-03-03 20:54:46 +03:00
|
|
|
|
let window
|
|
|
|
|
|
|
|
|
|
app.once('ready', () => {
|
|
|
|
|
window = new BrowserWindow({
|
|
|
|
|
frame: false,
|
|
|
|
|
titleBarStyle: 'hidden-inset',
|
|
|
|
|
width: 200,
|
|
|
|
|
height: 200,
|
|
|
|
|
backgroundColor: '#000'
|
|
|
|
|
})
|
|
|
|
|
window.loadURL('about:blank')
|
|
|
|
|
window.setTouchBar(touchBar)
|
|
|
|
|
})
|
2016-12-02 15:58:02 +03:00
|
|
|
|
```
|