diff --git a/platform/platform-api/src/com/intellij/ui/tabs/TabInfo.java b/platform/platform-api/src/com/intellij/ui/tabs/TabInfo.java index 56e646ab8f4d..b851d9745126 100644 --- a/platform/platform-api/src/com/intellij/ui/tabs/TabInfo.java +++ b/platform/platform-api/src/com/intellij/ui/tabs/TabInfo.java @@ -86,6 +86,12 @@ public final class TabInfo implements Queryable, PlaceProvider { private Queryable myQueryable; private DragOutDelegate myDragOutDelegate; + /** + * The tab which was selected before the mouse was pressed on this tab. Focus will be transferred to that tab if this tab is dragged + * out of its container. (IDEA-61536) + */ + private WeakReference myPreviousSelection = new WeakReference(null); + public TabInfo(final JComponent component) { myComponent = component; myPreferredFocusableComponent = component; @@ -371,6 +377,15 @@ public final class TabInfo implements Queryable, PlaceProvider { return myDragOutDelegate; } + public void setPreviousSelection(@Nullable TabInfo previousSelection) { + myPreviousSelection = new WeakReference(previousSelection); + } + + @Nullable + public TabInfo getPreviousSelection() { + return myPreviousSelection.get(); + } + public interface DragOutDelegate { void dragOutStarted(MouseEvent mouseEvent, TabInfo info); diff --git a/platform/platform-api/src/com/intellij/ui/tabs/impl/TabLabel.java b/platform/platform-api/src/com/intellij/ui/tabs/impl/TabLabel.java index a462865b3b2f..a35f05d2f2c3 100644 --- a/platform/platform-api/src/com/intellij/ui/tabs/impl/TabLabel.java +++ b/platform/platform-api/src/com/intellij/ui/tabs/impl/TabLabel.java @@ -88,6 +88,10 @@ public class TabLabel extends JPanel { addMouseListener(new MouseAdapter() { public void mousePressed(final MouseEvent e) { if (myTabs.isSelectionClick(e, false) && myInfo.isEnabled()) { + final TabInfo selectedInfo = myTabs.getSelectedInfo(); + if (selectedInfo != myInfo) { + myInfo.setPreviousSelection(selectedInfo); + } Component c = SwingUtilities.getDeepestComponentAt(e.getComponent(), e.getX(), e.getY()); if (c instanceof InplaceButton) return; myTabs.select(info, true); @@ -102,6 +106,7 @@ public class TabLabel extends JPanel { } public void mouseReleased(final MouseEvent e) { + myInfo.setPreviousSelection(null); handlePopup(e); } }); diff --git a/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/EditorTabbedContainer.java b/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/EditorTabbedContainer.java index 20a19762b1b8..b24815f3f014 100644 --- a/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/EditorTabbedContainer.java +++ b/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/EditorTabbedContainer.java @@ -212,7 +212,12 @@ final class EditorTabbedContainer implements Disposable, CloseAction.CloseTarget public ActionCallback removeTabAt(final int componentIndex, int indexToSelect, boolean transferFocus) { TabInfo toSelect = indexToSelect >= 0 && indexToSelect < myTabs.getTabCount() ? myTabs.getTabAt(indexToSelect) : null; - final ActionCallback callback = myTabs.removeTab(myTabs.getTabAt(componentIndex), toSelect, transferFocus); + final TabInfo info = myTabs.getTabAt(componentIndex); + // removing hidden tab happens on end of drag-out, we've already selected the correct tab for this case in dragOutStarted + if (info.isHidden()) { + toSelect = null; + } + final ActionCallback callback = myTabs.removeTab(info, toSelect, transferFocus); return myProject.isOpen() ? callback : new ActionCallback.Done(); } @@ -554,8 +559,12 @@ final class EditorTabbedContainer implements Disposable, CloseAction.CloseTarget @Override public void dragOutStarted(MouseEvent mouseEvent, TabInfo info) { + final TabInfo previousSelection = info.getPreviousSelection(); final Image img = myTabs.getComponentImage(info); info.setHidden(true); + if (previousSelection != null) { + myTabs.select(previousSelection, true); + } myFile = (VirtualFile)info.getObject(); Presentation presentation = new Presentation(info.getText());