-
Notifications
You must be signed in to change notification settings - Fork 29.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix context menu inaccurate positioning #187157
Conversation
1483543
to
df95df4
Compare
316ddaa
to
55e36ee
Compare
91e9110
to
f622827
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR! Have you been able to verify if the change addresses the mentioned issue ? I have not been able to repro them on my machine so cannot validate this change. But agree that Electron calculates the cursor coordinates when not explicitly configured at https://github.com/electron/electron/blob/93024be3b26ff66a7978ba278990a0a0f55bc9d3/shell/browser/api/electron_api_menu_views.cc#L33
Also a follow-up question, when would isCurrentCursor
not be needed ? I wondering if we should always use current cursor position for desktop. Maybe @sbatten can chime in on this.
Yes. It addresses the issue that the menu shifting from the current cursor position. So now if keeping cursor unmoved during right-click, there won't be accidental click. But if cursor is moved during right-click, there's still a chance of accidental click since Linux DE will acknowledge an menu item on mouseup -- this is a problem of Linux DE and we can't interfere with.
It might be easier to repro with
I don't know. But since this is a public API, I would rather ensure its forward compatibility so as not to break existing code. Adding an optional argument sounds reasonable. |
Thanks for testing, I have tried different variants documented in the upstream issue electron/electron#31641, unable to repro with my current display setup. I am good with merging this change once API changes are approved by @sbatten. We can get feedback from the affected users with insiders build. |
Don't calculate popup position for context menu if the desired position is current cursor position. Instead, pass x = undefined and y = undefined to popup(), and Electron will figure out the accurate value.
f622827
to
07604cf
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and hopefully should fix these related issues
Yeah, I am not 100% convinced it does. electron/electron#31641 is reported with a standalone sample that does seem to rely on Electron positioning the menu properly (code pointer).
Nevertheless, I think having a way to signal back to the Electron context menu to position it where the mouse is is an interesting idea. But the adoption is a bit challenging because from the outside we always have to pass in x
and y
for our custom menu to position properly which has no other way of getting to the cursor location otherwise.
On top of that, we seem to be having cases where - even though the context menu should open from mouse interaction - we put it to a slightly different location by a few pixels (for example here).
I think from the internal API I would expect another or
type to signal the mouse position is wanted, such as:
HTMLElement | { x: number; y: number; width?: number; height?: number; } | { mouseX: number; mouseY: number }
To make it very explicit that in the one case a x/y position is wanted and in the other case the mouse location is preferred.
Finally, - given my first point - I wonder if a pragmatic workaround for VS Code could be to always offset the menu by 1px
on Linux at least it the cursor location is wanted.
Something I have observed:
So here's my guessing -- this bug on VSCode has two causes
I think this PR fixes cause 2, and should reduce the possibility to trigger this bug on VSCode for certain users -- but sadly I have no approach to prove this. Perhaps this should be tested by those affected by this bug and collect their responses. Regarding your proposed alternatives:
This won't work for two reasons
This is also not possible, unfortunately. Although So overall, I think this PR is a more pragmatic solution. |
@hsfzxjy I started coding something up in https://github.com/microsoft/vscode/tree/ben/context-menu-mouse-position to make the mouse cursor location more explicit. I would take that as a first step, making sure that in all locations where we want the mouse location, we explicitly set it. Adoption is actually not so trivial because of the loose types, will clean the branch up going over all locations next. As a second step, I would like to experiment with letting Electron position the menu in those cases where the mouse location is preferred, but probably behind a setting and not enabled by default to get some feedback for whether this truly addresses the issue for all. This could be a PR from your end that is thus then much smaller and easier to get in. I agree with the sentiment that patching the location from our end is not clever and using PS: I found that only for the editor context menu we do some pixel pushing:
That is why I had asked in #113175 (comment) if this issue reproduces in other menus outside the editor where we do not apply this offset. |
I think #188674 is in good shape, running a build now to give people to test. Given the chance of breakage, this can only be merged early in a new release cycle, such as next week (this week we are winding down for endgame). Builds should also be used to test various things:
|
@bpasero It's OK. Thanks for the follow up to this long-term issue! |
This PR should fix #113175, fix #187111.
Previously VSCode adopts a formula like
clientX = Math.floor(pageX * zoom)
to calculate the coordiate on screen for showing up the context menu. The formula leads to numeric loss whenzoom != 1
, causing nonnegligible shift of context menu in some cases, and finally presented as user annoyance as described in abovementioned issues.However, for most of the time the desired position for context menu is the current position of cursor, which actually need not to be calculated by ourselves. Electron Docs states that leaving the
x
andy
arguments asundefined
will indicate the context menu popping up at current mouse cursor position.Therefore, this PR utilizes the Electron API for more accurate context menu popup positioning, and hopefully should fix these related issues, if there's no other defeat in upstream Electron.