]> git.gir.st - VimFx.git/blob - extension/lib/events-frame.coffee
Fix shortcut triggering on slowly loadnig pages
[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
28 listen: utils.listen.bind(null, FRAME_SCRIPT_ENVIRONMENT)
29 listenOnce: utils.listenOnce.bind(null, FRAME_SCRIPT_ENVIRONMENT)
30
31 addListeners: ->
32 messageManager.listen('locationChange', @vim.resetState.bind(@vim))
33
34 @listen('DOMWindowCreated', (event) =>
35 messageManager.send('DOMWindowCreated')
36 )
37
38 @listen('click', (event) =>
39 if @vim.mode == 'hints' and event.isTrusted
40 messageManager.send('enterMode', {mode: 'normal'})
41 )
42
43 @listen('overflow', (event) =>
44 return unless computedStyle = @vim.content.getComputedStyle(event.target)
45 return if computedStyle.getPropertyValue('overflow') == 'hidden'
46 @vim.state.scrollableElements.add(event.target)
47 )
48
49 @listen('underflow', (event) =>
50 @vim.state.scrollableElements.delete(event.target)
51 )
52
53 @listen('keydown', (event) =>
54 suppress = @vim.onInput(event)
55
56 # From this line on, the rest of the code in `addListeners` is more or
57 # less devoted to autofocus prevention. When enabled, focus events that
58 # occur before the user has interacted with page are prevented.
59 #
60 # If this keydown event wasn’t suppressed (`not suppress`), it’s an
61 # obvious interaction with the page. If it _was_ suppressed, though, it’s
62 # an interaction depending on the command triggered; if it calls
63 # `vim.markPageInteraction()` or not.
64 @vim.markPageInteraction() unless suppress
65 )
66
67 # Clicks are always counted as page interaction. Listen for 'mousedown'
68 # instead of 'click' to mark the interaction as soon as possible.
69 @listen('mousedown', (event) => @vim.markPageInteraction())
70
71 @listen('focus', (event) =>
72 target = event.originalTarget
73
74 options = @vim.options(['prevent_autofocus', 'prevent_autofocus_modes'])
75
76 # Save the last focused text input regardless of whether that input might
77 # be blurred because of autofocus prevention.
78 if utils.isTextInputElement(target)
79 @vim.state.lastFocusedTextInput = target
80
81 focusManager = Cc['@mozilla.org/focus-manager;1']
82 .getService(Ci.nsIFocusManager)
83
84 # Blur the focus target, if autofocus prevention is enabled…
85 if options.prevent_autofocus and
86 @vim.mode in options.prevent_autofocus_modes and
87 # …and the user has interacted with the page…
88 not @vim.state.hasInteraction and
89 # …and the event is programmatic (not caused by clicks or keypresses)…
90 focusManager.getLastFocusMethod(null) == 0 and
91 # …and the target may steal most keystrokes.
92 (utils.isTextInputElement(target) or utils.isContentEditable(target))
93 # Some sites (such as icloud.com) re-focuses inputs if they are blurred,
94 # causing an infinite loop of autofocus prevention and re-focusing.
95 # Therefore, blur events that happen just after an autofocus prevention
96 # are suppressed.
97 @listenOnce('blur', utils.suppressEvent)
98 target.blur()
99 )
100
101 @listen('blur', (event) =>
102 target = event.originalTarget
103
104 # If a text input is blurred in a background tab, it most likely means
105 # that the user switched tab, for example by pressing `<c-tab>`, while the
106 # text input was focused. The 'TabSelect' event fires first, then the
107 # 'blur' event. In this case, when switching back to that tab, the text
108 # input will be re-focused (because it was focused when you left the tab).
109 # This case is kept track of so that the autofocus prevention does not
110 # catch it.
111 if utils.isTextInputElement(target) or utils.isContentEditable(target)
112 messageManager.send('vimMethod', {method: 'isCurrent'}, (isCurrent) =>
113 @vim.state.shouldRefocus = not isCurrent
114 # Note that when switching to a non-Firefox window, blur events happen
115 # as usual, but `isCurrent` will be `true`. (`@vim` is still the
116 # current vim object in the current Firefox window, but the current
117 # Firefox window is not the current OS window). `shouldRefocus` should
118 # still be `true` in this case, though. However, it doesn’t matter
119 # that it isn’t, because it is only used in the 'TabSelect' event,
120 # which does not fire when returning from another window.
121 )
122 )
123
124 messageManager.listen('TabSelect', =>
125 # Reset `hasInteraction` when (re-)selecting a tab, in order to prevent
126 # the common “automatically re-focus when switching back to the tab”
127 # behaviour many sites have, unless a text input _should_ be re-focused
128 # when coming back to the tab (see above).
129 if @vim.state.shouldRefocus
130 @vim.state.hasInteraction = true
131 @vim.state.shouldRefocus = false
132 else
133 @vim.state.hasInteraction = false
134 )
135
136 module.exports = FrameEventManager
Imprint / Impressum