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