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