From e4d73ace2507aec8103bd7747a32efc25347042c Mon Sep 17 00:00:00 2001 From: Corey Johnson & Nathan Sobo Date: Thu, 29 Dec 2011 17:36:58 -0800 Subject: [PATCH] Bound up / down keys to moveUp / moveDown in fileFinder --- spec/atom/file-finder-spec.coffee | 6 +++--- spec/atom/window-spec.coffee | 16 ++++++++++------ spec/spec-helper.coffee | 10 ++++++++-- src/atom/file-finder.coffee | 14 +++++++++----- src/atom/window.coffee | 24 +++++++++++++++++++++++- src/stdlib/template.coffee | 2 +- 6 files changed, 54 insertions(+), 18 deletions(-) diff --git a/spec/atom/file-finder-spec.coffee b/spec/atom/file-finder-spec.coffee index 527b1ec91..16cef92b0 100644 --- a/spec/atom/file-finder-spec.coffee +++ b/spec/atom/file-finder-spec.coffee @@ -17,7 +17,7 @@ describe 'FileFinder', -> describe "when characters are typed into the input element", -> it "displays matching urls in the ol element and selects the first", -> finder.input.val('ap') - finder.input.keyup() + finder.input.trigger 'input' expect(finder.urlList.children().length).toBe 2 expect(finder.urlList.find('li:contains(app.coffee)').length).toBe 2 @@ -27,12 +27,12 @@ describe 'FileFinder', -> # we should clear the list before re-populating it finder.input.val('a/ap') - finder.input.keyup() + finder.input.trigger 'input' expect(finder.urlList.children().length).toBe 1 expect(finder.urlList.find('li:contains(atom/app.coffee)').length).toBe 1 - fdescribe "moveDown / moveUp", -> + describe "moveDown / moveUp", -> it "selects the next / previous url in the list", -> expect(finder.find('li:eq(0)')).toHaveClass "selected" expect(finder.find('li:eq(2)')).not.toHaveClass "selected" diff --git a/spec/atom/window-spec.coffee b/spec/atom/window-spec.coffee index dbed8f16d..b0ce31b01 100644 --- a/spec/atom/window-spec.coffee +++ b/spec/atom/window-spec.coffee @@ -32,13 +32,14 @@ describe "Window", -> expect(action2).not.toHaveBeenCalled() describe 'keyEventMatchesPattern', -> - it 'returns true if the modifiers and letter in the pattern match the key event', -> - expectMatch = (pattern) -> - expect(window.keyEventMatchesPattern(window.createKeyEvent(pattern), pattern)).toBeTruthy() + expectMatch = (pattern) -> + expect(window.keyEventMatchesPattern(window.createKeyEvent(pattern), pattern)).toBeTruthy() - expectNoMatch = (eventPattern, patternToTest) -> - event = window.createKeyEvent(eventPattern) - expect(window.keyEventMatchesPattern(event, patternToTest)).toBeFalsy() + expectNoMatch = (eventPattern, patternToTest) -> + event = window.createKeyEvent(eventPattern) + expect(window.keyEventMatchesPattern(event, patternToTest)).toBeFalsy() + + it 'returns true if the modifiers and letter in the pattern match the key event', -> expectMatch 'meta+a' expectMatch 'meta+1' @@ -56,6 +57,9 @@ describe "Window", -> expectNoMatch 'meta+a', 'meta+b' expectNoMatch 'meta+1', 'alt+1' + it 'handles named special keys (e.g. arrows, home)', -> + expectMatch 'up' + describe "bindMenuItem(path, keyPattern, action)", -> it "causes the given menu item to be added to the menu when the window is focused and removed when it is blurred", -> addedPaths = [] diff --git a/spec/spec-helper.coffee b/spec/spec-helper.coffee index 94204773f..1722ab68c 100644 --- a/spec/spec-helper.coffee +++ b/spec/spec-helper.coffee @@ -13,10 +13,16 @@ window.keydown = (pattern) -> window.createKeyEvent = (pattern) -> keys = pattern.split '+' + key = _.last(keys) + + if window.namedKeys[key] + key = window.namedKeys[key] + else + key = key.toUpperCase().charCodeAt 0 + $.Event "keydown", ctrlKey: 'ctrl' in keys altKey: 'alt' in keys shiftKey: 'shift' in keys metaKey: 'meta' in keys - which: _.last(keys).toUpperCase().charCodeAt 0 - + which: key diff --git a/src/atom/file-finder.coffee b/src/atom/file-finder.coffee index 76c40f38c..c5ef93f67 100644 --- a/src/atom/file-finder.coffee +++ b/src/atom/file-finder.coffee @@ -6,15 +6,22 @@ module.exports = class FileFinder extends Template content: -> @link rel: 'stylesheet', href: "#{require.resolve('file-finder.css')}?#{(new Date).getTime()}" - @div keydown: 'handleKeydown', class: 'file-finder', => + @div class: 'file-finder', => @ol outlet: 'urlList' - @input outlet: 'input', keyup: 'populateUrlList' + @input outlet: 'input', input: 'populateUrlList' viewProperties: urls: null initialize: ({@urls}) -> @populateUrlList() + @bindKey 'up', 'moveUp' + @bindKey 'down', 'moveDown' + + bindKey: (pattern, methodName) -> + @on 'keydown', (event) => + if window.keyEventMatchesPattern(event, pattern) + this[methodName]() populateUrlList: -> @urlList.empty() @@ -45,6 +52,3 @@ class FileFinder extends Template scoredUrls = ({url, score: stringScore(url, query)} for url in @urls) sortedUrls = scoredUrls.sort (a, b) -> a.score > b.score urlAndScore.url for urlAndScore in sortedUrls when urlAndScore.score > 0 - - handleKeydown: -> - console.log 'keydownnnn' diff --git a/src/atom/window.coffee b/src/atom/window.coffee index 0ab8dbf08..7076cd797 100644 --- a/src/atom/window.coffee +++ b/src/atom/window.coffee @@ -49,15 +49,36 @@ windowAdditions = keys.altKey == event.altKey and keys.shiftKey == event.shiftKey and keys.metaKey == event.metaKey and - event.which == keys.key.toUpperCase().charCodeAt 0 + event.which == keys.charCode + + namedKeys: + backspace: 8, tab: 9, clear: 12, + enter: 13, 'return': 13, + esc: 27, escape: 27, space: 32, + left: 37, up: 38, + right: 39, down: 40, + del: 46, 'delete': 46, + home: 36, end: 35, + pageup: 33, pagedown: 34, + ',': 188, '.': 190, '/': 191, + '`': 192, '-': 189, '=': 187, + ';': 186, '\'': 222, + '[': 219, ']': 221, '\\': 220 parseKeyPattern: (pattern) -> [modifiers..., key] = pattern.split '+' + if window.namedKeys[key] + charCode = window.namedKeys[key] + key = null + else + charCode = key.toUpperCase().charCodeAt 0 + ctrlKey: 'ctrl' in modifiers altKey: 'alt' in modifiers shiftKey: 'shift' in modifiers metaKey: 'meta' in modifiers + charCode: charCode key: key registerEventHandlers: -> @@ -68,6 +89,7 @@ windowAdditions = $(window).focus => @registerMenuItems() $(window).blur -> atom.native.resetMainMenu() + registerMenuItems: -> for path, {pattern} of @menuItemActions atom.native.addMenuItem(path, pattern) diff --git a/src/stdlib/template.coffee b/src/stdlib/template.coffee index e3ffd8dc0..a97632159 100644 --- a/src/stdlib/template.coffee +++ b/src/stdlib/template.coffee @@ -4,7 +4,7 @@ Builder = require 'template/builder' module.exports = class Template - @events: 'blur change click dblclick error focus keydown + @events: 'blur change click dblclick error focus input keydown keypress keyup load mousedown mousemove mouseout mouseover mouseup resize scroll select submit unload'.split /\s+/