Tk Source Code

View Ticket
Login
2025-03-17
14:51 Ticket [28d33f45ff] Aqua sometimes fails to move focus when the focused window is closed. status still Closed with 5 other changes artifact: 7575bd9bcd user: jan.nijtmans
14:49
Fix [28d33f45]: Aqua windows don't always move focus correctly. Fix some errors in focus.test - thanks to Erik Leunissen check-in: 0602f5b816 user: jan.nijtmans tags: core-9-0-branch
2025-03-15
14:14 Closed ticket [28d33f45ff]: Aqua sometimes fails to move focus when the focused window is closed. plus 4 other changes artifact: da7c9a9738 user: marc_culler
13:53
Fix [28d33f45]: Aqua windows don't always move focus correctly. check-in: 4443754b94 user: culler tags: trunk, main
01:43 Ticket [28d33f45ff] Aqua sometimes fails to move focus when the focused window is closed. status still Open with 4 other changes artifact: d3780f8751 user: marc_culler
00:45 Ticket [28d33f45ff]: 4 changes artifact: 66ee6344c5 user: marc_culler
00:28 Ticket [28d33f45ff]: 4 changes artifact: 34ea45e4d5 user: marc_culler
2025-03-14
22:59 Ticket [28d33f45ff]: 3 changes artifact: 418796d124 user: jan.nijtmans
20:24 Ticket [28d33f45ff]: 4 changes artifact: f2d7eea8a1 user: marc_culler
17:19 Ticket [28d33f45ff]: 4 changes artifact: ac19dedcdf user: marc_culler
07:32 Ticket [28d33f45ff]: 3 changes artifact: e246288610 user: jan.nijtmans
2025-03-13
13:48 Open ticket [28d33f45ff]. artifact: 7bccc29b38 user: marc_culler
13:06 Pending ticket [28d33f45ff]. artifact: 0b5c55305b user: jan.nijtmans
03:29 Closed ticket [28d33f45ff]. artifact: d11e1451f7 user: marc_culler
03:25 New ticket [28d33f45ff]. artifact: 016c1136b3 user: marc_culler

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.