2 # Copyright Simon Lydell 2015.
4 # This file is part of VimFx.
6 # VimFx is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # VimFx is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with VimFx. If not, see <http://www.gnu.org/licenses/>.
20 # This file creates VimFx’s toolbar button.
22 help = require('./help')
23 translate = require('./l10n')
24 utils = require('./utils')
26 cui = Cu.import('resource:///modules/CustomizableUI.jsm', {}).CustomizableUI
28 BUTTON_ID = 'VimFxButton'
30 injectButton = (vimfx) ->
33 defaultArea: cui.AREA_NAVBAR
35 tooltiptext: translate('button.tooltip.normal')
37 window = utils.getCurrentWindow()
38 button = getButton(window)
39 mode = button.getAttribute('vimfx-mode')
41 help.injectHelp(window, vimfx)
43 vimfx.getCurrentVim(window).enterMode('normal')
45 node.setAttribute('vimfx-mode', 'normal')
48 window = utils.getCurrentWindow()
49 button = getButton(window)
51 # - The 'modeChange' event provides the `vim` object that changed mode,
52 # but it might not be the current `vim` anymore, so always get the
54 # - A 'TabSelect' event fires for the current tab when Firefox starts.
55 # By then a `vim` object for that tab might not have been constructed
56 # yet. If so, simply do nothing.
57 return unless vim = vimfx.getCurrentVim(window)
59 button.setAttribute('vimfx-mode', vim.mode)
61 if vim.mode == 'normal'
62 translate('button.tooltip.normal')
64 translate('button.tooltip.other_mode',
65 translate("mode.#{ vim.mode }"), translate('mode.normal'))
66 button.setAttribute('tooltiptext', tooltip)
68 vimfx.on('modeChange', updateButton)
69 vimfx.on('TabSelect', updateButton)
71 module.onShutdown(cui.destroyWidget.bind(cui, BUTTON_ID))
73 getButton = (window) -> window.document.getElementById(BUTTON_ID)