Previously focused editor is re-focused on refresh

The isFocused property has now been added to editor state. When an editor is attached to the dom, it focuses itself automatically if and only if the @isFocused property is true. The @isFocused property gets assigned when the editor is constructed by the root view with its previous state.
This commit is contained in:
Nathan Sobo 2012-04-11 14:58:51 -06:00
parent 144fb29d83
commit 535fba1af8
5 changed files with 15 additions and 26 deletions

View File

@ -94,23 +94,3 @@ describe 'FileFinder', ->
it "returns paths sorted by score of match against the given query", ->
expect(finder.findMatches('ap')).toEqual ["app.coffee", "atom/app.coffee"]
expect(finder.findMatches('a/ap')).toEqual ["atom/app.coffee"]
describe "when it is removed", ->
input = null
beforeEach ->
input = $$ -> @input value : "this has focus"
input.attachToDom()
input.focus()
expect(document.activeElement).toBe input[0]
finder = new FileFinder(paths: [])
finder.attachToDom()
expect(document.activeElement).not.toBe input[0]
afterEach ->
input.remove()
it "returns focus to previous active element", ->
finder.remove()
expect(document.activeElement).toBe input[0]

View File

@ -44,6 +44,7 @@ describe "RootView", ->
delete atom.windowStatesByWindowNumber[$windowNumber]
it "stores its window state on the atom object by window number, then reassigns it next time the root view is constructed", ->
rootView.activeEditor().splitLeft()
expectedWindowState = rootView.getWindowState()
# simulate unload
@ -52,7 +53,9 @@ describe "RootView", ->
# simulate reload
newRootView = new RootView
expect(newRootView.getWindowState()).toEqual expectedWindowState
expect(newRootView.editors.length).toBe 2
describe "focus", ->
it "can receive focus if there is no active editor, but otherwise hands off focus to the active editor", ->
@ -81,6 +84,8 @@ describe "RootView", ->
editor3.setCursorScreenPosition([2, 3])
editor4.setBuffer(new Buffer(require.resolve 'fixtures/sample.txt'))
editor4.setCursorScreenPosition([0, 2])
rootView.attachToDom()
editor2.focus()
panesHtml = rootView.panes.html()
it "can reconstruct the split pane arrangement from the window state hash returned by getWindowState", ->
@ -112,6 +117,12 @@ describe "RootView", ->
expect(editor3.width()).toBeGreaterThan 0
expect(editor4.width()).toBeGreaterThan 0
# ensure correct editor is focused again
expect(editor2.isFocused).toBeTruthy()
expect(editor1.isFocused).toBeFalsy()
expect(editor3.isFocused).toBeFalsy()
expect(editor4.isFocused).toBeFalsy()
describe "split editor panes", ->
editor1 = null

View File

@ -168,7 +168,7 @@ class Editor extends View
@calculateDimensions()
@hiddenInput.width(@charWidth)
@setMaxLineLength() if @softWrap
@focus()
@focus() if @isFocused
rootView: ->
@parents('#root-view').view()
@ -227,12 +227,14 @@ class Editor extends View
buffer = editorState.buffer ? new Buffer
@editorStatesByBufferId[buffer.id] = editorState
@setBuffer(buffer)
@isFocused = editorState.isFocused
getEditorState: ->
buffer: @buffer
cursorScreenPosition: @getCursorScreenPosition().copy()
scrollTop: @scroller.scrollTop()
scrollLeft: @scroller.scrollLeft()
isFocused: @isFocused
saveEditorStateForCurrentBuffer: ->
@editorStatesByBufferId[@buffer.id] = @getEditorState()

View File

@ -16,7 +16,6 @@ class FileFinder extends View
initialize: ({@paths, @selected}) ->
requireStylesheet 'file-finder.css'
@maxResults = 10
@previousFocusedElement = $(document.activeElement)
@populatePathList()
@ -44,10 +43,6 @@ class FileFinder extends View
@selected(filePath) if filePath and @selected
@remove()
remove: ->
super()
@previousFocusedElement.focus()
moveUp: ->
@findSelectedLi()
.filter(':not(:first-child)')

View File

@ -192,3 +192,4 @@ class RootView extends View
paths: relativePaths
selected: (relativePath) => @open(relativePath)
@append @fileFinder
@fileFinder.editor.focus()