git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v3] git-gui: use gray background for inactive text widgets

From
Stefan Haller <stefan@haller-berlin.de>
Date
Dec 18, 2020, 09:43 UTC
Message-ID
<20201218094314.22138-1-stefan@haller-berlin.de>
In-Reply-To
<20201217214912.ycp7bidcyqwzslxy@yadavpratyush.com>

This makes it easier to see at a glance which of the four main views has the keyboard focus.

Signed-off-by: Stefan Haller <stefan@haller-berlin.de>
---
Hi Pratyush,

here's a rerolled version with added Signed-off-by and using local variables for inactive_select_bg and inactive_select_bg, hoping this might save you some time. Let me know if this is what you had in mind, I wasn't totally sure.

 git-gui.sh     | 18 ++++++++++++------
 lib/themed.tcl | 35 +++++++++++++++++++++++++----------
 2 files changed, 37 insertions(+), 16 deletions(-)
diff --git a/git-gui.sh b/git-gui.sh
index cc6c2aa..201524c 100755
--- a/git-gui.sh
+++ b/git-gui.sh
@@ -720,9 +720,6 @@ proc rmsel_tag {text} {
 		-background [$text cget -background] \
 		-foreground [$text cget -foreground] \
 		-borderwidth 0
-	$text tag conf in_sel\
-		-background $color::select_bg \
-		-foreground $color::select_fg
 	bind $text <Motion> break
 	return $text
 }
@@ -3328,11 +3325,20 @@ if {!$use_ttk} {
 	.vpane.files paneconfigure .vpane.files.index -sticky news
 }

+proc set_selection_colors {w has_focus} {
+	foreach tag [list in_diff in_sel] {
+		$w tag conf $tag \
+			-background [expr {$has_focus ? $color::select_bg : $color::inactive_select_bg}] \
+			-foreground [expr {$has_focus ? $color::select_fg : $color::inactive_select_fg}]
+	}
+}
+
 foreach i [list $ui_index $ui_workdir] {
 	rmsel_tag $i
-	$i tag conf in_diff \
-		-background $color::select_bg \
-		-foreground $color::select_fg
+
+	set_selection_colors $i 0
+	bind $i <FocusIn>	{ set_selection_colors %W 1 }
+	bind $i <FocusOut>	{ set_selection_colors %W 0 }
 }
 unset i

diff --git a/lib/themed.tcl b/lib/themed.tcl
index 244c061..0f6575a 100644
--- a/lib/themed.tcl
+++ b/lib/themed.tcl
@@ -6,19 +6,25 @@ namespace eval color {
 	# Variable colors
 	# Preffered way to set widget colors is using add_option.
 	# In some cases, like with tags in_diff/in_sel, we use these colors.
-	variable select_bg		lightgray
-	variable select_fg		black
+	variable select_bg				lightgray
+	variable select_fg				black
+	variable inactive_select_bg		lightgray
+	variable inactive_select_fg		black

 	proc sync_with_theme {} {
-		set base_bg		[ttk::style lookup . -background]
-		set base_fg		[ttk::style lookup . -foreground]
-		set text_bg		[ttk::style lookup Treeview -background]
-		set text_fg		[ttk::style lookup Treeview -foreground]
-		set select_bg	[ttk::style lookup Default -selectbackground]
-		set select_fg	[ttk::style lookup Default -selectforeground]
+		set base_bg		        [ttk::style lookup . -background]
+		set base_fg		        [ttk::style lookup . -foreground]
+		set text_bg		        [ttk::style lookup Treeview -background]
+		set text_fg		        [ttk::style lookup Treeview -foreground]
+		set select_bg	        [ttk::style lookup Default -selectbackground]
+		set select_fg	        [ttk::style lookup Default -selectforeground]
+		set inactive_select_bg	[convert_rgb_to_gray $select_bg]
+		set inactive_select_fg 	$select_fg

 		set color::select_bg $select_bg
 		set color::select_fg $select_fg
+		set color::inactive_select_bg $inactive_select_bg
+		set color::inactive_select_fg $inactive_select_fg

 		proc add_option {key val} {
 			option add $key $val widgetDefault
@@ -36,11 +42,20 @@ namespace eval color {
 		add_option *Text.Foreground $text_fg
 		add_option *Text.selectBackground $select_bg
 		add_option *Text.selectForeground $select_fg
-		add_option *Text.inactiveSelectBackground $select_bg
-		add_option *Text.inactiveSelectForeground $select_fg
+		add_option *Text.inactiveSelectBackground $inactive_select_bg
+		add_option *Text.inactiveSelectForeground $inactive_select_fg
 	}
 }

+proc convert_rgb_to_gray {rgb} {
+	# Simply take the average of red, green and blue. This wouldn't be good
+	# enough for, say, converting a photo to grayscale, but for this simple
+	# purpose of approximating the brightness of a color it's good enough.
+	lassign [winfo rgb . $rgb] r g b
+	set gray [expr {($r / 256 + $g / 256 + $b / 256) / 3}]
+	return [format "#%2.2X%2.2X%2.2X" $gray $gray $gray]
+}
+
 proc ttk_get_current_theme {} {
 	# Handle either current Tk or older versions of 8.5
 	if {[catch {set theme [ttk::style theme use]}]} {
--
2.29.0.21.g59e7d82785.dirty
Previous: Stefan HallerNext: Pratyush Yadav
Message 30 of 37 in “git-gui: Basic dark mode support”
  1. git-gui: Basic dark mode supportSerg Tereshchenko, Aug 24, 2020
  2. Matthias AßhauerAug 25, 2020
  3. Pratyush YadavSep 22, 2020
  4. git-gui: Basic dark mode supportSerg Tereshchenko, Sep 26, 2020
  5. Pratyush YadavOct 7, 2020
  6. Serg TereshchenkoOct 8, 2020
  7. Pratyush YadavOct 8, 2020
  8. Stefan HallerNov 21, 2020
  9. serg.partizan@gmail.comNov 22, 2020
  10. git-gui: Fix selected text colorsSerg Tereshchenko, Nov 22, 2020
  11. Stefan HallerNov 22, 2020
  12. serg.partizan@gmail.comNov 22, 2020
  13. git-gui: use gray selection background for inactive text widgetsStefan Haller, Nov 23, 2020
  14. serg.partizan@gmail.comNov 23, 2020
  15. Stefan HallerNov 23, 2020
  16. serg.partizan@gmail.comNov 23, 2020
  17. Stefan HallerNov 29, 2020
  18. serg.partizan@gmail.comNov 30, 2020
  19. Pratyush YadavNov 30, 2020
  20. Stefan HallerNov 30, 2020
  21. git-gui: keep showing selection when diff view gets deactivated on MacStefan Haller, Nov 30, 2020
  22. Stefan HallerNov 23, 2020
  23. serg.partizan@gmail.comNov 23, 2020
  24. Stefan HallerNov 24, 2020
  25. git-gui: use gray background for inactive text widgetsStefan Haller, Nov 24, 2020
  26. Pratyush YadavDec 17, 2020
  27. Stefan HallerDec 17, 2020
  28. Pratyush YadavDec 18, 2020
  29. Stefan HallerDec 18, 2020
  30. git-gui: use gray background for inactive text widgetsStefan Haller, Dec 18, 2020
  31. Pratyush YadavDec 18, 2020
  32. Pratyush YadavDec 18, 2020
  33. Pratyush YadavDec 17, 2020
  34. Pratyush YadavOct 7, 2020
  35. Serg TereshchenkoOct 8, 2020
  36. Pratyush YadavOct 8, 2020
  37. Serg TereshchenkoOct 8, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.