Ensure focus returns to the editor when file finder closes.

We just capture focusout events on root view… if anything other than the editors text area lost focus, we focus the editor again. This will likely need refinement when we add more widgets to the system, but its enough to make the fuzzy finder behave appropriately.
This commit is contained in:
Corey Johnson & Nathan Sobo
2012-01-11 13:13:37 -08:00
parent 83a68dbbb0
commit 17ad7a26e7
4 changed files with 52 additions and 40 deletions

View File

@@ -38,49 +38,54 @@ describe "RootView", ->
rootView.addPane $('<div id="foo">')
expect(rootView.vertical.children().length).toBe 2
describe ".toggleFileFinder()", ->
describe "when there is a project", ->
it "shows the FileFinder when it is not on screen and hides it when it is", ->
runs ->
describe "the file finder", ->
describe "when the toggle-file-finder event is triggered", ->
describe "when there is a project", ->
it "shows the FileFinder when it is not on screen and hides it when it is", ->
runs ->
$('#jasmine-content').append(rootView)
expect(rootView.find('.file-finder')).not.toExist()
waitsForPromise ->
rootView.resultOfTrigger 'toggle-file-finder'
runs ->
expect(rootView.find('.file-finder')).toExist()
expect(rootView.find('.file-finder input:focus')).toExist()
rootView.trigger 'toggle-file-finder'
expect(rootView.find('.editor textarea:focus')).toExist()
expect(rootView.find('.file-finder')).not.toExist()
it "shows all relative file paths for the current project", ->
waitsForPromise ->
rootView.resultOfTrigger 'toggle-file-finder'
waitsForPromise ->
project.getFilePaths().done (paths) ->
expect(rootView.fileFinder.urlList.children('li').length).toBe paths.length
for path in paths
relativePath = path.replace(project.url, '')
expect(rootView.fileFinder.urlList.find("li:contains(#{relativePath}):not(:contains(#{project.url}))")).toExist()
describe "when there is no project", ->
beforeEach ->
rootView = RootView.build()
it "does not open the FileFinder", ->
expect(rootView.editor.buffer.url).toBeUndefined()
expect(rootView.find('.file-finder')).not.toExist()
waitsForPromise ->
rootView.resultOfTrigger 'toggle-file-finder'
runs ->
expect(rootView.find('.file-finder')).toExist()
rootView.trigger 'toggle-file-finder'
expect(rootView.find('.file-finder')).not.toExist()
it "shows all relative file paths for the current project", ->
waitsForPromise ->
rootView.resultOfTrigger 'toggle-file-finder'
waitsForPromise ->
project.getFilePaths().done (paths) ->
expect(rootView.fileFinder.urlList.children('li').length).toBe paths.length
for path in paths
relativePath = path.replace(project.url, '')
expect(rootView.fileFinder.urlList.find("li:contains(#{relativePath}):not(:contains(#{project.url}))")).toExist()
describe "when there is no project", ->
beforeEach ->
rootView = RootView.build()
it "does not open the FileFinder", ->
expect(rootView.editor.buffer.url).toBeUndefined()
expect(rootView.find('.file-finder')).not.toExist()
rootView.trigger 'toggle-file-finder'
expect(rootView.find('.file-finder')).not.toExist()
describe "when a path is selected in the file finder", ->
it "opens the file associated with that path in the editor", ->
waitsForPromise -> rootView.resultOfTrigger 'toggle-file-finder'
runs ->
firstLi = rootView.fileFinder.find('li:first')
rootView.fileFinder.trigger 'select'
expect(rootView.editor.buffer.url).toBe(project.url + firstLi.text())
describe "when a path is selected in the file finder", ->
it "opens the file associated with that path in the editor", ->
waitsForPromise -> rootView.resultOfTrigger 'toggle-file-finder'
runs ->
firstLi = rootView.fileFinder.find('li:first')
rootView.fileFinder.trigger 'select'
expect(rootView.editor.buffer.url).toBe(project.url + firstLi.text())
describe "global keymap wiring", ->
commandHandler = null

View File

@@ -7,6 +7,7 @@ BindingSet = require 'binding-set'
afterEach ->
(new Native).resetMainMenu()
atom.globalKeymap.reset()
$('#jasmine-content').empty()
window.atom = new (require 'app')

View File

@@ -37,7 +37,7 @@ class Editor extends Template
@aceEditor.setTheme(require "ace/theme/twilight")
@aceEditor.setKeyboardHandler
handleKeyboard: (data, hashId, keyString, keyCode, event) =>
if event and @keyEventHandler?.handleKeyEvent(event)
if event and @keyEventHandler and @keyEventHandler.handleKeyEvent(event) == false
{ command: { exec: -> }}
else
null
@@ -45,6 +45,9 @@ class Editor extends Template
getAceSession: ->
@aceEditor.getSession()
focus: ->
@aceEditor.focus()
save: ->
if @buffer.url
@buffer.save()

View File

@@ -32,6 +32,9 @@ class RootView extends Template
@on 'toggle-file-finder', => @toggleFileFinder()
@on 'focusout', (e) =>
@editor.focus() unless e.target is @editor.find('textarea')[0]
createProject: (url) ->
if url
@project = new Project(fs.directory(url))