#
# <20230220.1226.14>
#
# An attempt to really wait for idle...
#
proc waitForIdle {} {
  update idletasks
  update idletasks
}


proc realWaitForIdle {} {
  set progress {}
  set tmp [clock clicks]

  while {[incr failsafe] < 100} {
    update
    lappend progress [expr {[set now [clock clicks]] - $tmp} ]
    if {($now - $tmp) < $::waitForIdle::base} {break}
    set tmp $now
  }
  # frputs failsafe progress
  return $progress

}

# A way more serious attempt...
namespace eval waitForIdle {
  variable base 0
  variable tmp 0

  # Calibrate a wait clock
  # realWaitForIdle returns a list of all times (units of clicks)
  # that it took to find a time less than or equal to the "base"
  # It is limited to 100 tries by the failsafe.
  # Thus when we call it with base = 0 as it is now, we will get
  # 100 times. To set the base we take 4 times the minimum.
  # frputs [realWaitForIdle]
  set base [expr {[tcl::mathfunc::min {*}[realWaitForIdle]] * 4}]
}  
