diff options
| author | Stephen Blott | 2015-10-02 06:40:45 +0100 |
|---|---|---|
| committer | Stephen Blott | 2015-10-02 06:40:45 +0100 |
| commit | c9af886a92e4b686456b34949e907a9d79fb223e (patch) | |
| tree | 0b7fce838253b7ddee9fe8b5f28cbb30427fe595 | |
| parent | 34452f22ec4c6403deecb59b73d237bcfa7c964a (diff) | |
| download | vimium-c9af886a92e4b686456b34949e907a9d79fb223e.tar.bz2 | |
Block keyboard events when a filtered hint matches.
Previously, we blocked keyboard events for a fixed 200ms.
With this PR, we continue blocking keyboard events until 150ms after the
last `keydown` or `keypress` event. So, we wait until we think the user
has stopped typing.
Fixes #1842.
| -rw-r--r-- | content_scripts/link_hints.coffee | 29 | ||||
| -rw-r--r-- | manifest.json | 2 | ||||
| -rw-r--r-- | tests/dom_tests/dom_tests.html | 2 |
3 files changed, 25 insertions, 8 deletions
diff --git a/content_scripts/link_hints.coffee b/content_scripts/link_hints.coffee index a5e94fd0..23f23c2e 100644 --- a/content_scripts/link_hints.coffee +++ b/content_scripts/link_hints.coffee @@ -34,8 +34,6 @@ class LinkHintsMode mode: undefined # Function that does the appropriate action on the selected link. linkActivator: undefined - # While in delayMode, all keypresses have no effect. - delayMode: false # Lock to ensure only one instance runs at a time. isActive: false # The link-hints "mode" (in the key-handler, indicator sense). @@ -286,7 +284,7 @@ class LinkHintsMode # Handles <Shift> and <Ctrl>. onKeyDownInMode: (hintMarkers, event) -> - return if @delayMode or event.repeat + return if event.repeat @keydownKeyChar = KeyboardUtils.getKeyChar(event).toLowerCase() previousTabCount = @tabCount @@ -333,7 +331,7 @@ class LinkHintsMode # Handles normal input. onKeyPressInMode: (hintMarkers, event) -> - return if @delayMode or event.repeat + return if event.repeat keyChar = String.fromCharCode(event.charCode).toLowerCase() if keyChar @@ -358,7 +356,6 @@ class LinkHintsMode # When only one link hint remains, this function activates it in the appropriate way. # activateLink: (matchedLink, delay = 0) -> - @delayMode = true clickEl = matchedLink.clickableItem if (DomUtils.isSelectable(clickEl)) DomUtils.simulateSelect(clickEl) @@ -400,7 +397,9 @@ class LinkHintsMode @tabCount = 0 if delay - Utils.setTimeout delay, -> + # Install a mode to block keyboard events if the user is still typing. The intention is to prevent the + # user from inadvertently launching Vimium commands when typing the link text. + new TypingProtector delay, -> deactivate() callback?() else @@ -656,6 +655,24 @@ numberToHintString = (number, characterSet, numHintDigits = 0) -> hintString.join("") +# Suppress all keyboard events until the user stops typing for sufficiently long. +class TypingProtector extends Mode + constructor: (delay, callback) -> + @timer = Utils.setTimeout delay, => @exit() + + handler = (event) => + clearTimeout @timer + @timer = Utils.setTimeout 150, => @exit() + DomUtils.suppressEvent event + @suppressEvent + + super + name: "hint/typing-protector" + keydown: handler + keypress: handler + keyup: => @stopBubblingAndTrue + + @onExit callback root = exports ? window root.LinkHints = LinkHints diff --git a/manifest.json b/manifest.json index 6d971db2..f0d1c8d7 100644 --- a/manifest.json +++ b/manifest.json @@ -42,12 +42,12 @@ "lib/handler_stack.js", "lib/settings.js", "lib/find_mode_history.js", + "content_scripts/mode.js", "content_scripts/ui_component.js", "content_scripts/link_hints.js", "content_scripts/vomnibar.js", "content_scripts/scroller.js", "content_scripts/marks.js", - "content_scripts/mode.js", "content_scripts/mode_insert.js", "content_scripts/mode_passkeys.js", "content_scripts/mode_find.js", diff --git a/tests/dom_tests/dom_tests.html b/tests/dom_tests/dom_tests.html index 25c5f8ba..8d355c6d 100644 --- a/tests/dom_tests/dom_tests.html +++ b/tests/dom_tests/dom_tests.html @@ -37,11 +37,11 @@ <script type="text/javascript" src="../../lib/clipboard.js"></script> <script type="text/javascript" src="../../lib/settings.js"></script> <script type="text/javascript" src="../../lib/find_mode_history.js"></script> + <script type="text/javascript" src="../../content_scripts/mode.js"></script> <script type="text/javascript" src="../../content_scripts/ui_component.js"></script> <script type="text/javascript" src="../../content_scripts/link_hints.js"></script> <script type="text/javascript" src="../../content_scripts/vomnibar.js"></script> <script type="text/javascript" src="../../content_scripts/scroller.js"></script> - <script type="text/javascript" src="../../content_scripts/mode.js"></script> <script type="text/javascript" src="../../content_scripts/mode_passkeys.js"></script> <script type="text/javascript" src="../../content_scripts/mode_insert.js"></script> <script type="text/javascript" src="../../content_scripts/mode_find.js"></script> |
