From d01780a61303087820e57c722a135ac506857ccc Mon Sep 17 00:00:00 2001 From: Jia Hao Date: Tue, 19 Jan 2016 19:53:10 +0800 Subject: [PATCH] Implement basic application menu with shortcuts which supersedes mousetrap --- app/assets/js/index.js | 25 ---- app/assets/js/libs/mousetrap.min.js | 11 -- app/buildMenu.js | 199 ++++++++++++++++++++++++++++ app/index.html | 1 - app/main.js | 3 + 5 files changed, 202 insertions(+), 37 deletions(-) delete mode 100644 app/assets/js/libs/mousetrap.min.js create mode 100644 app/buildMenu.js diff --git a/app/assets/js/index.js b/app/assets/js/index.js index 6506cc5..63deb18 100644 --- a/app/assets/js/index.js +++ b/app/assets/js/index.js @@ -40,30 +40,5 @@ ipc.on('params', function(event, message) { var webViewDiv = document.getElementById('webViewDiv'); webViewDiv.appendChild(webView); - - Mousetrap.bind('mod+c', function(e) { - var webView = document.getElementById('webView'); - webView.copy(); - }); - - Mousetrap.bind('mod+x', function(e) { - var webView = document.getElementById('webView'); - webView.cut(); - }); - - Mousetrap.bind('mod+v', function(e) { - var webView = document.getElementById('webView'); - webView.paste(); - }); - - Mousetrap.bind('mod+a', function(e) { - var webView = document.getElementById('webView'); - webView.selectAll(); - }); - - Mousetrap.bind('mod+z', function(e) { - var webView = document.getElementById('webView'); - webView.undo(); - }); }); diff --git a/app/assets/js/libs/mousetrap.min.js b/app/assets/js/libs/mousetrap.min.js deleted file mode 100644 index 291aff8..0000000 --- a/app/assets/js/libs/mousetrap.min.js +++ /dev/null @@ -1,11 +0,0 @@ -/* mousetrap v1.5.3 craig.is/killing/mice */ -(function(C,r,g){function t(a,b,h){a.addEventListener?a.addEventListener(b,h,!1):a.attachEvent("on"+b,h)}function x(a){if("keypress"==a.type){var b=String.fromCharCode(a.which);a.shiftKey||(b=b.toLowerCase());return b}return l[a.which]?l[a.which]:p[a.which]?p[a.which]:String.fromCharCode(a.which).toLowerCase()}function D(a){var b=[];a.shiftKey&&b.push("shift");a.altKey&&b.push("alt");a.ctrlKey&&b.push("ctrl");a.metaKey&&b.push("meta");return b}function u(a){return"shift"==a||"ctrl"==a||"alt"==a|| -"meta"==a}function y(a,b){var h,c,e,g=[];h=a;"+"===h?h=["+"]:(h=h.replace(/\+{2}/g,"+plus"),h=h.split("+"));for(e=0;em||l.hasOwnProperty(m)&&(k[l[m]]=m)}e=k[h]?"keydown":"keypress"}"keypress"==e&&g.length&&(e="keydown");return{key:c,modifiers:g,action:e}}function B(a,b){return null===a||a===r?!1:a===b?!0:B(a.parentNode,b)}function c(a){function b(a){a= -a||{};var b=!1,n;for(n in q)a[n]?b=!0:q[n]=0;b||(v=!1)}function h(a,b,n,f,c,h){var g,e,l=[],m=n.type;if(!d._callbacks[a])return[];"keyup"==m&&u(a)&&(b=[a]);for(g=0;g":".","?":"/","|":"\\"},z={option:"alt",command:"meta","return":"enter", -escape:"esc",plus:"+",mod:/Mac|iPod|iPhone|iPad/.test(navigator.platform)?"meta":"ctrl"},k;for(g=1;20>g;++g)l[111+g]="f"+g;for(g=0;9>=g;++g)l[g+96]=g;c.prototype.bind=function(a,b,c){a=a instanceof Array?a:[a];this._bindMultiple.call(this,a,b,c);return this};c.prototype.unbind=function(a,b){return this.bind.call(this,a,function(){},b)};c.prototype.trigger=function(a,b){if(this._directMap[a+":"+b])this._directMap[a+":"+b]({},a);return this};c.prototype.reset=function(){this._callbacks={};this._directMap= -{};return this};c.prototype.stopCallback=function(a,b){return-1<(" "+b.className+" ").indexOf(" mousetrap ")||B(b,this.target)?!1:"INPUT"==b.tagName||"SELECT"==b.tagName||"TEXTAREA"==b.tagName||b.isContentEditable};c.prototype.handleKey=function(){return this._handleKey.apply(this,arguments)};c.init=function(){var a=c(r),b;for(b in a)"_"!==b.charAt(0)&&(c[b]=function(b){return function(){return a[b].apply(a,arguments)}}(b))};c.init();C.Mousetrap=c;"undefined"!==typeof module&&module.exports&&(module.exports= -c);"function"===typeof define&&define.amd&&define(function(){return c})})(window,document); diff --git a/app/buildMenu.js b/app/buildMenu.js new file mode 100644 index 0000000..6e9330e --- /dev/null +++ b/app/buildMenu.js @@ -0,0 +1,199 @@ +var electron = require('electron'); +var Menu = electron.Menu; + +module.exports = function (app) { + if (Menu.getApplicationMenu()) + return; + + var template = [ + { + label: 'Edit', + submenu: [ + { + label: 'Undo', + accelerator: 'CmdOrCtrl+Z', + role: 'undo' + }, + { + label: 'Redo', + accelerator: 'Shift+CmdOrCtrl+Z', + role: 'redo' + }, + { + type: 'separator' + }, + { + label: 'Cut', + accelerator: 'CmdOrCtrl+X', + role: 'cut' + }, + { + label: 'Copy', + accelerator: 'CmdOrCtrl+C', + role: 'copy' + }, + { + label: 'Paste', + accelerator: 'CmdOrCtrl+V', + role: 'paste' + }, + { + label: 'Select All', + accelerator: 'CmdOrCtrl+A', + role: 'selectall' + }, + ] + }, + { + label: 'View', + submenu: [ + { + label: 'Reload', + accelerator: 'CmdOrCtrl+R', + click: function(item, focusedWindow) { + if (focusedWindow) + focusedWindow.reload(); + } + }, + { + label: 'Toggle Full Screen', + accelerator: (function() { + if (process.platform == 'darwin') + return 'Ctrl+Command+F'; + else + return 'F11'; + })(), + click: function(item, focusedWindow) { + if (focusedWindow) + focusedWindow.setFullScreen(!focusedWindow.isFullScreen()); + } + }, + { + label: 'Toggle Window Developer Tools', + accelerator: (function() { + if (process.platform == 'darwin') + return 'Alt+Command+I'; + else + return 'Ctrl+Shift+I'; + })(), + click: function(item, focusedWindow) { + if (focusedWindow) + focusedWindow.toggleDevTools(); + } + }, + { + label: 'Toggle Web Developer Tools', + accelerator: (function() { + if (process.platform == 'darwin') + return 'Alt+Command+J'; + else + return 'Ctrl+Shift+J'; + })(), + click: function(item, focusedWindow) { + if (focusedWindow) + focusedWindow.toggleDevTools(); + } + }, + ] + }, + { + label: 'Window', + role: 'window', + submenu: [ + { + label: 'Minimize', + accelerator: 'CmdOrCtrl+M', + role: 'minimize' + }, + { + label: 'Close', + accelerator: 'CmdOrCtrl+W', + role: 'close' + }, + ] + }, + { + label: 'Help', + role: 'help', + submenu: [ + { + label: 'Learn More', + click: function() { shell.openExternal('http://electron.atom.io') } + }, + { + label: 'Documentation', + click: function() { + shell.openExternal( + `https://github.com/atom/electron/tree/v${process.versions.electron}/docs#readme` + ) + } + }, + { + label: 'Community Discussions', + click: function() { shell.openExternal('https://discuss.atom.io/c/electron') } + }, + { + label: 'Search Issues', + click: function() { shell.openExternal('https://github.com/atom/electron/issues') } + } + ] + }, + ]; + + if (process.platform == 'darwin') { + template.unshift({ + label: 'Electron', + submenu: [ + { + label: 'About Electron', + role: 'about' + }, + { + type: 'separator' + }, + { + label: 'Services', + role: 'services', + submenu: [] + }, + { + type: 'separator' + }, + { + label: 'Hide Electron', + 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(); } + }, + ] + }); + template[3].submenu.push( + { + type: 'separator' + }, + { + label: 'Bring All to Front', + role: 'front' + } + ); + } + + var menu = Menu.buildFromTemplate(template); + Menu.setApplicationMenu(menu); +}; diff --git a/app/index.html b/app/index.html index f8d1347..d4da9f6 100644 --- a/app/index.html +++ b/app/index.html @@ -4,7 +4,6 @@ Nativefier - diff --git a/app/main.js b/app/main.js index 68d9a27..b7b673b 100644 --- a/app/main.js +++ b/app/main.js @@ -9,6 +9,8 @@ var app = electron.app; var BrowserWindow = electron.BrowserWindow; var ipc = electron.ipcMain; +var buildMenu = require('./buildMenu'); + const APP_ARGS_FILE_PATH = __dirname + '/nativefier.json'; require('crash-reporter').start(); @@ -23,6 +25,7 @@ app.on('window-all-closed', function() { }); app.on('ready', function() { + buildMenu(app); mainWindow = new BrowserWindow( { width: appArgs.width || 1280,