Tk Source Code

View Ticket
Login
Ticket UUID: c0662eca6edac87ba04aa91eb5d2d7d94803b94a
Title: On aqua, the after callbacks are delayed until releasing the mouse button
Type: Bug Version: all
Submitter: nemethi Created on: 2023-06-04 19:53:52
Subsystem: 66. Aqua Window Operations Assigned To: nobody
Priority: 5 Medium Severity: Minor
Status: Open Last Modified: 2026-10-12 21:48:25
Resolution: None Closed By: nobody
    Closed on:
Description:
The following simple script doesn't work as expected on aqua:

set count 0
bind . <Configure> { after 100 [list puts "<Configure> [incr count]"] }

If you are resizing the main window with the mouse, nothing gets printed to the terminal as long as the mouse button is is pressed state.  After releasing mouse button 1, a series of "<Configure> ..." lines are output -- all the after 100 callbacks scheduled during the resize operation get fired when the mouse button is released.  On Linux and Windows one can see the texts during the resize operation, as expected.
User Comments: chrstphrchvz added on 2026-10-12 21:48:25:

Typo in my previous comment: I meant _runBackgroundLoop, not _runLoopMode.

I have pushed a branch aqua-liveresize-runbackgroundloop-experiment resembling what I tried a few years ago.

There is a related issue to be aware of, and which my branch does not affect. A comment in NSWindow.h (still present in the macOS 26.5 SDK) says:

NSWindowWillStartLiveResizeNotification is sent when the user starts a live resize operation via a mouseDown in the resize corner. The notification will be sent before the window size is changed. Note that this notification is sent once for a sequence of window resize operations

But rather than windowLiveResize: being called immediately when pressing the mouse button down on the window edge/corner (as the comment seems to suggest), it is only called once the mouse is also dragged. And during the time after clicking and before dragging, Tcl events are not handled. At least that is what I observed using macOS 15 Sequoia (which is the latest I am stuck on for the time being); I haven't checked on earlier versions.


chrstphrchvz added on 2026-10-11 04:04:53:

If I add puts to the bind script:

set count 0
bind . <Configure> {
    puts "bind $count"
    after 100 [list puts "<Configure> [incr count]"]
}

and then resize the window, I observe the bind script outputting a line immediately each time it is run. The output from the after 100 … script does not appear until the live resize is over, because timer events are not handled until then. So I do not think output buffering is relevant here.

I tried again to make _runLoopMode run during live resizing, and I observe that the after 100 … script output does appear during resizing. I will see if I can push a branch with what I tried.


marc_culler (claiming to be Marc Culler) added on 2026-10-11 00:34:47:
Is it possible that the messages are written to stdout and buffered
because stdout cannot be flushed until the Tcl event loop resumes,
so all of the buffered text gets printed when that happens?

marc_culler (claiming to be Marc Culler) added on 2026-10-10 19:53:30:
OK. Good.

How do the configure events which are handled immediately in setFrameSize
trigger the procs which are bound to <Configure>?  And why don't the procs
run when the event is handled?  Why does Tcl wait until the Tcl event loop
starts up again to run them?  Where are those procs stored while the Tcl
event loop is stalled due to the runloop mode being NSEventTrackingRunloopMode?

chrstphrchvz added on 2026-10-10 19:22:16:

_runBackgroundLoop is only ever called from the main thread, not a background thread. All that the background thread does is repeatedly call performSelectorOnMainThread:… to tell the main thread to eventually call _runBackgroundLoop. If there are still reasons not to perform certain AppKit tasks or handle Tk event callbacks from _runBackgroundLoop, being run from a background thread is not one of them.


marc_culler (claiming to be Marc Culler) added on 2026-10-10 15:03:00:
Please ignore my last comment which was intended for a different ticket
but got submitted here by mistake.

marc_culler (claiming to be Marc Culler) added on 2026-10-10 14:54:55:
I think the guard in deferredUpdateLayer is now correct.  I added a
boolean property windowIsDead to the TKContentView class and I set that
to YES in tkWmDeadWindow.  I check the property in deferredUpdateLayer.

