本文へスキップ

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」ボタンを置き換えるTouchBarItemnullに設定すると、デフォルトの「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)
})

上記の例を実行する

上記の例を実行するには(例を実行するディレクトリでターミナルが開いていることを前提として)、

  1. 上記ファイルをコンピューターにtouchbar.jsとして保存します。
  2. npm install electronを使用してElectronをインストールします。
  3. Electron内で例を実行します: ./node_modules/.bin/electron touchbar.js

その後、新しいElectronウィンドウが表示され、アプリがタッチバー(またはタッチバーエミュレーター)で実行されます。