]> git.gir.st - VimFx.git/blob - extension/lib/events.coffee
Re-implement "switch back to tab" logic
[VimFx.git] / extension / lib / events.coffee
1 ###
2 # Copyright Anton Khodakivskiy 2012, 2013, 2014.
3 # Copyright Simon Lydell 2013, 2014.
4 #
5 # This file is part of VimFx.
6 #
7 # VimFx is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # VimFx is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with VimFx. If not, see <http://www.gnu.org/licenses/>.
19 ###
20
21 # This file sets up all event listeners needed to power VimFx: To know when to
22 # launch commands and to provide state to them. Events in web page content are
23 # listened for in events-frame.coffee.
24
25 button = require('./button')
26 utils = require('./utils')
27
28 HELD_MODIFIERS_ATTRIBUTE = 'vimfx-held-modifiers'
29
30 class UIEventManager
31 constructor: (@vimfx, @window) ->
32 @listen = utils.listen.bind(null, @window)
33 @listenOnce = utils.listenOnce.bind(null, @window)
34
35 # This flag controls whether to suppress the various key events or not.
36 @suppress = false
37
38 # When a menu or panel is shown VimFx should temporarily stop processing
39 # keyboard input, allowing accesskeys to be used.
40 @popupPassthrough = false
41
42 addListeners: ->
43 checkPassthrough = (value, event) =>
44 target = event.originalTarget
45 if target.nodeName in ['menupopup', 'panel']
46 @popupPassthrough = value
47
48 @listen('popupshown', checkPassthrough.bind(null, true))
49 @listen('popuphidden', checkPassthrough.bind(null, false))
50
51 @listen('keydown', (event) =>
52 try
53 # No matter what, always reset the `suppress` flag, so we don't suppress
54 # more than intended.
55 @suppress = false
56
57 if @popupPassthrough
58 # The `@popupPassthrough` flag is set a bit unreliably. Sometimes it
59 # can be stuck as `true` even though no popup is shown, effectively
60 # disabling the extension. Therefore we check if there actually _are_
61 # any open popups before stopping processing keyboard input. This is
62 # only done when popups (might) be open (not on every keystroke) of
63 # performance reasons.
64 #
65 # The autocomplete popup in text inputs (for example) is technically a
66 # panel, but it does not respond to key presses. Therefore
67 # `[ignorekeys="true"]` is excluded.
68 #
69 # coffeelint: disable=max_line_length
70 # <https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XUL/PopupGuide/PopupKeys#Ignoring_Keys>
71 # coffeelint: enable=max_line_length
72 popups = @window.document.querySelectorAll(
73 ':-moz-any(menupopup, panel):not([ignorekeys="true"])'
74 )
75 for popup in popups
76 return if popup.state == 'open'
77 @popupPassthrough = false # No popup was actually open.
78
79 return unless vim = @vimfx.getCurrentVim(@window)
80
81 if vim.isFrameEvent(event)
82 vim._listenOnce('consumeKeyEvent', ({ focusType }) =>
83 @consumeKeyEvent(vim, event, focusType, { isFrameEvent: true })
84 return @suppress
85 )
86 else
87 @consumeKeyEvent(vim, event, utils.getFocusType(event))
88 # This also suppresses the 'keypress' event.
89 utils.suppressEvent(event) if @suppress
90
91 catch error
92 console.error(utils.formatError(error))
93 )
94
95 @listen('keyup', (event) =>
96 utils.suppressEvent(event) if @suppress
97 @setHeldModifiers(event, {filterCurrentOnly: true})
98 )
99
100 checkFindbar = (mode, event) =>
101 target = event.originalTarget
102 findBar = @window.gBrowser.getFindBar()
103 if target == findBar._findField.mInputField
104 return unless vim = @vimfx.getCurrentVim(@window)
105 vim.enterMode(mode)
106
107 @listen('focus', checkFindbar.bind(null, 'find'))
108 @listen('blur', checkFindbar.bind(null, 'normal'))
109
110 @listen('click', (event) =>
111 target = event.originalTarget
112 return unless vim = @vimfx.getCurrentVim(@window)
113
114 # If the user clicks the reload button or a link when in hints mode, we’re
115 # going to end up in hints mode without any markers. Or if the user clicks
116 # a text input, then that input will be focused, but you can’t type in it
117 # (instead markers will be matched). So if the user clicks anything in
118 # hints mode it’s better to leave it.
119 if vim.mode == 'hints' and not vim.isFrameEvent(event) and
120 # Exclude the VimFx button, though, since clicking it returns to normal
121 # mode. Otherwise we’d first return to normal mode and then the button
122 # would open the help dialog.
123 target != button.getButton(@window)
124 vim.enterMode('normal')
125 )
126
127 @listen('TabSelect', @vimfx.emit.bind(@vimfx, 'TabSelect'))
128
129 lastUrl = null
130 progressListener =
131 onLocationChange: (progress, request, location, flags) =>
132 url = location.spec
133 refresh = (url == lastUrl)
134 lastUrl = url
135 unless flags & Ci.nsIWebProgressListener.LOCATION_CHANGE_SAME_DOCUMENT
136 return unless vim = @vimfx.getCurrentVim(@window)
137 vim._onLocationChange(url, {refresh})
138
139 @window.gBrowser.addProgressListener(progressListener)
140 module.onShutdown(=>
141 @window.gBrowser.removeProgressListener(progressListener)
142 )
143
144 consumeKeyEvent: (vim, event, focusType, options = {}) ->
145 match = vim._consumeKeyEvent(event, focusType)
146 switch
147 when not match
148 @suppress = null
149 when match.specialKeys['<late>']
150 @suppress = false
151 @consumeLateKeydown(vim, event, match, options)
152 else
153 @suppress = vim._onInput(match, options)
154 @setHeldModifiers(event)
155
156 consumeLateKeydown: (vim, event, match, options) ->
157 { isFrameEvent = false } = options
158
159 # The passed in `event` is the regular non-late browser UI keydown event.
160 # It is only used to set held keys. This is easier than sending an event
161 # subset from frame scripts.
162 listener = ({ defaultPrevented }) =>
163 @suppress =
164 if defaultPrevented
165 false
166 else
167 vim._onInput(match, options)
168 @setHeldModifiers(event)
169 return @suppress
170
171 if isFrameEvent
172 vim._listenOnce('lateKeydown', listener)
173 else
174 @listenOnce('keydown', ((lateEvent) =>
175 listener(lateEvent)
176 if @suppress
177 utils.suppressEvent(lateEvent)
178 @listenOnce('keyup', utils.suppressEvent, false)
179 ), false)
180
181 setHeldModifiers: (event, { filterCurrentOnly = false } = {}) ->
182 mainWindow = @window.document.documentElement
183 modifiers =
184 if filterCurrentOnly
185 mainWindow.getAttribute(HELD_MODIFIERS_ATTRIBUTE)
186 else
187 if @suppress == null then 'alt ctrl meta shift' else ''
188 isHeld = (modifier) -> event["#{ modifier }Key"]
189 mainWindow.setAttribute(HELD_MODIFIERS_ATTRIBUTE,
190 modifiers.split(' ').filter(isHeld).join(' '))
191
192 module.exports = UIEventManager
Imprint / Impressum