]> git.gir.st - VimFx.git/blob - extension/lib/events-frame.coffee
Fix the empty notification for the `gH` command
[VimFx.git] / extension / lib / events-frame.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 is the equivalent to events.coffee, but for frame scripts.
21
22 notation = require('vim-like-key-notation')
23 commands = require('./commands-frame')
24 messageManager = require('./message-manager')
25 utils = require('./utils')
26
27 class FrameEventManager
28 constructor: (@vim) ->
29 @numFocusToSuppress = 0
30 @keepInputs = false
31 @currentUrl = false
32
33 listen: utils.listen.bind(null, FRAME_SCRIPT_ENVIRONMENT)
34 listenOnce: utils.listenOnce.bind(null, FRAME_SCRIPT_ENVIRONMENT)
35
36 addListeners: ->
37 # If the page already was loaded when VimFx was initialized, send the
38 # 'frameCanReceiveEvents' message straight away.
39 if @vim.content.document.readyState == 'complete'
40 messageManager.send('frameCanReceiveEvents', true)
41
42 @listen('readystatechange', (event) =>
43 target = event.originalTarget
44 @currentUrl = @vim.content.location.href
45
46 if target == @vim.content.document
47 switch target.readyState
48 when 'interactive'
49 messageManager.send('locationChange', @currentUrl)
50 when 'complete'
51 messageManager.send('frameCanReceiveEvents', true)
52 )
53
54 @listen('pageshow', (event) =>
55 [oldUrl, @currentUrl] = [@currentUrl, @vim.content.location.href]
56
57 # When navigating the history, `event.persisted` is `true` (meaning that
58 # the page loaded from cache) and 'readystatechange' won’t be fired, so
59 # send a 'locationChange' message to make sure that the blacklist is
60 # applied etc. The reason we don’t simply _always_ do this on the
61 # 'pageshow' event, is because it usually fires too late. However, it also
62 # fires after having moved a tab to another window. In that case it is
63 # _not_ a location change; the blacklist should not be applied.
64 if event.persisted
65 url = @vim.content.location.href
66 messageManager.send('cachedPageshow', null, (movedToNewTab) =>
67 if not movedToNewTab and oldUrl != @currentUrl
68 messageManager.send('locationChange', @currentUrl)
69 )
70 )
71
72 @listen('pagehide', (event) =>
73 target = event.originalTarget
74 @currentUrl = null
75
76 if target == @vim.content.document
77 messageManager.send('frameCanReceiveEvents', false)
78
79 # If the target isn’t the topmost document, it means that a frame has
80 # changed: It could have been removed or its `src` attribute could have
81 # been changed. If the frame contains other frames, 'pagehide' events have
82 # already been fired for them.
83 @vim.resetState(target)
84 )
85
86 messageManager.listen('getMarkableElementsMovements', (data, {callback}) =>
87 diffs = @vim.state.markerElements.map(({element, originalRect}) ->
88 newRect = element.getBoundingClientRect()
89 return {
90 dx: newRect.left - originalRect.left
91 dy: newRect.top - originalRect.top
92 }
93 )
94 messageManager.send(callback, diffs)
95 )
96
97 @listen('overflow', (event) =>
98 target = event.originalTarget
99 @vim.state.scrollableElements.addChecked(target)
100 )
101
102 @listen('underflow', (event) =>
103 target = event.originalTarget
104 @vim.state.scrollableElements.deleteChecked(target)
105 )
106
107 @listen('keydown', (event) =>
108 @keepInputs = false
109
110 suppress = @vim.onInput(event)
111
112 # This also suppresses the 'keypress' and 'keyup' events. (Yes, in frame
113 # scripts, suppressing the 'keydown' events does seem to even suppress
114 # the 'keyup' event!)
115 utils.suppressEvent(event) if suppress
116
117 # From this line on, the rest of the code in `addListeners` is more or
118 # less devoted to autofocus prevention. When enabled, focus events that
119 # occur before the user has interacted with page are prevented.
120 #
121 # If this keydown event wasn’t suppressed (`not suppress`), it’s an
122 # obvious interaction with the page. If it _was_ suppressed, though, it’s
123 # an interaction depending on the command triggered; if it calls
124 # `vim.markPageInteraction()` or not.
125 @vim.markPageInteraction() unless suppress
126 )
127
128 @listen('keydown', ((event) =>
129 suppress = messageManager.get('lateKeydown', {
130 defaultPrevented: event.defaultPrevented
131 })
132
133 if @vim.state.inputs and @vim.mode == 'normal' and not suppress and
134 not event.defaultPrevented
135 # There is no need to take `ignore_keyboard_layout` and `translations`
136 # into account here, since we want to override the _native_ `<tab>`
137 # behavior. Then, `event.key` is the way to go. (Unless the prefs are
138 # customized. YAGNI until requested.)
139 keyStr = notation.stringify(event)
140 options = @vim.options(['focus_previous_key', 'focus_next_key'])
141 direction = switch keyStr
142 when '' then null
143 when options.focus_previous_key then -1
144 when options.focus_next_key then +1
145 else null
146 if direction?
147 suppress = commands.move_focus({@vim, direction})
148 @keepInputs = true
149
150 utils.suppressEvent(event) if suppress
151 ), false)
152
153 @listen('mousedown', (event) =>
154 # Allow clicking on another text input without exiting “gi mode”. Listen
155 # for 'mousedown' instead of 'click', because only the former runs before
156 # the 'blur' event. Also, `event.originalTarget` does _not_ work here.
157 @keepInputs = (@vim.state.inputs and event.target in @vim.state.inputs)
158
159 # Clicks are always counted as page interaction. Listen for 'mousedown'
160 # instead of 'click' to mark the interaction as soon as possible.
161 @vim.markPageInteraction()
162 )
163
164 messageManager.listen('browserRefocus', =>
165 # Suppress the next two focus events (for `document` and `window`; see
166 # `blurActiveBrowserElement`).
167 @numFocusToSuppress = 2
168 )
169
170 sendFocusType = =>
171 return unless activeElement = utils.getActiveElement(@vim.content)
172 focusType = utils.getFocusType(activeElement)
173 messageManager.send('focusType', focusType)
174
175 @listen('focus', (event) =>
176 target = event.originalTarget
177
178 if @numFocusToSuppress > 0
179 utils.suppressEvent(event)
180 @numFocusToSuppress--
181 return
182
183 @vim.state.explicitBodyFocus = (target == @vim.content.document.body)
184
185 sendFocusType()
186
187 # Reset `hasInteraction` when (re-)selecting a tab, or coming back from
188 # another window, in order to prevent the common “automatically re-focus
189 # when switching back to the tab” behaviour many sites have, unless a text
190 # input _should_ be re-focused when coming back to the tab (see the 'blur'
191 # event below).
192 if target == @vim.content.document
193 if @vim.state.shouldRefocus
194 @vim.state.hasInteraction = true
195 @vim.state.shouldRefocus = false
196 else
197 @vim.state.hasInteraction = false
198 return
199
200 # Save the last focused text input regardless of whether that input might
201 # be blurred because of autofocus prevention.
202 if utils.isTextInputElement(target)
203 @vim.state.lastFocusedTextInput = target
204 @vim.state.hasFocusedTextInput = true
205
206 focusManager = Cc['@mozilla.org/focus-manager;1']
207 .getService(Ci.nsIFocusManager)
208
209 # When moving a tab to another window, there is a short period of time
210 # when there’s no listener for this call.
211 return unless options = @vim.options(
212 ['prevent_autofocus', 'prevent_autofocus_modes']
213 )
214
215 # Blur the focus target, if autofocus prevention is enabled…
216 if options.prevent_autofocus and
217 @vim.mode in options.prevent_autofocus_modes and
218 # …and the user has interacted with the page…
219 not @vim.state.hasInteraction and
220 # …and the event is programmatic (not caused by clicks or keypresses)…
221 focusManager.getLastFocusMethod(null) == 0 and
222 # …and the target may steal most keystrokes.
223 utils.isTypingElement(target)
224 # Some sites (such as icloud.com) re-focuses inputs if they are blurred,
225 # causing an infinite loop of autofocus prevention and re-focusing.
226 # Therefore, blur events that happen just after an autofocus prevention
227 # are suppressed.
228 @listenOnce('blur', utils.suppressEvent)
229 target.blur()
230 @vim.state.hasFocusedTextInput = false
231 )
232
233 @listen('blur', (event) =>
234 target = event.originalTarget
235
236 sendFocusType()
237
238 # If a text input is blurred immediately before the document loses focus,
239 # it most likely means that the user switched tab, for example by pressing
240 # `<c-tab>`, or switched to another window, while the text input was
241 # focused. In this case, when switching back to that tab, the text input
242 # will, and should, be re-focused (because it was focused when you left
243 # the tab). This case is kept track of so that the autofocus prevention
244 # does not catch it.
245 if utils.isTypingElement(target)
246 utils.nextTick(@vim.content, =>
247 @vim.state.shouldRefocus = not @vim.content.document.hasFocus()
248
249 # “gi mode” ends when blurring a text input, unless `<tab>` was just
250 # pressed.
251 unless @vim.state.shouldRefocus or @keepInputs
252 commands.clear_inputs({@vim})
253 )
254 )
255
256 module.exports = FrameEventManager
Imprint / Impressum