2 # Copyright Anton Khodakivskiy 2012, 2013, 2014.
3 # Copyright Simon Lydell 2013, 2014.
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 utils = require('./utils')
28 HELD_MODIFIERS_ATTRIBUTE = 'vimfx-held-modifiers'
31 constructor: (@vimfx, @window) ->
32 @listen = utils.listen.bind(null, @window)
33 @listenOnce = utils.listenOnce.bind(null, @window)
35 # This flag controls whether to suppress the various key events or not.
38 # When a menu or panel is shown VimFx should temporarily stop processing
39 # keyboard input, allowing accesskeys to be used.
40 @popupPassthrough = false
43 checkPassthrough = (value, event) =>
44 target = event.originalTarget
45 if target.nodeName in ['menupopup', 'panel']
46 @popupPassthrough = value
48 @listen('popupshown', checkPassthrough.bind(null, true))
49 @listen('popuphidden', checkPassthrough.bind(null, false))
51 @listen('keydown', (event) =>
53 # No matter what, always reset the `suppress` flag, so we don't suppress
58 # The `@popupPassthrough` flag is set a bit unreliably. Sometimes it
59 # can be stuck as `true` even though no popup is shown, effectively
60 # disabling the extension. Therefore we check if there actually _are_
61 # any open popups before stopping processing keyboard input. This is
62 # only done when popups (might) be open (not on every keystroke) of
63 # performance reasons.
65 # The autocomplete popup in text inputs (for example) is technically a
66 # panel, but it does not respond to key presses. Therefore
67 # `[ignorekeys="true"]` is excluded.
69 # coffeelint: disable=max_line_length
70 # <https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XUL/PopupGuide/PopupKeys#Ignoring_Keys>
71 # coffeelint: enable=max_line_length
72 popups = @window.document.querySelectorAll(
73 ':-moz-any(menupopup, panel):not([ignorekeys="true"])'
76 return if popup.state == 'open'
77 @popupPassthrough = false # No popup was actually open.
79 return unless vim = @vimfx.getCurrentVim(@window)
81 if vim.isFrameEvent(event)
82 vim._listenOnce('consumeKeyEvent', ({ focusType }) =>
83 @consumeKeyEvent(vim, event, focusType, { isFrameEvent: true })
87 @consumeKeyEvent(vim, event, utils.getFocusType(event))
88 # This also suppresses the 'keypress' event.
89 utils.suppressEvent(event) if @suppress
92 console.error(utils.formatError(error))
95 @listen('keyup', (event) =>
96 utils.suppressEvent(event) if @suppress
97 @setHeldModifiers(event, {filterCurrentOnly: true})
100 checkFindbar = (mode, event) =>
101 target = event.originalTarget
102 findBar = @window.gBrowser.getFindBar()
103 if target == findBar._findField.mInputField
104 return unless vim = @vimfx.getCurrentVim(@window)
107 @listen('focus', checkFindbar.bind(null, 'find'))
108 @listen('blur', checkFindbar.bind(null, 'normal'))
110 @listen('click', (event) =>
111 target = event.originalTarget
112 return unless vim = @vimfx.getCurrentVim(@window)
114 # If the user clicks the reload button or a link when in hints mode, we’re
115 # going to end up in hints mode without any markers. Or if the user clicks
116 # a text input, then that input will be focused, but you can’t type in it
117 # (instead markers will be matched). So if the user clicks anything in
118 # hints mode it’s better to leave it.
119 if vim.mode == 'hints' and not vim.isFrameEvent(event) and
120 # Exclude the VimFx button, though, since clicking it returns to normal
121 # mode. Otherwise we’d first return to normal mode and then the button
122 # would open the help dialog.
123 target != button.getButton(@window)
124 vim.enterMode('normal')
127 @listen('TabSelect', @vimfx.emit.bind(@vimfx, 'TabSelect'))
131 onLocationChange: (progress, request, location, flags) =>
133 refresh = (url == lastUrl)
135 unless flags & Ci.nsIWebProgressListener.LOCATION_CHANGE_SAME_DOCUMENT
136 return unless vim = @vimfx.getCurrentVim(@window)
137 vim._onLocationChange(url, {refresh})
139 @window.gBrowser.addProgressListener(progressListener)
141 @window.gBrowser.removeProgressListener(progressListener)
144 consumeKeyEvent: (vim, event, focusType, options = {}) ->
145 match = vim._consumeKeyEvent(event, focusType)
149 when match.specialKeys['<late>']
151 @consumeLateKeydown(vim, event, match, options)
153 @suppress = vim._onInput(match, options)
154 @setHeldModifiers(event)
156 consumeLateKeydown: (vim, event, match, options) ->
157 { isFrameEvent = false } = options
159 # The passed in `event` is the regular non-late browser UI keydown event.
160 # It is only used to set held keys. This is easier than sending an event
161 # subset from frame scripts.
162 listener = ({ defaultPrevented }) =>
167 vim._onInput(match, options)
168 @setHeldModifiers(event)
172 vim._listenOnce('lateKeydown', listener)
174 @listenOnce('keydown', ((lateEvent) =>
177 utils.suppressEvent(lateEvent)
178 @listenOnce('keyup', utils.suppressEvent, false)
181 setHeldModifiers: (event, { filterCurrentOnly = false } = {}) ->
182 mainWindow = @window.document.documentElement
185 mainWindow.getAttribute(HELD_MODIFIERS_ATTRIBUTE)
187 if @suppress == null then 'alt ctrl meta shift' else ''
188 isHeld = (modifier) -> event["#{ modifier }Key"]
189 mainWindow.setAttribute(HELD_MODIFIERS_ATTRIBUTE,
190 modifiers.split(' ').filter(isHeld).join(' '))
192 module.exports = UIEventManager