I think this fix is now ready to merge, but I am going to ask Nicolas Bas
to test against his app.  His report of artifacts when the main window of
his app is withdrawn, configured, and deiconified was the original reason
for adding the event loop inside updateLayer.

Thank you all for your comments and if you have any more, please share
them.

marc_culler (claiming to be Marc Culler) added on 2026-10-10 04:57:35:
I think the AppKit handles open menus and window resizing in very similar
ways. While a menu is open Appkit runs a runloop with mode NSEventTrackingMode.
Gemini claims that the same is true during a liveResize (but I haven't checked
that).  In both cases there are starting and ending notifications, and we
subscribe to both.

So I would say that it is very likely possible to run the backgroundLoop
thread during a liveResize.  However, I an not sure that would do what
Csaba wants, and I think it would be very risky.

During a live resize, AppKit repeatedly calls the setFrameSize method of
NSView, which we override.  That method directly resizes and redraws the
window but also generates configure events to inform Tk of the new size.
However, those configure events are not placed on the Tcl event queue.
They are handled immediately.  They are generated with the following code:

TkGenWMConfigureEvent(tkwin, Tk_X(tkwin), Tk_Y(tkwin), width, height,
                TK_SIZE_CHANGED | TK_MACOSX_HANDLE_EVENT_IMMEDIATELY);

It is necessary for those size-change events to be handled immediately
so the window can be redrawn correctly during the resize.

I don't understand the mechanism which allows bind to know about those
events at all, but the fact that the messages get printed out after the
resize indicates that it does.  However, since the events are handled
immediately I don't see how the background loop would ever see them,
even if TCL_WINDOW_EVENTS were added to the current _runBackgroundLoop
method which looks like this:

- (void) _runBackgroundLoop
{
    while(Tcl_DoOneEvent(TCL_IDLE_EVENTS|TCL_TIMER_EVENTS|TCL_DONT_WAIT)){
    }
}

An important thing to worry about here is that there are operations
which AppKit requires must be done in the main thread.  If the background
thread were to run a bind proc which involve any of those forbidden
operations, Wish would crash.  There is no list of which operations are
forbidden. Apple keeps that secret, and just tells developers that they
should do everything in the main thread. If arbitrary procs can be
bound to configure events and executed by the backgroundLoop thread
then we have no way to prevent those crashes.

So I think that carrying out this plan, even if it is possible, carries
a lot of risk.

serhiy.storchaka added on 2026-10-08 13:08:10:

This has the same cause as [3ceb6b0f65]: during a live resize AppKit runs its own event loop, and only the redraw is done there. With the drawing to a bitmap in 9.0 ([1dfe4fe90a]) flicker may no longer be a reason to suspend other events. Marc, could TKBackgroundLoop, which keeps Tk events running while a menu is open, also be run during a live resize (between viewWillStartLiveResize and viewDidEndLiveResize), as Christopher tried?


kevin_walzer added on 2023-06-05 01:40:09:
Christopher is correct that Tk drawing/event processing is suspended during a resize event--this was to prevent flickers and other artifacts from appearing during and after the resizing. Apple has overhauled its drawing routines several times in recent years, often with minimal documentation, and this solution allows smooth rendering of windows after the resizing is complete.

chrstphrchvz added on 2023-06-05 00:37:37:

The animated wave widget demo is another example affected by this issue.

Resizing a toplevel on Tk Aqua involves an AppKit function call which blocks until the resize finishes, during which the only Tcl events handled are apparently are those for trying to redraw the toplevel (“live resizing”). There was a similar issue where Tcl events stopped whenever a menu was posted, and it was dealt with using a temporary AppKit runloop. It may be possible to reuse that runloop for toplevel resizing (something I had tried as part of another experiment: https://github.com/chrstphrchvz/tk/compare/2826273e8276...2c2e6a9bb2ae). But this seems to be an area in which it has long been difficult for Tk Aqua to avoid platform inconsistencies or regressions (see Daniel Steffen’s comment dated 2010-11-03 00:01:16 on [3028676]).