Make TreeView focus the active editor when a file is selected w/ the keyboard

This commit is contained in:
Nathan Sobo
2012-09-18 13:00:16 -06:00
parent fe8b547a34
commit 68effe3303
3 changed files with 17 additions and 10 deletions

View File

@@ -466,10 +466,11 @@ describe "TreeView", ->
describe "tree-view:open-selected-entry", ->
describe "when a file is selected", ->
it "opens the file in the editor", ->
it "opens the file in the editor and focuses it", ->
treeView.root.find('.file:contains(sample.js)').click()
treeView.root.trigger 'tree-view:open-selected-entry'
expect(rootView.getActiveEditor().getPath()).toBe require.resolve('fixtures/sample.js')
expect(rootView.getActiveEditor().isFocused).toBeTruthy()
describe "when a directory is selected", ->
it "expands or collapses the directory", ->

View File

@@ -102,7 +102,7 @@ class RootView extends View
changeFocus = options.changeFocus ? true
allowActiveEditorChange = options.allowActiveEditorChange ? false
unless editSession = @openInExistingEditor(path, allowActiveEditorChange)
unless editSession = @openInExistingEditor(path, allowActiveEditorChange, changeFocus)
editSession = @project.buildEditSessionForPath(path)
editor = new Editor({editSession})
pane = new Pane(editor)
@@ -110,12 +110,14 @@ class RootView extends View
if changeFocus
editor.focus()
else
@makeEditorActive(editor)
@makeEditorActive(editor, changeFocus)
editSession
openInExistingEditor: (path, allowActiveEditorChange) ->
openInExistingEditor: (path, allowActiveEditorChange, changeFocus) ->
if activeEditor = @getActiveEditor()
activeEditor.focus() if changeFocus
path = @project.resolve(path) if path
if editSession = activeEditor.activateEditSessionForPath(path)
@@ -124,7 +126,7 @@ class RootView extends View
if allowActiveEditorChange
for editor in @getEditors()
if editSession = editor.activateEditSessionForPath(path)
editor.focus()
@makeEditorActive(editor, changeFocus)
return editSession
editSession = @project.buildEditSessionForPath(path)
@@ -134,7 +136,11 @@ class RootView extends View
editorFocused: (editor) ->
@makeEditorActive(editor) if @panes.containsElement(editor)
makeEditorActive: (editor) ->
makeEditorActive: (editor, focus) ->
if focus
editor.focus()
return
previousActiveEditor = @panes.find('.editor.active').view()
previousActiveEditor?.removeClass('active').off('.root-view')
editor.addClass('active')

View File

@@ -47,7 +47,7 @@ class TreeView extends View
@on 'move-down', => @moveDown()
@on 'tree-view:expand-directory', => @expandDirectory()
@on 'tree-view:collapse-directory', => @collapseDirectory()
@on 'tree-view:open-selected-entry', => @openSelectedEntry()
@on 'tree-view:open-selected-entry', => @openSelectedEntry(true)
@on 'tree-view:move', => @moveSelectedEntry()
@on 'tree-view:add', => @add()
@on 'tree-view:remove', => @removeSelectedEntry()
@@ -88,7 +88,7 @@ class TreeView extends View
switch e.originalEvent?.detail ? 1
when 1
@selectEntry(entry)
@openSelectedEntry() if (entry instanceof FileView)
@openSelectedEntry(false) if (entry instanceof FileView)
when 2
if entry.is('.selected.file')
@rootView.getActiveEditor().focus()
@@ -176,12 +176,12 @@ class TreeView extends View
directory.collapse()
@selectEntry(directory)
openSelectedEntry: ->
openSelectedEntry: (changeFocus) ->
selectedEntry = @selectedEntry()
if (selectedEntry instanceof DirectoryView)
selectedEntry.view().toggleExpansion()
else if (selectedEntry instanceof FileView)
@rootView.open(selectedEntry.getPath(), changeFocus: false)
@rootView.open(selectedEntry.getPath(), { changeFocus })
moveSelectedEntry: ->
entry = @selectedEntry()