gitk: add theme selection to color configuration page

gitk allows configuring a particular theme in its configuration file (default on linux: ~/.config/git/gitk), but offers no ability to modify this from gitk's configuration editor. Let's add this to the color configuration page. Present the offered themes in a list, and allow choosing / modifying a theme definition file ($themeloader). Update the list of themes if the theme file is modified, and update the theme if specifically requested (by default, just change the value for use after gitk is restarted). Any theme definition file can change the global options database, affecting potentially any theme. So, the ultimate configuration should have either - no theme definition file (themeloader = {}), and a native Tk, theme, or - themeloader naming a valid file, and $theme naming a theme defined by that file. But, there is no trivial way to enforce the above. Shrug. Helped-by: Johannes Sixt <j6t@kdbg.org> Signed-off-by: Mark Levedahl <mlevedahl@gmail.com>

Mark Levedahl committed Sep 22, 2025 at 13:15 UTC c0932eda80bce73a72d73ded74a5e618c520afb2
1 file changed +53 -1
gitk
+53 -1
@@ -11735,9 +11735,32 @@ proc prefspage_general {notebook} {
11735 proc prefspage_colors {notebook} {
11736 global bgcolor fgcolor ctext diffcolors selectbgcolor markbgcolor
11737 global diffbgcolors
11738 + global themeloader
11739
11740 set page [create_prefs_page $notebook.colors]
11741
11742 + ttk::label $page.themesel -font mainfontbold \
11743 + -text [mc "Themes - change requires restart"]
11744 + grid $page.themesel - -sticky w -pady 10
11745 + ttk::label $page.themelabel -text [mc "Theme to use after restart"]
11746 + makedroplist $page.theme theme {*}[lsort [ttk::style theme names]]
11747 + grid x $page.themelabel $page.theme -sticky w
11748 +
11749 + ttk::entry $page.tloadvar -textvariable themeloader
11750 + ttk::frame $page.tloadframe
11751 + ttk::label $page.tloadframe.l -text [mc "Theme definition file"]
11752 + ttk::button $page.tloadframe.b -text [mc "Choose..."] \
11753 + -command [list choose_themeloader $page]
11754 + pack $page.tloadframe.l $page.tloadframe.b -side left -padx 2
11755 + pack configure $page.tloadframe.l -padx 0
11756 + grid x $page.tloadframe $page.tloadvar -sticky ew
11757 +
11758 + ttk::label $page.themelabel2 -text \
11759 + [mc "The theme definition file may affect all themes."]
11760 + ttk::button $page.themebut2 -text [mc "Apply theme"] \
11761 + -command [list updatetheme $page]
11762 + grid x $page.themebut2 $page.themelabel2 -sticky w
11763 +
11764 ttk::label $page.cdisp -text [mc "Colors: press to choose"] -font mainfontbold
11765 grid $page.cdisp - -sticky w -pady 10
11766 label $page.bg -padx 40 -relief sunk -background $bgcolor
@@ -11878,6 +11901,34 @@ proc run_themeloader {f} {
11901 return [dict get $::_themefiles_seen $fn]
11902 }
11903
11904 +proc updatetheme {prefspage {dotheme 1}} {
11905 + global theme
11906 + global themeloader
11907 + if {$themeloader ne {}} {
11908 + if {![run_themeloader $themeloader]} {
11909 + set themeloader {}
11910 + return
11911 + } else {
11912 + $prefspage.theme configure -values \
11913 + [lsort [ttk::style theme names]]
11914 + }
11915 + }
11916 + if {$dotheme} {
11917 + ttk::style theme use $theme
11918 + set_gui_colors
11919 + prefspage_set_colorswatches $prefspage
11920 + }
11921 +}
11922 +
11923 +proc choose_themeloader {prefspage} {
11924 + global themeloader
11925 + set tfile [tk_getOpenFile -title [mc "Gitk: select theme definition"] -multiple false]
11926 + if {$tfile ne {}} {
11927 + set themeloader $tfile
11928 + updatetheme $prefspage 0
11929 + }
11930 +}
11931 +
11932 proc choosecolor {v vi prefspage x} {
11933 global $v
11934
@@ -11949,6 +12000,7 @@ proc prefscan {} {
12000 catch {destroy $prefstop}
12001 unset prefstop
12002 fontcan
12003 + setttkstyle
12004 set_gui_colors
12005 }
12006
@@ -12759,7 +12811,7 @@ set nullid2 "0000000000000000000000000000000000000001"
12811 set nullfile "/dev/null"
12812
12813 if {[file exists $themeloader]} {
12762 - if {[run_themeloader $themeloader] == 0} {
12814 + if {![run_themeloader $themeloader]} {
12815 puts stderr "Could not interpret themeloader: $themeloader"
12816 exit 1
12817 }