Tk Source Code

Artifact [541cbc64]
Login

Artifact 541cbc64a5a5312e07767d06ddda6405044e3a5a75fde7b941528f9754af673f:

Attachment "sample_implementation-1.1.tcl" to ticket [85c83974] added by erikleunissen 2021-01-11 18:07:06.
# Record whether we need to account for pointer warping at idle time:
#
# - In Tk releases 8.6 and older, pointer warping is scheduled for execution at idle time.
# - In release 8.7 and newer this happens synchronously.
#
set ::idle_pointer_warping [expr {[package vcompare [package present Tk] 8.7] >= 0?0:1}]


# controlPointerWarpTiming --
#
# Ensures proper timing of the following details of pointer warping:
#
# a. Allow pointer warping to happen if it was scheduled for execution at idle time.
#
# b. Work around a race condition associated with OS notification of mouse motion by Windows 10.
#
#    When calling [event generate $w $event -warp 1 ...], the following sequence occurs:
#    - At some point in the processing of this command, either via a synchronous execution path,
#      or asynchronously at idle time, Tk calls an OS function[*] to carry out the mouse cursor motion.
#    - Tk has previously registered a callback function[**] with the OS, for the OS to call in order to notify Tk when a mouse move is completed.
#    - Tk doesn't wait for the callback function to receive the notification from the OS, but continues processing.
#      This suits most use cases because (usually) the notification comes quickly enough (range: a few ms?). However ...
#    - A problem arises if Tk performs some processing, immediately following up on [event generate $w $event -warp 1 ...], and that
#      processing relies on the mouse pointer having actually moved. If such processing happens just before the notification from the OS
#      has been received, Tk will be using not yet updated info (e.g. mouse coordinates). Hickup, choke etc ... !
#
#            [*]  the function SendInput() of the Win32 API
#            [**] the callback function is TkWinChildProc()
#
#   This timing issue can be addressed by putting the Tk process on hold (do nothing at all) for a somewhat extended amount of time,
#   while letting the OS complete its job in the meantime. This is exactly what is accomplished by calling [after ms].
#
#        ----
#        For the history of this issue pleasse refer to Tk ticket [69b48f427e], specifically the comment on 2019-10-27 14:24:26.
#
proc controlPointerWarpTiming {script {duration 50}} {
	uplevel 1 $script
	
	if {$::idle_pointer_warping} {
		update idletasks ;# see a. above
	}
	if {($::tcl_platform(os) eq "Windows NT") && ($::tcl_platform(osVersion) eq "10.0")} {
		after $duration ;# see b. above
	}
}