]> git.gir.st - VimFx.git/blob - extension/lib/button.coffee
Make it possible to escape from stuck "auto insert mode"
[VimFx.git] / extension / lib / button.coffee
1 ###
2 # Copyright Simon Lydell 2015, 2016.
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('./translate')
24 utils = require('./utils')
25
26 cui = Cu.import('resource:///modules/CustomizableUI.jsm', {}).CustomizableUI
27
28 BUTTON_ID = 'VimFxButton'
29
30 injectButton = (vimfx) ->
31 cui.createWidget({
32 id: BUTTON_ID
33 defaultArea: cui.AREA_NAVBAR
34 label: 'VimFx'
35 tooltiptext: translate('button.tooltip.normal')
36 onCommand: (event) ->
37 button = event.originalTarget
38 window = button.ownerGlobal
39 return unless vim = vimfx.getCurrentVim(window)
40
41 # If we somehow have gotten stuck with `vim.focusType == 'editable'`,
42 # allow the buttton to reset to 'none'.
43 vimfx.modes.normal.commands.esc.run({vim})
44
45 if vim.mode == 'normal'
46 help.toggleHelp(window, vimfx)
47 else
48 vim.enterMode('normal')
49 })
50 module.onShutdown(-> cui.destroyWidget(BUTTON_ID))
51
52 vimfx.on('modeDisplayChange', ({vim}) ->
53 {window} = vim
54 # When the browser starts, the button might not be available yet.
55 return unless button = getButton(window)
56
57 tooltip =
58 if vim.mode == 'normal'
59 translate('button.tooltip.normal')
60 else
61 translate(
62 'button.tooltip.other_mode',
63 translate("mode.#{vim.mode}"),
64 translate('mode.normal')
65 )
66 button.setAttribute('tooltiptext', tooltip)
67 )
68
69 getButton = (window) -> window.document.getElementById(BUTTON_ID)
70
71 module.exports = {
72 injectButton
73 getButton
74 }
Imprint / Impressum