TouchBar
クラス: TouchBar
ネイティブmacOSアプリケーション用のTouchBarレイアウトを作成します。
プロセス: メイン
new TouchBar(options)
指定されたアイテムで新しいタッチバーを作成します。BrowserWindow.setTouchBar
を使用して、ウィンドウにTouchBar
を追加します。
注意: TouchBar API は現在実験的な段階であり、将来の Electron リリースで変更または削除される可能性があります。
ヒント: Touch Barを搭載したMacBookをお持ちでない場合は、Touch Bar Simulatorを使用して、アプリでのTouch Barの使用をテストできます。
静的プロパティ
TouchBarButton
TouchBarButton
クラスへのtypeof TouchBarButton
参照。
TouchBarColorPicker
TouchBarColorPicker
クラスへのtypeof TouchBarColorPicker
参照。
TouchBarGroup
TouchBarGroup
クラスへのtypeof TouchBarGroup
参照。
TouchBarLabel
TouchBarLabel
クラスへのtypeof TouchBarLabel
参照。
TouchBarPopover
TouchBarPopover
クラスへのtypeof TouchBarPopover
参照。
TouchBarScrubber
TouchBarScrubber
クラスへのtypeof TouchBarScrubber
参照。
TouchBarSegmentedControl
TouchBarSegmentedControl
クラスへのtypeof TouchBarSegmentedControl
参照。
TouchBarSlider
TouchBarSlider
クラスへのtypeof TouchBarSlider
参照。
TouchBarSpacer
TouchBarSpacer
クラスへのtypeof TouchBarSpacer
参照。
TouchBarOtherItemsProxy
TouchBarOtherItemsProxy
クラスへのtypeof TouchBarOtherItemsProxy
参照。
インスタンスプロパティ
以下のプロパティは、TouchBar
のインスタンスで使用できます。
touchBar.escapeItem
設定されている場合、タッチバーの「esc」ボタンを置き換えるTouchBarItem
。null
に設定すると、デフォルトの「esc」ボタンが復元されます。この値を変更すると、タッチバーの脱出アイテムがすぐに更新されます。
例
以下は、ボタンとラベルを使用したシンプルなスロットマシンのタッチバーゲームの例です。
const { app, BrowserWindow, TouchBar } = require('electron')
const { TouchBarLabel, TouchBarButton, TouchBarSpacer } = TouchBar
let spinning = false
// Reel labels
const reel1 = new TouchBarLabel({ label: '' })
const reel2 = new TouchBarLabel({ label: '' })
const reel3 = new TouchBarLabel({ label: '' })
// Spin result label
const result = new TouchBarLabel({ label: '' })
// 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 = () => {
const values = ['🍒', '💎', '7️⃣', '🍊', '🔔', '⭐', '🍇', '🍀']
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!'
result.textColor = '#FDFF00'
} else if (uniqueValues === 2) {
// 2 values are the same
result.label = '😍 Winner!'
result.textColor = '#FDFF00'
} else {
// No values are the same
result.label = '🙁 Spin Again'
result.textColor = null
}
spinning = false
}
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
]
})
let window
app.whenReady().then(() => {
window = new BrowserWindow({
frame: false,
titleBarStyle: 'hiddenInset',
width: 200,
height: 200,
backgroundColor: '#000'
})
window.loadURL('about:blank')
window.setTouchBar(touchBar)
})
上記の例を実行する
上記の例を実行するには(例を実行するディレクトリでターミナルが開いていることを前提として)、
- 上記ファイルをコンピューターに
touchbar.js
として保存します。 npm install electron
を使用してElectronをインストールします。- Electron内で例を実行します:
./node_modules/.bin/electron touchbar.js
その後、新しいElectronウィンドウが表示され、アプリがタッチバー(またはタッチバーエミュレーター)で実行されます。