Tk Source Code

Artifact [95cd7cc4]
Login

Artifact 95cd7cc43314c5852b3d1632f87a39a75a3f493e12a4366962ff72ef22ec134b:

Attachment "tk-94e45daa.patch" to ticket [85c83974] added by erikleunissen 2021-02-09 20:45:17.
diff -ur tk-trunk.old/tests/bind.test tk-trunk.new/tests/bind.test
--- tk-trunk.old/tests/bind.test	2021-02-07 15:18:33.000000000 +0100
+++ tk-trunk.new/tests/bind.test	2021-02-07 21:21:28.000000000 +0100
@@ -44,8 +44,7 @@
 toplevel .top
 wm geometry .top 50x50-50-50
 update
-event generate .top <Button-1> -warp 1
-update
+controlPointerWarpTiming {event generate .top <Button-1> -warp 1}
 destroy .top
 
 test bind-1.1 {bind command} -body {
@@ -6247,8 +6246,7 @@
     focus -force .t.f
     update
 } -body {
-    event generate .t.f <Button-1> -warp 1
-    after 50  ; # Win specific - wait for SendInput to be executed
+    controlPointerWarpTiming {event generate .t.f <Button-1> -warp 1}
     event generate .t.f <ButtonRelease-1>
     destroy .t.f
     update  ;  # shall simply not crash
@@ -6828,13 +6826,11 @@
     # how much the pointer moved
     wm geometry .top +200+200
     update
-    event generate .top <Motion> -x 20 -y 20 -warp 1
-    after 50  ; # Win specific - wait for SendInput to be executed
+    controlPointerWarpTiming {event generate .top <Motion> -x 20 -y 20 -warp 1}
     set pointerPos1 [winfo pointerxy .top]
     wm geometry .top +600+600
     update
-    event generate .top <Motion> -x 20 -y 20 -warp 1
-    after 50  ; # Win specific - wait for SendInput to be executed
+    controlPointerWarpTiming {event generate .top <Motion> -x 20 -y 20 -warp 1}
     set pointerPos2 [winfo pointerxy .top]
     # from the first warped position to the second one, the mouse
     # pointer should have moved the same amount as the window moved
@@ -6851,11 +6847,9 @@
 test bind-34.2 {-warp works relatively to the screen} -setup {
 } -body {
     # Contrary to bind-34.1, we're directly checking screen coordinates
-    event generate {} <Motion> -x 20 -y 20 -warp 1
-    after 50  ; # Win specific - wait for SendInput to be executed
+    controlPointerWarpTiming {event generate {} <Motion> -x 20 -y 20 -warp 1}
     set res [winfo pointerxy .]
-    event generate {} <Motion> -x 200 -y 200 -warp 1
-    after 50  ; # Win specific - wait for SendInput to be executed
+    controlPointerWarpTiming {event generate {} <Motion> -x 200 -y 200 -warp 1}
     lappend res {*}[winfo pointerxy .]
 } -cleanup {
 } -result {20 20 200 200}
@@ -6872,8 +6866,7 @@
     }
     set res {}
 } -body {
-    event generate {} <Motion> -x 0 -y 0 -warp 1
-    after 50  ; # Win specific - wait for SendInput to be executed
+    controlPointerWarpTiming {event generate {} <Motion> -x 0 -y 0 -warp 1}
     foreach dim [winfo pointerxy .] {
         if {$dim <= $halo} {
             lappend res ok
@@ -6881,10 +6874,8 @@
             lappend res $dim
         }
     }
-    event generate {} <Motion> -x 100 -y 100 -warp 1
-    after 50  ; # Win specific - wait for SendInput to be executed
-    event generate {} <Motion> -x -1 -y -1 -warp 1
-    after 50  ; # Win specific - wait for SendInput to be executed
+    controlPointerWarpTiming {event generate {} <Motion> -x 100 -y 100 -warp 1}
+    controlPointerWarpTiming {event generate {} <Motion> -x -1 -y -1 -warp 1}
     foreach dim [winfo pointerxy .] {
         if {$dim <= $halo} {
             lappend res ok
@@ -7028,8 +7019,7 @@
 } -result pass
 
 test bind-36.1 {pointer warp with grab on toplevel, bug [e3888d5820]} -setup {
-    event generate {} <Motion> -warp 1 -x 50 -y 50
-    after 50  ; # Win specific - wait for SendInput to be executed
+    controlPointerWarpTiming {event generate {} <Motion> -warp 1 -x 50 -y 50}
     toplevel .top
     grab release .top
     wm geometry .top 200x200+300+300
@@ -7044,14 +7034,11 @@
     after 100 ; update
 } -body {
     grab .top
-    event generate .top.l <Motion> -warp 1 -x 10 -y 10
-    after 50  ; # Win specific - wait for SendInput to be executed
+    controlPointerWarpTiming {event generate .top.l <Motion> -warp 1 -x 10 -y 10}
     foreach {x1 y1} [winfo pointerxy .top.l] {}
-    event generate {} <Motion> -warp 1 -x 50 -y 50
-    after 50  ; # Win specific - wait for SendInput to be executed
+    controlPointerWarpTiming {event generate {} <Motion> -warp 1 -x 50 -y 50}
     grab release .top
-    event generate .top.l <Motion> -warp 1 -x 10 -y 10
-    after 50  ; # Win specific - wait for SendInput to be executed
+    controlPointerWarpTiming {event generate .top.l <Motion> -warp 1 -x 10 -y 10}
     foreach {x2 y2} [winfo pointerxy .top.l] {}
     # success if the coords are the same with or without the grab, and if they
     # are at (10,10) inside the label widget as requested by the warping
diff -ur tk-trunk.old/tests/constraints.tcl tk-trunk.new/tests/constraints.tcl
--- tk-trunk.old/tests/constraints.tcl	2021-02-07 15:18:33.000000000 +0100
+++ tk-trunk.new/tests/constraints.tcl	2021-02-09 20:44:18.000000000 +0100
@@ -172,6 +172,69 @@
             return $r
         }
 
+	#
+	#  CONTROL TIMING ASPECTS OF POINTER WARPING
+	#
+	# The proc [controlPointerWarpTiming] takes care of the following timing
+	# details of pointer warping:
+	#
+	# a. Allow pointer warping to happen if it was scheduled for execution 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 and no extra
+	#      control is needed.
+	#    The namespace variable idle_pointer_warping records which of these is
+	#    the case.
+	#
+	# 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 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.
+	#
+	variable idle_pointer_warping [expr {[package vcompare [package present Tk] 8.7] >= 0?0:1}]
+	proc controlPointerWarpTiming {script {duration 50}} {
+		uplevel 1 $script
+		
+		variable idle_pointer_warping
+		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
+		}
+	}
+	namespace export controlPointerWarpTiming
+
     }
 }
 
