beetlebug▾
/

Visual diffs

A visual diff compares a window before and after a change. It gives the changed pixels, a picture of them, and the widgets that moved, changed size, or changed an option. Use it when a change of code moves a widget by a few pixels, and you cannot see where.

The bug: a field moved

The order form of examples/debug/order.tcl has three fields in a grid. A change of the code gave the field Quantity more padding at its left:

grid configure .f.e2 -padx {6 0}

The window looks almost the same. examples/debug/visual-diff.tcl keeps a mark of the window, makes the change, and asks for the diff:

source [file join [file dirname [info script]] order.tcl]
update
ztk::debug mark before
grid configure .f.e2 -padx {6 0}
update
set d [ztk::debug diff before -image]

ztk --headless --scale 2 examples/debug/visual-diff.tcl out prints these lines and writes the three pictures into the folder out. The script prints each item of moved on its own line:

pixels 9142
box 64 0 162 186
size {440 372} {452 372}
moved . {0 0 220 186} {0 0 226 186}
moved .f {0 0 220 186} {0 0 226 186}
moved .f.e2 {64 73 144 29} {70 73 144 29}
moved .f.save {151 145 57 31} {157 145 57 31}
The order form before the change
Before: the mark.
The order form after the change
After: Quantity is 6 pixels to the right.
The changed pixels in red on a pale copy of the form
The diff: each changed pixel is red.

Read the answer from the widgets to the pixels:

box is in logical pixels. size and pixels count device pixels, so at --scale 2 they are twice the logical size.

The answer of ztk::debug diff

Key What it has
pixels The count of device pixels that changed.
box The bounding box of the changed pixels in logical pixels: x, y, width, height.
size The old and the new size of the image in device pixels, when it changed.
added, removed The widgets that are new, and the widgets that are gone.
moved Each widget whose box changed: the path, the old box, and the new box.
mapped Each widget whose map state changed: the path, the old state, and the new state.
options Each option whose text changed: the path, the option, the old text, and the new text.
image With -image: a PNG of the new image as base64 text. A changed pixel is red, and each other pixel is a pale copy.

The answer has only the keys with a change. The answer pixels 0 with no other key means that the image and the widgets are the same. -tolerance n counts a pixel only when a color channel changed by more than n (0 to 255, default 0). ztk::debug marks gives the names of the marks, and ztk::debug forget name removes one.

ztk::debug mark name ?path? keeps the image of the toplevel of path (default .) and the state of each widget in it: the class, the box, the map state, and the text of each option. ztk::debug changes name gives the same answer as diff, as lines for a person. This answer is from a run at --scale 1:

size 220x186 -> 226x186
2396 pixels changed in 64,0 162x186
moved . 0,0 220x186 -> 0,0 226x186
moved .f 0,0 220x186 -> 0,0 226x186
moved .f.e2 64,73 144x29 -> 70,73 144x29
moved .f.save 151,145 57x31 -> 157,145 57x31

To save the diff picture, decode the image key:

set f [open diff.png wb]
puts -nonewline $f [binary decode base64 [dict get $d image]]
close $f

In a test: a golden picture

A golden picture is a PNG file of the window that a person checked. A test compares the window with that file. examples/debug/golden.tcl does it with the visual diff:

  1. ztk::snapshot . gives the window as a PNG.
  2. A label in a second toplevel shows the golden picture, and ztk::debug mark keeps it.
  3. The same label then shows the new picture, and ztk::debug diff compares the two.
set now [ztk::snapshot .]
set f [open golden-order.png rb]
image create photo pic -data [read $f]
close $f
toplevel .cmp
label .cmp.pic -image pic -borderwidth 0 -padx 0 -pady 0
pack .cmp.pic
update
ztk::debug mark golden .cmp
pic configure -data $now
update
set d [ztk::debug diff golden -tolerance 24]
if {[dict get $d pixels] == 0 && ![dict exists $d size]} {
    puts "ok: the window equals golden-order.png"
} else {
    puts "FAIL: [ztk::debug changes golden]"
    exit 1
}

ztk --headless examples/debug/golden.tcl prints ok: the window equals golden-order.png. With the change of padding (the argument bug), it prints FAIL: 4131 pixels changed in 9,14 202x162 and exits with 1. The argument update writes the golden picture again.

Choose the tolerance with care:

Run the test at one scale. A golden picture at --scale 2 does not equal a window at --scale 1.

From an agent

ztk --mcp app.tcl serves the visual diff as the tools ui_mark and ui_diff (MCP tools). An agent marks the window, makes its change, and reads the diff before it reports the work as done. The four requests of JSON-RPC are in examples/debug/mcp-diff.jsonl:

Request Method or tool Arguments
1 initialize
2 ui_mark {"name":"before"}
3 tcl_eval {"script":"grid configure .f.e2 -padx {6 0}"}
4 ui_diff {"name":"before"}

Request 4 in full is:

{"jsonrpc":"2.0","id":4,"method":"tools/call",
 "params":{"name":"ui_diff","arguments":{"name":"before"}}}

In the file, each request is on one line.

ztk --mcp order.tcl < mcp-diff.jsonl in examples/debug gives the text of ztk::debug changes as the answer of ui_diff. With the argument image set to true, the answer also has the PNG with the changed pixels in red, so an agent that reads images can look at the change.

tests/debug-examples.sh runs each example of this page and checks its lines.