メインコンテンツへ飛ぶ

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 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 and Help.

戻り値 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.

  • 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.

  • template (MenuItemConstructorOptions | MenuItem)[]

Returns Menu

Generally, the template is an array of options for constructing a MenuItem. 使用方法は上記を参照できます。

template の要素に他のフィールドを付けることもでき、それらは構築されたメニューアイテムのプロパティになります。

インスタンスメソッド

menu オブジェクトには以下のメソッドがあります。

  • options Object (任意)
    • window BaseWindow (optional) - Default is the focused window.
    • frame WebFrameMain (optional) - Provide the relevant frame if you want certain OS-level features such as Writing Tools on macOS to function correctly. Typically, this should be params.frame from the context-menu event on a WebContents, or the focusedFrame property of a WebContents.
    • x number (任意) - 既定ではマウスカーソルの現在位置です。 y が宣言されている場合は宣言する必要があります。
    • y number (任意) - 既定ではマウスカーソルの現在位置です。 x が宣言されている場合は宣言する必要があります。
    • positioningItem number (任意) macOS - マウスカーソルの位置に配置するメニューアイテムのインデックス。 既定値は -1 です。
    • sourceType string (任意) Windows Linux - これは、context-menu イベントによって提供される menuSourceType と対応しなければなりません。 この値の手動設定は非推奨です。他の API から受け取った値を与えるか、undefined のままにしておいてください。 none, mouse, keyboard, touch, touchMenu, longPress, longTap, touchHandle, stylus, adjustSelection, adjustSelectionReset のいずれかになります。
    • callback Function (任意) - メニューが閉じたしたときに呼ばれます。

Pops up this menu as a context menu in the BaseWindow.

[!TIP] For more details, see the Context Menu guide.

  • window BaseWindow (optional) - Default is the focused window.

Closes the context menu in the window.

menu に menuItem を追加します。

  • id string

戻り値 MenuItem | null - 指定した id のアイテム。

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 のアイテムが入った配列 MenuItem[]

Each Menu consists of multiple MenuItem instances and each MenuItem can nest a Menu into its submenu property.