Tk Source Code

View Ticket
Login
Ticket UUID: 28d33f45ffb1a5fbc2b65c4b83218f59b3a3c634
Title: Aqua sometimes fails to move focus when the focused window is closed.
Type: Bug Version: 9.0.1
Submitter: marc_culler Created on: 2025-03-13 03:25:01
Subsystem: 66. Aqua Window Operations Assigned To: marc_culler
Priority: 5 Medium Severity: Minor
Status: Closed Last Modified: 2025-03-17 14:51:07
Resolution: Fixed Closed By: jan.nijtmans
    Closed on: 2025-03-17 14:51:07
Description:
One example of this, pointed out by Nicolas Bats is that if one selects the
"About" menu item, causing a standard About Panel to open, then when the About
Panel is closed no window receives focus.  The correct behavior is that the
focus should move to the window still on the screen which is in the stacking
order.

I have fixed this in the aqua_key_windows branch by adding code in the NSApp
method which handles notifications that a window has obtained or resigned
key status.
User Comments: jan.nijtmans added on 2025-03-17 14:51:07:

Now [0602f5b81637cd74|backported] to 9.0 as well.

Thanks!


marc_culler (claiming to be Marc Culler) added on 2025-03-15 01:43:17:
OK, it works again and tests still pass.

marc_culler (claiming to be Marc Culler) added on 2025-03-15 00:45:58:
This is really a classic case.  Now that the tests pass it is no longer the
case that closing the About window transfers focus to the window which
had focus before it was opened.  So it looks like another round will be
needed.

marc_culler (claiming to be Marc Culler) added on 2025-03-15 00:28:48:
You're welcome!

jan.nijtmans added on 2025-03-14 22:59:13:

Many thanks for taking this!


marc_culler (claiming to be Marc Culler) added on 2025-03-14 20:24:02:
I have now dealt with the issues in the focus tests.  The assignment
of the key window now takes into account which interpreter is associated
with the window, and only transfers focus to a window with the same
interpreter (i.e. with the same "Tk application").  On Aqua there is only
one NSApplication, but that one NSApplication can host multiple
interpreters, each of which is considered a separate Tk application.

The focus command takes into account which Tk application a window belongs
to. This now works the way that the tests expect.

The hang was caused by the focusClear proc.  There is no way to arrange
that a Tk application has no focused window other than to create another
Tk application and give it focus.  So that is what focusClear does.  To
avoid test failures on Aqua because focusClear did not work reliably to
clear the focus (i.e. it would fail sporadically on the CI runner) I had
added a while loop that runs until the focus command returns an empty
string.  But with the first round of aqua_key_events that command was
hanging because the focus would get reassigned to the highest window
in the same NSApplication, which was in the original Tk application,
as soon as the first Tk application had no focused window.  This would
prevent the focusClear loop from terminating.

Now, with the interpreter checks, it is allowed for the main Tk
application to have no focused windows, and it is no longer necessary
to have a loop in the focusClear proc.  Moreover all focus tests pass.

In fact I have checked that merging main with aqua_key_windows produces
a Tk for which all tests pass on the CI runner.  I did that by cloning
the merge into a github repository in my own account for which I can
trigger a CI run when I want.

I have scheduled a CI run for aqua_key_windows in the tcltk/tk github
repository which will run tonight. When that succeeds I will push the
merged main to fossil and we can wait another day to check that all
tests still pass.

marc_culler (claiming to be Marc Culler) added on 2025-03-14 17:19:36:
Yes, I know that.  I did not add that to fix the focus tests.  I am
working on fixing the focus tests now.

I removed the changes which broke the focus test from main.  They are
all back in the aqua_key_windows branch.

jan.nijtmans added on 2025-03-14 07:32:43:
Checking the TK_ALREADY_DEAD flag doesn't help :-(

[https://github.com/tcltk/tk/actions/runs/13851459698/job/38759337333]

marc_culler added on 2025-03-13 13:48:55:
Thanks Jan.  Sorry about that.  I reopened this ticket and renamed the last
commit back into the bugfix branch.

jan.nijtmans added on 2025-03-13 13:06:01:

focus.test is hanging now.

See: https://github.com/tcltk/tk/actions/runs/13828797808/job/38702606780

That's why I didn't cherry-pick it to the core-9-0-branch yet.