diff options
| author | Stephen Blott | 2015-06-03 11:42:25 +0100 | 
|---|---|---|
| committer | Stephen Blott | 2015-06-03 11:42:25 +0100 | 
| commit | 03ef7f2a8525e8fbfc67b04d7a0ce47522449d03 (patch) | |
| tree | 193ab324ab77714f123826ca5c212708c69c0aea | |
| parent | d3e0974640f8138c26b010bd63e3d9caa2ffdfef (diff) | |
| download | vimium-03ef7f2a8525e8fbfc67b04d7a0ce47522449d03.tar.bz2 | |
Refactor to avoid having to cover all keyboard event cases.
It's pretty common that we want to suppress all keyboard events, so
let's support that in modes.coffee, thereby simplifying handlers
elsewhere.
| -rw-r--r-- | content_scripts/marks.coffee | 15 | ||||
| -rw-r--r-- | content_scripts/mode.coffee | 9 | 
2 files changed, 13 insertions, 11 deletions
| diff --git a/content_scripts/marks.coffee b/content_scripts/marks.coffee index f3dfd465..8ba45fd4 100644 --- a/content_scripts/marks.coffee +++ b/content_scripts/marks.coffee @@ -2,15 +2,13 @@  exit = (mode, continuation = null) ->    mode.exit()    continuation?() -  false  Marks =    activateCreateMode: ->      mode = new Mode        name: "create-mark"        indicator: "Create mark?" -      keypress: -> false -      keyup: -> false +      suppressAllKeyboardEvents: true        keydown: (event) ->          keyChar = KeyboardUtils.getKeyChar(event)          if /[A-Z]/.test keyChar @@ -27,17 +25,14 @@ Marks =              scrollX: window.scrollX,              scrollY: window.scrollY            exit mode, -> HUD.showForDuration "Created local mark '#{keyChar}'.", 1000 -        else if event.shiftKey -          false -        else +        else if not event.shiftKey            exit mode    activateGotoMode: ->      mode = new Mode        name: "goto-mark"        indicator: "Go to mark?" -      keypress: -> false -      keyup: -> false +      suppressAllKeyboardEvents: true        keydown: (event) ->          keyChar = KeyboardUtils.getKeyChar(event)          if /[A-Z]/.test keyChar @@ -55,9 +50,7 @@ Marks =                HUD.showForDuration "Jumped to local mark '#{keyChar}'", 1000              else                HUD.showForDuration "Local mark not set: '#{keyChar}'.", 1000 -        else if event.shiftKey -          false -        else +        else if not event.shiftKey            exit mode  root = exports ? window diff --git a/content_scripts/mode.coffee b/content_scripts/mode.coffee index f631b4cd..b2019ef9 100644 --- a/content_scripts/mode.coffee +++ b/content_scripts/mode.coffee @@ -47,6 +47,15 @@ class Mode      @id = "#{@name}-#{@count}"      @log "activate:", @id +    # If options.suppressAllKeyboardEvents is truthy, then all keyboard events are suppressed.  This avoids +    # the need for modes which block all keyboard events to 1) provide handlers for all keyboard events, +    # and 2) worry about their return value. +    if options.suppressAllKeyboardEvents +      for type in [ "keydown", "keypress", "keyup" ] +        do (type) -> +          handler = options[type] +          options[type] = (event) -> handler? event; false +      @push        keydown: @options.keydown || null        keypress: @options.keypress || null | 