Only in tk-trunk.new/tests: constraints.tcl~
diff -ur tk-trunk.old/tests/event.test tk-trunk.new/tests/event.test
--- tk-trunk.old/tests/event.test	2021-02-07 15:18:33.000000000 +0100
+++ tk-trunk.new/tests/event.test	2021-02-08 00:06:20.000000000 +0100
@@ -829,8 +829,7 @@
     bind all <Enter> {lappend res %W}
     pack [frame .top.f -bg green -width 50 -height 50] -anchor se -side bottom
     update
-    event generate .top.f <Motion> -warp 1 -x 25 -y 25  ; # <Enter> sent to .top and .top.f
-    after 50  ; # Win specific - wait for SendInput to be executed
+    controlPointerWarpTiming {event generate .top.f <Motion> -warp 1 -x 25 -y 25}
     update  ; # idletasks not enough
     destroy .top.f  ; # no <Enter> event sent
     update
diff -ur tk-trunk.old/tests/menu.test tk-trunk.new/tests/menu.test
--- tk-trunk.old/tests/menu.test	2021-02-07 15:18:33.000000000 +0100
+++ tk-trunk.new/tests/menu.test	2021-02-07 21:26:12.000000000 +0100
@@ -3957,9 +3957,7 @@
     pack .top.mb
     update
     # simulate mouse click on the menubutton, which posts its menu
-    event generate .top.mb <Button-1> -warp 1
-    update
-    after 50
+    controlPointerWarpTiming {event generate .top.mb <Button-1> -warp 1}
     event generate .top.mb <ButtonRelease-1>
     update
     # simulate mouse click on the menu again, i.e. without
