fix(menu): Allow fast-switch between accounts

This commit is contained in:
Ben Gotow 2015-11-06 18:19:42 -08:00
parent 2e3ab31952
commit e87b5700e2
3 changed files with 27 additions and 6 deletions

View file

@ -7,7 +7,6 @@
'body': 'body':
'cmd-m': 'application:minimize' 'cmd-m': 'application:minimize'
'cmd-h': 'application:hide' 'cmd-h': 'application:hide'
'cmd-0': 'application:show-main-window'
'cmd-alt-h': 'application:hide-other-applications' 'cmd-alt-h': 'application:hide-other-applications'
'cmd-ctrl-f': 'window:toggle-full-screen' 'cmd-ctrl-f': 'window:toggle-full-screen'
'alt-cmd-ctrl-m': 'application:zoom' 'alt-cmd-ctrl-m': 'application:zoom'

View file

@ -78,7 +78,6 @@
{ label: 'Minimize', command: 'application:minimize' } { label: 'Minimize', command: 'application:minimize' }
{ label: 'Zoom', command: 'application:zoom' } { label: 'Zoom', command: 'application:zoom' }
{ type: 'separator' } { type: 'separator' }
{ label: 'Message Viewer', command: 'application:show-main-window' }
{ type: 'separator' } { type: 'separator' }
{ label: 'Bring All to Front', command: 'application:bring-all-windows-to-front' } { label: 'Bring All to Front', command: 'application:bring-all-windows-to-front' }
] ]

View file

@ -1,6 +1,7 @@
Actions = require '../actions' Actions = require '../actions'
Account = require '../models/account' Account = require '../models/account'
Utils = require '../models/utils' Utils = require '../models/utils'
MenuHelpers = require '../../menu-helpers'
DatabaseStore = require './database-store' DatabaseStore = require './database-store'
_ = require 'underscore' _ = require 'underscore'
@ -33,16 +34,37 @@ class AccountStore
if newAccountIds.length > 0 if newAccountIds.length > 0
Actions.selectAccountId(newAccountIds[0]) Actions.selectAccountId(newAccountIds[0])
atom.commands.add 'body', @_accountSwitchCommands() @_setupFastAccountCommands()
_accountSwitchCommands: -> _setupFastAccountCommands: ->
commands = {} commands = {}
[0..8].forEach (index) => [0..8].forEach (index) =>
key = "application:select-account-#{index}" key = "application:select-account-#{index}"
commands[key] = _.partial(@_selectAccountByIndex, index) commands[key] = _.partial(@_selectAccountByIndex, index)
return commands atom.commands.add('body', commands)
_setupFastAccountMenu: ->
windowMenu = _.find atom.menu.template, ({label}) -> MenuHelpers.normalizeLabel(label) is 'Window'
return unless windowMenu
submenu = _.reject windowMenu.submenu, (item) -> item.account
console.log(submenu)
return unless submenu
idx = _.findIndex submenu, ({type}) -> type is 'separator'
return unless idx > 0
accountMenuItems = @items().map (item, idx) =>
{
label: item.emailAddress,
command: "application:select-account-#{idx}",
account: true
}
submenu.splice(idx + 1, 0, accountMenuItems...)
windowMenu.submenu = submenu
atom.menu.update()
_selectAccountByIndex: (index) => _selectAccountByIndex: (index) =>
require('ipc').send('command', 'application:show-main-window')
index = Math.min(@_accounts.length - 1, Math.max(0, index)) index = Math.min(@_accounts.length - 1, Math.max(0, index))
Actions.selectAccountId(@_accounts[index].id) Actions.selectAccountId(@_accounts[index].id)
@ -55,6 +77,7 @@ class AccountStore
@_index = Math.min(@_accounts.length - 1, Math.max(0, index)) @_index = Math.min(@_accounts.length - 1, Math.max(0, index))
@_tokens = atom.config.get(saveTokensKey) || {} @_tokens = atom.config.get(saveTokensKey) || {}
@_setupFastAccountMenu()
@trigger() @trigger()
_save: => _save: =>
@ -67,7 +90,7 @@ class AccountStore
onSelectAccountId: (id) => onSelectAccountId: (id) =>
idx = _.findIndex @_accounts, (a) -> a.id is id idx = _.findIndex @_accounts, (a) -> a.id is id
return if idx is -1 return if idx is -1 or @_index is idx
atom.config.set(saveIndexKey, idx) atom.config.set(saveIndexKey, idx)
@_index = idx @_index = idx
@trigger() @trigger()