| Ticket UUID: | b164efb546662c239ca45aa20bad3ed4f5104e04 | |||
| Title: | Mouse event to wrong window (MacOS and Linux) | |||
| Type: | Bug | Version: | 8.6.12 | |
| Submitter: | jal_frezie | Created on: | 2021-12-02 15:42:46 | |
| Subsystem: | 01. Bindings | Assigned To: | marc_culler | |
| Priority: | 5 Medium | Severity: | Severe | |
| Status: | Closed | Last Modified: | 2026-10-27 11:51:54 | |
| Resolution: | Fixed | Closed By: | nemethi | |
| Closed on: | 2026-10-27 11:51:54 | |||
| Description: |
If you have two top-level windows and bind clicks in one of them to a command, then the first click in the other will call that command with coordinates that would be correct if the first window actually extended over that point on the screen. e.g., % wish % info patchlevel 8.6.12 % toplevel .b .b % bind .b <Button-1> "puts %x&%y" % 42&30 111&130 78&139 116&139 116&139 116&139 83&119 2171&710 39&61 89&41 2147&670 Both to-levels are their default size, the lines with the large values are produced by clicks in the original top-level, not .b Needless to say this causes chaos in my apps. Also, sometimes the first click in a different top-level fails to focus that window. | |||
| User Comments: |
nemethi (claiming to be Csaba Nemethi) added on 2026-10-27 11:51:54:
The code in the aqua-right-click-target branch fixes not only the bug reported in [061f633f43], but also the right-click case that led to reopening this ticket (I have tested it successfully on macOS 26). The Linux case works for me as expected. Closing the ticket again with resolution "Fixed". serhiy.storchaka added on 2026-09-29 15:24:11: The right-click case is also reported in [061f633f43]. A right or middle click does not make the window the key window, so the event was sent to the toplevel which has the focus, with the coordinates of the clicked window. Left clicks were fixed here in 2021, but right and middle clicks were not. Proposed fix in branch aqua-right-click-target (not tested, I have no macOS). If the Linux case is still reproducible, please provide a script. With a popup menu posted, the next click is taken by the menu's grab, which is expected. jal_frezie added on 2022-07-25 12:58:01: Re-opening this because the original bug still occurs if binding to Button-2 (right-click) instead of Button-1 (left-click). Note that something similar occurs in Linux when binding to Button-3 (right-click). If one toplevel is active and you right click in another toplevel, the event gets sent to the first one. Right click in the second again and no event is sent but the second toplevel becomes active. Right click again and the newly active toplevel gets the event. It all works OK in Windows (version 8.6.9 tested) -- the Button-3 (right-click) event always gets sent to the window that was clicked in. marc_culler (claiming to be Marc Culler) added on 2021-12-08 04:32:57: Hi Nicolas, I watched your video a few times and I could not figure out what you think is the wrong behavior. It is correct that events should go to the window which has focus, and that is what I saw. I assume that you are also using ScrollUtils to allow the mouse wheel to scroll a window which does not have focus, and I also thought I saw that. Anyway, I will need more details, and I think they should go in a new ticket. marc_culler (claiming to be Marc Culler) added on 2021-12-07 23:24:30: It could be related but it probably deserves its own ticket. I believe that Two-finger gestures send scroll wheel events, not mouse events. Both are handled in the same file, but I did not change anything about scroll wheel event handling. Can you please try with a mouse and see if you can isolate which problems are related to the scroll wheel and which to the mouse? nab added on 2021-12-07 22:26:06: Hi Marc, https://vimeo.com/654296551 here's a video of my app using latest mac_styles_87 branch. the first part (till 11 sec) show you the normal behavior where I have two fingers on the trackpad and scrolling tablelist. after 11s you can see that if mouse pointer is just a little bit over toplevel, mouse events are delivered to the latest toplevel in front (can they be delivered to the toplevel where mouse is over?) and offsets are strange... is it related to this ticket? or should I open a new one? best regards, nicolas marc_culler (claiming to be Marc Culler) added on 2021-12-06 13:57:40: Thanks, Jasper. I have merged the changes into core-8-6-branch and main, and I'll close this ticket now. jal_frezie added on 2021-12-06 09:51:18: Looks good now. Thanks for a great job! marc_culler (claiming to be Marc Culler) added on 2021-12-05 16:52:52: This is fixed now. Mouse coordinates vary continuously (so to speak, even though they are integers) as the mouse leaves or enters a window during a drag. Those coordinates are computed in processMouseEvent, rather than in Tk_UpdatePointer, because processMouseEvent sends the XEvent directly. There were two bugs in that computation. (1) Forgetting that non-root toplevels have parents. (2) Forgetting to adjust for the height of the titlebar in the case where the top window containing the pointer is a toplevel. These were bugs, plain and simple, and entirely my fault. Also, not hard to fix. You are so good at finding these things, Jasper, I hope you will continue to look for them! marc_culler (claiming to be Marc Culler) added on 2021-12-05 15:23:29: I was wrong about the title bar drag regression. I was testing with the core-8-6-branch. Oops. But that is good news. marc_culler (claiming to be Marc Culler) added on 2021-12-05 15:14:02: Thank you Jasper, as always. That is very helpful! I also see that the B1-Motion events when dragging by the title bar have been resurrected. I will have another go at it. The macOS APIs are not at all horrible, just mostly undocumented. People are meant to use them via XCode, with help from the Interface Builder. And the job that Tk does is done for them by a black box. I find myself wishing that we had a regression test suite for mouse event behavior. I am not sure how such a thing could be built, though. We do use simulated mouse events for some tests (on macOS only) but the simulated events are handled separately so they are not usable for testing the handling of normal events. jal_frezie added on 2021-12-05 13:04:59: Hi Marc, there are still bugs I'm afraid. With the same setup as before, I find that when I click in .b and drag into its title bar or out of it altogether, the coordinate system for the %x and %y changes, then goes back to normal when I continue the drag back in. Like this... 190&10 194|13 197|17 99|-663 102|-660 104|-658 105|-658 105|-658 105|-658 105|-657 106|-657 106|-657 106|-656 106|-656 106|-656 107|-655 107|-655 107|-654 107|-654 107|-654 104|-655 199|25 186|21 166|15 ... I've never tried to use the MacOS APIs directly, and I'm not tempted, it sounds horrible. marc_culler (claiming to be Marc Culler) added on 2021-12-04 20:08:19: Hi Jasper, your latest "strange situation" is now fixed. The missing click event was a telltale sign. It turned out that there was a code block that explicitly ignored left mouse button clicks in the case where the application had no focused windows. Removing that code block fixed the problem. But that code block was there for a reason. It was the fix for ticket [7bda9882cb] which, amazingly enough, is a ticket posted by you, in May, that had to do with double-clicking in a window without focus. You had noticed that when you did that, there were duplicate double-click events. The underlying issue causing [7bda9882cb] was that when a double-click event occurs in an unfocused window Apple sends many, many extraneous mouse events. The extra double-click was caused by these extraneous events. The original fix for [7bda9882cb] was to simply ignore all mouse click events until some window has focus, and that fix caused the behavior you reported below. I was eventually able to figure out a more refined criterion for deciding whether an Apple mouse event is extraneous, based on the clickCount attribute of an NSEvent of type NSLeftMouseUp or NSLeftMouseDown. Needless to say, this was another reverse engineering project, since Apple does not document any of these behaviors of their window manager. So I am ready for you to test again, please. marc_culler (claiming to be Marc Culler) added on 2021-12-03 18:12:28: Thanks, Jasper. That doesn't look too bad, in the sense that there is at least a clear place to start looking. The main problem seems to be in the code which determines which window will receive focus. On the other hand, that doesn't explain the missing click, which seems strange. I'll see what I can find. It is not strange to move windows around and click here and there. To see the strangeness you need to try to explain why things like this are happening, which involves reverse engineering Apple's logic in deciding which events to send to an application when. And that is pretty strange, I can assure you. jal_frezie added on 2021-12-03 17:32:16: It's getting better but still not quite right. Now here's what I call a "certain strange situation": Start Wish from the command line. Drag the top-level somewhere well away from its default position. Create a new top-level and add bindings for it: % toplevel .b .b % bind .b <Button-1> "puts %x&%y" % bind .b <B1-Motion> "puts %x|%y" ...and before doing anything else, do a drag in the original top-level. You will see the motion, but not the starting click, reported as if it were at the same position in .b: % 95|76 94|76 94|76 94|76 94|76 94|76 93|77 ... This happens for one drag. But if you click in .b, then click on the desktop or in a window of another application, you can then drag in the original top-level once again and see co-ordinates in the terminal as if you were dragging in .b. Actually not that strange a situation... marc_culler (claiming to be Marc Culler) added on 2021-12-03 16:35:08: The only way I found to generate the bad coordinates was to first move '.' then click and drag in the unfocused '.b'. An Enter and Leave for '.b' (including clicking in the title bar) would restore the correct coordinates. Bad coordinates would never appear for '.' no matter what I did and they would not occur for '.b' unless I moved '.'. I made a change which seems to fix this - I am no longer able to generate any bad coordinates for B1-Motion events. I think the problem was that I needed to set [NSApp tkPointerWindow] when switching to the new focus window, but the mechanism leading to the bad coordinates and their values remain mysterious. I guess it is not too surprising that the root window would be handled slightly differently. marc_culler (claiming to be Marc Culler) added on 2021-12-03 15:02:26: Oops. I figured out how to get the bad coordinates. One more round, I guess. marc_culler (claiming to be Marc Culler) added on 2021-12-03 13:41:54: OK. I am not seeing any anomalies with commit c65a5947. That commit is ready for testing. For me: * The coordinates are correct when clicking or dragging in an (initially) unfocused window, and the correct window receives the <B1-Motion> events. * Moving a window by clicking and dragging in its titlebar does not generate any <1> or <B1-Motion> events. Neither does resizing the window by clicking and dragging an edge or the invisible grip in the lower right corner. * Enter and Leave events work correctly, and report entering or leaving the contentView of the window. * Clicking and dragging in a window and then moving the pointer outside the window continues to generate <B1-Motion> events until the button is released. marc_culler (claiming to be Marc Culler) added on 2021-12-03 13:17:22: Sorry, it is not ready. I am seeing a different problem now, that B1-Motion events stop when the mouse leaves the window. That is a regression that will break scrollbars. So don't bother to test yet. marc_culler (claiming to be Marc Culler) added on 2021-12-03 12:57:39: I checked, and I am not seeing any wrong coordinates. If you are still seeing them please provide detailed instructions to help me see them. marc_culler (claiming to be Marc Culler) added on 2021-12-03 12:54:05: Oh. Now I think I understand what you were saying. I will check if that is still happening. It may still be too early for testing. marc_culler (claiming to be Marc Culler) added on 2021-12-03 12:50:38: Hi Jasper,
Thanks for helping me test this.
| What went wrong?
I fixed the completely broken behavior of Enter and Leave events on the Mac,
reported in another ticket. That required signing up for more mouse events
via an event monitor in order to work around the design of TkUpdatePointer.
That required reworking how mouse events are handled. In this world nothing
gets fixed without other things breaking, but eventually it settles down. I
think we are close now with this one.
| It all seemed to work OK in TclTk 8.6.11
Not *all* - just the parts that you were using!
I don't understand what you are saying about the last update, but that
may not matter because that update was unfinished anyway. Please test
the current tip of the bugfix branch. I am not able to produce any of the
anomalies that you are reporting when I use it. But you may be able to,
or to find some new ones. So please try to do that and let me know. Thanks.
I am testing like this:
% bind all <1> {puts "%W: B1 at %x, %y"}
% bind all <B1-Motion> {puts "%W: drag at %x, %y"}
% bind all <Enter> {puts "%W: Enter"}
% bind all <Leave> {puts "%W: Leave"}
% toplevel .b
jal_frezie added on 2021-12-03 09:05:31: Hi Marc, Having compiled with your latest update I am still getting the problem in the example I posted earlier. Initially the click and drag coordinates reported are correct, but after a click or drag in the original top-level, a drag in .b reports the wrong coordinates (although those for the click are right) and continues to do so until the title bar of .b is clicked. jal_frezie added on 2021-12-03 08:24:47: What went wrong? It all seemed to work OK in TclTk 8.6.11 marc_culler (claiming to be Marc Culler) added on 2021-12-03 02:25:46: I have partially fixed these issues in the bugfix branch, but there is a tricky point about dragging a window by its title bar. I thought I could determine whether a window is being dragged by its title bar by checking whether the mouse is in the title bar when an NSMouseDragged event is received. But that is wrong. When a window that is being dragged by its title bar touches another window there is a moment of stickiness during which the window remains in place, with a common border edge with the other window, even though the mouse continues to move. This can result in the mouse moving out of the titlebar. When the sticky moment ends and the drag resumes, the window is being dragged by a point which is outside of the title bar. So I guess there needs to be a "window is being dragged by its title bar" state which persists until an NSLeftMouseUp event arrives, even if the mouse has left the title bar. This will take some work. nab added on 2021-12-02 19:16:59: HI, same issue with mac_styles_87 branch ++ jal_frezie added on 2021-12-02 18:59:05: Hi Marc, thanks for having a look at this for me. After compiling that branch, the click bindings work OK, but if you also bind a command to <B1-Motion> for .b in the same example, the coordinates returned are wrong. Also that command is sometimes called when moving the original top-level around by its title bar. % bind .b <B1-Motion> "puts %x!%y" ... 103&73 -492!-137 -492!-137 -491!-136 ... marc_culler (claiming to be Marc Culler) added on 2021-12-02 18:09:57: Thank you, Jasper. I will need more information before I can look at the issue of clicks not changing focus. I do not see that. But I think I have fixed the other problem in the bug-b164efb546 branch. Would you please test that branch? | |||
Home
Timeline
Branches
Tags
Forum
Tickets
Download
Wiki