Allow keyboard control to work in the staging widgets.

Keyboard focus was restricted to the commit message widget and users were forced to use the mouse to select files in the workdir widget and only then could use a key combination to stage the file. It is now possible to use key navigation (Ctrl-Tab, arrow keys and Ctrl-T or Ctrl-U) to stage and unstage files. Suggested by @koppor in git-for-window/git issue #859 Signed-off-by: Pat Thoyts <patthoyts@users.sourceforge.net>

Pat Thoyts committed Oct 1, 2016 at 22:04 UTC 088ad75dc279614849f92e5ae0a2b579b26719eb
1 file changed +32 -12
git-gui.sh
+32 -12
@@ -2505,13 +2505,28 @@ proc force_first_diff {after} {
2505 }
2506 }
2507
2508 -proc toggle_or_diff {w x y} {
2508 +proc toggle_or_diff {mode w args} {
2509 global file_states file_lists current_diff_path ui_index ui_workdir
2510 global last_clicked selected_paths
2511
2512 - set pos [split [$w index @$x,$y] .]
2513 - set lno [lindex $pos 0]
2514 - set col [lindex $pos 1]
2512 + if {$mode eq "click"} {
2513 + foreach {x y} $args break
2514 + set pos [split [$w index @$x,$y] .]
2515 + foreach {lno col} $pos break
2516 + } else {
2517 + if {$last_clicked ne {}} {
2518 + set lno [lindex $last_clicked 1]
2519 + } else {
2520 + set lno [expr {int([lindex [$w tag ranges in_diff] 0])}]
2521 + }
2522 + if {$mode eq "toggle"} {
2523 + set col 0; set y 2
2524 + } else {
2525 + incr lno [expr {$mode eq "up" ? -1 : 1}]
2526 + set col 1
2527 + }
2528 + }
2529 +
2530 set path [lindex $file_lists($w) [expr {$lno - 1}]]
2531 if {$path eq {}} {
2532 set last_clicked {}
@@ -2519,6 +2534,7 @@ proc toggle_or_diff {w x y} {
2534 }
2535
2536 set last_clicked [list $w $lno]
2537 + focus $w
2538 array unset selected_paths
2539 $ui_index tag remove in_sel 0.0 end
2540 $ui_workdir tag remove in_sel 0.0 end
@@ -2598,7 +2614,7 @@ proc add_range_to_selection {w x y} {
2614 global file_lists last_clicked selected_paths
2615
2616 if {[lindex $last_clicked 0] ne $w} {
2601 - toggle_or_diff $w $x $y
2617 + toggle_or_diff click $w $x $y
2618 return
2619 }
2620
@@ -3188,6 +3204,7 @@ text $ui_index -background white -foreground black \
3204 -borderwidth 0 \
3205 -width 20 -height 10 \
3206 -wrap none \
3207 + -takefocus 1 -highlightthickness 1\
3208 -cursor $cursor_ptr \
3209 -xscrollcommand {.vpane.files.index.sx set} \
3210 -yscrollcommand {.vpane.files.index.sy set} \
@@ -3208,6 +3225,7 @@ text $ui_workdir -background white -foreground black \
3225 -borderwidth 0 \
3226 -width 20 -height 10 \
3227 -wrap none \
3228 + -takefocus 1 -highlightthickness 1\
3229 -cursor $cursor_ptr \
3230 -xscrollcommand {.vpane.files.workdir.sx set} \
3231 -yscrollcommand {.vpane.files.workdir.sy set} \
@@ -3815,10 +3833,10 @@ bind . <$M1B-Key-r> ui_do_rescan
3833 bind . <$M1B-Key-R> ui_do_rescan
3834 bind . <$M1B-Key-s> do_signoff
3835 bind . <$M1B-Key-S> do_signoff
3818 -bind . <$M1B-Key-t> do_add_selection
3819 -bind . <$M1B-Key-T> do_add_selection
3820 -bind . <$M1B-Key-u> do_unstage_selection
3821 -bind . <$M1B-Key-U> do_unstage_selection
3836 +bind . <$M1B-Key-t> { toggle_or_diff toggle %W }
3837 +bind . <$M1B-Key-T> { toggle_or_diff toggle %W }
3838 +bind . <$M1B-Key-u> { toggle_or_diff toggle %W }
3839 +bind . <$M1B-Key-U> { toggle_or_diff toggle %W }
3840 bind . <$M1B-Key-j> do_revert_selection
3841 bind . <$M1B-Key-J> do_revert_selection
3842 bind . <$M1B-Key-i> do_add_all
@@ -3830,9 +3848,11 @@ bind . <$M1B-Key-plus> {show_more_context;break}
3848 bind . <$M1B-Key-KP_Add> {show_more_context;break}
3849 bind . <$M1B-Key-Return> do_commit
3850 foreach i [list $ui_index $ui_workdir] {
3833 - bind $i <Button-1> "toggle_or_diff $i %x %y; break"
3834 - bind $i <$M1B-Button-1> "add_one_to_selection $i %x %y; break"
3835 - bind $i <Shift-Button-1> "add_range_to_selection $i %x %y; break"
3851 + bind $i <Button-1> { toggle_or_diff click %W %x %y; break }
3852 + bind $i <$M1B-Button-1> { add_one_to_selection %W %x %y; break }
3853 + bind $i <Shift-Button-1> { add_range_to_selection %W %x %y; break }
3854 + bind $i <Key-Up> { toggle_or_diff up %W; break }
3855 + bind $i <Key-Down> { toggle_or_diff down %W; break }
3856 }
3857 unset i
3858