Tk Source Code

View Ticket
Login
2026-08-19
09:51 • Ticket [591829e9] Incorrect initial pointer window under macOS/aqua status still Open with 3 other changes artifact: 951daf7f user: jan.nijtmans
2026-05-20
11:56 • Ticket [591829e9]: 3 changes artifact: 20d0dedf user: nab
08:54 • Ticket [591829e9]: 3 changes artifact: dbb8d010 user: erikleunissen
2026-05-19
21:37 • Ticket [591829e9]: 4 changes artifact: 2a1eaaa2 user: marc_culler
21:15 • Add attachment tk-bug-591829e948-A-followup.patch to ticket [591829e9] artifact: 79943012 user: nab
21:14 • Ticket [591829e9] Incorrect initial pointer window under macOS/aqua status still Open with 3 other changes artifact: a492cee9 user: nab
18:18 • Ticket [591829e9]: 3 changes artifact: 7b13e8c1 user: nab
17:35 • Ticket [591829e9]: 3 changes artifact: e9f16325 user: erikleunissen
17:13 • Ticket [591829e9]: 3 changes artifact: 092f7f4d user: erikleunissen
14:24 • Ticket [591829e9]: 3 changes artifact: 921e8be2 user: nab
11:18 • Ticket [591829e9]: 3 changes artifact: e71788ce user: erikleunissen
2026-05-09
16:48 • Add attachment call_graphs.txt to ticket [591829e9] artifact: d3802f60 user: erikleunissen
16:37 • Ticket [591829e9] Incorrect initial pointer window under macOS/aqua status still Open with 3 other changes artifact: 870fa67a user: erikleunissen
2026-03-19
13:45 • Ticket [591829e9]: 4 changes artifact: 77e86e32 user: marc_culler
08:30 • Ticket [591829e9]: 3 changes artifact: 693628de user: erikleunissen
08:00 • Ticket [591829e9]: 3 changes artifact: 18f7519d user: jan.nijtmans
01:35 • Ticket [591829e9]: 4 changes artifact: ff99b27b user: marc_culler
01:32 • Ticket [591829e9]: 4 changes artifact: 42fc3ca8 user: marc_culler
00:31 • Ticket [591829e9]: 4 changes artifact: 007b14ba user: marc_culler
2026-03-18
17:49 • Ticket [591829e9]: 4 changes artifact: 96d2ac38 user: marc_culler
15:49 • Ticket [591829e9]: 3 changes artifact: b30cc6bf user: erikleunissen
15:42 • Ticket [591829e9]: 3 changes artifact: 3a380b3c user: erikleunissen
13:34 • Ticket [591829e9]: 4 changes artifact: 5efe5096 user: marc_culler
2026-03-17
17:08 • Ticket [591829e9]: 3 changes artifact: e3b020ec user: erikleunissen
12:00 • Ticket [591829e9]: 3 changes artifact: ddbbb371 user: erikleunissen
2026-03-16
19:31 • Ticket [591829e9]: 3 changes artifact: ae1b1353 user: erikleunissen
03:03
Possible fix for [591829e948]. check-in: bbf5c56a user: culler tags: bug-591829e948-MC
2026-03-15
21:59 • Ticket [591829e9] Incorrect initial pointer window under macOS/aqua status still Open with 3 other changes artifact: 42abd037 user: erikleunissen
21:39 • Ticket [591829e9]: 3 changes artifact: 769f93fc user: erikleunissen
21:10 • Ticket [591829e9]: 3 changes artifact: 15c81779 user: erikleunissen
21:00 • Ticket [591829e9]: 3 changes artifact: 0bd3450c user: erikleunissen
20:51 • Ticket [591829e9]: 3 changes artifact: ba2b4c76 user: erikleunissen
12:38 • Ticket [591829e9]: 3 changes artifact: 26fef106 user: erikleunissen
11:53 • Ticket [591829e9]: 3 changes artifact: 6cd87f9a user: erikleunissen
2026-03-14
19:00 • Ticket [591829e9]: 3 changes artifact: cfc28733 user: erikleunissen
13:32 • Ticket [591829e9]: 3 changes artifact: 2c9fde55 user: erikleunissen
13:27
Fix [591829e948]-B: "winfo containing" shouldn't consider unmapped toplevel windows check-in: a34167a5 user: erikleunissen tags: core-8-6-branch
13:25
Fix [591829e948]-B: "winfo containing" shouldn't consider unmapped toplevel windows check-in: 5261b9cd user: erikleunissen tags: core-9-0-branch
13:23
Fix [591829e948]-B: "winfo containing" shouldn't consider unmapped toplevel windows check-in: 782befd2 user: erikleunissen tags: trunk, main
2026-03-13
12:18 • Ticket [591829e9] Incorrect initial pointer window under macOS/aqua status still Open with 3 other changes artifact: 741d4dc1 user: erikleunissen
2026-03-05
15:07 • Ticket [591829e9]: 3 changes artifact: ba55d613 user: erikleunissen
15:06 • Ticket [591829e9]: 3 changes artifact: 24824da2 user: erikleunissen
14:52 • Ticket [591829e9]: 3 changes artifact: 5b8e298c user: erikleunissen
11:04 • Ticket [591829e9]: 3 changes artifact: 254b9b42 user: jan.nijtmans
2026-03-04
22:06
Bug [591829e948]-B: Add non-regression test check-in: 1f046e22 user: erikleunissen tags: bug-591829e948-B
2026-03-03
23:28 • Add attachment exercise.tcl to ticket [591829e9] artifact: 0a34b892 user: erikleunissen
23:27 • Delete attachment "exercise.tcl" from ticket [591829e9] artifact: 3a572031 user: erikleunissen
20:57 • Ticket [591829e9] Incorrect initial pointer window under macOS/aqua status still Open with 3 other changes artifact: f377a923 user: erikleunissen
20:55 • Add attachment exercise.tcl to ticket [591829e9] artifact: 2ff73bf3 user: erikleunissen
20:54 • Delete attachment "exercise.tcl" from ticket [591829e9] artifact: ba1edd1c user: erikleunissen
2026-02-15
22:46 • Add attachment exercise.tcl to ticket [591829e9] artifact: dc68d572 user: erikleunissen
22:45 • Delete attachment "exercise.tcl" from ticket [591829e9] artifact: bffe827f user: erikleunissen
21:58 • Add attachment exercise.tcl to ticket [591829e9] artifact: 61bd8b2e user: erikleunissen
21:58 • Delete attachment "exercise.tcl" from ticket [591829e9] artifact: 52ca0ae0 user: erikleunissen
15:24 • Add attachment exercise.tcl to ticket [591829e9] artifact: 2561c8f6 user: erikleunissen
15:24 • Delete attachment "exercise.tcl" from ticket [591829e9] artifact: 484da0a9 user: erikleunissen
15:24 • Ticket [591829e9] Incorrect initial pointer window under macOS/aqua status still Open with 3 other changes artifact: 1ef07d1a user: erikleunissen
14:49 • Add attachment exercise.tcl to ticket [591829e9] artifact: 4b99245b user: erikleunissen
14:48 • New ticket [591829e9] Incorrect initial pointer window under macOS/aqua. artifact: 873a2d0e user: erikleunissen

