Update menu + menu-item modules

This commit is contained in:
Milan Burda
2016-03-26 02:11:19 +01:00
parent b0de3d5e0d
commit 2bd3c04d61
3 changed files with 88 additions and 32 deletions
+32 -15
View File
@@ -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 = <Electron.MenuItemOptions[]>[
{
label: 'Electron',
submenu: [
{
label: 'About Electron',
selector: 'orderFrontStandardAboutPanel:'
role: 'about'
},
{
type: 'separator'
},
{
label: 'Services',
submenu: <any[]>[]
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'
}
]
},
+43 -10
View File
@@ -3,29 +3,62 @@
// Definitions by: jedmao <https://github.com/jedmao/>, rhysd <https://rhysd.github.io>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference path="github-electron.browser-window.d.ts" />
/// <reference path="github-electron.menu.d.ts" />
/// <reference path="github-electron.native-image.d.ts" />
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;
}
}
+13 -7
View File
@@ -4,15 +4,18 @@
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference path="github-electron.browser-window.d.ts" />
/// <reference path="github-electron.event-emitter.d.ts" />
/// <reference path="github-electron.menu-item.d.ts" />
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 menus items.
*/
items: MenuItem[];
}
}