Repository: spyder-ide/spyder

Issue:
Middle mouse button click on editor tabs may close wrong tab
Found a minor UI issue: When clicking the middle mouse button on the filename in an editor tab, it closes that tab, as expected. However, when doing this anywhere on the "X" symbol part of the tab, Spyder closes the next tab to the right.

E.g. in this image: 
![Untitled](https://user-images.githubusercontent.com/34485360/80745300-4fd53600-8add-11ea-81c8-334ed0880208.png)
Middle mouse button click in the red area should close "temp.py", but it closes "untitled1.py". 
(Literal) Edge case: Clicking in the green area should close "untitled1.py", but does nothing.

Python 3.7.7 64-bit | Qt 5.9.6 | PyQt5 5.9.2 | Windows 10
clean miniconda environment & spyder --reset


Discussion:
Thanks for the report @matheger . I do not have a mouse handy to check this problem, but what you say indeed suggest something is wrong.

Will try to tackle this in a future release.

Cheers!
Hi @goanpeca I'm unable to check this issue because I don't have a mouse either. We can ask in the team to see if someone can reproduce this.
I can reproduce this in Ubuntu
>Hi @goanpeca I'm unable to check this issue because I don't have a mouse either. We can ask in the team to see if someone can reproduce this.

@steff456 you can overcome this by using https://karabiner-elements.pqrs.org/ with some configuration like the following one:

<img width="958" alt="Screen Shot 2020-05-04 at 16 23 17" src="https://user-images.githubusercontent.com/3627835/81015053-97342d00-8e23-11ea-8ed1-00df2ac3925c.png">

On Mac the `X` is to the left, but if I click on the right where the `X` would be on other operating systems, I get the same behavior. On the animation, I clicked F1 (Fn+F1) in the `py` portion of the name and the next tab closed instead of the one I clicked. So I can reproduce this.

![pylint](https://user-images.githubusercontent.com/3627835/81015268-04e05900-8e24-11ea-9cab-3e77c6a46d93.gif)


Resolving patch:
diff --git a/spyder/widgets/tabs.py b/spyder/widgets/tabs.py
--- a/spyder/widgets/tabs.py
+++ b/spyder/widgets/tabs.py
@@ -352,16 +352,34 @@ def add_corner_widgets(self, widgets, corner=Qt.TopRightCorner):
         self.set_corner_widgets({corner:
                                  self.corner_widgets.get(corner, [])+widgets})
 
+    def get_offset_pos(self, event):
+        """
+        Add offset to position event to capture the mouse cursor
+        inside a tab.
+        """
+        # This is necessary because self.tabBar().tabAt(event.pos()) is not
+        # returning the expected index. For further information see
+        # spyder-ide/spyder#12617
+        point = event.pos()
+        if sys.platform == 'darwin':
+            # The close button on tab is on the left
+            point.setX(point.x() + 3)
+        else:
+            # The close buttton on tab is on the right
+            point.setX(point.x() - 30)
+        return self.tabBar().tabAt(point)
+
     def contextMenuEvent(self, event):
         """Override Qt method"""
-        self.setCurrentIndex(self.tabBar().tabAt(event.pos()))
+        index = self.get_offset_pos(event)
+        self.setCurrentIndex(index)
         if self.menu:
             self.menu.popup(event.globalPos())
 
     def mousePressEvent(self, event):
         """Override Qt method"""
         if event.button() == Qt.MidButton:
-            index = self.tabBar().tabAt(event.pos())
+            index = self.get_offset_pos(event)
             if index >= 0:
                 self.sig_close_tab.emit(index)
                 event.accept()


Test patch: