]> git.gir.st - VimFx.git/blob - extension/packages/events.coffee
Merge branch 'develop' into proper-modes
[VimFx.git] / extension / packages / events.coffee
1 utils = require 'utils'
2 keyUtils = require 'key-utils'
3 { Vim } = require 'vim'
4 { getPref } = require 'prefs'
5 { updateToolbarButton } = require 'button'
6 { unload } = require 'unload'
7 { commands } = require 'commands'
8 { modes } = require 'modes'
9
10 { interfaces: Ci } = Components
11
12 # Not suppressing Esc allows for stopping the loading of the page as well as closing many custom
13 # dialogs (and perhaps other things -- Esc is a very commonly used key). There are two reasons we
14 # might suppress it in other modes. If some custom dialog of a website is open, we should be able to
15 # cancel hint markers on it without closing it. Secondly, otherwise cancelling hint markers on
16 # google causes its search bar to be focused.
17 NEVER_SUPPRESS_IN_NORMAL_MODE = ['Esc']
18
19 # TODO: Should 'Esc' be configurable?
20 newFunc = (window) -> new Vim({window, commands, modes, esc: 'Esc'})
21 vimBucket = new utils.Bucket(utils.getWindowId, newFunc)
22
23 keyStrFromEvent = (event) ->
24 { ctrlKey: ctrl, metaKey: meta, altKey: alt, shiftKey: shift } = event
25
26 if !meta and !alt
27 return unless keyChar = keyUtils.keyCharFromCode(event.keyCode, shift)
28 keyStr = keyUtils.applyModifiers(keyChar, ctrl, alt, meta)
29 return keyStr
30
31 return null
32
33 # Passthrough mode is activated when VimFx should temporarily stop processing keyboard input, for
34 # example when a menu is shown.
35 passthrough = false
36 checkPassthrough = (event) ->
37 if event.target.nodeName in ['menupopup', 'panel']
38 passthrough = switch event.type
39 when 'popupshown' then true
40 when 'popuphidden' then false
41
42 suppress = false
43 keyListener = (event) ->
44 try
45 return if passthrough or getPref('disabled')
46 return unless window = utils.getEventCurrentTabWindow(event)
47 return unless vim = vimBucket.get(window)
48 return if vim.blacklisted
49
50 if event.type == 'keydown'
51 suppress = false
52
53 return unless keyStr = keyStrFromEvent(event)
54
55 isEditable = utils.isElementEditable(event.originalTarget)
56 unless isEditable and keyStr != vim.esc
57
58 # This check must be done before `vim.onInput()` below, since that call might change the
59 # mode. We are interested in the mode at the beginning of the events, not whatever it might
60 # be afterwards.
61 suppressException = (vim.mode == Vim.MODE_NORMAL and keyStr in NEVER_SUPPRESS_IN_NORMAL_MODE)
62
63 suppress = vim.onInput(keyStr, event)
64 if suppressException
65 suppress = false
66
67 if suppress
68 event.preventDefault()
69 event.stopPropagation()
70
71 catch error
72 console.log("#{ error } (in #{ event.type })\n#{ error.stack.replace(/@.+-> /g, '@') }")
73
74 removeVimFromTab = (tab, gBrowser) ->
75 return unless browser = gBrowser.getBrowserForTab(tab)
76 vimBucket.forget(browser.contentWindow)
77
78 # The following listeners are installed on every top level Chrome window
79 windowsListeners =
80 keydown: keyListener
81 keypress: keyListener
82 keyup: keyListener
83 popupshown: checkPassthrough
84 popuphidden: checkPassthrough
85
86 # When the top level window closes we should release all Vims that were
87 # associated with tabs in this window
88 DOMWindowClose: (event) ->
89 return unless { gBrowser } = event.originalTarget
90 for tab in gBrowser.tabs
91 removeVimFromTab(tab, gBrowser)
92
93 TabClose: (event) ->
94 return unless { gBrowser } = utils.getEventRootWindow(event) ? {}
95 tab = event.originalTarget
96 removeVimFromTab(tab, gBrowser)
97
98 # Update the toolbar button icon to reflect the blacklisted state
99 TabSelect: (event) ->
100 return unless window = event.originalTarget?.linkedBrowser?.contentDocument?.defaultView
101 return unless vim = vimBucket.get(window)
102 return unless rootWindow = utils.getRootWindow(window)
103 updateToolbarButton(rootWindow, {blacklisted: vim.blacklisted})
104
105 # This listener works on individual tabs within Chrome Window
106 tabsListener =
107 # Listenfor location changes and disable the extension on blacklisted urls
108 onLocationChange: (browser, webProgress, request, location) ->
109 return unless vim = vimBucket.get(browser.contentWindow)
110
111 # If the location changes when in hints mode (for example because the reload button has been
112 # clicked), we're going to end up in hints mode without any markers. So switch back to normal
113 # mode in that case.
114 if vim.mode == 'hints'
115 vim.enterNormalMode()
116
117 return unless rootWindow = utils.getRootWindow(vim.window)
118 vim.blacklisted = utils.isBlacklisted(location.spec)
119 updateToolbarButton(rootWindow, {blacklisted: vim.blacklisted})
120
121 addEventListeners = (window) ->
122 for name, listener of windowsListeners
123 window.addEventListener(name, listener, true)
124
125 window.gBrowser.addTabsProgressListener(tabsListener)
126
127 unload ->
128 for name, listener of windowsListeners
129 window.removeEventListener(name, listener, true)
130
131 window.gBrowser.removeTabsProgressListener(tabsListener)
132
133 exports.addEventListeners = addEventListeners
Imprint / Impressum