threads / patch / 64146

patchgitk: make the 'Tags and Heads' window geometry sticky

Subject: [PATCH] gitk: make the 'Tags and Heads' window geometry sticky

## tl;dr

2 messages between Sep 13, 2025 and Sep 18, 2025. Diffs are folded; open one to read it.

replies: 1people: 2as markdown or json

Michael Rappazzo· Sep 13, 2025, 18:10 UTC · lore

Currently, the Tags and Heads window always opens at a default position and size, requiring users to reposition it each time.

This change saves and restores the Tags and Heads window size and position relative to the main gitk window. The geometry is stored in the config file as `geometry(showrefs)` and persists between gitk sessions. The window position is stored relative to the main window, so it maintains the same spatial relationship when the main window is moved or when gitk is restarted on different monitors.

Signed-off-by: Michael Rappazzo <rappazzo@gmail.com>
---
 gitk | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 73 insertions(+), 2 deletions(-)
Show changes to gitk +73 −2
diff --git a/gitk b/gitk
index 6e4d71d585..49b2ceefc0 100755
--- a/gitk
+++ b/gitk
@@ -3114,6 +3114,19 @@ proc savestuff {w} {
         puts $f "set geometry(pwsash1) \"[.tf.histframe.pwclist sashpos 1] 1\""
         puts $f "set geometry(botwidth) [winfo width .bleft]"
         puts $f "set geometry(botheight) [winfo height .bleft]"
+        if {[winfo exists .showrefs]} {
+            set geom [parse_geometry [wm geometry .showrefs]]
+            set main_geom [parse_geometry [wm geometry .]]
+            if {[llength $geom] == 4 && [llength $main_geom] == 4} {
+                lassign $geom w h x y
+                lassign $main_geom mw mh mx my
+                set rel_x [expr {$x - $mx}]
+                set rel_y [expr {$y - $my}]
+                puts $f "set geometry(showrefs) \"${w}x${h}+${rel_x}+${rel_y}\""
+            }
+        } elseif {[info exists geometry(showrefs)]} {
+            puts $f "set geometry(showrefs) \"$geometry(showrefs)\""
+        }
 
         array set view_save {}
         array set views {}
@@ -10207,11 +10220,13 @@ proc showrefs {} {
     if {[winfo exists $top]} {
         raise $top
         refill_reflist
+        wm protocol $top WM_DELETE_WINDOW [list destroy_showrefs $top]
         return
     }
     ttk_toplevel $top
     wm title $top [mc "Tags and heads: %s" [file tail [pwd]]]
     make_transient $top .
+    wm protocol $top WM_DELETE_WINDOW [list destroy_showrefs $top]
     text $top.list -background $bgcolor -foreground $fgcolor \
         -selectbackground $selectbgcolor -font mainfont \
         -xscrollcommand "$top.xsb set" -yscrollcommand "$top.ysb set" \
@@ -10237,8 +10252,8 @@ proc showrefs {} {
     ttk::checkbutton $top.sort -text [mc "Sort refs by type"] \
         -variable sortrefsbytype -command {refill_reflist}
     grid $top.sort - -sticky w -pady 2
-    ttk::button $top.close -command [list destroy $top] -text [mc "Close"]
-    bind $top <Key-Escape> [list destroy $top]
+    ttk::button $top.close -command [list destroy_showrefs $top] -text [mc "Close"]
+    bind $top <Key-Escape> [list destroy_showrefs $top]
     grid $top.close -
     grid columnconfigure $top 0 -weight 1
     grid rowconfigure $top 0 -weight 1
@@ -10247,6 +10262,7 @@ proc showrefs {} {
     bind $top.list <ButtonRelease-1> {sel_reflist %W %x %y; break}
     set reflist {}
     refill_reflist
+    bind $top <Map> [list restore_showrefs_geometry $top]
 }
 
 proc sel_reflist {w x y} {
@@ -10279,6 +10295,61 @@ proc reflistfilter_change {n1 n2 op} {
     after 200 refill_reflist
 }
 
+proc parse_geometry {geom_string} {
+    if {[regexp {^(\d+)x(\d+)\+(-?\d+)\+(-?\d+)$} $geom_string -> w h x y]} {
+        return [list $w $h $x $y]
+    }
+    return {}
+}
+
+proc save_showrefs_geometry {top} {
+    global geometry
+
+    if {![winfo exists $top] || ![winfo exists .]} return
+
+    set geom [parse_geometry [wm geometry $top]]
+    set main_geom [parse_geometry [wm geometry .]]
+    if {[llength $geom] == 4 && [llength $main_geom] == 4} {
+        lassign $geom w h x y
+        lassign $main_geom mw mh mx my
+        set rel_x [expr {$x - $mx}]
+        set rel_y [expr {$y - $my}]
+        set geometry(showrefs) "${w}x${h}+${rel_x}+${rel_y}"
+    }
+}
+
+proc restore_showrefs_geometry {top} {
+    global geometry
+
+    if {![info exists geometry(showrefs)] || ![winfo exists $top] || ![winfo exists .]} return
+
+    set saved_geom [parse_geometry $geometry(showrefs)]
+    set main_geom [parse_geometry [wm geometry .]]
+    if {[llength $saved_geom] == 4 && [llength $main_geom] == 4} {
+        lassign $saved_geom w h rel_x rel_y
+        lassign $main_geom mw mh mx my
+
+        set abs_x [expr {$mx + $rel_x}]
+        set abs_y [expr {$my + $rel_y}]
+
+        # Ensure window stays on screen
+        set screen_w [winfo screenwidth .]
+        set screen_h [winfo screenheight .]
+        if {$abs_x < 0} { set abs_x 0 }
+        if {$abs_y < 0} { set abs_y 0 }
+        if {$abs_x + $w > $screen_w} { set abs_x [expr {$screen_w - $w}] }
+        if {$abs_y + $h > $screen_h} { set abs_y [expr {$screen_h - $h}] }
+
+        wm geometry $top "${w}x${h}+${abs_x}+${abs_y}"
+    }
+    bind $top <Map> {}
+}
+
+proc destroy_showrefs {top} {
+    save_showrefs_geometry $top
+    destroy $top
+}
+
 proc refill_reflist {} {
     global reflist reflistfilter showrefstop headids tagids otherrefids sortrefsbytype
     global curview upstreamofref
-- 
2.51.0
Johannes Sixt· Sep 18, 2025, 17:57 UTC · re: Michael Rappazzo · lore

Re: [PATCH] gitk: make the 'Tags and Heads' window geometry sticky

Am 13.09.25 um 20:10 schrieb Michael Rappazzo:
Show 9 quoted lines
> Currently, the Tags and Heads window always opens at a default position
> and size, requiring users to reposition it each time. 
> 
> This change saves and restores the Tags and Heads window size and position
> relative to the main gitk window. The geometry is stored in the config file
> as `geometry(showrefs)` and persists between gitk sessions. The window
> position is stored relative to the main window, so it maintains the same
> spatial relationship when the main window is moved or when gitk is restarted
> on different monitors.

Thank you for this patch. In general, I like the direction this is going. I am baffled, though, by the sheer number of lines that must be added to achieve the goal.

I tested the patch Windows and Linux (KDE), and found some deficiencies on both.

During a session, on Windows, size and position are retained and on Linux, only the size is retained (the position is always the default). In both environments, persistence across session happens only when Gitk is closed while the Tags and Heads window is open, but not when it is closed via any of the means available.

To reduce the code a bit, would it be possible to set the global geometry(showrefs) from a handler of the Configure event? Then it does not have to be set on any other occasion.

Show 5 quoted lines
> 
> Signed-off-by: Michael Rappazzo <rappazzo@gmail.com>
> ---
>  gitk | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
>  1 file changed, 73 insertions(+), 2 deletions(-)
Show 6 quoted lines
> +proc parse_geometry {geom_string} {
> +    if {[regexp {^(\d+)x(\d+)\+(-?\d+)\+(-?\d+)$} $geom_string -> w h x y]} {
> +        return [list $w $h $x $y]
> +    }
> +    return {}
> +}

Are there any occasions where it is expected that the regular expression does not match? If not, let's fail with an error. Then we don't have to verify the return value at the call sites.

Show 21 quoted lines
> +proc restore_showrefs_geometry {top} {
> +    global geometry
> +
> +    if {![info exists geometry(showrefs)] || ![winfo exists $top] || ![winfo exists .]} return
> +
> +    set saved_geom [parse_geometry $geometry(showrefs)]
> +    set main_geom [parse_geometry [wm geometry .]]
> +    if {[llength $saved_geom] == 4 && [llength $main_geom] == 4} {
> +        lassign $saved_geom w h rel_x rel_y
> +        lassign $main_geom mw mh mx my
> +
> +        set abs_x [expr {$mx + $rel_x}]
> +        set abs_y [expr {$my + $rel_y}]
> +
> +        # Ensure window stays on screen
> +        set screen_w [winfo screenwidth .]
> +        set screen_h [winfo screenheight .]
> +        if {$abs_x < 0} { set abs_x 0 }
> +        if {$abs_y < 0} { set abs_y 0 }
> +        if {$abs_x + $w > $screen_w} { set abs_x [expr {$screen_w - $w}] }
> +        if {$abs_y + $h > $screen_h} { set abs_y [expr {$screen_h - $h}] }

Consider the case where the stored height exceeds $screen_h. The title bar is moved out of the screen due to the order of these corrections. Let's correct the lower and right bounds first, and the upper and left bounds second. Then the title bar stays on the screen.

Show 5 quoted lines
> +
> +        wm geometry $top "${w}x${h}+${abs_x}+${abs_y}"
> +    }
> +    bind $top <Map> {}
> +}
-- Hannes

← back to recent threads