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

Delay activation of item on mousedown event #439

Merged
merged 2 commits into from
May 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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