Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
Merge pull request #439 from atom/fb-mdt-delay-item-activation
Browse files Browse the repository at this point in the history
Delay activation of item on mousedown event
  • Loading branch information
Antonio Scandurra authored May 15, 2017
2 parents fcd14eb + a295987 commit 6372f97
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
10 changes: 8 additions & 2 deletions lib/tab-bar-view.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
24 changes: 15 additions & 9 deletions spec/tabs-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6372f97

Please sign in to comment.