]> git.gir.st - VimFx.git/blob - extension/lib/events-frame.coffee
Properly exit "`gi` mode"
[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 listen: utils.listen.bind(null, FRAME_SCRIPT_ENVIRONMENT)
33 listenOnce: utils.listenOnce.bind(null, FRAME_SCRIPT_ENVIRONMENT)
34
35 addListeners: ->
36 # If the page already was loaded when VimFx was initialized, send the
37 # 'DOMWindowCreated' message straight away.
38 if @vim.content.document.readyState in ['interactive', 'complete']
39 messageManager.send('DOMWindowCreated')
40 else
41 @listen('DOMWindowCreated', (event) ->
42 messageManager.send('DOMWindowCreated')
43 )
44
45 @listen('readystatechange', (event) =>
46 target = event.originalTarget
47
48 # When the page starts loading `.readyState` changes to 'interactive'.
49 return unless target.readyState == 'interactive'
50
51 # If the topmost document starts loading, it means that we have navigated
52 # to a new page or refreshed the page.
53 if target == @vim.content.document
54 @vim.resetState()
55 messageManager.send('locationChange', @vim.content.location.href)
56 else
57 # If the target isn’t the topmost document, it means that a frame has
58 # started loading. Some sites change the `src` attribute of `<iframe>`s
59 # dynamically. Any scrollable elements for the old frame `src` then
60 # become dead and need to be filtered out.
61 @vim.state.scrollableElements.reject(Cu.isDeadWrapper)
62 )
63
64 @listen('click', (event) =>
65 if @vim.mode == 'hints' and event.isTrusted
66 messageManager.send('enterMode', {mode: 'normal'})
67 )
68
69 @listen('overflow', (event) =>
70 target = event.originalTarget
71 return unless computedStyle = @vim.content.getComputedStyle(target)
72 return if computedStyle.getPropertyValue('overflow') == 'hidden'
73 @vim.state.scrollableElements.add(target)
74 )
75
76 @listen('underflow', (event) =>
77 target = event.originalTarget
78 @vim.state.scrollableElements.delete(target)
79 )
80
81 @listen('keydown', (event) =>
82 @keepInputs = false
83
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
107 if @vim.state.inputs and @vim.mode == 'normal' and not suppress and
108 not event.defaultPrevented
109 # There is no need to take `ignore_keyboard_layout` and `translations`
110 # into account here, since we want to override the _native_ `<tab>`
111 # behavior. Then, `event.key` is the way to go. (Unless the prefs are
112 # customized. YAGNI until requested.)
113 keyStr = notation.stringify(event)
114 options = @vim.options(['focus_previous_key', 'focus_next_key'])
115 direction = switch keyStr
116 when '' then null
117 when options.focus_previous_key then -1
118 when options.focus_next_key then +1
119 else null
120 if direction?
121 suppress = commands.move_focus({@vim, direction})
122 @keepInputs = true
123
124 utils.suppressEvent(event) if suppress
125 ), false)
126
127 @listen('mousedown', (event) =>
128 # Allow clicking on another text input without exiting “gi mode”. Listen
129 # for 'mousedown' instead of 'click', because only the former runs before
130 # the 'blur' event. Also, `event.originalTarget` does _not_ work here.
131 @keepInputs = (@vim.state.inputs and event.target in @vim.state.inputs)
132
133 # Clicks are always counted as page interaction. Listen for 'mousedown'
134 # instead of 'click' to mark the interaction as soon as possible.
135 @vim.markPageInteraction()
136 )
137
138 messageManager.listen('browserRefocus', =>
139 # Suppress the next two focus events (for `document` and `window`; see
140 # `blurActiveBrowserElement`).
141 @numFocusToSuppress = 2
142 )
143
144 @listen('focus', (event) =>
145 target = event.originalTarget
146
147 if @numFocusToSuppress > 0
148 utils.suppressEvent(event)
149 @numFocusToSuppress--
150 return
151
152 # Reset `hasInteraction` when (re-)selecting a tab, or coming back from
153 # another window, in order to prevent the common “automatically re-focus
154 # when switching back to the tab” behaviour many sites have, unless a text
155 # input _should_ be re-focused when coming back to the tab (see the 'blur'
156 # event below).
157 if target == @vim.content.document
158 if @vim.state.shouldRefocus
159 @vim.state.hasInteraction = true
160 @vim.state.shouldRefocus = false
161 else
162 @vim.state.hasInteraction = false
163 return
164
165 # Save the last focused text input regardless of whether that input might
166 # be blurred because of autofocus prevention.
167 if utils.isTextInputElement(target)
168 @vim.state.lastFocusedTextInput = target
169
170 focusManager = Cc['@mozilla.org/focus-manager;1']
171 .getService(Ci.nsIFocusManager)
172
173 # When moving a tab to another window, there is a short period of time
174 # when there’s no listener for this call.
175 return unless options = @vim.options(
176 ['prevent_autofocus', 'prevent_autofocus_modes']
177 )
178
179 # Blur the focus target, if autofocus prevention is enabled…
180 if options.prevent_autofocus and
181 @vim.mode in options.prevent_autofocus_modes and
182 # …and the user has interacted with the page…
183 not @vim.state.hasInteraction and
184 # …and the event is programmatic (not caused by clicks or keypresses)…
185 focusManager.getLastFocusMethod(null) == 0 and
186 # …and the target may steal most keystrokes.
187 (utils.isTypingElement(target) or utils.isContentEditable(target))
188 # Some sites (such as icloud.com) re-focuses inputs if they are blurred,
189 # causing an infinite loop of autofocus prevention and re-focusing.
190 # Therefore, blur events that happen just after an autofocus prevention
191 # are suppressed.
192 @listenOnce('blur', utils.suppressEvent)
193 target.blur()
194 )
195
196 @listen('blur', (event) =>
197 target = event.originalTarget
198
199 # If a text input is blurred immediately before the document loses focus,
200 # it most likely means that the user switched tab, for example by pressing
201 # `<c-tab>`, or switched to another window, while the text input was
202 # focused. In this case, when switching back to that tab, the text input
203 # will, and should, be re-focused (because it was focused when you left
204 # the tab). This case is kept track of so that the autofocus prevention
205 # does not catch it.
206 if utils.isTypingElement(target) or utils.isContentEditable(target)
207 utils.nextTick(@vim.content, =>
208 @vim.state.shouldRefocus = not @vim.content.document.hasFocus()
209
210 # “gi mode” ends when blurring a text input, unless `<tab>` was just
211 # pressed.
212 unless @vim.state.shouldRefocus or @keepInputs
213 commands.clear_inputs({@vim})
214 )
215 )
216
217 module.exports = FrameEventManager
Imprint / Impressum