From c9af886a92e4b686456b34949e907a9d79fb223e Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Fri, 2 Oct 2015 06:40:45 +0100 Subject: 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. --- content_scripts/link_hints.coffee | 29 +++++++++++++++++++++++------ manifest.json | 2 +- 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 and . 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 @@ + - -- cgit v1.2.3