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