Mailspring/internal_packages/account-sidebar/lib/account-commands.coffee
Ben Gotow a48ddd51f8 refactor(menus): Major prune of keymaps/commands, real conditional menus
Summary:
Keymaps & menus CSON => JSON, remove AtomKeymaps, CommandRegistry use of CSS selectors, use Mousetrap instead

Important Notes:

- The `application:` prefix is reserved for commands which are handled in the application process. Don't use it for other things. You will not receive the events in the window.

- Maintaining dynamic menus seems to come with quite an overhead, because Electron updates the entire menu every time. In the future, we'll need https://github.com/electron/electron/issues/528 to really make things nice. I will be tracking this upstream.

- The format for keyboard shortcuts has changed. `cmd-X` is now `command+shift+x`

Test Plan: Run tests

Reviewers: juan, evan

Reviewed By: evan

Differential Revision: https://phab.nylas.com/D2917
2016-04-24 12:16:25 -05:00

89 lines
2.7 KiB
CoffeeScript

_ = require 'underscore'
{AccountStore, MenuHelpers} = require 'nylas-exports'
SidebarActions = require './sidebar-actions'
class AccountCommands
@_focusAccounts: (accounts) ->
SidebarActions.focusAccounts(accounts)
NylasEnv.show() unless NylasEnv.isVisible()
@_isSelected: (account, focusedAccounts) =>
if focusedAccounts.length > 1
return account instanceof Array
else if focusedAccounts.length is 1
return account?.id is focusedAccounts[0].id
else
return false
@registerCommands: (accounts) ->
@_commandsDisposable?.dispose()
commands = {}
allKey = "window:select-account-0"
commands[allKey] = @_focusAccounts.bind(@, accounts)
[1..8].forEach (index) =>
account = accounts[index - 1]
return unless account
key = "window:select-account-#{index}"
commands[key] = @_focusAccounts.bind(@, [account])
@_commandsDisposable = NylasEnv.commands.add(document.body, commands)
@registerMenuItems: (accounts, focusedAccounts) ->
windowMenu = _.find NylasEnv.menu.template, ({label}) ->
MenuHelpers.normalizeLabel(label) is 'Window'
return unless windowMenu
submenu = _.reject windowMenu.submenu, (item) -> item.account
return unless submenu
idx = _.findIndex submenu, ({type}) -> type is 'separator'
return unless idx > 0
template = @menuTemplate(accounts, focusedAccounts)
submenu.splice(idx + 1, 0, template...)
windowMenu.submenu = submenu
NylasEnv.menu.update()
@menuItem: (account, idx, {isSelected, clickHandlers} = {}) =>
item = {
label: account.label ? "All Accounts",
command: "window:select-account-#{idx}",
account: true
}
if isSelected
item.type = 'checkbox'
item.checked = true
if clickHandlers
accounts = if account instanceof Array then account else [account]
item.click = @_focusAccounts.bind(@, accounts)
item.accelerator = "CmdOrCtrl+#{idx + 1}"
return item
@menuTemplate: (accounts, focusedAccounts, {clickHandlers} = {}) =>
template = []
multiAccount = accounts.length > 1
if multiAccount
isSelected = @_isSelected(accounts, focusedAccounts)
template = [
@menuItem(accounts, 0, {isSelected, clickHandlers})
]
template = template.concat accounts.map((account, idx) =>
# If there's only one account, it should be mapped to command+1, not command+2
accIdx = if multiAccount then idx + 1 else idx
isSelected = @_isSelected(account, focusedAccounts)
return @menuItem(account, accIdx, {isSelected, clickHandlers})
)
return template
@register: (accounts, focusedAccounts) ->
@registerCommands(accounts)
@registerMenuItems(accounts, focusedAccounts)
module.exports = AccountCommands