非常教程

Electron参考手册

MenuItem

MenuItem

添加项目到本机应用程序菜单和环境菜单。

过程:Main

Menu举例来看。

new MenuItem(options)

  • options 目的
    • click功能(可选) - 将click(menuItem, browserWindow, event)在菜单项被点击时调用。
      • menuItem 菜单项
      • browserWindow 浏览器窗口
      • event 事件
-  `role` String (optional) - Define the action of the menu item, when specified the `click` property will be ignored. See [roles](about:blank#roles).
-  `type` String (optional) - Can be `normal`, `separator`, `submenu`, `checkbox` or `radio`.
-  `label` String - (optional)
-  `sublabel` String - (optional)
-  `accelerator` [Accelerator](../accelerator/index) (optional)
-  `icon` ([NativeImage](../native-image/index) | String) (optional)
-  `enabled` Boolean (optional) - If false, the menu item will be greyed out and unclickable.
-  `visible` Boolean (optional) - If false, the menu item will be entirely hidden.
-  `checked` Boolean (optional) - Should only be specified for `checkbox` or `radio` type menu items.
-  `submenu` (MenuItemConstructorOptions[] | Menu) (optional) - Should be specified for `submenu` type menu items. If `submenu` is specified, the `type: 'submenu'` can be omitted. If the value is not a `Menu` then it will be automatically converted to one using `Menu.buildFromTemplate`.
-  `id` String (optional) - Unique within a single menu. If defined then it can be used as a reference to this item by the position attribute.
-  `position` String (optional) - This field allows fine-grained definition of the specific location within a given menu.

角色

角色允许菜单项具有预定义的行为。

最好指定role与标准角色相匹配的任何菜单项,而不是尝试手动实现click函数中的行为。内置role行为将提供最佳的本地体验。

在使用role和时,labelaccelerator值是可选的,并且将默认为每个平台的适当值。

role属性可以具有以下值:

  • undo
  • redo
  • cut
  • copy
  • paste
  • pasteandmatchstyle
  • selectall
  • delete
  • minimize - Minimize current window
  • close - Close current window
  • quit- Quit the application
  • reload - Reload the current window
  • forcereload - Reload the current window ignoring the cache.
  • toggledevtools - Toggle developer tools in the current window
  • togglefullscreen- Toggle full screen mode on the current window
  • resetzoom - Reset the focused page’s zoom level to the original size
  • zoomin - Zoom in the focused page by 10%
  • zoomout - Zoom out the focused page by 10%
  • editMenu - Whole default “Edit” menu (Undo, Copy, etc.)
  • windowMenu - Whole default “Window” menu (Minimize, Close, etc.)

macOS 上提供以下其他角色:

  • about- 映射到orderFrontStandardAboutPanel行动
  • hide- 映射到hide行动
  • hideothers- 映射到hideOtherApplications行动
  • unhide- 映射到unhideAllApplications行动
  • startspeaking- 映射到startSpeaking行动
  • stopspeaking- 映射到stopSpeaking行动
  • front- 映射到arrangeInFront行动
  • zoom- 映射到performZoom行动
  • window - 子菜单是一个“窗口”菜单
  • help - 子菜单是一个“帮助”菜单
  • services - 子菜单是“服务”菜单

当在 MacOS 指定rolelabelaccelerator是会影响菜单项的唯一选择。所有其他选项将被忽略。

实例属性

以下属性可用于以下MenuItem实例:

menuItem.enabled

一个 Boolean指示是否启用该项目,此属性可以动态更改。

menuItem.visible

一个Boolean指示项目是否可见,此属性可以动态更改。

menuItem.checked

一个 Boolean表示是否选中该项目,该属性可以动态更改。

一个checkbox菜单项将切换checked上,并选择关闭属性。

一个radio菜单项checked在点击时会打开它的属性,并将关闭同一菜单中所有相邻项目的属性。

您可以添加一个click函数来执行其他行为。

menuItem.label

一个String代表菜单项可见标签

menuItem.click

一个Function,当菜单项获得的点击事件被触发

MenuItem相关

Electron

Electron 是一个使用 JavaScript, HTML 和 CSS 等 Web 技术创建原生程序的框架,它负责比较难搞的部分,你只需把精力放在你的应用的核心上即可。

主页 https://electron.atom.io/
源码 https://github.com/electron/electron
发布版本 1.7.9

Electron目录

1.指南 | Guides
2.指南·开发 | Guides: Development
3.Webview 组件 |
4.加速器 | Accelerator
5.API
6.API·对象 | API·Objects
7.app
8.autoUpdater
9.浏览器总览 | BrowserView
10.浏览器窗口 | BrowserWindow
11.浏览器窗口代理 | BrowserWindowProxy)
12.ClientRequest
13.剪贴板 | clipboard
14.内容追踪 | contentTracing
15.小型文字档案 | Cookies
16.crashReporter
17.调试器 | Debugger
18.desktopCapturer
19.dialog
20.电子下载 | DownloadItem
21.环境变量 | Environment Variables
22.无框窗口 | Frameless Window
23.globalShortcut
24.IncomingMessage
25.ipcMain
26.IPC渲染器 | ipcRenderer
27.语言环境 | Locales
28.画面 | Menu
29.MenuItem
30.本地图像 | nativeImage
31.net
32.通知 | Notification
33.权限监控 | powerMonitor
34.权限存储拦截器 | powerSaveBlocker
35.处理 | process
36.协议 | protocol
37.远程 | remote
38.sandbox
39.屏幕 | screen
40.会话 | session
41.shell
42.系统表现 | systemPreferences
43.触摸板 | TouchBar
44.触摸板按钮 | TouchBarButton
45.触摸板颜色选择器 | TouchBarColorPicker
46.触摸板组 | TouchBarGroup
47.触摸板标签 | TouchBarLabel
48.触摸板弹出框 | TouchBarPopover
49.触摸板清理 | TouchBarScrubber
50.触摸板分段控制 | TouchBarSegmentedControl
51.触摸板滑块 | TouchBarSlider
52.触摸板间隔 | TouchBarSpacer
53.Tray
54.网页内容 | webContents
55.网页框架 | webFrame
56.网页要求 | WebRequest
57.窗口开启 | window.open