Menu
クラス: Menu
ネイティブアプリケーションのメニューとコンテキストメニューを作成します。
Process: Main
[!TIP] See also: A detailed guide about how to implement menus in your application.
[!WARNING] Electron's built-in classes cannot be subclassed in user code. For more information, see the FAQ.
new Menu()
新しいメニューを作成します
静的メソッド
Menu
クラスは以下の静的メソッドを持ちます。
Menu.setApplicationMenu(menu)
menu
Menu | null
macOS では、 menu
をアプリケーションメニューとして設定します。 Windows と Linux では、 menu
は各ウィンドウのトップメニューとして設定されます。
更に Windows と Linux では、最上位のアイテム名に &
を使用して、アクセラレータを生成させるときに取得する文字を指定できます。 たとえば、ファイルメニューに &File
を使用すると、その関連付けされたメニューを開く Alt-F
アクセラレータが生成されます。 ボタンラベルでその指定をした文字には下線が引かれ、&
文字はボタンラベルに表示されません。
メニューアイテム名の &
文字をエスケープするには、&
を続けて書きます。 例えば、&&File
とするとボタンラベルに &File
が表示されます。
null
を渡すと、既定のメニューが表示されなくなります。 Windows と Linux では、さらにウィンドウからメニューバーを削除します。
[!NOTE] The default menu will be created automatically if the app does not set one. It contains standard items such as
File
,Edit
,View
,Window
andHelp
.
Menu.getApplicationMenu()
戻り値 Menu | null
- セットされていれば menu を、そうでなければ null
を返します。
[!NOTE] The returned
Menu
instance doesn't support dynamic addition or removal of menu items. Instance properties can still be dynamically modified.
Menu.sendActionToFirstResponder(action)
macOS
action
string
action
をアプリケーションの最初のレスポンダーに送信します。 macOS メニューの既定の動作をエミュレートするために使用されます。 Usually you would use the role
property of a MenuItem
.
See the macOS Cocoa Event Handling Guide for more information on macOS' native actions.
Menu.buildFromTemplate(template)
template
(MenuItemConstructorOptions | MenuItem)[]
Returns Menu
Generally, the template
is an array of options
for constructing a MenuItem. 使用方法は上記を参照できます。
template
の要素に他のフィールドを付けることもでき、それらは構築されたメニューアイテムのプロパティになります。
インスタンスメソッド
menu
オブジェクトには以下のメソッドがあります。
menu.popup([options])
Pops up this menu as a context menu in the BaseWindow
.
[!TIP] For more details, see the Context Menu guide.
menu.closePopup([window])
window
BaseWindow (optional) - Default is the focused window.
Closes the context menu in the window
.
menu.append(menuItem)
menuItem
MenuItem
menu に menuItem
を追加します。
menu.getMenuItemById(id)
id
string
戻り値 MenuItem | null
- 指定した id
のアイテム。
menu.insert(pos, menuItem)
pos
IntegermenuItem
MenuItem
menu の pos
の位置に menuItem
を挿入します。
インスタンスイベント
new Menu
で作成されたオブジェクトまたは Menu.buildFromTemplate
によって返されたオブジェクトは、以下のイベントが発生します。
[!NOTE] Some events are only available on specific operating systems and are labeled as such.
イベント: 'menu-will-show'
戻り値:
event
Event
menu.popup()
が呼ばれたときに発生します。
イベント: 'menu-will-close'
戻り値:
event
Event
手動か menu.closePopup()
でポップアップが閉じられたときに発生します。
インスタンスプロパティ
menu
オブジェクトには更に以下のプロパティがあります。
menu.items
menu のアイテムが入った配列 MenuItem[]
。
Each Menu
consists of multiple MenuItem
instances and each MenuItem
can nest a Menu
into its submenu
property.