2 # Copyright Anton Khodakivskiy 2012, 2013, 2014.
3 # Copyright Simon Lydell 2013, 2014, 2015.
5 # This file is part of VimFx.
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.
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.
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/>.
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.
25 button = require('./button')
26 messageManager = require('./message-manager')
27 utils = require('./utils')
29 HELD_MODIFIERS_ATTRIBUTE = 'vimfx-held-modifiers'
32 constructor: (@vimfx, @window) ->
33 @listen = utils.listen.bind(null, @window)
34 @listenOnce = utils.listenOnce.bind(null, @window)
36 # This flag controls whether to suppress the various key events or not.
39 # If a matched shortcut has the `<late>` special key, this flag is set to
43 # When a menu or panel is shown VimFx should temporarily stop processing
44 # keyboard input, allowing accesskeys to be used.
45 @popupPassthrough = false
47 @enteredKeys = new EnteredKeysManager(@window)
50 checkPassthrough = (value, event) =>
51 target = event.originalTarget
52 if target.nodeName in ['menupopup', 'panel']
53 @popupPassthrough = value
55 @listen('popupshown', checkPassthrough.bind(null, true))
56 @listen('popuphidden', checkPassthrough.bind(null, false))
58 @listen('keydown', (event) =>
59 # No matter what, always reset the `@suppress` flag, so we don't
60 # suppress more than intended.
63 # Reset the `@late` flag, telling any late listeners for the previous
68 # The `@popupPassthrough` flag is set a bit unreliably. Sometimes it
69 # can be stuck as `true` even though no popup is shown, effectively
70 # disabling the extension. Therefore we check if there actually _are_
71 # any open popups before stopping processing keyboard input. This is
72 # only done when popups (might) be open (not on every keystroke) of
73 # performance reasons.
75 # The autocomplete popup in text inputs (for example) is technically a
76 # panel, but it does not respond to key presses. Therefore
77 # `[ignorekeys="true"]` is excluded.
79 # coffeelint: disable=max_line_length
80 # <https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XUL/PopupGuide/PopupKeys#Ignoring_Keys>
81 # coffeelint: enable=max_line_length
82 popups = @window.document.querySelectorAll(
83 ':-moz-any(menupopup, panel):not([ignorekeys="true"])'
86 return if popup.state == 'open'
87 @popupPassthrough = false # No popup was actually open.
89 return unless vim = @vimfx.getCurrentVim(@window)
91 if vim.isUIEvent(event)
92 focusType = utils.getFocusType(event.originalTarget)
93 @consumeKeyEvent(vim, event, focusType, event)
94 # This also suppresses the 'keypress' event.
95 utils.suppressEvent(event) if @suppress
97 vim._listenOnce('consumeKeyEvent', ({focusType}) =>
98 @consumeKeyEvent(vim, event, focusType)
103 @listen('keyup', (event) =>
104 utils.suppressEvent(event) if @suppress
105 @setHeldModifiers(event, {filterCurrentOnly: true})
108 handleFocusRelatedEvent = (options, event) =>
109 target = event.originalTarget
110 return unless vim = @vimfx.getCurrentVim(@window)
112 findBar = @window.gBrowser.getFindBar()
113 if target == findBar._findField.mInputField
114 vim.enterMode(options.mode)
116 if vim.isUIEvent(event)
117 focusType = utils.getFocusType(utils.getActiveElement(@window))
118 @vimfx.emit('focusTypeChange', {vim, focusType})
120 @listen('focus', handleFocusRelatedEvent.bind(null, {mode: 'find'}))
121 @listen('blur', handleFocusRelatedEvent.bind(null, {mode: 'normal'}))
123 @listen('click', (event) =>
124 target = event.originalTarget
125 return unless vim = @vimfx.getCurrentVim(@window)
127 # If the user clicks the reload button or a link when in hints mode, we’re
128 # going to end up in hints mode without any markers. Or if the user clicks
129 # a text input, then that input will be focused, but you can’t type in it
130 # (instead markers will be matched). So if the user clicks anything in
131 # hints mode it’s better to leave it.
132 if vim.mode == 'hints' and not vim._state.allowNextHintsClick and
133 # Exclude the VimFx button, though, since clicking it returns to normal
134 # mode. Otherwise we’d first return to normal mode and then the button
135 # would open the help dialog.
136 target != button.getButton(@window)
137 vim.enterMode('normal')
139 vim._state.allowNextHintsClick = false
142 @listen('overflow', (event) =>
143 target = event.originalTarget
144 return unless vim = @vimfx.getCurrentVim(@window)
145 vim._state.scrollableElements.addChecked(target)
148 @listen('underflow', (event) =>
149 target = event.originalTarget
150 return unless vim = @vimfx.getCurrentVim(@window)
151 vim._state.scrollableElements.deleteChecked(target)
154 @listen('TabSelect', (event) =>
155 @vimfx.emit('TabSelect', event)
157 return unless vim = @vimfx.getCurrentVim(@window)
158 vim.hideNotification()
161 @listen('TabClose', (event) =>
162 browser = @window.gBrowser.getBrowserForTab(event.originalTarget)
163 return unless vim = @vimfx.vims.get(browser)
164 # Note: `lastClosedVim` must be stored so that any window can access it.
165 @vimfx.lastClosedVim = vim
168 messageManager.listen('cachedPageshow', ((data, args) =>
169 {target: browser, callback} = args
170 exit = (movedToNewTab) ->
171 messageManager.send(callback, movedToNewTab) if callback
173 [oldVim, @vimfx.lastClosedVim] = [@vimfx.lastClosedVim, null]
178 if @vimfx.vims.has(browser)
179 vim = @vimfx.vims.get(browser)
180 if vim._messageManager == vim.browser.messageManager
184 # If we get here, it means that we’ve detected a tab dragged from one
185 # window to another. If so, the `vim` object from the last closed tab (the
186 # moved tab) should be re-used. See the commit message for commit bb70257d
188 oldVim._setBrowser(browser)
189 @vimfx.vims.set(browser, oldVim)
190 @vimfx.emit('modeChange', oldVim)
192 ), @window.messageManager)
194 consumeKeyEvent: (vim, event, focusType, uiEvent = false) ->
195 match = vim._consumeKeyEvent(event, focusType)
198 if @vimfx.options.notify_entered_keys
199 if match.type in ['none', 'full'] or match.focus != null
200 @enteredKeys.clear(vim)
202 @enteredKeys.push(vim, match.keyStr, @vimfx.options.timeout)
204 vim.hideNotification()
206 if match.specialKeys['<late>']
208 @consumeLateKeydown(vim, event, match, uiEvent)
210 @suppress = vim._onInput(match, uiEvent)
213 @setHeldModifiers(event)
215 consumeLateKeydown: (vim, event, match, uiEvent) ->
218 # The passed in `event` is the regular non-late browser UI keydown event.
219 # It is only used to set held keys. This is easier than sending an event
220 # subset from frame scripts.
221 listener = ({defaultPrevented}) =>
222 # `@late` is reset on every keydown. If it is no longer `true`, it means
223 # that the page called `event.stopPropagation()`, which prevented this
224 # listener from running for that event.
230 vim._onInput(match, uiEvent)
231 @setHeldModifiers(event)
235 @listenOnce('keydown', ((lateEvent) =>
238 utils.suppressEvent(lateEvent)
239 @listenOnce('keyup', utils.suppressEvent, false)
242 vim._listenOnce('lateKeydown', listener)
244 setHeldModifiers: (event, {filterCurrentOnly = false} = {}) ->
245 mainWindow = @window.document.documentElement
248 mainWindow.getAttribute(HELD_MODIFIERS_ATTRIBUTE)
250 if @suppress == null then 'alt ctrl meta shift' else ''
251 isHeld = (modifier) -> event["#{modifier}Key"]
252 mainWindow.setAttribute(HELD_MODIFIERS_ATTRIBUTE,
253 modifiers.split(' ').filter(isHeld).join(' '))
255 class EnteredKeysManager
256 constructor: (@window) ->
263 notifier.hideNotification()
265 push: (notifier, keyStr, duration) ->
268 notifier.notify(@keys.join(''))
269 @timeout = @window.setTimeout(@clear.bind(this, notifier), duration)
272 @window.clearTimeout(@timeout) if @timeout?
275 module.exports = UIEventManager