]> git.gir.st - VimFx.git/blob - extension/lib/button.coffee
Support multi-process Firefox
[VimFx.git] / extension / lib / button.coffee
1 ###
2 # Copyright Simon Lydell 2015.
3 #
4 # This file is part of VimFx.
5 #
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.
10 #
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.
15 #
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/>.
18 ###
19
20 # This file creates VimFx’s toolbar button.
21
22 help = require('./help')
23 translate = require('./l10n')
24 utils = require('./utils')
25
26 BUTTON_ID = 'VimFxButton'
27
28 injectButton = (vimfx, window) ->
29 cui = window.CustomizableUI
30 button = null
31 cui.createWidget({
32 id: BUTTON_ID
33 defaultArea: cui.AREA_NAVBAR
34 label: 'VimFx'
35 tooltiptext: translate('button.tooltip.normal')
36 onCommand: ->
37 mode = button.getAttribute('vimfx-mode')
38 if mode == 'normal'
39 help.injectHelp(window, vimfx)
40 else
41 vimfx.getCurrentVim(window).enterMode('normal')
42 onCreated: (node) ->
43 button = node
44 button.setAttribute('vimfx-mode', 'normal')
45
46 updateButton = ->
47 # - The 'modeChange' event provides the `vim` object that changed mode,
48 # but it might not be the current `vim` anymore, so always get the
49 # current instance.
50 # - A 'TabSelect' event fires for the current tab when Firefox starts.
51 # By then a `vim` object for that tab might not have been constructed
52 # yet. If so, simply do nothing.
53 return unless vim = vimfx.getCurrentVim(window)
54 button.setAttribute('vimfx-mode', vim.mode)
55 tooltip =
56 if vim.mode == 'normal'
57 translate('button.tooltip.normal')
58 else
59 translate('button.tooltip.other_mode',
60 translate("mode.#{ vim.mode }"), translate('mode.normal'))
61 button.setAttribute('tooltiptext', tooltip)
62
63 vimfx.on('modeChange', updateButton)
64 utils.listen(window, 'TabSelect', updateButton)
65 })
66 module.onShutdown(cui.destroyWidget.bind(cui, BUTTON_ID))
67
68 module.exports = {
69 injectButton
70 BUTTON_ID
71 }
Imprint / Impressum