diff --git a/spec/app/pane-spec.coffee b/spec/app/pane-spec.coffee index 331e414e4..b24f9540c 100644 --- a/spec/app/pane-spec.coffee +++ b/spec/app/pane-spec.coffee @@ -8,8 +8,8 @@ describe "Pane", -> beforeEach -> container = new PaneContainer - view1 = $$ -> @div id: 'view-1', 'View 1' - view2 = $$ -> @div id: 'view-2', 'View 2' + view1 = $$ -> @div id: 'view-1', tabindex: -1, 'View 1' + view2 = $$ -> @div id: 'view-2', tabindex: -1, 'View 2' editSession1 = project.buildEditSession('sample.js') editSession2 = project.buildEditSession('sample.txt') pane = new Pane(view1, editSession1, view2, editSession2) @@ -144,6 +144,62 @@ describe "Pane", -> expect(editSession1.destroyed).toBeTruthy() expect(editSession2.destroyed).toBeTruthy() + describe "when there are other panes", -> + [paneToLeft, paneToRight] = [] + + beforeEach -> + pane.showItem(editSession1) + paneToLeft = pane.splitLeft() + paneToRight = pane.splitRight() + container.attachToDom() + + describe "when the removed pane is focused", -> + it "activates and focuses the next pane", -> + pane.focus() + pane.remove() + expect(paneToLeft.isActive()).toBeFalsy() + expect(paneToRight.isActive()).toBeTruthy() + expect(paneToRight).toMatchSelector ':has(:focus)' + + describe "when the removed pane is active but not focused", -> + it "activates the next pane, but does not focus it", -> + $(document.activeElement).blur() + expect(pane).not.toMatchSelector ':has(:focus)' + pane.makeActive() + pane.remove() + expect(paneToLeft.isActive()).toBeFalsy() + expect(paneToRight.isActive()).toBeTruthy() + expect(paneToRight).not.toMatchSelector ':has(:focus)' + + describe "when the removed pane is not active", -> + it "does not affect the active pane or the focus", -> + paneToLeft.focus() + expect(paneToLeft.isActive()).toBeTruthy() + expect(paneToRight.isActive()).toBeFalsy() + + pane.remove() + expect(paneToLeft.isActive()).toBeTruthy() + expect(paneToRight.isActive()).toBeFalsy() + expect(paneToLeft).toMatchSelector ':has(:focus)' + + describe "when it is the last pane", -> + beforeEach -> + expect(container.getPanes().length).toBe 1 + window.rootView = focus: jasmine.createSpy("rootView.focus") + + describe "when the removed pane is focused", -> + it "calls focus on rootView so we don't lose focus", -> + container.attachToDom() + pane.focus() + pane.remove() + expect(rootView.focus).toHaveBeenCalled() + + describe "when the removed pane is not focused", -> + it "does not call focus on root view", -> + expect(pane).not.toMatchSelector ':has(:focus)' + pane.remove() + expect(rootView.focus).not.toHaveBeenCalled() + describe "when the pane is focused", -> it "focuses the current item view", -> focusHandler = jasmine.createSpy("focusHandler") diff --git a/spec/spec-helper.coffee b/spec/spec-helper.coffee index 910c8d238..485521129 100644 --- a/spec/spec-helper.coffee +++ b/spec/spec-helper.coffee @@ -73,7 +73,7 @@ afterEach -> keymap.bindingSets = bindingSetsToRestore keymap.bindingSetsByFirstKeystrokeToRestore = bindingSetsByFirstKeystrokeToRestore if rootView? - rootView.deactivate() + rootView.deactivate?() window.rootView = null if project? project.destroy() diff --git a/src/app/pane-container.coffee b/src/app/pane-container.coffee index aae999567..f9471d0b8 100644 --- a/src/app/pane-container.coffee +++ b/src/app/pane-container.coffee @@ -19,9 +19,19 @@ class PaneContainer extends View focusNextPane: -> panes = @getPanes() - currentIndex = panes.indexOf(@getFocusedPane()) + if panes.length > 1 + currentIndex = panes.indexOf(@getFocusedPane()) + nextIndex = (currentIndex + 1) % panes.length + panes[nextIndex].focus() + true + else + false + + makeNextPaneActive: -> + panes = @getPanes() + currentIndex = panes.indexOf(@getActivePane()) nextIndex = (currentIndex + 1) % panes.length - panes[nextIndex].focus() + panes[nextIndex].makeActive() getRoot: -> @children().first().view() diff --git a/src/app/pane.coffee b/src/app/pane.coffee index cb5e3636d..99f3d6d32 100644 --- a/src/app/pane.coffee +++ b/src/app/pane.coffee @@ -171,7 +171,13 @@ class Pane extends View # find parent elements before removing from dom container = @getContainer() parentAxis = @parent('.row, .column') + if @is(':has(:focus)') + rootView?.focus() unless container.focusNextPane() + else if @isActive() + container.makeNextPaneActive() + super + if parentAxis.children().length == 1 sibling = parentAxis.children().detach() parentAxis.replaceWith(sibling)