Restore focus after select list detaches

Previously if the select list was cancelled with no open
editors the mini editor would be given focus before being
detached causing nothing to have focus after detach completed.

Now the select list tracks the previously focused elements and
restores focus to that element at the end of the cancel.
This commit is contained in:
Kevin Sawicki
2013-01-23 09:32:49 -08:00
parent f0aa9c136f
commit 001bb3a862
6 changed files with 48 additions and 17 deletions

View File

@@ -37,7 +37,6 @@ class GrammarView extends SelectList
cancelled: ->
@miniEditor.setText('')
@editor.rootView()?.focus() if @miniEditor.isFocused
confirmed: (grammar) ->
@cancel()
@@ -48,5 +47,7 @@ class GrammarView extends SelectList
@editor.reloadGrammar()
attach: ->
super
@editor.rootView()?.append(this)
@miniEditor.focus()

View File

@@ -121,10 +121,21 @@ class SelectList extends View
else
@cancel()
attach: ->
@storeFocusedElement()
storeFocusedElement: ->
@previouslyFocusedElement = $(':focus')
restoreFocus: ->
@previouslyFocusedElement?.focus()
cancel: ->
@list.empty()
@cancelling = true
miniEditorFocused = @miniEditor.isFocused
@cancelled()
@detach()
@restoreFocus() if miniEditorFocused
@cancelling = false
clearTimeout(@scheduleTimeout)

View File

@@ -23,7 +23,8 @@ class CommandPaletteView extends SelectList
super
attach: ->
@previouslyFocusedElement = $(':focus')
super
@keyBindings = _.losslessInvert(keymap.bindingsForElement(@previouslyFocusedElement))
events = []
@@ -53,4 +54,3 @@ class CommandPaletteView extends SelectList
@previouslyFocusedElement.trigger(eventName)
cancelled: ->
@previouslyFocusedElement.focus() if @miniEditor.isFocused

View File

@@ -187,21 +187,38 @@ describe 'FuzzyFinder', ->
describe "common behavior between file and buffer finder", ->
describe "when the fuzzy finder is cancelled", ->
it "detaches the finder and focuses the previously focused element", ->
rootView.attachToDom()
activeEditor = rootView.getActiveEditor()
activeEditor.focus()
describe "when an editor is open", ->
it "detaches the finder and focuses the previously focused element", ->
rootView.attachToDom()
activeEditor = rootView.getActiveEditor()
activeEditor.focus()
rootView.trigger 'fuzzy-finder:toggle-file-finder'
expect(finder.hasParent()).toBeTruthy()
expect(activeEditor.isFocused).toBeFalsy()
expect(finder.miniEditor.isFocused).toBeTruthy()
rootView.trigger 'fuzzy-finder:toggle-file-finder'
expect(finder.hasParent()).toBeTruthy()
expect(activeEditor.isFocused).toBeFalsy()
expect(finder.miniEditor.isFocused).toBeTruthy()
finder.cancel()
finder.cancel()
expect(finder.hasParent()).toBeFalsy()
expect(activeEditor.isFocused).toBeTruthy()
expect(finder.miniEditor.isFocused).toBeFalsy()
expect(finder.hasParent()).toBeFalsy()
expect(activeEditor.isFocused).toBeTruthy()
expect(finder.miniEditor.isFocused).toBeFalsy()
describe "when no editors are open", ->
it "detaches the finder and focuses the previously focused element", ->
rootView.attachToDom()
rootView.getActiveEditor().destroyActiveEditSession()
rootView.trigger 'fuzzy-finder:toggle-file-finder'
expect(finder.hasParent()).toBeTruthy()
expect(rootView.isFocused).toBeFalsy()
expect(finder.miniEditor.isFocused).toBeTruthy()
finder.cancel()
expect(finder.hasParent()).toBeFalsy()
expect($(document.activeElement).view()).toBe rootView
expect(finder.miniEditor.isFocused).toBeFalsy()
describe "cached file paths", ->
it "caches file paths after first time", ->

View File

@@ -74,7 +74,6 @@ class FuzzyFinderView extends SelectList
cancelled: ->
@miniEditor.setText('')
@rootView.focus() if @miniEditor.isFocused
toggleFileFinder: ->
if @hasParent()
@@ -157,5 +156,7 @@ class FuzzyFinderView extends SelectList
@setArray(@paths)
attach: ->
super
@rootView.append(this)
@miniEditor.focus()

View File

@@ -90,9 +90,10 @@ class SymbolsView extends SelectList
cancelled: ->
@miniEditor.setText('')
@rootView.focus() if @miniEditor.isFocused
attach: ->
super
@rootView.append(this)
@miniEditor.focus()