Skip to content

Commit

Permalink
⬆️ menus.
Browse files Browse the repository at this point in the history
  • Loading branch information
hokein committed May 21, 2016
1 parent 6b8113f commit 30943ee
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 143 deletions.
44 changes: 23 additions & 21 deletions menus/index.html
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
<script>
var remote = require('remote');
var Menu = remote.require('menu');
var MenuItem = remote.require('menu-item');

var submenu = new Menu();
submenu.append(new MenuItem({ type: 'checkbox', label: 'box1' }));
submenu.append(new MenuItem({ type: 'checkbox', label: 'box2' }));
submenu.append(new MenuItem({ type: 'checkbox', label: 'box3' }));
submenu.append(new MenuItem({ type: 'checkbox', label: 'box4' }));

var menu1 = new Menu();
menu1.append(new MenuItem({ label: 'MenuItem1',click: function() { alert('item 1 clicked'); } }));
menu1.append(new MenuItem({ label: 'MenuItem2', type: 'checkbox', checked: true }));
menu1.append(new MenuItem({ label: 'Disk', submenu: submenu}));

window.addEventListener('contextmenu', function (e) {
e.preventDefault();
menu1.popup(remote.getCurrentWindow());
}, false);
</script>
<script>
const {remote} = require('electron');
const {Menu, MenuItem} = remote;

let submenu = new Menu();
submenu.append(new MenuItem({ type: 'checkbox', label: 'box1' }));
submenu.append(new MenuItem({ type: 'checkbox', label: 'box2' }));
submenu.append(new MenuItem({ type: 'checkbox', label: 'box3' }));
submenu.append(new MenuItem({ type: 'checkbox', label: 'box4' }));

let menu1 = new Menu();
menu1.append(new MenuItem({ label: 'MenuItem1', click () { alert('item 1 clicked'); } }));
menu1.append(new MenuItem({ label: 'MenuItem2', type: 'checkbox', checked: true }));
menu1.append(new MenuItem({ label: 'Disk', submenu: submenu}));

window.addEventListener('contextmenu', function (e) {
e.preventDefault();
menu1.popup(remote.getCurrentWindow());
}, false);
</script>


<h1>Right click the webpage to show the menu.<h1>
238 changes: 117 additions & 121 deletions menus/main.js
Original file line number Diff line number Diff line change
@@ -1,121 +1,117 @@
var app = require('app'); // Module to control application life.
var BrowserWindow = require('browser-window'); // Module to create native browser window.
var Menu = require('menu');

// Report crashes to our server.
require('crash-reporter').start();

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the javascript object is GCed.
var mainWindow = null;

// Quit when all windows are closed.
app.on('window-all-closed', function() {
if (process.platform != 'darwin')
app.quit();
});

// This method will be called when Electron has done everything
// initialization and ready for creating browser windows.
app.on('ready', function() {
// Create the browser window.
mainWindow = new BrowserWindow({width: 800, height: 600});

// and load the index.html of the app.
mainWindow.loadUrl('file://' + __dirname + '/index.html');

var application_menu = [
{
label: 'menu1',
submenu: [
{
label: 'Undo',
accelerator: 'CmdOrCtrl+Z',
role: 'undo'
},
{
label: 'Open',
accelerator: 'CmdOrCtrl+O',
click: function() {
require('electron').dialog.showOpenDialog({ properties: [ 'openFile', 'openDirectory', 'multiSelections' ]});
}
},
{
label: 'submenu1',
submenu: [
{
label: 'item1',
accelerator: 'CmdOrCtrl+A',
click: function() {
mainWindow.openDevTools();
}
},
{
label: 'item2',
accelerator: 'CmdOrCtrl+B',
click: function() {
mainWindow.closeDevTools();
}
}
]
}
]
}
];
if (process.platform == 'darwin') {
var name = require('electron').app.getName();
application_menu.unshift({
label: name,
submenu: [
{
label: 'About ' + name,
role: 'about'
},
{
type: 'separator'
},
{
label: 'Services',
role: 'services',
submenu: []
},
{
type: 'separator'
},
{
label: 'Hide ' + name,
accelerator: 'Command+H',
role: 'hide'
},
{
label: 'Hide Others',
accelerator: 'Command+Shift+H',
role: 'hideothers'
},
{
label: 'Show All',
role: 'unhide'
},
{
type: 'separator'
},
{
label: 'Quit',
accelerator: 'Command+Q',
click: function() { app.quit(); }
},
]
});
}

menu = Menu.buildFromTemplate(application_menu);
Menu.setApplicationMenu(menu);

// Emitted when the window is closed.
mainWindow.on('closed', function() {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null;
});
});
const electron = require('electron');
const {app, BrowserWindow, Menu} = require('electron');

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the javascript object is GCed.
let mainWindow;

// Quit when all windows are closed.
app.on('window-all-closed', () => {
if (process.platform != 'darwin')
app.quit();
});

// This method will be called when Electron has done everything
// initialization and ready for creating browser windows.
app.on('ready', function() {
// Create the browser window.
mainWindow = new BrowserWindow({width: 800, height: 600});

// and load the index.html of the app.
mainWindow.loadURL('file://' + __dirname + '/index.html');

var application_menu = [
{
label: 'menu1',
submenu: [
{
label: 'Undo',
accelerator: 'CmdOrCtrl+Z',
role: 'undo'
},
{
label: 'Open',
accelerator: 'CmdOrCtrl+O',
click: () => {
electron.dialog.showOpenDialog({ properties: [ 'openFile', 'openDirectory', 'multiSelections' ]});
}
},
{
label: 'submenu1',
submenu: [
{
label: 'item1',
accelerator: 'CmdOrCtrl+A',
click: () => {
mainWindow.openDevTools();
}
},
{
label: 'item2',
accelerator: 'CmdOrCtrl+B',
click: () => {
mainWindow.closeDevTools();
}
}
]
}
]
}
];
if (process.platform == 'darwin') {
const name = app.getName();
application_menu.unshift({
label: name,
submenu: [
{
label: 'About ' + name,
role: 'about'
},
{
type: 'separator'
},
{
label: 'Services',
role: 'services',
submenu: []
},
{
type: 'separator'
},
{
label: 'Hide ' + name,
accelerator: 'Command+H',
role: 'hide'
},
{
label: 'Hide Others',
accelerator: 'Command+Shift+H',
role: 'hideothers'
},
{
label: 'Show All',
role: 'unhide'
},
{
type: 'separator'
},
{
label: 'Quit',
accelerator: 'Command+Q',
click: () => { app.quit(); }
},
]
});
}

menu = Menu.buildFromTemplate(application_menu);
Menu.setApplicationMenu(menu);

// Emitted when the window is closed.
mainWindow.on('closed', function() {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null;
});
});
2 changes: 1 addition & 1 deletion menus/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name" : "menus-demo",
"version" : "0.1.0",
"version" : "1.1.0",
"main" : "main.js"
}

0 comments on commit 30943ee

Please sign in to comment.