Skip to content

Commit 7b0cfe1

Browse files
committed
Merge branch 'sh/inactive-background'
Set a different background color for selections in inactive widgets. This inactive color is calculated from the current theme colors to make sure it works for all themes. * sh/inactive-background: git-gui: use gray background for inactive text widgets
2 parents 62aed98 + da4d86d commit 7b0cfe1

File tree

2 files changed

+37
-16
lines changed

2 files changed

+37
-16
lines changed

git-gui.sh

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -720,9 +720,6 @@ proc rmsel_tag {text} {
720720
-background [$text cget -background] \
721721
-foreground [$text cget -foreground] \
722722
-borderwidth 0
723-
$text tag conf in_sel\
724-
-background $color::select_bg \
725-
-foreground $color::select_fg
726723
bind $text <Motion> break
727724
return $text
728725
}
@@ -3328,11 +3325,20 @@ if {!$use_ttk} {
33283325
.vpane.files paneconfigure .vpane.files.index -sticky news
33293326
}
33303327
3328+
proc set_selection_colors {w has_focus} {
3329+
foreach tag [list in_diff in_sel] {
3330+
$w tag conf $tag \
3331+
-background [expr {$has_focus ? $color::select_bg : $color::inactive_select_bg}] \
3332+
-foreground [expr {$has_focus ? $color::select_fg : $color::inactive_select_fg}]
3333+
}
3334+
}
3335+
33313336
foreach i [list $ui_index $ui_workdir] {
33323337
rmsel_tag $i
3333-
$i tag conf in_diff \
3334-
-background $color::select_bg \
3335-
-foreground $color::select_fg
3338+
3339+
set_selection_colors $i 0
3340+
bind $i <FocusIn> { set_selection_colors %W 1 }
3341+
bind $i <FocusOut> { set_selection_colors %W 0 }
33363342
}
33373343
unset i
33383344

lib/themed.tcl

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,25 @@ namespace eval color {
66
# Variable colors
77
# Preffered way to set widget colors is using add_option.
88
# In some cases, like with tags in_diff/in_sel, we use these colors.
9-
variable select_bg lightgray
10-
variable select_fg black
9+
variable select_bg lightgray
10+
variable select_fg black
11+
variable inactive_select_bg lightgray
12+
variable inactive_select_fg black
1113

1214
proc sync_with_theme {} {
13-
set base_bg [ttk::style lookup . -background]
14-
set base_fg [ttk::style lookup . -foreground]
15-
set text_bg [ttk::style lookup Treeview -background]
16-
set text_fg [ttk::style lookup Treeview -foreground]
17-
set select_bg [ttk::style lookup Default -selectbackground]
18-
set select_fg [ttk::style lookup Default -selectforeground]
15+
set base_bg [ttk::style lookup . -background]
16+
set base_fg [ttk::style lookup . -foreground]
17+
set text_bg [ttk::style lookup Treeview -background]
18+
set text_fg [ttk::style lookup Treeview -foreground]
19+
set select_bg [ttk::style lookup Default -selectbackground]
20+
set select_fg [ttk::style lookup Default -selectforeground]
21+
set inactive_select_bg [convert_rgb_to_gray $select_bg]
22+
set inactive_select_fg $select_fg
1923

2024
set color::select_bg $select_bg
2125
set color::select_fg $select_fg
26+
set color::inactive_select_bg $inactive_select_bg
27+
set color::inactive_select_fg $inactive_select_fg
2228

2329
proc add_option {key val} {
2430
option add $key $val widgetDefault
@@ -36,11 +42,20 @@ namespace eval color {
3642
add_option *Text.Foreground $text_fg
3743
add_option *Text.selectBackground $select_bg
3844
add_option *Text.selectForeground $select_fg
39-
add_option *Text.inactiveSelectBackground $select_bg
40-
add_option *Text.inactiveSelectForeground $select_fg
45+
add_option *Text.inactiveSelectBackground $inactive_select_bg
46+
add_option *Text.inactiveSelectForeground $inactive_select_fg
4147
}
4248
}
4349

50+
proc convert_rgb_to_gray {rgb} {
51+
# Simply take the average of red, green and blue. This wouldn't be good
52+
# enough for, say, converting a photo to grayscale, but for this simple
53+
# purpose of approximating the brightness of a color it's good enough.
54+
lassign [winfo rgb . $rgb] r g b
55+
set gray [expr {($r / 256 + $g / 256 + $b / 256) / 3}]
56+
return [format "#%2.2X%2.2X%2.2X" $gray $gray $gray]
57+
}
58+
4459
proc ttk_get_current_theme {} {
4560
# Handle either current Tk or older versions of 8.5
4661
if {[catch {set theme [ttk::style theme use]}]} {

0 commit comments

Comments
 (0)