Electron中文手册
  • 文档概述
  • 开发指南目录
  • 构建说明(Linux)
  • 构建说明(macOS)
  • 构建说明(Windows)
  • 构建系统概述
  • 编码规范
  • 在 macOS 中调试
  • 在 Windows 中调试
  • 在C ++代码中使用clang-format
  • 在调试器中设置符号服务器
  • 源代码目录结构
  • Chrome升级清单
  • Chromium 开发指南
  • V8 开发指南
  • 教程目录
    • 关于 Electron
    • Electron 版本说明
    • API弃用说明
    • 快速入门
    • 支持的平台
    • 桌面环境集成
    • 系统通知
    • 离屏渲染
    • 在线/离线事件检测
    • 多线程
    • REPL
    • 键盘快捷键
    • DevTools扩展
    • 使用原生模块
    • 使用 Pepper Flash 插件
    • 使用 Selenium 和 WebDriver
    • 使用 Widevine CDM 插件
    • 应用分发
    • 应用打包
    • 主进程调试
    • 使用 node-inspector 进行主进程调试
    • 使用 VSCode 进行主进程调试
    • Mac App Store应用提交指南
    • Windows App Store应用提交指南
    • 安全,本地功能和你的责任
    • Headless CI Systems 测试
  • API接口目录
    • API接口之公用接口
      • 应用语言
      • 开发概要
      • 专业术语
      • 常见问题
      • 环境变量
      • 快捷键字符串
      • 命令行
      • 客户端请求
      • 剪贴板
      • <File> H5 File文件操作
      • 无框窗口
      • <window.open> window.open打开新窗口或打开时传递消息
      • 沙盒选项
    • API接口之主进程接口
      • 整体控制
      • 全局快捷键
      • 图标创建与应用
      • 屏幕
      • 窗口
      • 菜单
      • 菜单项
      • 系统托盘
      • 网页内容
      • 从主进程到渲染进程的异步通信
      • 对话框
      • 创建和控制视图
      • 会话
      • 会话,缓存和代理等控制
      • 页面请求
      • HTTP/HTTPS请求处理
      • 协议的注册和处理
      • 使用系统默认应用程序管理文件或URL
      • 下载项管理
      • 进程控制
      • Chromium原生网络库
      • 获取系统首选项
      • 电源状态
      • 节能管理
      • 调试工具
      • 奔溃报告
      • 性能数据收集
      • 自动更新
      • TouchBar触摸条
      • TouchBar触摸条按钮
      • TouchBar触摸条拾色器
      • TouchBar触摸条分组
      • TouchBar触摸条scrubber
      • TouchBar触摸条分段控件
      • TouchBar触摸条label标签
      • TouchBar触摸条弹出框
      • TouchBar触摸条滑块
      • TouchBar触摸条间隔符
    • API接口之渲染进程接口
      • 页面渲染
      • <webview> webview标签
      • 渲染进程与主进程通信
      • 从渲染进程到主进程的异步通信
      • 子窗口
      • 捕获桌面资源
  • 结构列表
    • 蓝牙设备对象
    • 证书对象
    • 证书主体对象
    • Cookie对象
    • 崩溃报告对象
    • 桌面捕获源对象
    • 显示器对象
    • 打印机信息对象
    • 文件过滤器对象
    • 最近使用的项目
    • 常用列表项
    • CPU使用率对象即程序占用的CPU资源
    • IO值对象
    • 内存信息对象
    • 进程内存信息对象
    • 内存使用详细信息
    • Mime类型缓冲区
    • 矩形对象
    • 删除客户端证书对象
    • 删除密码对象
    • Scrubber项对象
    • 分段控制对象
    • 快捷方式对象
    • 任务对象
    • 缩略图工具栏按钮对象
    • 上传blob对象
    • 上传数据对象
    • 上传文件系统对象
    • 上传文件对象
    • 上传原始数据对象
Powered by GitBook
On this page
  • new TouchBar(options) 实验功能
  • 上方代码的运行方法

Was this helpful?

  1. API接口目录
  2. API接口之主进程接口

TouchBar触摸条

Previous自动更新NextTouchBar触摸条按钮

Last updated 4 years ago

Was this helpful?

为本机macOS应用程序创建TouchBar布局

进程:

new TouchBar(options) 实验功能

用途:使用指定项目创建新的触摸条,使用 BrowserWindow.setTouchBar将 TouchBar加到窗口中

  • options - Object

    • items ( | | | | | | )[]

    • escapeItem ( | | | | | | | | ) (可选)

提醒: 如果您没有自带触控板的MacBook的话,可以使用这个.

注意: TouchBar API 目前是实验功能,未来可能删除.

下面是个用在摇一摇或老虎机,贩卖机上的简单的带有按钮的触摸条例子:

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

const {TouchBarLabel, TouchBarButton, TouchBarSpacer} = TouchBar

let spinning = false

//卷轴标签
const reel1 = new TouchBarLabel()
const reel2 = new TouchBarLabel()
const reel3 = new TouchBarLabel()

//旋转结果标签
const result = new TouchBarLabel()

//旋转按钮
const spin = new TouchBarButton({
  label: '🎰 Spin',
  backgroundColor: '#7851A9',
  click: () => {
   //忽略已经旋转的点击
    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 {
  //每次旋转减慢一点
        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([
  spin,
  new TouchBarSpacer({size: 'large'}),
  reel1,
  new TouchBarSpacer({size: 'small'}),
  reel2,
  new TouchBarSpacer({size: 'small'}),
  reel3,
  new TouchBarSpacer({size: 'large'}),
  result
])

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)
})

上方代码的运行方法

假设已有终端已打开您期望的例子并打算运行上方的例子,那么你需要这么做:

  1. 将上面的内容以 touchbar.js文件保存到计算机中.

  2. 如果没有安装electron的话,则通过npm安装electron 如 npm install electron.

  3. 在Electron中运行: ./node_modules/.bin/electron touchbar.js

这样,您就能看到一个新窗口,而触摸条中也将运行相应程序.

TouchBarButton
TouchBarColorPicker
TouchBarGroup
TouchBarLabel
TouchBarPopover
TouchBarSlider
TouchBarSpacer
TouchBarButton
TouchBarColorPicker
TouchBarGroup
TouchBarLabel
TouchBarPopover
TouchBarScrubber
TouchBarSegmentedControl
TouchBarSlider
TouchBarSpacer
触控板模拟器
主进程