Skip to content

Commit

Permalink
desktop: move menus into config files and cleanup
Browse files Browse the repository at this point in the history
Includes menus for linux and windows. They have not been tested yet
  • Loading branch information
evanlucas committed Apr 3, 2016
1 parent 8dfca48 commit 742412e
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 99 deletions.
110 changes: 44 additions & 66 deletions desktop.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ const inherits = require('util').inherits
const App = require('./app')
const remote = require('remote')
const app = remote.require('app')
const BrowserWindow = remote.require('browser-window')
const ipcRenderer = require('electron').ipcRenderer
const shell = require('shell')
const currentWindow = remote.getCurrentWindow()
const Menu = remote.require('menu')
const pkg = require('./package')

module.exports = Desktop

Expand All @@ -33,78 +34,55 @@ function Desktop() {
}
inherits(Desktop, App)

Desktop.prototype.setupMenu = function setupMenu() {
const template = [
{
label: 'EyeAreSee'
, submenu: [
{ label: 'About'
, click: () => {
this.router.goto('/about')
}
}
, { label: 'Settings'
, accelerator: 'CommandOrControl+,'
, click: () => {
this.router.goto('/settings')
}
}
, { type: 'separator' }
, { label: 'Toggle DevTools'
, accelerator: 'Alt+Command+I'
, click: function() {
BrowserWindow.getFocusedWindow().toggleDevTools()
}
}
, { label: 'Quit'
, accelerator: 'Command+Q'
, click: function() { app.quit() }
}
]
Desktop.prototype._handlers = function _handlers() {
return {
'application:devtools': () => {
const win = this.window
if (win) win.toggleDevTools()
}
, {
label: 'Edit'
, submenu: [
{ label: 'Undo', accelerator: 'Command+Z', selector: 'undo:' }
, { label: 'Redo', accelerator: 'Shift+Command+Z', selector: 'redo:' }
, { type: 'separator' }
, { label: 'Cut', accelerator: 'Command+X', selector: 'cut:' }
, { label: 'Copy', accelerator: 'Command+C', selector: 'copy:' }
, { label: 'Paste', accelerator: 'Command+V', selector: 'paste:' }
]
, 'application:next-panel': () => { this.panels.nextPanel() }
, 'application:prev-panel': () => { this.panels.previousPanel() }
, 'application:show-userbar': () => { this.panels.showUserbar() }
, 'application:hide-userbar': () => { this.panels.hideUserbar() }
, 'application:quit': () => { app.quit() }
, 'application:minimize': () => {
const win = this.window
if (win) win.minimize()
}
, {
label: 'View'
, submenu: [
{ label: 'Next Panel'
, accelerator: 'CommandOrControl+Alt+Down'
, click: () => {
this.panels.nextPanel()
}
}
, { label: 'Previous Panel'
, accelerator: 'CommandOrControl+Alt+Up'
, click: () => {
this.panels.previousPanel()
}
}
, { label: 'Show Userbar'
, accelerator: 'CommandOrControl+Alt+Left'
, click: () => {
this.panels.showUserbar()
, 'application:maximize': () => {
const win = this.window
if (win) win.maximize()
}
, 'application:open-repo': () => { this.emit('openUrl', pkg.repository.url) }
}
}

Desktop.prototype.setupMenu = function setupMenu() {
const handlers = this._handlers()

const tmp = require(`./lib/menus/${process.platform}`)
for (let toplevel of tmp) {
const sub = toplevel.submenu
if (sub && sub.length) {
for (let item of sub) {
if (item.url) {
item.click = () => {
this.router.goto(item.url)
}
}
, { label: 'Hide Userbar'
, accelerator: 'CommandOrControl+Alt+Right'
, click: () => {
this.panels.hideUserbar()
} else if (item.command) {
item.click = () => {
if (handlers[item.command]) {
handlers[item.command]()
} else {
ipcRenderer.send('command', item.command)
}
}
}
]
}
}
]
}

const menu = Menu.buildFromTemplate(template)
const menu = Menu.buildFromTemplate(tmp)
Menu.setApplicationMenu(menu)
}

Expand Down
36 changes: 3 additions & 33 deletions lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const electron = require('electron')
const app = electron.app
const Menu = electron.Menu
const ipcMain = electron.ipcMain
const BrowserWindow = electron.BrowserWindow

module.exports = App

Expand All @@ -29,48 +30,17 @@ App.prototype.handleEvents = function() {
app.quit()
})

app.on('open-url', (ev, url) => {
ev.preventDefault()
console.log('open-url', ev, url)
})

if (process.platform === 'darwin') {
this.on('app:hide', () => {
Menu.sendActionToFirstResponder('hide:')
})
this.on('app:minimize', () => {
Menu.sendActionToFirstResponder('performMiniaturize:')
})
this.on('app:zoom', () => {
this.on('application:zoom', () => {
Menu.sendActionToFirstResponder('zoom:')
})
} else {
this.on('app:minimize', () => {
BrowserWindow.getFocusedWindow().minimize()
})
this.on('app:zoom', () => {
this.on('application:zoom', () => {
BrowserWindow.getFocusedWindow().maximize()
})
}

this.openPathOnEvent('app:about', '/about')
this.openPathOnEvent('app:show-settings', '/config')
this.openPathOnEvent('app:show-license', '/license')

ipcMain.on('command', (ev, cmd) => {
console.log('emit %s', cmd)
this.emit(cmd)
})

ipcMain.on('window-command', (ev, command, ...args) => {
if (this.window[command])
this.window[command](...args)
})
}

App.prototype.openPathOnEvent = function(ev, path) {
this.on(ev, () => {
console.log('openPathOnEvent %s %s', ev, path)
this.window.webContents.send('goto:url', path)
})
}
62 changes: 62 additions & 0 deletions lib/menus/darwin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
'use strict'

module.exports = [
{ label: 'eyearesee'
, submenu: [
{ label: 'About', url: '/about' }
, { label: 'Preferences'
, url: '/settings'
, accelerator: 'CmdOrCtrl+,'
}
, { type: 'separator' }
, { label: 'Quit', accelerator: 'Command+Q', command: 'application:quit' }
]
}
, { label: 'Edit'
, submenu: [
{ label: 'Undo', accelerator: 'Command+Z', selector: 'undo:' }
, { label: 'Redo', accelerator: 'Shift+Command+Z', selector: 'redo:' }
, { type: 'separator' }
, { label: 'Cut', accelerator: 'Command+X', selector: 'cut:' }
, { label: 'Copy', accelerator: 'Command+C', selector: 'copy:' }
, { label: 'Paste', accelerator: 'Command+V', selector: 'paste:' }
]
}
, { label: 'View'
, submenu: [
{ label: 'Toggle DevTools', command: 'application:devtools' }
, { type: 'separator' }
, { label: 'Next Panel'
, accelerator: 'CommandOrControl+Alt+Down'
, command: 'application:next-panel'
}
, { label: 'Previous Panel'
, accelerator: 'CommandOrControl+Alt+Up'
, command: 'application:prev-panel'
}
, { label: 'Show Userbar'
, accelerator: 'CommandOrControl+Alt+Left'
, command: 'application:show-userbar'
}
, { label: 'Hide Userbar'
, accelerator: 'CommandOrControl+Alt+Right'
, command: 'application:hide-userbar'
}
]
}
, { label: 'Window'
, submenu: [
{ label: 'Minimize'
, accelerator: 'Command+M'
, command: 'application:minimize'
}
, { label: 'Maximize', command: 'application:maximize' }
, { label: 'Zoom', command: 'application:zoom' }
]
}
, { label: 'Help'
, submenu: [
{ label: 'Repository', command: 'application:open-repo' }
]
}
]
47 changes: 47 additions & 0 deletions lib/menus/linux.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict'

module.exports = [
{ label: 'File'
, submenu: [
{ label: 'Preferences', url: '/settings', accelerator: 'CmdOrCtrl+,' }
, { label: 'Exit', command: 'application:quit' }
]
}
, { 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: 'View'
, submenu: [
{ label: 'Toggle DevTools', command: 'application:devtools' }
, { type: 'separator' }
, { label: 'Next Panel'
, accelerator: 'CmdOrCtrl+Alt+Down'
, command: 'application:next-panel'
}
, { label: 'Previous Panel'
, accelerator: 'CmdOrCtrl+Alt+Up'
, command: 'application:prev-panel'
}
, { label: 'Show Userbar'
, accelerator: 'CmdOrCtrl+Alt+Left'
, command: 'application:show-userbar'
}
, { label: 'Hide Userbar'
, accelerator: 'CmdOrCtrl+Alt+Right'
, command: 'application:hide-userbar'
}
]
}
, { label: 'Help'
, submenu: [
{ label: 'About', url: '/about' }
]
}
]
47 changes: 47 additions & 0 deletions lib/menus/win32.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict'

module.exports = [
{ label: 'File'
, submenu: [
{ label: 'Preferences', url: '/settings', accelerator: 'CmdOrCtrl+,' }
, { label: 'Exit', command: 'application:quit' }
]
}
, { 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: 'View'
, submenu: [
{ label: 'Toggle DevTools', command: 'application:devtools' }
, { type: 'separator' }
, { label: 'Next Panel'
, accelerator: 'CmdOrCtrl+Alt+Down'
, command: 'application:next-panel'
}
, { label: 'Previous Panel'
, accelerator: 'CmdOrCtrl+Alt+Up'
, command: 'application:prev-panel'
}
, { label: 'Show Userbar'
, accelerator: 'CmdOrCtrl+Alt+Left'
, command: 'application:show-userbar'
}
, { label: 'Hide Userbar'
, accelerator: 'CmdOrCtrl+Alt+Right'
, command: 'application:hide-userbar'
}
]
}
, { label: 'Help'
, submenu: [
{ label: 'About', url: '/about' }
]
}
]

0 comments on commit 742412e

Please sign in to comment.