From 2bd3c04d614d1a59b35c88214031b7bf5a74d96d Mon Sep 17 00:00:00 2001 From: Milan Burda Date: Fri, 25 Mar 2016 21:44:25 +0100 Subject: [PATCH] Update menu + menu-item modules --- github-electron/github-electron-main-tests.ts | 47 ++++++++++------ .../github-electron.menu-item.d.ts | 53 +++++++++++++++---- github-electron/github-electron.menu.d.ts | 20 ++++--- 3 files changed, 88 insertions(+), 32 deletions(-) diff --git a/github-electron/github-electron-main-tests.ts b/github-electron/github-electron-main-tests.ts index efaadcffe..0bec03bd1 100644 --- a/github-electron/github-electron-main-tests.ts +++ b/github-electron/github-electron-main-tests.ts @@ -325,6 +325,16 @@ ipcMain.on('synchronous-message', (event: Electron.IpcMainEvent, arg: any) => { event.returnValue = 'pong'; }); +// menu-item +// https://github.com/atom/electron/blob/master/docs/api/menu-item.md + +var menuItem = new MenuItem({}); + +menuItem.label = 'Hello World!'; +menuItem.click = (menuItem, browserWindow) => { + console.log('click', menuItem, browserWindow); +}; + // menu // https://github.com/atom/electron/blob/master/docs/api/menu.md @@ -332,22 +342,29 @@ var menu = new Menu(); menu.append(new MenuItem({ label: 'MenuItem1', click: () => { console.log('item 1 clicked'); } })); menu.append(new MenuItem({ type: 'separator' })); menu.append(new MenuItem({ label: 'MenuItem2', type: 'checkbox', checked: true })); +menu.insert(0, menuItem); + +console.log(menu.items); + +var pos = screen.getCursorScreenPoint(); +menu.popup(null, pos.x, pos.y); // main.js -var template = [ +var template = [ { label: 'Electron', submenu: [ { label: 'About Electron', - selector: 'orderFrontStandardAboutPanel:' + role: 'about' }, { type: 'separator' }, { label: 'Services', - submenu: [] + role: 'services', + submenu: [] }, { type: 'separator' @@ -355,16 +372,16 @@ var template = [ { label: 'Hide Electron', accelerator: 'Command+H', - selector: 'hide:' + role: 'hide' }, { label: 'Hide Others', accelerator: 'Command+Shift+H', - selector: 'hideOtherApplications:' + role: 'hideothers' }, { label: 'Show All', - selector: 'unhideAllApplications:' + role: 'unhide' }, { type: 'separator' @@ -382,12 +399,12 @@ var template = [ { label: 'Undo', accelerator: 'Command+Z', - selector: 'undo:' + role: 'undo' }, { label: 'Redo', accelerator: 'Shift+Command+Z', - selector: 'redo:' + role: 'redo' }, { type: 'separator' @@ -395,22 +412,22 @@ var template = [ { label: 'Cut', accelerator: 'Command+X', - selector: 'cut:' + role: 'cut' }, { label: 'Copy', accelerator: 'Command+C', - selector: 'copy:' + role: 'copy' }, { label: 'Paste', accelerator: 'Command+V', - selector: 'paste:' + role: 'paste' }, { label: 'Select All', accelerator: 'Command+A', - selector: 'selectAll:' + role: 'selectall' } ] }, @@ -435,19 +452,19 @@ var template = [ { label: 'Minimize', accelerator: 'Command+M', - selector: 'performMiniaturize:' + role: 'minimize' }, { label: 'Close', accelerator: 'Command+W', - selector: 'performClose:' + role: 'close' }, { type: 'separator' }, { label: 'Bring All to Front', - selector: 'arrangeInFront:' + role: 'front' } ] }, diff --git a/github-electron/github-electron.menu-item.d.ts b/github-electron/github-electron.menu-item.d.ts index b35fcc72f..7280e37bc 100644 --- a/github-electron/github-electron.menu-item.d.ts +++ b/github-electron/github-electron.menu-item.d.ts @@ -3,29 +3,62 @@ // Definitions by: jedmao , rhysd // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +/// /// /// declare namespace Electron { - + /** + * The MenuItem allows you to add items to an application or context menu. + */ class MenuItem { - constructor(options?: MenuItemOptions); - options: MenuItemOptions; + /** + * Create a new menu item. + */ + constructor(options: MenuItemOptions); + + click: (menuItem: MenuItem, browserWindow: BrowserWindow) => void; + /** + * Read-only property. + */ + type: MenuItemType; + /** + * Read-only property. + */ + role: MenuItemRole | MenuItemRoleMac; + /** + * Read-only property. + */ + accelerator: string; + /** + * Read-only property. + */ + icon: NativeImage | string; + /** + * Read-only property. + */ + submenu: Menu | MenuItemOptions[]; + + label: string; + sublabel: string; + enabled: boolean; + visible: boolean; + checked: boolean; } + type MenuItemType = 'normal' | 'separator' | 'submenu' | 'checkbox' | 'radio'; + type MenuItemRole = 'undo' | 'redo' | 'cut' | 'copy' | 'paste' | 'selectall' | 'minimize' | 'close'; + type MenuItemRoleMac = 'about' | 'hide' | 'hideothers' | 'unhide' | 'front' | 'window' | 'help' | 'services'; + interface MenuItemOptions { /** * Callback when the menu item is clicked. */ - click?: Function; - /** - * Call the selector of first responder when clicked (OS X only). - */ - selector?: string; + click?: (menuItem: MenuItem, browserWindow: BrowserWindow) => void; /** * Can be normal, separator, submenu, checkbox or radio. */ - type?: string; + type?: MenuItemType; label?: string; sublabel?: string; /** @@ -92,6 +125,6 @@ declare namespace Electron { /** * Define the action of the menu item, when specified the click property will be ignored */ - role?: string; + role?: MenuItemRole | MenuItemRoleMac; } } diff --git a/github-electron/github-electron.menu.d.ts b/github-electron/github-electron.menu.d.ts index d5d49eb2f..6fa4941c1 100644 --- a/github-electron/github-electron.menu.d.ts +++ b/github-electron/github-electron.menu.d.ts @@ -4,15 +4,18 @@ // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped /// +/// /// declare namespace Electron { /** * The Menu class is used to create native menus that can be used as application - * menus and context menus. Each menu consists of multiple menu items, and each - * menu item can have a submenu. + * menus and context menus. This module is a main process module which can be used + * in a render process via the remote module. + * + * Each menu consists of multiple menu items, and each menu item can have a submenu. */ - class Menu { + class Menu extends EventEmitter { /** * Creates a new menu. */ @@ -23,9 +26,9 @@ declare namespace Electron { */ static setApplicationMenu(menu: Menu): void; /** - * Sends the action to the first responder of application, this is used for - * emulating default Cocoa menu behaviors, usually you would just use the - * selector property of MenuItem. + * Sends the action to the first responder of application. + * This is used for emulating default Cocoa menu behaviors, + * usually you would just use the role property of MenuItem. * * Note: This method is OS X only. */ @@ -43,7 +46,7 @@ declare namespace Electron { * @param x Horizontal coordinate where the menu will be placed. * @param y Vertical coordinate where the menu will be placed. */ - popup(browserWindow: BrowserWindow, x?: number, y?: number): void; + popup(browserWindow?: BrowserWindow, x?: number, y?: number): void; /** * Appends the menuItem to the menu. */ @@ -52,6 +55,9 @@ declare namespace Electron { * Inserts the menuItem to the pos position of the menu. */ insert(position: number, menuItem: MenuItem): void; + /** + * @returns an array containing the menu’s items. + */ items: MenuItem[]; } }