Tk Source Code

View Ticket
Login
Ticket UUID: 2e3d6cdae4cb3c16a23aba0fb57e0ddd99af40a
Title: X11: a posted menu stays open when the application loses the focus or its toplevel moves
Type: Bug Version: 8.6.18, 9.0.5, 9.1.1
Submitter: serhiy.storchaka Created on: 2026-10-26 20:48:21
Subsystem: 12. Unix Menus Assigned To: nobody
Priority: 5 Medium Severity: Minor
Status: Closed Last Modified: 2026-10-28 13:09:54
Resolution: Fixed Closed By: nemethi
    Closed on: 2026-10-28 13:09:54
Description:

When a menu of the menubar is open and another window is activated, the menu stays open on top of that window. When the toplevel is moved, the menu stays where it was. This happens in a Wayland session (seen with KDE), where Tk runs on Xwayland: the compositor switches and moves windows although Tk holds a global grab, which on X11 would prevent it.

menu .m
menu .m.file -tearoff 0
.m add cascade -label File -menu .m.file
foreach l {Open Save Quit} {.m.file add command -label $l}
. configure -menu .m
# Open the File menu, then switch to another window (Alt+Tab),
# or move the main window.

This is not specific to Tk: the menus of GTK3 programs running on X11 (e.g. Synaptic) behave the same, since on X11 a menu is a separate override-redirect window which the compositor does not associate with its parent. Native Wayland programs do not have the problem.

Tk could close the menu itself: it gets the <FocusOut> of the application and the <Configure> of the toplevel, but keeps the menu posted (on X11 with openbox and without a window manager too, when another client takes the focus or the toplevel is moved). Unposting the posted menus (tk::MenuUnpost) in these cases would match what native menus do. This concerns only posted menus, i.e. dropdown menus of the menubar or of a menubutton, cascades and popup menus; a torn-off menu is a toplevel of its own and should stay. On the wayland branch menus are xdg_popup surfaces, which the compositor already handles.

User Comments: nemethi (claiming to be Csaba Nemethi) added on 2026-10-28 13:09:54:

Serhiy, many thanks! Merged into main, core-9-0-branch, and core-8-6-branch by commits [3a85c84a], [c03ff3a8], and [bee05fcb].


serhiy.storchaka added on 2026-10-26 21:45:14:

Fixed on branch menu-unpost-focus-move [572cc8c45b]: the toplevel which posted the menus is watched while they are posted, and they are unposted when it is moved or the application loses the focus, for the menus of the menubar, of menubuttons, and the cascades of torn-off menus (the torn-off menu itself stays). A popup menu is unposted when the toplevel which had the focus is moved, but not when another application takes the focus: while the popup has the grab, Tk ignores the FocusOut of the toplevel (tkFocus.c, [220892]). New tests menu-49.1 to 49.6. Tested on X11 (Xvfb, with openbox and without a window manager) and on Windows, where it changes nothing; not tested in a Wayland session or on macOS.