TouchBar¶
[!WARNING] Electron 的内置类无法在用户代码中进行子类化。 更多信息,请参阅 常见问题。
类:TouchBar¶
为原生 macOS 应用创建 TouchBar 布局
进程:主进程
new TouchBar(options)¶
optionsObjectitems(TouchBarButton | TouchBarColorPicker | TouchBarGroup | TouchBarLabel | TouchBarPopover | TouchBarScrubber | TouchBarSegmentedControl | TouchBarSlider | TouchBarSpacer)[](可选)escapeItem(TouchBarButton | TouchBarColorPicker | TouchBarGroup | TouchBarLabel | TouchBarPopover | TouchBarScrubber | TouchBarSegmentedControl | TouchBarSlider | TouchBarSpacer | null)(可选)
使用指定项创建新的触控栏。使用 BrowserWindow.setTouchBar 将 TouchBar 添加到窗口。
[!NOTE] TouchBar API 目前处于实验阶段,在未来的 Electron 版本中可能会更改或移除。
静态属性¶
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¶
一个 TouchBarItem,设置后将替换触控栏上的“esc”按钮。设置为 null 可恢复默认的“esc”按钮。更改此值会立即更新触控栏中的退出项。
示例¶
以下是一个简单的老虎机触控栏游戏示例,包含一个按钮和一些标签。
const { app, BrowserWindow, TouchBar } = require('electron')
const { TouchBarLabel, TouchBarButton, TouchBarSpacer } = TouchBar
let spinning = false
// 卷轴标签
const reel1 = new TouchBarLabel({ label: '' })
const reel2 = new TouchBarLabel({ label: '' })
const reel3 = new TouchBarLabel({ label: '' })
// 旋转结果标签
const result = new TouchBarLabel({ label: '' })
// 旋转按钮
const spin = new TouchBarButton({
label: '🎰 Spin',
backgroundColor: '#7851A9',
click: () => {
// 如果已在旋转中则忽略点击
if (spinning) {
return
}
spinning = true
result.label = ''
let timeout = 10
const spinLength = 4 * 1000 // 4 秒
const startTime = Date.now()
const spinReels = () => {
updateReels()
if (Date.now() - startTime >= spinLength) {
finishSpin()
} else {
// 每次旋转稍微减慢速度
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) {
// 3 个值全部相同
result.label = '💰 Jackpot!'
result.textColor = '#FDFF00'
} else if (uniqueValues === 2) {
// 2 个值相同
result.label = '😍 Winner!'
result.textColor = '#FDFF00'
} else {
// 没有值相同
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 窗口,并且该应用正在你的触控栏(或触控栏模拟器)中运行。
本页原文 Markdown:在 AtomGit 查看·内容源自开源项目 el/electron