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