Tk Source Code

View Ticket
Login
Ticket UUID: 061f633f4316dec2ff4bc4884b3d00e1edc96db8
Title: MacOS aqua: <<ContextMenu >> binding sometimes triggers for incorrect toplevel
Type: Bug Version: 9.0.2, 8.6.18, 9.0.5, 9.1b1
Submitter: SVIDevelopment Created on: 2026-09-18 10:36:52
Subsystem: (unused) Assigned To: jan.nijtmans
Priority: 5 Medium Severity: Minor
Status: Closed Last Modified: 2026-10-08 11:55:03
Resolution: Fixed Closed By: jan.nijtmans
    Closed on: 2026-10-08 11:55:03
Description:
In some cases, the ContextMenu binding triggers for the toplevel that currently has focus, instead of the toplevel underneath the mouse pointer. In the example below, when the red canvas has focus and you right-click on the blue canvas, you get the red menu. I would expect to get either the blue menu, or no menu at all.

Tested on MacOS 15.

--------------------------------------------------------------------

proc popdbg {W X Y} {
    puts "W=$W  toplevel=[winfo toplevel $W]  containing=[winfo containing $X $Y]  @ $X,$Y  focus=[focus]"
}

set toplevel1 .
wm geometry $toplevel1 400x300+650+350

set canvas1 [canvas $toplevel1.canvas1 -width 200 -height 200 -bg red]
grid $canvas1
set menu1 [menu $toplevel1.m -tearoff 0]
$menu1 add command -label "RED"
bind $canvas1 <<ContextMenu>> "popdbg %W %X %Y; tk_popup $menu1 %X %Y"

set toplevel2 [toplevel $toplevel1.top2]
wm geometry $toplevel2 300x200+200+400

set canvas2 [canvas $toplevel2.canvas2 -width 200 -height 200 -bg blue]
grid $canvas2
set menu2 [menu $toplevel2.m -tearoff 0]
$menu2 add command -label "BLUE"
bind $canvas2 <<ContextMenu>> "popdbg %W %X %Y; tk_popup $menu2 %X %Y"

--------------------------------------------------------------------------
User Comments: jan.nijtmans added on 2026-10-08 11:55:03:

Fixed [3362fcc006a9101d|here], and in core-9-0-branch and core-8-6-branch.

I tested it, it works fine.

Many thanks!


serhiy.storchaka added on 2026-09-29 15:57:06:

A right or middle click does not make the window the key window, so tkEventTarget is still the toplevel which has the focus. The event is then sent to the widget of that toplevel at the coordinates of the clicked window. A left click resets the target to the clicked toplevel, but a right or middle click does not. The same code is in 8.6 and 9.0.

Proposed fix in branch aqua-right-click-target: right and middle button events in a toplevel which is not the key window are sent to that toplevel, and the focus does not change. I could not test it, as I have no macOS.