Ticket UUID: 591829e94815173b70f1a71788c13eccb8f75134
Title: Incorrect initial pointer window under macOS/aqua
Type: Bug Version: 9.1a0
Submitter: erikleunissen Created on: 2026-02-15 14:48:59
Subsystem: 99. Other Assigned To: nobody
Priority: 5 Medium Severity: Important
Status: Open Last Modified: 2026-08-19 09:51:02
Resolution: None Closed By: nobody
    Closed on:
Description:
Tk's notion of the pointer window, immediately after application launch, is
incorrect if:
* the windowing system is aqua
* the root window has not yet been mapped
* prior to application launch, the screen pointer happens to reside at a
  position where Tk's root window would become displayed according to
  "wm geometry .".

In this situation, the expected value returned by:

    "winfo containing [winfo pointerx .] [winfo pointery .]"

is the empty string, indicating that no window in the present Tk application
contains the screen pointer. This expected value is correctly returned under
Linux/x11 and win32. However, under macOS/aqua the value "." is returned,
falsely indicating that the screen pointer is inside the Tk root window.

Please find attached the script file exercise.tcl to demonstrate the issue.
Here is the result when I run it on macOS/aqua (Sequoia):

% tclsh exercise.tcl
|.|

On my Linux/x11 and win32 systems, the result is:
% tclsh exercise.tcl
||
User Comments: jan.nijtmans added on 2026-08-19 09:51:02:

So, there are two branches now with a proposed solution. Let's see what can be made of this ....


nab added on 2026-05-20 11:56:07:
Hi Erik,

please don't misunderstand my message... your work is really great and it would be such a shame if you don't go ahead.

my latest contribution (AI made) was just to alert you about side effect (only for my app apparently...) of your work.

maybe some pieces of the patch worth to be considered, that was just my intention.

