]> git.gir.st - VimFx.git/blob - extension/lib/events.coffee
Fix blacklisting on already loaded tabs
[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
34 # This flag controls whether to suppress the various key events or not.
35 @suppress = false
36
37 # When a menu or panel is shown VimFx should temporarily stop processing
38 # keyboard input, allowing accesskeys to be used.
39 @popupPassthrough = false
40
41 addListeners: ->
42 checkPassthrough = (value, event) =>
43 if event.target.nodeName in ['menupopup', 'panel']
44 @popupPassthrough = value
45
46 @listen('popupshown', checkPassthrough.bind(null, true))
47 @listen('popuphidden', checkPassthrough.bind(null, false))
48
49 @listen('keydown', (event) =>
50 try
51 # No matter what, always reset the `suppress` flag, so we don't suppress
52 # more than intended.
53 @suppress = false
54
55 if @popupPassthrough
56 # The `@popupPassthrough` flag is set a bit unreliably. Sometimes it
57 # can be stuck as `true` even though no popup is shown, effectively
58 # disabling the extension. Therefore we check if there actually _are_
59 # any open popups before stopping processing keyboard input. This is
60 # only done when popups (might) be open (not on every keystroke) of
61 # performance reasons.
62 #
63 # The autocomplete popup in text inputs (for example) is technically a
64 # panel, but it does not respond to key presses. Therefore
65 # `[ignorekeys="true"]` is excluded.
66 #
67 # coffeelint: disable=max_line_length
68 # <https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XUL/PopupGuide/PopupKeys#Ignoring_Keys>
69 # coffeelint: enable=max_line_length
70 popups = @window.document.querySelectorAll(
71 ':-moz-any(menupopup, panel):not([ignorekeys="true"])'
72 )
73 for popup in popups
74 return if popup.state == 'open'
75 @popupPassthrough = false # No popup was actually open.
76
77 vim = @vimfx.getCurrentVim(@window)
78
79 if vim.isFrameEvent(event)
80 vim._listenOnce('onInput-frame', ({ @suppress }) =>
81 @setHeldModifiers(event)
82 )
83 else
84 @suppress = vim._onInput(event, utils.getFocusType(event))
85 @setHeldModifiers(event)
86 utils.suppressEvent(event) if @suppress
87
88 catch error
89 console.error(utils.formatError(error))
90 )
91
92 @listen('keypress', (event) =>
93 utils.suppressEvent(event) if @suppress
94 )
95
96 @listen('keyup', (event) =>
97 utils.suppressEvent(event) if @suppress
98 @setHeldModifiers(event, {filterCurrentOnly: true})
99 )
100
101 checkFindbar = (mode, event) =>
102 target = event.originalTarget
103 findBar = @window.gBrowser.getFindBar()
104 if target == findBar._findField.mInputField
105 vim = @vimfx.getCurrentVim(@window)
106 vim.enterMode(mode)
107
108 @listen('focus', checkFindbar.bind(null, 'find'))
109 @listen('blur', checkFindbar.bind(null, 'normal'))
110
111 @listen('click', (event) =>
112 target = event.originalTarget
113 vim = @vimfx.getCurrentVim(@window)
114
115 # If the user clicks the reload button or a link when in hints mode, we’re
116 # going to end up in hints mode without any markers. Or if the user clicks
117 # a text input, then that input will be focused, but you can’t type in it
118 # (instead markers will be matched). So if the user clicks anything in
119 # hints mode it’s better to leave it.
120 if vim.mode == 'hints' and not vim.isFrameEvent(event) and
121 # Exclude the VimFx button, though, since clicking it returns to normal
122 # mode. Otherwise we’d first return to normal mode and then the button
123 # would open the help dialog.
124 target != button.getButton(@window)
125 vim.enterMode('normal')
126 )
127
128 @listen('TabSelect', (event) =>
129 @vimfx.emit('TabSelect', event)
130
131 # The initial 'TabSelect' when the browser starts fires before a vim
132 # object has been created for that tab.
133 return unless vim = @vimfx.getCurrentVim(@window)
134 vim._send('TabSelect')
135 )
136
137 progressListener =
138 onLocationChange: (progress, request, location, flags) =>
139 unless flags & Ci.nsIWebProgressListener.LOCATION_CHANGE_SAME_DOCUMENT
140 # `onLocationChange` might fire before a vim object has been created
141 # for the current tab.
142 return unless vim = @vimfx.getCurrentVim(@window)
143 vim._onLocationChange(location.spec)
144
145 @window.gBrowser.addProgressListener(progressListener)
146 module.onShutdown(=>
147 @window.gBrowser.removeProgressListener(progressListener)
148 )
149
150 setHeldModifiers: (event, { filterCurrentOnly = false } = {}) ->
151 mainWindow = @window.document.documentElement
152 modifiers =
153 if filterCurrentOnly
154 mainWindow.getAttribute(HELD_MODIFIERS_ATTRIBUTE)
155 else
156 if @suppress == null then 'alt ctrl meta shift' else ''
157 isHeld = (modifier) -> event["#{ modifier }Key"]
158 mainWindow.setAttribute(HELD_MODIFIERS_ATTRIBUTE,
159 modifiers.split(' ').filter(isHeld).join(' '))
160
161 module.exports = UIEventManager
Imprint / Impressum