Repository: spyder-ide/spyder

Issue:
Right click on editor tabs selects the wrong tab
## Description

### What steps will reproduce the problem?

<!--- You can use Markdown here --->
In the editor:

Right-click on the left, the right tab is selected:
![image](https://user-images.githubusercontent.com/6740194/115043758-24598000-9ed5-11eb-8049-c7bbac69577e.png)
Right-click on the right, the next tab is selected:
![image](https://user-images.githubusercontent.com/6740194/115043864-3b986d80-9ed5-11eb-93d2-51bc7b1aab3d.png)

If I select: "Set console working directory" the wrong directory is indeed selected


## Versions

* Spyder version: 5.1.0.dev0 b699e315d
* Python version: 3.8.6
* Qt version: 5.12.10
* PyQt5 version: 5.12.3
* Operating System: Darwin 20.3.0

### Dependencies

```

# Mandatory:
applaunchservices >=0.1.7     :  0.2.1 (OK)
atomicwrites >=1.2.0          :  1.4.0 (OK)
chardet >=2.0.0               :  3.0.4 (OK)
cloudpickle >=0.5.0           :  1.6.0 (OK)
cookiecutter >=1.6.0          :  1.7.2 (OK)
diff_match_patch >=20181111   :  20200713 (OK)
intervaltree >=3.0.2          :  3.1.0 (OK)
IPython >=7.6.0               :  7.22.0 (OK)
jedi =0.17.2                  :  0.17.2 (OK)
jsonschema >=3.2.0            :  3.2.0 (OK)
keyring >=17.0.0              :  21.4.0 (OK)
nbconvert >=4.0               :  6.0.7 (OK)
numpydoc >=0.6.0              :  1.1.0 (OK)
parso =0.7.0                  :  0.7.0 (OK)
pexpect >=4.4.0               :  4.8.0 (OK)
pickleshare >=0.4             :  0.7.5 (OK)
psutil >=5.3                  :  5.7.2 (OK)
pygments >=2.0                :  2.7.1 (OK)
pylint >=1.0                  :  2.6.0 (OK)
pyls >=0.36.2;<1.0.0          :  0.36.2 (OK)
pyls_black >=0.4.6            :  0.4.6 (OK)
pyls_spyder >=0.3.2           :  0.3.2 (OK)
qdarkstyle =3.0.2             :  3.0.2 (OK)
qstylizer >=0.1.10            :  0.1.10 (OK)
qtawesome >=1.0.2             :  1.0.2 (OK)
qtconsole >=5.0.3             :  5.1.0.dev0 (OK)
qtpy >=1.5.0                  :  1.9.0 (OK)
setuptools >=39.0.0           :  50.3.0 (OK)
sphinx >=0.6.6                :  3.2.1 (OK)
spyder_kernels >=2.0.1;<2.1.0 :  2.1.0.dev0 (OK)
textdistance >=4.2.0          :  4.2.0 (OK)
three_merge >=0.1.1           :  0.1.1 (OK)
watchdog >=0.10.3;<2.0.0      :  0.10.3 (OK)
zmq >=17                      :  19.0.2 (OK)

# Optional:
cython >=0.21                 :  0.29.21 (OK)
matplotlib >=2.0.0            :  3.3.2 (OK)
numpy >=1.7                   :  1.19.2 (OK)
pandas >=1.1.1                :  1.1.3 (OK)
scipy >=0.17.0                :  1.5.2 (OK)
sympy >=0.7.3                 :  1.6.2 (OK)
```



Discussion:
Hi @impact27. Thanks for reporting this. I can successfully reproduce this issue. Also there seems to be some kind of lag before selecting the tab. 
@dalthviz can you test this in Windows and let us know if it happens there too?
I can't reproduce this on Linux, so maybe it's a Mac issue only.
@juanis2112 @impact27 @ccordoba12 I was able to reproduce this on Windows:

![rclick](https://user-images.githubusercontent.com/16781833/115277175-3b51d980-a144-11eb-8a4d-b7d9b2202a71.gif)

I think some sort of invisible overlapping between tabs could be causing this?
I can reproduce this too, but on Windows, the area where you need to click to reproduce the bug seems smaller than on Mac.

![image](https://user-images.githubusercontent.com/10170372/115451031-a950dc00-a1ea-11eb-925c-d8d73a393ab2.png)


Resolving patch:
diff --git a/spyder/widgets/tabs.py b/spyder/widgets/tabs.py
--- a/spyder/widgets/tabs.py
+++ b/spyder/widgets/tabs.py
@@ -359,17 +359,11 @@ 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 button on tab is on the right
-            point.setX(point.x() - 30)
-        return self.tabBar().tabAt(point)
+        # This is necessary because event.pos() is the position in this
+        # widget, not in the tabBar. see spyder-ide/spyder#12617
+        tb = self.tabBar()
+        point = tb.mapFromGlobal(event.globalPos())
+        return tb.tabAt(point)
 
     def contextMenuEvent(self, event):
         """Override Qt method"""


Test patch: