Updated 2012-12-04 17:01:22 by pooryorick

Description  edit

Richard Suchenwirth 2005-07-09 - There are several ways to catch the attention of a user to a window, e.g. an alert or dialog - flashing colors are sometimes used. One can also make a toplevel "jitter", i.e. move slightly to the left and bottom, and back again. One pixel is enough to catch attention, as windows usually (and thankfully!) stand still. I saw this effect in an ad on AOL's home page, and even though I'd hardly ever use it, in this Saturday breakfast fun project I wanted to code it - "because we can" :) Here it is, in bare-bones fashion. If you drag the window, it will snap back to its original position.

Changes  edit

PYK 2012-12-04: removed update, added jitter parameter
proc jitter {w {dt 200} {jitter 1}}  {
    if [winfo exists $w] {
        regexp {\+(\d+)\+(\d+)} [wm geometry $w] -> x y
        wm geometry $w +[expr $x+$jitter]+[expr $x+$jitter]
        after $dt [list after idle [list jitter $w $dt [expr {-$jitter}]]]
    }
}

#-- Testing
pack [label .l -text "Jitter test"]
jitter .
bind . <Escape> {exec wish $argv0 &; exit}

MG July 9 2005 - Edited the after 1 ... line in the jitter proc, so that it also passes the time, $dt. Otherwise, using anything but the default only had an effect the first time, and was afterwards ignored.

LWS - Oct 9 2005 - Parameterized - so you can stop the thing, too!
proc jitter {w {msdelay 100} {times -1} {dx 2} {dy 2}} {
    if { [winfo exists $w] } {
        #regexp {\+(\d+)\+(\d+)} [wm geometry $w] -> x y
        scan [wm geometry $w] "%*dx%*d+%d+%d" x y
        wm geometry $w +[expr {$x+$dx}]+[expr {$y+$dy}]
        if {$times || $times < 0} {
            after $msdelay [list after idle [list jitter $w $msdelay [expr {$times - 1}] \
                [expr {-$dx}] [expr {-$dy}]]]
        }
    }
}

MG Made some changes to speed it up a little:

  • Use scan instead of regexp to parse the results of wm geometry
  • Braced the expr calls - the last goes down from 7 ms to 0 ms, as reported by time, just by being braced!
  • Braced the first if, for the same reason

PT Believe it or not, this is used in the MSN chat client. People can cause the other users window to shake to catch their attention. The above code might be handy in aMSN which is a Tcl implementation of MSN chat.