best regards,
nicolas

erikleunissen added on 2026-05-20 08:54:29:
@marc_culler, @nab, all,

I'm going to suspend my work to bring this ticket forward as of now.

The reason is that it's giving me too much worries to manage contributions
like the one from Nicolas. I wasn't made for that sort of thing. I am unable to
evaluate Nicolas' contribution, both w.r.t. the content matter, and - what's
maybe more important - w.r.t. procedure, i.e. how to proceed with this ticket.

I'm sorry.

I'm leaving the ticket and the development branch bug-591829e948-A in their
current state to the TCT, to take over the initiative entirely, and to decide at
liberty how to proceed with the ticket.

Note: the Github CI runs that happened after my summary and call for a final
review below have all green check marks:

    [https://github.com/tcltk/tk/actions?query=branch%3Abug-591829e948-A]

Good luck,
Erik Leunissen.

marc_culler (claiming to be Marc Culler) added on 2026-05-19 21:37:11:
My first reaction is that removing [self onScreen] and replacing it by some
other code that does essentially the same thing in essentially the same
way but has a different name does not sound worth the effort.

Also, I disagree with Claude's claim that "any application that reconfigures
the contents of a toplevel between two appearances" will see the artifacts
that you (Nicolas) see with your app.  There are many apps that do that without
any bad effects.  Your app happens to be especially challenging for Tk to deal
with in this respect. That is why I wanted you to test this branch.  (Thanks
for doing that!)

nab added on 2026-05-19 21:14:52:
Erik, please find attached a patch that worked for my app and here's the summary:

## Summary

This branch removes the `[self onScreen]` flag and the call to
`makeKeyAndOrderFront:` that used to be gated by it. As a side effect
two user-visible regressions appear on macOS/Aqua, both variants of
ticket d1989fb7cf which the trunk comment in `tkMacOSXInit.c` already
warns against:

1. **At application launch**: the root window appears empty on screen
   before the startup script finishes mapping the children of `.`.
2. **After `wm withdraw . / rebuild children / wm deiconify .`** (any
   application that reconfigures the contents of a toplevel between
   two appearances): when the window comes back, fragments of the
   previous layout bleed through the `CALayer.contents` cache while
   the new widgets paint themselves into the existing bitmap context
   one rectangle at a time.

The attached patch restores correct behaviour without reintroducing
the `[self onScreen]` flag the branch deliberately removed.

## Approach

Three orthogonal mechanisms, each addressing one part of the problem.

### 1. Defer the root window's `orderFront:` to an idle handler

The root NSWindow is created inside `TkpInit`, before the startup
script (or `wish`'s first event loop pass) has had a chance to create
its children. Synchronously calling `makeKeyAndOrderFront:` from
inside `XMapWindow` therefore exposes an empty NSWindow.

A new `DeferredOrderFrontRootWindow` idle task (in
`tkMacOSXSubwindows.c`) takes over: drain
`TCL_WINDOW_EVENTS | TCL_IDLE_EVENTS` to a fixed point, force one
synchronous `updateLayer` to publish the freshly-drawn bitmap, then
call `makeKeyAndOrderFront:`. `TCL_WINDOW_EVENTS` matters because
`TkMacOSXRedrawViewIdleTask` posts Expose events via
`[view generateExposeEvents:]`, and the widget `DisplayXxx` procs are
only scheduled once those Expose events are dispatched — which does
not happen if the drain only consumes `TCL_IDLE_EVENTS`.

The `[NSApp activateIgnoringOtherApps:YES]` that lived at the top of
`XMapWindow`'s "real toplevel" branch is moved into its non-root
sub-branch, because for the root it posts
`NSApplicationDidBecomeActiveNotification` whose handler
`applicationActivate:` calls `[[self keyWindow] orderFront:self]` —
which would defeat the deferral. The `makeKeyAndOrderFront:` issued
by `DeferredOrderFrontRootWindow` activates the app naturally when it
eventually fires.

The matching synchronous `orderFront:` calls in `TkpWmSetState` and
`WmDeiconifyCmd` (`tkMacOSXWm.c`) are skipped for the root window for
the same reason. Non-root toplevels are unaffected.

### 2. Restore the one-shot drain semantic under a new name

A `BOOL tkNeedsInitialDrain` property on `TKContentView`
(`tkMacOSXPrivate.h`) is set to `YES` at view creation, re-armed to
`YES` each time the toplevel is unmapped or withdrawn (in
`XUnmapWindow`, in `applicationActivate:`'s withdrawn-window cleanup,
and in `WmIconWindowCmd`), and cleared by `updateLayer` once it has
drained pending `TCL_IDLE_EVENTS`. `updateLayer` now drains when
either `!self.window.isVisible` (initial mapping) or
`self.tkNeedsInitialDrain` (post-deiconify of an already-visible
NSWindow).

This is functionally equivalent to what `[self onScreen]` did, but
under a new name so the branch's deliberate removal of `onScreen` is
not reverted.

### 3. Wipe `layer.contents` and `tkLayerBitmapContext` at toplevel unmap

Without this, `CALayer` keeps showing the previous bitmap until
something redraws on top of it. New child display procs paint only
their own rectangles into the bitmap, so the "between rectangles"
pixels stay populated with the previous toplevel's contents and
become visible the moment the NSWindow is re-ordered front. The
existing `resetTkLayerBitmapContext` creates a fresh, empty
`CGBitmapContext`; setting `view.layer.contents = nil` discards the
cached `CGImage`. Applied at all three sites where the branch used
to call `[[win contentView] setOnScreen:NO]` on trunk:
`XUnmapWindow`, `applicationActivate:`'s withdrawn-window cleanup,
and `WmIconWindowCmd`.

### Safety: cancel the deferred orderFront on toplevel destruction

`TkWmDeadWindow` now cancels any pending `DeferredOrderFrontRootWindow`
on the NSWindow it is about to release, so the idle handler cannot
dereference a freed window.

## Compatibility with the 591829e948 fix

The fix for the original ticket — replacing `Tk_IsMapped(winPtr)` with
`[w isMemberOfClass:[TKWindow class]] && w.isVisible` in
`FrontMostToplevelAtPoint` — is untouched. Deferring `orderFront:`
for the root keeps `[w isVisible]` at `NO` until the children are
mapped, so `winfo containing` still returns `""` at startup. Once
the deferred `makeKeyAndOrderFront:` fires, `isVisible` flips to
`YES` exactly when the window is actually on screen, which is also
when it should be hit-tested.

## Files

```
 macosx/tkMacOSXPrivate.h     |   14 ++
 macosx/tkMacOSXSubwindows.c  |  150 ++++++++++++++++++++++++++++++--
 macosx/tkMacOSXWindowEvent.c |   29 ++++++-
 macosx/tkMacOSXWm.c          |   41 ++++++++-
 4 files changed, 220 insertions(+), 14 deletions(-)
```

No changes to `tkMacOSXInit.c`.

## Test plan

Manual on macOS Aqua, against a Tk application with a non-trivial
root window:

1. **Launch**: root window appears with its widgets in place. No
   "empty frame" flash.
   `winfo containing [winfo pointerx .] [winfo pointery .]`
   immediately after `wish` startup returns `""` (the 591829e948 fix
   still in effect).
2. **Deiconify after content rebuild**:
   `wm withdraw .; destroy <children of .>; <recreate children>;
   wm deiconify .` should make the root window reappear with the new
   contents only, no fragment of the previous layout visible.
3. **Other toplevels**:
   `toplevel .t; pack [label .t.l -text foo]; wm withdraw .t;
   <reconfigure .t>; wm deiconify .t` should present the reconfigured
   contents from the first frame, instead of the old layout cached
   by CALayer.
4. **`wish` test suite**: `make test` on this branch should not
   regress.

nab added on 2026-05-19 18:18:28:
my intention was not to commit in bug-591829e948-A but rather let you know a possible issue.
you certainly have a better knowledge on map/unmap than me.

I'll try investigation on my local copy, of course. In the mean time you can exercise your code with my app...

++

erikleunissen added on 2026-05-19 17:35:45:
Oh, and if you were to provide a reproducible script (in the future), I would
be able to proceed with any observed issue on my part.

erikleunissen added on 2026-05-19 17:13:34:
Hi Nicolas,

Thanks for your interest, and for exercising branch bug-591829e948-A with your
code.

Of course, I'm happy to let you inspect/use the code in branch bug-591829e948-A.
But if that involves making changes (like "setting debug info"), I request that
you do that on a copy on your local system, or on a separate branch of your own.
Please don't commit to the branch bug-591829e948-A at https://core.tcl-lang.org

I'd be very interested to know if you find something wrong with the changes on
branch bug-591829e948-A.

nab added on 2026-05-19 14:24:24:
Hi Erik,
I've tested your work in bug-591829e948-A branch with my app.

I've a little bit concern when I try it with my app which is (at launch) composed by two toplevels.

with Tk's trunk the first toplevel "appears" filled with its widgets, then the second one appears (not so long time after the first one) filled with its widgets.

with bug-591829e948-A branch, the first toplevel "appears" filled with its widgets, the second one appears with no widgets inside (width and height are ok) then after a short time all widgets appears.

it's a quite huge GUI (which works well by the way) and I've never succeeded to build a script that describe the issue...

is there's anyway to set some debug info in bug-591829e948-A branch in order to check what is the exact flow ? (maybe it was working in trunk by chance...)

best regards,
nicolas

erikleunissen added on 2026-05-19 11:18:24:
Position of clearing the TK_MAPPED flag finally understood
==========================================================

I did not include the changes to macosx/tkMacOSXWm.c on branch bug-591829e948-MC
in branch bug-591829e948-A. The reason for that is that I did not understand
the mechanism that caused test winfo-4.8 to fail unless the statement

    winPtr->flags &= ~TK_MAPPED

was moved to a position in the code before the call to XUnmapWindow().

I finally found out how that works, and that resolves my reluctance to importing
the changes to macosx/tkMacOSXWm.c on branch bug-591829e948-MC. Actually, I've
become in favor of importing it, with an adjustment of the position where
TK_MAPPED is cleared, because it allows usage of Tk_Ismapped() inside
TkMacOSXAssignNewKeyWindow() without surprises like the failing test winfo-4.8.

The import has now been carried out, with comments added to explain the
mechanism that couples the clearing of the TK_MAPPED bit with the execution
of orderOut via XUnmapWindow. See:

    [https://core.tcl-lang.org/tk/vdiff?from=e5809ea7ff7d96a4&to=2b67acc1333bdfd4]

erikleunissen added on 2026-05-09 16:37:56:
SUMMARY AND REQUEST FOR REVIEW OF BRANCH bug-591829e948-A
=========================================================

* The main cause for the issue reported in this ticket is this line:

     [https://core.tcl-lang.org/tk/file?ci=74ad039533be203d&name=macosx%2FtkMacOSXInit.c&ln=656]

  It induces processing of idle tasks, which pulls in - for synchronous execution -
  the call to XMapWindow(), which was scheduled for execution at idle time.
  For an overview of the calling sequence of relevant procs, please see the
  call graph for trunk, now attached to the ticket.

  The very fundament of branch bug-591829e948-A is to refrain from this
  pulling in of idle tasks, leaving them to be truly executed at idle time.
  Compare the call graph for that branch (also depicted in that same attachment)
  to the one for trunk to appreciate the changes to the calling sequence.
  For sake of brevity, the rest of this post refers to this approach as "@CallSeqChange".

  Besides addressing the issue for the present ticket, this approach relieves
  us from the need for several other complications, allowing for more
  simplifications as described in the next three points. While these simplifications
  are not related to the cause of the issue reported in the present ticket,
  the opportunity for them comes natuarally with the change to the calling sequence.

* There is no need anymore for this scheduling:

    [https://core.tcl-lang.org/tk/file?ci=74ad039533be203d&name=macosx%2FtkMacOSXInit.c&ln=668]

  because what showRootWindow() does can be done synchronously from XMapWindow()
  if/when it is executed at idle time @CallSeqChange.

* The problem described by this comment:

     [https://core.tcl-lang.org/tk/file?ci=dc733ea5f685efb8&name=macosx%2FtkMacOSXSubwindows.c&ln=179-189]

  does not exist anymore (because of the changes @CallSeqChange ?).
  Therefore, branch bug-591829e948-A can simply pass YES here.

* The variable "initialized" inside XMapWindow() has no use anymore because
  @CallSeqChange that function is truly executed at idle time, and therefore
  application initialization has been completed when it executes.

* Improvements from branch bug-591829e948-MC have been imported except one,
  which made test winfo-4.8 fail.

* Github-CI is satisfied with the changes on branch bug-591829e948-A:

     [https://github.com/tcltk/tk/actions?query=branch%3Abug-591829e948-A]


I intend to merge branch bug-591829e948-A into trunk and core-9-0-branch.
To that end, I invite review of branch bug-591829e948-A.

For your convenience, here is the diff over that branch:

     [https://core.tcl-lang.org/tk/vdiff?branch=bug-591829e948-A]

marc_culler (claiming to be Marc Culler) added on 2026-03-19 13:45:14:
Hi Eric,

They don't relate to this ticket at all.  I posted them in the wrong
ticket by mistake.  Sorry!

They were meant to go in ticket [7f67bb4054].  I would remove them from
this one if I could (and they are now irrelevant to [7f67bb4054] as well).
But I don't know how to do that.

erikleunissen added on 2026-03-19 08:30:07:
@Marc:

How does your post from 2026-03-18 13:34:26, and those following up on it,
relate to the present ticket?

jan.nijtmans added on 2026-03-19 08:00:17:

> How does an unused label in a Tk file prevent Tcl from building?

Because the Windows debug-build is done with options cause any warning to be an error. When any build fails, all other builds are canceled. One of the other builds was in the process of building Tcl; when it is canceled any error-message can result there. Just try to find the build where the first error occurs, that's the one with a useful error-message.


marc_culler (claiming to be Marc Culler) added on 2026-03-19 01:35:04:
And now I also see:

D:\a\tk\tk\tk\win\..\generic\tkMenu.c(2626): error C2220: the following warning is treated as an error
D:\a\tk\tk\tk\win\..\generic\tkMenu.c(2626): warning C4102: 'meError': unreferenced label


So I guess this will all be settled tomorrow.

marc_culler (claiming to be Marc Culler) added on 2026-03-19 01:32:36:
Hmmm.  Now I see the error happening in the Tk build, with no mention of
the zipfile.

marc_culler (claiming to be Marc Culler) added on 2026-03-19 00:31:46:
Hi Jan,

How does an unused label in a Tk file prevent Tcl from building?

marc_culler (claiming to be Marc Culler) added on 2026-03-18 17:49:15:
More to the point, the changes were all to Tk. The error happens when
building Tcl.  The Tk build doesn't even start. So the MSVC failure has
notthing to do with Tk.  It seems to have something to do with building
the zipfile for the Tcl library, based on the error message.  I am not
sure how the Tk actions in the bugfix branch are getting Tcl source code,
but that could be the issue.

erikleunissen added on 2026-03-18 15:49:47:
But the bug fix branch made only changes to macOS specific code, so what's the deal with MS Windows?

erikleunissen added on 2026-03-18 15:42:26:
At Github there are no problems regarding msvc builds, and the tests in the
Tk test suite (as far as constraints allow) all pass there:

    [https://github.com/tcltk/tk/actions?query=branch%3Abug-591829e948-A]

marc_culler (claiming to be Marc Culler) added on 2026-03-18 13:34:26:
It looks like the menuDraw tests pass in all cases where the build
succeeds, which unfortunately does not include any msvc builds for
Windows.  They seem to fail in a call to nmake while building Tcl:

Building Tcl library zip file D:\a\tk\tk\tcl\win\Release_AMD64_VC1944\libtcl9.1a2.zip
NMAKE : fatal error U1058: terminated by user

erikleunissen added on 2026-03-17 17:08:31:
The incompatibility between the branches bug-591829e948-A and bug-591829e948-MC
appears to be caused by the following statements in branch bug-591829e948-MC:

    [https://core.tcl-lang.org/tk/file?ci=5c07a8f0272562c2&name=macosx%2FtkMacOSXSubwindows.c&ln=262]

and

    [https://core.tcl-lang.org/tk/file?ci=5c07a8f0272562c2&name=macosx%2FtkMacOSXInit.c&ln=481]

These statements have no use anymore in branch bug-591829e948-A because it executes
the code formerly inside showRootWindow(), now synchronously inside XMapWindow().
This change makes that there is no correct/useful location in the code anymore
for these explicit manipulations of the TK_MAPPED flag.

The remedy is simple. We restore what was backed out by [a8a4401ea4], except
for these two statements, leaving this matter as it was before copying code
from branch bug-591829e948-MC.

erikleunissen added on 2026-03-17 12:00:24:
I copied the improvements from branch bug-591829e948-MC into branch
bug-591829e948-A (commits [8239f0e37c] and [6b9090c408]), and let
branch bug-591829e948-A exercise at Github.

Alas, this made the non-regression test fail plus a few other tests.
I can reproduce that locally on macOS Sequoia, where I also can see
bad things happening to windows on the screen.

So, while each branch runs fine separately, they cannot be combined
by simply copying the extras from bug-591829e948-MC into bug-591829e948-A.

Therefore, I backed out the commits that copied the changes from branch
bug-591829e948-MC into branch bug-591829e948-A (commit [a8a4401ea4]).

erikleunissen added on 2026-03-16 19:31:14:
Marc Culler and I had a private conversation which lead to another development
branch bug-591829e948-MC. This branch includes improvements to the macOS
specific Tk initialization that I could not have come up with, given my
limited understanding of objective C and the macOS/aqua specific API.

Development of the definite fix for issue A. is going to proceed from branch
bug-591829e948-A, and it will receive the improvements from branch bug-591829e948-MC.

erikleunissen added on 2026-03-15 21:59:12:
The the 6 extra test failures were cause by not restoring the root window
in test tkInit-1.0. Now fixed in [4ca747abd3].

erikleunissen added on 2026-03-15 21:39:42:
The test failures do also occur if tkInit.test is executed with trunk (but then we have 7 test failures because the non-regression test also fails on trunk of course.

The 6 test failures do not occur when selectively skipping only the
non-regression test for the present issue A, as follows:

    TESTFLAGS="-notfile tkInit.test" make test


So, we may conclude that:
* the fix in branch bug-591829e948-A does not lead to extra test failures
  in the Tk test suite
* something in tkInit.test triggers the 6 extra test failures in unixEmbed.test
  and unixWm.test

erikleunissen added on 2026-03-15 21:10:52:
As expected, the 6 test failures also do not occur when invoking the test suite with each test file in a separate interpreter:

    TESTFLAGS="-singleproc 0" make test

erikleunissen added on 2026-03-15 21:00:54:
I completely forgot to post my initial analysis of issue A.
Some of it may already have been superseded by earlier posts, but here it is:

Summary
-------
Issue A. is caused by a difference w.r.t. the mapping of the Tk root window
between the macosx-specific Tk initialization function TkpInit() and those
for the other major platforms. As a side effect, the pointer window reported by:

	winfo containing [winfo pointerx .] [winfo pointery .]

is also different, but the screen pointer per se is not essential to the issue.

Analysis
--------
The mapping of the root window at Tk initialization is a task that is scheduled
for execution at idle time by the generic initialization function TkCreateFrame(),
which in turn is called by Initialize() and TkInit(), which are also part of
the generic Tk initialization.

Where x11 and win32 let the mapping of the root window occur at idle time
(i.e. after Tk initialization has completed), macOS/aqua enforces that the
mapping of the root window takes place synchronously. It does so by early
executing the tasks that were previously scheduled for execution at idle time
by the generic Tk initialization. This is done in the macOS-specific TkpInit()
function at the following location in the Tk codebase:

      [https://core.tcl-lang.org/tk/file?ci=4d4b38b175811d3d&name=macosx%2FtkMacOSXInit.c&proof=669936549&ln=649-656]

Because of this early mapping the screen pointer may indeed become contained
in the root window if it happens to be at the right position on the screen.
So, there is nothing wrong with "winfo containing". There is an unexpected
early initial mapping of the root window.

Beware that:
* What works confusingly is that the initial mapping of the root window
  doesn't make it visible (even if no other window obscures it). So the user
  cannot see the root window containing the screen pointer by inserting
  "after 2000" as the very first command after having loaded Tk.
  Visibility is induced by the immediately successive code section, which
  schedules showRootWindow() for execution at idle time.
* the comment in the above code block is ill-positioned, and therefore misleading:
  at the location of the comment, the root window has NOT been mapped yet. It's
  essential for the understanding of the present issue, that it is the enforced
  execution of idle tasks that induces the mapping of the root window. This is
  exercised by doing "make shell" on commit [393029f6e2].

B. Proposed resolution of the difference between platforms
----------------------------------------------------------
Branch bug-591829e948-A implements asynchronous mapping of the root window
for macOS/aqua, aligning Tk's behaviour with other platforms.

Beware that the code in TkpInit() holds several other adjustments that regard the
initial display of the root window. These adjustments are intricate w.r.t. to
their timing, and most of them aren't exercised by a (non-regression) test.
For example:

  commit [c7a9e22a1c]
  commit [47f17f3d60] for ticket [5869c270bd]

Given that these adjustments relate to timing, it's not only important to check
whether the proposed fix breaks some of these intricate adjustments, but also
whether it maybe takes away the reason for them, rendering them unnecessary.

C. Questions
------------
* Obviously, the current code in the macosx-specific TkpInit() function that
  implements synchronous mapping is deliberate. This raises the following
  question:

    "What is the reason for the choice to map the root window on macOS/aqua
     synchronously, and thereby introducing a deviaton from the behaviour on
     the other major platforms?"

  This question doesn't only pertain to the present issue, but it is especially
  relevant because the initial synchronous mapping doesn't lead to visibility
  on the screen. Only after having processed all idle tasks the root window
  becomes visible. This would be a reason to defer the entire process of
  displaying the root window, its mapping included, to idle time.

* Does the reversal to asynchronous mapping on macOS/aqua affect any of the fixes
  mentioned in section B. ?

erikleunissen added on 2026-03-15 20:51:22:
The 6 test failures do not occur when invoking the Tk test suite as follows:

    TESTFLAGS="-file \"unixEmbed.test unixWm.test\"" make test

So, something in previously executed test files is interfering.

erikleunissen added on 2026-03-15 12:38:18:
I've stretched my efforts to resolve issue A. to the point that I find it
no longer "affordable" to proceed any further on my own. That's just because
I'm unfamiliar with the macOS codebase and with objective C.

Please consider branch bug-591829e948-A as the expression of a somewhat
worked out approach/idea.

I think it is important in this stage that the approach receives judgement
from those more experienced in the macOS arena; a judgement whether the
approach is promising enough to justify further effort; and if so,
directions for proceeding.

erikleunissen added on 2026-03-15 11:53:30:
Added a non-regression test (in a new test file tkInit.test) and
a suggested fix.

Results at [cd824c7973]:

* the root window now becomes visible on screen.
* Running the Tk test suite results in the following 6 extra
  test failures:

	unixEmbed-5.1a
	unixEmbed-5.3a
	unixEmbed-7.2a
	unixEmbed-8.1a
	unixEmbed-8.2
	unixWm-9.4

  This needs more investigation.

erikleunissen added on 2026-03-14 19:00:23:
Created bug fix branch bug-591829e948-A, and added debug statements demonstrating
that it is the execution of idle tasks in macosx/TkpInit() that enforces the
initial mapping of the root window. See commit [393029f6e2].

erikleunissen added on 2026-03-14 13:32:01:
Merged the fix for issue B. into trunk, core-9-0-branch and core-8-6-branch.

erikleunissen added on 2026-03-13 12:18:22:
Completing the table from my post on 2026-03-03 20:57:34 for MS Windows:

Results:
                       ismapped     state
----------------------------------------------
Linux/x11            :     0        normal
macOS/aqua (Sequoia) :     1        normal
win32                :     0        normal

Now we can conclude that macOS/aqua is the exception regarding the result for
[wm ismapped .] immediately upon Tk initialization.

erikleunissen added on 2026-03-05 15:07:59:
Added a non-regression test + proposed fix for issue B in branch bug-591829e948-B.
The proposed fix includes a rename of function FrontWindowAtLocation() and
some adjustments of the function header to reflect the function's purpose better.

erikleunissen added on 2026-03-05 15:06:17:
From further code inspection I conclude that we have two issues that have
separate origins in the Tk codebase:
A. the incorrect initial value of [winfo containing [winfo pointerx .] [winfo pointery .]]
   before having withdrawn the Tk root window
B. its persistence after [wm withdraw .]

The cause for issue B is in the function FrontWindowAtLocation() at this location:

    [https://core.tcl-lang.org/tk/file?ci=891c0545035ac390&name=macosx%2FtkMacOSXWm.c&proof=954740294&ln=814]

Here, the function considers all toplevel windows regardless their mapping state.
However, "winfo containing" is about the actual situation on the screen, and
therefore unmapped windows ought be excluded from the search.

I'm going to create a bug-fix branch for each of these issues, so that they can be
handled separately:
* bug-591829e948-A
* bug-591829e948-B

erikleunissen added on 2026-03-05 14:52:08:
Thanks Jan.

Before crying "halleluja", please understand that branch bug-591829e948-B
addresses only one of the issues in this ticket. A follow-up post will describe
the issues in this ticket more in overview.

jan.nijtmans added on 2026-03-05 11:04:39:

Your patch (in branch "bug-591829e948-B") looks good to me.

+1


erikleunissen added on 2026-03-03 20:57:34:
Another interesting fact is the initial state of the Tk root window before it is mapped,
as returned by [wm ismapped .] and [wm state .].

Results:
                       ismapped     state
----------------------------------------------
Linux/x11            :     0        normal
macOS/aqua (Sequoia) :     1        normal
win32                :    TODO      TODO

I won't have access to a MS Windows system for another week or so, to determine
the results for the win32 windowing system.

The file "exercise.tcl" has been updated once more to include these aspects.

erikleunissen added on 2026-02-15 15:24:06:
I'd like to add that the false information under macOS/aqua persists, even after
having issued an explicit "wm withdraw ."

I've uploaded a new file exercise.tcl which determines the pointer window just
before, and just after an initial "wm withdraw ."

Results with my systems:

Linux/x11 and win32: |||

macOS/aqua (Sequoia): |.|.|

Attachments: