Menu
クラス: Menu
Create application menus and context menus.
プロセス: メイン
The presentation of menus varies depending on the operating system:
- Under Windows and Linux, menus are visually similar to Chromium.
- Under macOS, these will be native menus.
あなたのアプリケーションにどうやってメニューを実装するかの詳細なガイド もご覧ください。
[!WARNING] Electron 組み込みのクラスはユーザコードでサブクラス化できません。詳細については、FAQ をご参照ください。
new Menu()
新しいメニューを作成します
静的メソッド
Menu クラスは以下の静的メソッドを持ちます。
Menu.setApplicationMenu(menu)
menuMenu | null
macOS では、 menu をアプリケーションメニューとして設定します。 Windows と Linux では、 menu は各ウィンドウのトップメニューとして設定されます。
On Linux, the menu bar uses GTK theme colors when GTK is available. If GTK cannot initialize, it uses Chromium's built-in light or dark menu colors, following nativeTheme.shouldUseDarkColors.
更に Windows と Linux では、最上位のアイテム名に & を使用して、アクセラレータを生成させるときに取得する文字を指定できます。たとえば、ファイルメニューに &File を使用すると、その関連付けされたメニューを開く Alt-F アクセラレータが生成されます。ボタンラベルでその指定をした文字には下線が引かれ、& 文字はボタンラベルに表示されません。
In order to escape the & character in an item name, add a preceding &. 例えば、&&File とするとボタンラベルに &File が表示されます。
null を渡すと、既定のメニューが表示されなくなります。 Windows と Linux では、さらにウィンドウからメニューバーを削除します。
[!NOTE] アプリがメニューを設定しない場合、自動でデフォルトメニューが作成されます。 It contains standard items such as
File,Edit,View, andWindow.
Menu.getApplicationMenu()
戻り値 Menu | null - セットされていれば menu を、そうでなければ null を返します。
[!NOTE] 返される
Menuインスタンスは、メニュー項目の動的な追加または削除ができません。インスタンスプロパティ であれば、動的に変更ができます。
Menu.setHighlightColors(options) Windows
History
| Version(s) | Changes |
|---|---|
None | API ADDED |
Sets the colors used to highlight the hovered or keyboard-focused item in every menu the app shows, including context menus and the menu bar's dropdowns. Light and dark colors are set separately, and any color left unset keeps the default for that mode. Each call replaces the colors set by any previous call. Passing null restores the defaults.
const { Menu } = require('electron')
Menu.setHighlightColors({
light: { background: '#0067c0', foreground: '#ffffff' },
dark: { background: '#4cc2ff', foreground: '#000000' }
})
Menus opened after the call use the new colors. The colors are not applied while Windows high contrast mode or forced colors are active, so the user's system colors take priority.
This method has no effect on macOS and Linux.
Menu.sendActionToFirstResponder(action) macOS
actionstring
action をアプリケーションの最初のレスポンダーに送信します。 macOS メニューの既定の動作をエミュレートするために使用されます。通常は MenuItem の role プロパティを使用します。
macOS のネイティブの動作の詳細については、 macOS の Cocoa Event Handling Guide を参照してください。
Menu.buildFromTemplate(template)
template(MenuItemConstructorOptions | MenuItem)[]
戻り値 Menu
一般的に、template は MenuItem を構築するための options の配列です。使用方法は上記を参照できます。
template の要素に他のフィールドを付けることもでき、それらは構築されたメニューアイテムのプロパティになります。
インスタンスメソッド
menu オブジェクトには以下のメソッドがあります。
menu.popup([options])
このメニューを BaseWindow 内のコンテキストメニューとしてポップアップします。
[!TIP] 詳細については、コンテキストメニュー のガイドをご参照ください。
menu.closePopup([window])
windowBaseWindow (任意) - 省略値はフォーカスされたウインドウです。
window 内のこのコンテキストメニューを閉じます。
menu.append(menuItem)
menuItemMenuItem
menu に menuItem を追加します。
menu.getMenuItemById(id)
idstring
Returns MenuItem | null - the item with the specified id
menu.insert(pos, menuItem)
posIntegermenuItemMenuItem
menu の pos の位置に menuItem を挿入します。
インスタンスイベント
new Menu で作成されたオブジェクトまたは Menu.buildFromTemplate によって返されたオブジェクトは、以下のイベントが発生します。
[!NOTE] いくつかのイベントは特定のオペレーティングシステムでのみ利用可能で、そのように注記がつけられています。
イベント: 'menu-will-show'
戻り値:
eventEvent
menu.popup() が呼ばれたときに発生します。
イベント: 'menu-will-close'
戻り値:
eventEvent
手動か menu.closePopup() でポップアップが閉じられたときに発生します。
インスタンスプロパティ
menu オブジェクトには更に以下のプロパティがあります。
menu.items
A MenuItem[] array containing the menu's items.
各 Menu は複数の MenuItem インスタンスで構成され、各 MenuItem は submenu プロパティに Menu をネストできます。