]> git.gir.st - VimFx.git/blob - extension/lib/events.coffee
Add support for `<late>` shortcuts
[VimFx.git] / extension / lib / events.coffee
1 ###
2 # Copyright Anton Khodakivskiy 2012, 2013, 2014.
3 # Copyright Simon Lydell 2013, 2014.
4 #
5 # This file is part of VimFx.
6 #
7 # VimFx is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # VimFx is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with VimFx. If not, see <http://www.gnu.org/licenses/>.
19 ###
20
21 # This file sets up all event listeners needed to power VimFx: To know when to
22 # launch commands and to provide state to them. Events in web page content are
23 # listened for in events-frame.coffee.
24
25 button = require('./button')
26 utils = require('./utils')
27
28 HELD_MODIFIERS_ATTRIBUTE = 'vimfx-held-modifiers'
29
30 class UIEventManager
31 constructor: (@vimfx, @window) ->
32 @listen = utils.listen.bind(null, @window)
33 @listenOnce = utils.listenOnce.bind(null, @window)
34
35 # This flag controls whether to suppress the various key events or not.
36 @suppress = false
37
38 # When a menu or panel is shown VimFx should temporarily stop processing
39 # keyboard input, allowing accesskeys to be used.
40 @popupPassthrough = false
41
42 addListeners: ->
43 checkPassthrough = (value, event) =>
44 if event.target.nodeName in ['menupopup', 'panel']
45 @popupPassthrough = value
46
47 @listen('popupshown', checkPassthrough.bind(null, true))
48 @listen('popuphidden', checkPassthrough.bind(null, false))
49
50 @listen('keydown', (event) =>
51 try
52 # No matter what, always reset the `suppress` flag, so we don't suppress
53 # more than intended.
54 @suppress = false
55
56 if @popupPassthrough
57 # The `@popupPassthrough` flag is set a bit unreliably. Sometimes it
58 # can be stuck as `true` even though no popup is shown, effectively
59 # disabling the extension. Therefore we check if there actually _are_
60 # any open popups before stopping processing keyboard input. This is
61 # only done when popups (might) be open (not on every keystroke) of
62 # performance reasons.
63 #
64 # The autocomplete popup in text inputs (for example) is technically a
65 # panel, but it does not respond to key presses. Therefore
66 # `[ignorekeys="true"]` is excluded.
67 #
68 # coffeelint: disable=max_line_length
69 # <https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XUL/PopupGuide/PopupKeys#Ignoring_Keys>
70 # coffeelint: enable=max_line_length
71 popups = @window.document.querySelectorAll(
72 ':-moz-any(menupopup, panel):not([ignorekeys="true"])'
73 )
74 for popup in popups
75 return if popup.state == 'open'
76 @popupPassthrough = false # No popup was actually open.
77
78 vim = @vimfx.getCurrentVim(@window)
79
80 if vim.isFrameEvent(event)
81 vim._listenOnce('consumeKeyEvent', ({ focusType }) =>
82 @consumeKeyEvent(vim, event, focusType, { isFrameEvent: true })
83 return @suppress
84 )
85 else
86 @consumeKeyEvent(vim, event, utils.getFocusType(event))
87 # This also suppresses the 'keypress' event.
88 utils.suppressEvent(event) if @suppress
89
90 catch error
91 console.error(utils.formatError(error))
92 )
93
94 @listen('keyup', (event) =>
95 utils.suppressEvent(event) if @suppress
96 @setHeldModifiers(event, {filterCurrentOnly: true})
97 )
98
99 checkFindbar = (mode, event) =>
100 target = event.originalTarget
101 findBar = @window.gBrowser.getFindBar()
102 if target == findBar._findField.mInputField
103 vim = @vimfx.getCurrentVim(@window)
104 vim.enterMode(mode)
105
106 @listen('focus', checkFindbar.bind(null, 'find'))
107 @listen('blur', checkFindbar.bind(null, 'normal'))
108
109 @listen('click', (event) =>
110 target = event.originalTarget
111 vim = @vimfx.getCurrentVim(@window)
112
113 # If the user clicks the reload button or a link when in hints mode, we’re
114 # going to end up in hints mode without any markers. Or if the user clicks
115 # a text input, then that input will be focused, but you can’t type in it
116 # (instead markers will be matched). So if the user clicks anything in
117 # hints mode it’s better to leave it.
118 if vim.mode == 'hints' and not vim.isFrameEvent(event) and
119 # Exclude the VimFx button, though, since clicking it returns to normal
120 # mode. Otherwise we’d first return to normal mode and then the button
121 # would open the help dialog.
122 target != button.getButton(@window)
123 vim.enterMode('normal')
124 )
125
126 @listen('TabSelect', (event) =>
127 @vimfx.emit('TabSelect', event)
128
129 # The initial 'TabSelect' when the browser starts fires before a vim
130 # object has been created for that tab.
131 return unless vim = @vimfx.getCurrentVim(@window)
132 vim._send('TabSelect')
133 )
134
135 progressListener =
136 onLocationChange: (progress, request, location, flags) =>
137 unless flags & Ci.nsIWebProgressListener.LOCATION_CHANGE_SAME_DOCUMENT
138 # `onLocationChange` might fire before a vim object has been created
139 # for the current tab.
140 return unless vim = @vimfx.getCurrentVim(@window)
141 vim._onLocationChange(location.spec)
142
143 @window.gBrowser.addProgressListener(progressListener)
144 module.onShutdown(=>
145 @window.gBrowser.removeProgressListener(progressListener)
146 )
147
148 consumeKeyEvent: (vim, event, focusType, options = {}) ->
149 match = vim._consumeKeyEvent(event, focusType)
150 switch
151 when not match
152 @suppress = null
153 when match.specialKeys['<late>']
154 @suppress = false
155 @consumeLateKeydown(vim, event, match, options)
156 else
157 @suppress = vim._onInput(match, options)
158 @setHeldModifiers(event)
159
160 consumeLateKeydown: (vim, event, match, options) ->
161 { isFrameEvent = false } = options
162
163 # The passed in `event` is the regular non-late browser UI keydown event.
164 # It is only used to set held keys. This is easier than sending an event
165 # subset from frame scripts.
166 listener = ({ defaultPrevented }) =>
167 @suppress =
168 if defaultPrevented
169 false
170 else
171 vim._onInput(match, options)
172 @setHeldModifiers(event)
173 return @suppress
174
175 if isFrameEvent
176 vim._listenOnce('lateKeydown', listener)
177 else
178 @listenOnce('keydown', ((lateEvent) =>
179 listener(lateEvent)
180 if @suppress
181 utils.suppressEvent(lateEvent)
182 @listenOnce('keyup', utils.suppressEvent, false)
183 ), false)
184
185 setHeldModifiers: (event, { filterCurrentOnly = false } = {}) ->
186 mainWindow = @window.document.documentElement
187 modifiers =
188 if filterCurrentOnly
189 mainWindow.getAttribute(HELD_MODIFIERS_ATTRIBUTE)
190 else
191 if @suppress == null then 'alt ctrl meta shift' else ''
192 isHeld = (modifier) -> event["#{ modifier }Key"]
193 mainWindow.setAttribute(HELD_MODIFIERS_ATTRIBUTE,
194 modifiers.split(' ').filter(isHeld).join(' '))
195
196 module.exports = UIEventManager
Imprint / Impressum