diff -ur tk-trunk.old/tests/textTag.test tk-trunk.new/tests/textTag.test
--- tk-trunk.old/tests/textTag.test	2021-02-07 15:18:33.000000000 +0100
+++ tk-trunk.new/tests/textTag.test	2021-02-08 10:31:29.000000000 +0100
@@ -1237,7 +1237,7 @@
 test textTag-15.1 {TkTextBindProc} -setup {
     .t tag delete x y
     wm geometry . +200+200 ; update
-    event generate {} <Motion> -warp 1 -x 5 -y 5 ; update idletasks ; after 50
+    controlPointerWarpTiming {event generate {} <Motion> -warp 1 -x 5 -y 5}
 } -body {
     bind .t <ButtonRelease> {lappend x up}
     .t tag bind x <ButtonRelease> {lappend x x-up}
@@ -1263,7 +1263,7 @@
 test textTag-15.2 {TkTextBindProc} -setup {
     .t tag delete x y
     wm geometry . +200+200 ; update
-    event generate {} <Motion> -warp 1 -x 5 -y 5 ; update idletasks ; after 50
+    controlPointerWarpTiming {event generate {} <Motion> -warp 1 -x 5 -y 5}
 } -body {
     .t tag bind x <Enter> {lappend x x-enter}
     .t tag bind x <Button> {lappend x x-down}
@@ -1292,7 +1292,7 @@
 test textTag-15.3 {TkTextBindProc} -setup {
     .t tag delete x y
     wm geometry . +200+200 ; update
-    event generate {} <Motion> -warp 1 -x 5 -y 5 ; update idletasks ; after 50
+    controlPointerWarpTiming {event generate {} <Motion> -warp 1 -x 5 -y 5}
 } -body {
     .t tag bind x <Enter> {lappend x x-enter}
     .t tag bind x <Button-1> {lappend x x-down}
@@ -1326,7 +1326,7 @@
 test textTag-16.1 {TkTextPickCurrent procedure} -setup {
     .t tag delete {*}[.t tag names]
     wm geometry . +200+200 ; update
-    event generate {} <Motion> -warp 1 -x 5 -y 5 ; update idletasks ; after 50
+    controlPointerWarpTiming {event generate {} <Motion> -warp 1 -x 5 -y 5}
 } -body {
     event gen .t <ButtonRelease-1> -state 0x100 -x $x1 -y $y1
     set x [.t index current]
@@ -1349,7 +1349,7 @@
 } -setup {
     .t tag delete {*}[.t tag names]
     wm geometry . +200+200 ; update
-    event generate {} <Motion> -warp 1 -x 5 -y 5 ; update idletasks ; after 50
+    controlPointerWarpTiming {event generate {} <Motion> -warp 1 -x 5 -y 5}
     .t configure -font $textWidgetFont -wrap none
 } -body {
     .t tag configure big -font $bigFont
@@ -1369,7 +1369,7 @@
         .t tag remove $i 1.0 end
     }
     wm geometry . +200+200 ; update
-    event generate {} <Motion> -warp 1 -x 5 -y 5 ; update idletasks ; after 50
+    controlPointerWarpTiming {event generate {} <Motion> -warp 1 -x 5 -y 5}
 } -body {
     foreach i {a b c d} {
         .t tag bind $i <Enter> "lappend x enter-$i"
@@ -1397,7 +1397,7 @@
         .t tag remove $i 1.0 end
     }
     wm geometry . +200+200 ; update
-    event generate {} <Motion> -warp 1 -x 5 -y 5 ; update idletasks ; after 50
+    controlPointerWarpTiming {event generate {} <Motion> -warp 1 -x 5 -y 5}
 } -body {
     foreach i {a b c d} {
         .t tag bind $i <Enter> "lappend x enter-$i"
@@ -1426,7 +1426,7 @@
         .t tag remove $i 1.0 end
     }
     wm geometry . +200+200 ; update
-    event generate {} <Motion> -warp 1 -x 5 -y 5 ; update idletasks ; after 50
+    controlPointerWarpTiming {event generate {} <Motion> -warp 1 -x 5 -y 5}
     .t configure -font $textWidgetFont -wrap none
 } -body {
     .t tag configure big -font $bigFont
@@ -1447,7 +1447,7 @@
         .t tag remove $i 1.0 end
     }
     wm geometry . +200+200 ; update
-    event generate {} <Motion> -warp 1 -x 5 -y 5 ; update idletasks ; after 50
+    controlPointerWarpTiming {event generate {} <Motion> -warp 1 -x 5 -y 5}
     .t configure -font $textWidgetFont -wrap none
 } -body {
     .t tag configure big -font $bigFont
@@ -1469,7 +1469,7 @@
         .t tag remove $i 1.0 end
     }
     wm geometry . +200+200 ; update
-    event generate {} <Motion> -warp 1 -x 5 -y 5 ; update idletasks ; after 50
+    controlPointerWarpTiming {event generate {} <Motion> -warp 1 -x 5 -y 5}
     .t configure -font $textWidgetFont -wrap none
 } -body {
     .t tag configure big -font $bigFont
@@ -1502,7 +1502,7 @@
 test textTag-18.1 {TkTextPickCurrent tag bindings} -setup {
     destroy .t
     wm geometry . +200+200 ; update
-    event generate {} <Motion> -warp 1 -x 5 -y 5 ; update idletasks ; after 50
+    controlPointerWarpTiming {event generate {} <Motion> -warp 1 -x 5 -y 5}
 } -body {
     text .t -width 30 -height 4 -relief sunken -borderwidth 10 \
       -highlightthickness 10 -pady 2