diff --git a/lib/tab-bar-view.coffee b/lib/tab-bar-view.coffee index d54a80b3..da3fafd2 100644 --- a/lib/tab-bar-view.coffee +++ b/lib/tab-bar-view.coffee @@ -404,8 +404,14 @@ class TabBarView @rightClickedTab.element.classList.add('right-clicked') event.preventDefault() else if event.which is 1 and not event.target.classList.contains('close-icon') - @pane.activateItem(tab.item) - setImmediate => @pane.activate() unless @pane.isDestroyed() + # Delay action. This is important because the browser will set the focus + # as part of the default action of the mousedown event; therefore, any + # change we make to the focus as part of the handler would be overwritten. + # We could use `preventDefault()` to address this, but that would also + # make the tab undraggable. + setImmediate => + @pane.activateItem(tab.item) + @pane.activate() unless @pane.isDestroyed() else if event.which is 2 @pane.destroyItem(tab.item) event.preventDefault() diff --git a/spec/tabs-spec.coffee b/spec/tabs-spec.coffee index 4209c19a..5e23817f 100644 --- a/spec/tabs-spec.coffee +++ b/spec/tabs-spec.coffee @@ -261,19 +261,25 @@ describe "TabBarView", -> spyOn(pane, 'activate') event = triggerMouseEvent('mousedown', tabBar.tabAtIndex(0).element, which: 1) - expect(pane.getActiveItem()).toBe pane.getItems()[0] - expect(event.preventDefault).not.toHaveBeenCalled() # allows dragging - - event = triggerMouseEvent('mousedown', tabBar.tabAtIndex(2).element, which: 1) - expect(pane.getActiveItem()).toBe pane.getItems()[2] - expect(event.preventDefault).not.toHaveBeenCalled() # allows dragging - # Pane activation is delayed because focus is stolen by the tab bar # immediately afterward unless propagation of the mousedown event is # stopped. But stopping propagation of the mousedown event prevents the # dragstart event from occurring. - waits(1) - runs -> expect(pane.activate.callCount).toBe 2 + expect(pane.getActiveItem()).not.toBe(pane.getItems()[0]) + waitsFor -> + pane.getActiveItem() is pane.getItems()[0] + + runs -> + expect(event.preventDefault).not.toHaveBeenCalled() # allows dragging + event = triggerMouseEvent('mousedown', tabBar.tabAtIndex(2).element, which: 1) + expect(pane.getActiveItem()).not.toBe(pane.getItems()[2]) + + waitsFor -> + pane.getActiveItem() is pane.getItems()[2] + + runs -> + expect(event.preventDefault).not.toHaveBeenCalled() # allows dragging + expect(pane.activate.callCount).toBe 2 it "closes the tab when middle clicked", -> jasmine.attachToDOM(tabBar.element) # Remove after Atom 1.2.0 is released