]> git.gir.st - VimFx.git/blob - extension/lib/events-frame.coffee
Enhance `overflow` check for scrollable elements
[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 messageManager = require('./message-manager')
23 utils = require('./utils')
24
25 class FrameEventManager
26 constructor: (@vim) ->
27 @numFocusToSuppress = 0
28
29 listen: utils.listen.bind(null, FRAME_SCRIPT_ENVIRONMENT)
30 listenOnce: utils.listenOnce.bind(null, FRAME_SCRIPT_ENVIRONMENT)
31
32 addListeners: ->
33 # If the page already was loaded when VimFx was initialized, send the
34 # 'DOMWindowCreated' message straight away.
35 if @vim.content.document.readyState in ['interactive', 'complete']
36 messageManager.send('DOMWindowCreated')
37 else
38 @listen('DOMWindowCreated', (event) ->
39 messageManager.send('DOMWindowCreated')
40 )
41
42 @listen('readystatechange', (event) =>
43 target = event.originalTarget
44
45 # If the topmost document starts loading, it means that we have navigated
46 # to a new page or refreshed the page.
47 if target == @vim.content.document and target.readyState == 'interactive'
48 messageManager.send('locationChange', @vim.content.location.href)
49 )
50
51 @listen('pagehide', (event) =>
52 target = event.originalTarget
53
54 if target == @vim.content.document
55 @vim.resetState()
56 else
57 # If the target isn’t the topmost document, it means that a frame has
58 # changed: It could have been removed or its `src` attribute could have
59 # been changed. Any scrollable elements in the frame (or its sub-frames)
60 # then need to be filtered out.
61 @vim.state.scrollableElements
62 .reject(utils.windowContainsDeep.bind(null, target.defaultView))
63 )
64
65 @listen('click', (event) =>
66 if @vim.mode == 'hints' and event.isTrusted
67 messageManager.send('enterMode', {mode: 'normal'})
68 )
69
70 @listen('overflow', (event) =>
71 target = event.originalTarget
72 return unless computedStyle = @vim.content.getComputedStyle(target)
73 unless computedStyle.getPropertyValue('overflow-y') == 'hidden' and
74 computedStyle.getPropertyValue('overflow-x') == 'hidden'
75 @vim.state.scrollableElements.add(target)
76 )
77
78 @listen('underflow', (event) =>
79 target = event.originalTarget
80 @vim.state.scrollableElements.delete(target)
81 )
82
83 @listen('keydown', (event) =>
84 suppress = @vim.onInput(event)
85
86 # This also suppresses the 'keypress' and 'keyup' events. (Yes, in frame
87 # scripts, suppressing the 'keydown' events does seem to even suppress
88 # the 'keyup' event!)
89 utils.suppressEvent(event) if suppress
90
91 # From this line on, the rest of the code in `addListeners` is more or
92 # less devoted to autofocus prevention. When enabled, focus events that
93 # occur before the user has interacted with page are prevented.
94 #
95 # If this keydown event wasn’t suppressed (`not suppress`), it’s an
96 # obvious interaction with the page. If it _was_ suppressed, though, it’s
97 # an interaction depending on the command triggered; if it calls
98 # `vim.markPageInteraction()` or not.
99 @vim.markPageInteraction() unless suppress
100 )
101
102 @listen('keydown', ((event) ->
103 suppress = messageManager.get('lateKeydown', {
104 defaultPrevented: event.defaultPrevented
105 })
106 utils.suppressEvent(event) if suppress
107 ), false)
108
109 # Clicks are always counted as page interaction. Listen for 'mousedown'
110 # instead of 'click' to mark the interaction as soon as possible.
111 @listen('mousedown', @vim.markPageInteraction.bind(@vim))
112
113 messageManager.listen('browserRefocus', =>
114 # Suppress the next two focus events (for `document` and `window`; see
115 # `blurActiveBrowserElement`).
116 @numFocusToSuppress = 2
117 )
118
119 @listen('focus', (event) =>
120 target = event.originalTarget
121
122 if @numFocusToSuppress > 0
123 utils.suppressEvent(event)
124 @numFocusToSuppress--
125 return
126
127 # Reset `hasInteraction` when (re-)selecting a tab, or coming back from
128 # another window, in order to prevent the common “automatically re-focus
129 # when switching back to the tab” behaviour many sites have, unless a text
130 # input _should_ be re-focused when coming back to the tab (see the 'blur'
131 # event below).
132 if target == @vim.content.document
133 if @vim.state.shouldRefocus
134 @vim.state.hasInteraction = true
135 @vim.state.shouldRefocus = false
136 else
137 @vim.state.hasInteraction = false
138 return
139
140 # Save the last focused text input regardless of whether that input might
141 # be blurred because of autofocus prevention.
142 if utils.isTextInputElement(target)
143 @vim.state.lastFocusedTextInput = target
144
145 focusManager = Cc['@mozilla.org/focus-manager;1']
146 .getService(Ci.nsIFocusManager)
147
148 # When moving a tab to another window, there is a short period of time
149 # when there’s no listener for this call.
150 return unless options = @vim.options(
151 ['prevent_autofocus', 'prevent_autofocus_modes']
152 )
153
154 # Blur the focus target, if autofocus prevention is enabled…
155 if options.prevent_autofocus and
156 @vim.mode in options.prevent_autofocus_modes and
157 # …and the user has interacted with the page…
158 not @vim.state.hasInteraction and
159 # …and the event is programmatic (not caused by clicks or keypresses)…
160 focusManager.getLastFocusMethod(null) == 0 and
161 # …and the target may steal most keystrokes.
162 (utils.isTypingElement(target) or utils.isContentEditable(target))
163 # Some sites (such as icloud.com) re-focuses inputs if they are blurred,
164 # causing an infinite loop of autofocus prevention and re-focusing.
165 # Therefore, blur events that happen just after an autofocus prevention
166 # are suppressed.
167 @listenOnce('blur', utils.suppressEvent)
168 target.blur()
169 )
170
171 @listen('blur', (event) =>
172 target = event.originalTarget
173
174 # If a text input is blurred immediately before the document loses focus,
175 # it most likely means that the user switched tab, for example by pressing
176 # `<c-tab>`, or switched to another window, while the text input was
177 # focused. In this case, when switching back to that tab, the text input
178 # will, and should, be re-focused (because it was focused when you left
179 # the tab). This case is kept track of so that the autofocus prevention
180 # does not catch it.
181 if utils.isTypingElement(target) or utils.isContentEditable(target)
182 utils.nextTick(@vim.content, =>
183 @vim.state.shouldRefocus = not @vim.content.document.hasFocus()
184 )
185 )
186
187 module.exports = FrameEventManager
Imprint / Impressum