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>
main^2^2
Mark Levedahl 2025-09-22 13:15:49 -04:00
parent 830c4578cd
commit c0932eda80
1 changed files with 53 additions and 1 deletions

54
gitk
View File

@ -11735,9 +11735,32 @@ proc prefspage_general {notebook} {
proc prefspage_colors {notebook} {
global bgcolor fgcolor ctext diffcolors selectbgcolor markbgcolor
global diffbgcolors
global themeloader

set page [create_prefs_page $notebook.colors]

ttk::label $page.themesel -font mainfontbold \
-text [mc "Themes - change requires restart"]
grid $page.themesel - -sticky w -pady 10
ttk::label $page.themelabel -text [mc "Theme to use after restart"]
makedroplist $page.theme theme {*}[lsort [ttk::style theme names]]
grid x $page.themelabel $page.theme -sticky w

ttk::entry $page.tloadvar -textvariable themeloader
ttk::frame $page.tloadframe
ttk::label $page.tloadframe.l -text [mc "Theme definition file"]
ttk::button $page.tloadframe.b -text [mc "Choose..."] \
-command [list choose_themeloader $page]
pack $page.tloadframe.l $page.tloadframe.b -side left -padx 2
pack configure $page.tloadframe.l -padx 0
grid x $page.tloadframe $page.tloadvar -sticky ew

ttk::label $page.themelabel2 -text \
[mc "The theme definition file may affect all themes."]
ttk::button $page.themebut2 -text [mc "Apply theme"] \
-command [list updatetheme $page]
grid x $page.themebut2 $page.themelabel2 -sticky w

ttk::label $page.cdisp -text [mc "Colors: press to choose"] -font mainfontbold
grid $page.cdisp - -sticky w -pady 10
label $page.bg -padx 40 -relief sunk -background $bgcolor
@ -11878,6 +11901,34 @@ proc run_themeloader {f} {
return [dict get $::_themefiles_seen $fn]
}

proc updatetheme {prefspage {dotheme 1}} {
global theme
global themeloader
if {$themeloader ne {}} {
if {![run_themeloader $themeloader]} {
set themeloader {}
return
} else {
$prefspage.theme configure -values \
[lsort [ttk::style theme names]]
}
}
if {$dotheme} {
ttk::style theme use $theme
set_gui_colors
prefspage_set_colorswatches $prefspage
}
}

proc choose_themeloader {prefspage} {
global themeloader
set tfile [tk_getOpenFile -title [mc "Gitk: select theme definition"] -multiple false]
if {$tfile ne {}} {
set themeloader $tfile
updatetheme $prefspage 0
}
}

proc choosecolor {v vi prefspage x} {
global $v

@ -11949,6 +12000,7 @@ proc prefscan {} {
catch {destroy $prefstop}
unset prefstop
fontcan
setttkstyle
set_gui_colors
}

@ -12759,7 +12811,7 @@ set nullid2 "0000000000000000000000000000000000000001"
set nullfile "/dev/null"

if {[file exists $themeloader]} {
if {[run_themeloader $themeloader] == 0} {
if {![run_themeloader $themeloader]} {
puts stderr "Could not interpret themeloader: $themeloader"
exit 1
}