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