Commit

8676efdc43b2b9fc1531954b8e7978f23b4b9bcc

gitk: condense repetitive code around color buttons into foreach loops

The color selection elements in the Preferences dialog are set up in a
very uniform manner. The code doing that has grown in the past by simply
modifying a copy of a paragraph. Extract the varying parts into a list
and then operate the repeating parts in a foreach loop. This helps a
later change where we want to set up the UI elements in a different way.

Change the UI names "markbgsep" and "selbgsep" to drop the "sep", which
has obviously been left over by accident when "hunksep" was copied.

Change the suffix "but" to "btn", which resonates a bit better when
reading the code.

Signed-off-by: Johannes Sixt <j6t@kdbg.org>

Johannes Sixt <j6t@kdbg.org> · 2026-08-03 10:58 UTC

Browse files
1 file changed, +55 −59
modified gitk
@@ -11832,55 +11832,46 @@ proc prefspage_colors {notebook} {
11832 11832
11833 11833 ttk::label $page.cdisp -text [mc "Colors: press to choose"] -font mainfontbold
11834 11834 grid $page.cdisp - -sticky w -pady 10
11835 - label $page.bg -padx 40 -relief sunk
11836 - ttk::button $page.bgbut -text [mc "Background"] \
11837 - -command [list choosecolor bgcolor {} $page [mc "background"]]
11838 - grid x $page.bgbut $page.bg -sticky w
11839 -
11840 - label $page.fg -padx 40 -relief sunk
11841 - ttk::button $page.fgbut -text [mc "Foreground"] \
11842 - -command [list choosecolor fgcolor {} $page [mc "foreground"]]
11843 - grid x $page.fgbut $page.fg -sticky w
11844 -
11845 - label $page.diffold -padx 40 -relief sunk
11846 - ttk::button $page.diffoldbut -text [mc "Diff: old lines"] \
11847 - -command [list choosecolor diffcolors 0 $page [mc "diff old lines"]]
11848 - grid x $page.diffoldbut $page.diffold -sticky w
11849 -
11850 - label $page.diffoldbg -padx 40 -relief sunk
11851 - ttk::button $page.diffoldbgbut -text [mc "Diff: old lines bg"] \
11852 - -command [list choosecolor diffbgcolors 0 $page [mc "diff old lines bg"]]
11853 - grid x $page.diffoldbgbut $page.diffoldbg -sticky w
11854 -
11855 - label $page.diffnew -padx 40 -relief sunk
11856 - ttk::button $page.diffnewbut -text [mc "Diff: new lines"] \
11857 - -command [list choosecolor diffcolors 1 $page [mc "diff new lines"]]
11858 - grid x $page.diffnewbut $page.diffnew -sticky w
11859 -
11860 - label $page.diffnewbg -padx 40 -relief sunk
11861 - ttk::button $page.diffnewbgbut -text [mc "Diff: new lines bg"] \
11862 - -command [list choosecolor diffbgcolors 1 $page [mc "diff new lines bg"]]
11863 - grid x $page.diffnewbgbut $page.diffnewbg -sticky w
11864 -
11865 - label $page.hunksep -padx 40 -relief sunk
11866 - ttk::button $page.hunksepbut -text [mc "Diff: hunk header"] \
11867 - -command [list choosecolor diffcolors 2 $page [mc "diff hunk header"]]
11868 - grid x $page.hunksepbut $page.hunksep -sticky w
11869 -
11870 - label $page.markbgsep -padx 40 -relief sunk
11871 - ttk::button $page.markbgbut -text [mc "Marked line bg"] \
11872 - -command [list choosecolor markbgcolor {} $page [mc "marked line background"]]
11873 - grid x $page.markbgbut $page.markbgsep -sticky w
11874 -
11875 - label $page.selbgsep -padx 40 -relief sunk
11876 - ttk::button $page.selbgbut -text [mc "Select bg"] \
11877 - -command [list choosecolor selectbgcolor {} $page [mc "background"]]
11878 - grid x $page.selbgbut $page.selbgsep -sticky w
11879 -
11880 - label $page.linkfg -padx 40 -relief sunk
11881 - ttk::button $page.linkfgbut -text [mc "Link"] \
11882 - -command [list choosecolor linkfgcolor {} $page [mc "link"]]
11883 - grid x $page.linkfgbut $page.linkfg -sticky w
11835 +
11836 + set coloruielems [list \
11837 + bg bgcolor {} \
11838 + [mc "Background"] \
11839 + [mc "background"] \
11840 + fg fgcolor {} \
11841 + [mc "Foreground"] \
11842 + [mc "foreground"] \
11843 + diffold diffcolors 0 \
11844 + [mc "Diff: old lines"] \
11845 + [mc "diff old lines"] \
11846 + diffoldbg diffbgcolors 0 \
11847 + [mc "Diff: old lines bg"] \
11848 + [mc "diff old lines bg"] \
11849 + diffnew diffcolors 1 \
11850 + [mc "Diff: new lines"] \
11851 + [mc "diff new lines"] \
11852 + diffnewbg diffbgcolors 1 \
11853 + [mc "Diff: new lines bg"] \
11854 + [mc "diff new lines bg"] \
11855 + hunksep diffcolors 2 \
11856 + [mc "Diff: hunk header"] \
11857 + [mc "diff hunk header"] \
11858 + markbg markbgcolor {} \
11859 + [mc "Marked line bg"] \
11860 + [mc "marked line background"] \
11861 + selbg selectbgcolor {} \
11862 + [mc "Select bg"] \
11863 + [mc "background"] \
11864 + linkfg linkfgcolor {} \
11865 + [mc "Link"] \
11866 + [mc "link"] \
11867 + ]
11868 +
11869 + foreach {uielem colorvar idx label title} $coloruielems {
11870 + label $page.$uielem -padx 40 -relief sunk
11871 + ttk::button $page.${uielem}btn -text $label \
11872 + -command [list choosecolor $colorvar $idx $page $title]
11873 + grid x $page.${uielem}btn $page.$uielem -sticky w
11874 + }
11884 11875
11885 11876 grid columnconfigure $page 2 -weight 1
11886 11877 prefspage_set_colorswatches $page
@@ -11892,16 +11883,21 @@ proc prefspage_set_colorswatches {page} {
11892 11883 global bgcolor fgcolor diffcolors selectbgcolor markbgcolor
11893 11884 global diffbgcolors linkfgcolor
11894 11885
11895 - $page.bg configure -background $bgcolor
11896 - $page.fg configure -background $fgcolor
11897 - $page.diffold configure -background [lindex $diffcolors 0]
11898 - $page.diffoldbg configure -background [lindex $diffbgcolors 0]
11899 - $page.diffnew configure -background [lindex $diffcolors 1]
11900 - $page.diffnewbg configure -background [lindex $diffbgcolors 1]
11901 - $page.hunksep configure -background [lindex $diffcolors 2]
11902 - $page.markbgsep configure -background $markbgcolor
11903 - $page.selbgsep configure -background $selectbgcolor
11904 - $page.linkfg configure -background $linkfgcolor
11886 + set coloruielems [list \
11887 + bg $bgcolor \
11888 + fg $fgcolor \
11889 + diffold [lindex $diffcolors 0] \
11890 + diffoldbg [lindex $diffbgcolors 0] \
11891 + diffnew [lindex $diffcolors 1] \
11892 + diffnewbg [lindex $diffbgcolors 1] \
11893 + hunksep [lindex $diffcolors 2] \
11894 + markbg $markbgcolor \
11895 + selbg $selectbgcolor \
11896 + linkfg $linkfgcolor \
11897 + ]
11898 + foreach {uielem color} $coloruielems {
11899 + $page.$uielem configure -background $color
11900 + }
11905 11901 }
11906 11902
11907 11903 proc prefspage_fonts {notebook} {