@@ -3,8 +3,10 @@ package gui
33import (
44 "fmt"
55
6+ "github.com/jesseduffield/gocui"
67 "github.com/jesseduffield/lazygit/pkg/gui/context"
78 "github.com/jesseduffield/lazygit/pkg/gui/types"
9+ "github.com/jesseduffield/lazygit/pkg/utils"
810 "github.com/samber/lo"
911)
1012
@@ -14,18 +16,18 @@ import (
1416// space. Right now most windows are 1:1 with views, except for commitFiles which
1517// is a view that moves between windows
1618
17- func (gui * Gui ) initialWindowViewNameMap (contextTree * context.ContextTree ) map [string ] string {
18- result := map [string ] string {}
19+ func (gui * Gui ) initialWindowViewNameMap (contextTree * context.ContextTree ) * utils. ThreadSafeMap [string , string ] {
20+ result := utils . NewThreadSafeMap [string , string ]()
1921
2022 for _ , context := range contextTree .Flatten () {
21- result [ context .GetWindowName ()] = context .GetViewName ()
23+ result . Set ( context .GetWindowName (), context .GetViewName () )
2224 }
2325
2426 return result
2527}
2628
2729func (gui * Gui ) getViewNameForWindow (window string ) string {
28- viewName , ok := gui .State .WindowViewNameMap [ window ]
30+ viewName , ok := gui .State .WindowViewNameMap . Get ( window )
2931 if ! ok {
3032 panic (fmt .Sprintf ("Viewname not found for window: %s" , window ))
3133 }
@@ -50,7 +52,7 @@ func (gui *Gui) setWindowContext(c types.Context) {
5052 gui .resetWindowContext (c )
5153 }
5254
53- gui .State .WindowViewNameMap [ c .GetWindowName ()] = c .GetViewName ()
55+ gui .State .WindowViewNameMap . Set ( c .GetWindowName (), c .GetViewName () )
5456}
5557
5658func (gui * Gui ) currentWindow () string {
@@ -59,39 +61,57 @@ func (gui *Gui) currentWindow() string {
5961
6062// assumes the context's windowName has been set to the new window if necessary
6163func (gui * Gui ) resetWindowContext (c types.Context ) {
62- for windowName , viewName := range gui .State .WindowViewNameMap {
64+ for _ , windowName := range gui .State .WindowViewNameMap .Keys () {
65+ viewName , ok := gui .State .WindowViewNameMap .Get (windowName )
66+ if ! ok {
67+ continue
68+ }
6369 if viewName == c .GetViewName () && windowName != c .GetWindowName () {
6470 for _ , context := range gui .State .Contexts .Flatten () {
6571 if context .GetKey () != c .GetKey () && context .GetWindowName () == windowName {
66- gui .State .WindowViewNameMap [ windowName ] = context .GetViewName ()
72+ gui .State .WindowViewNameMap . Set ( windowName , context .GetViewName () )
6773 }
6874 }
6975 }
7076 }
7177}
7278
73- func (gui * Gui ) moveToTopOfWindow (context types.Context ) {
79+ // moves given context's view to the top of the window and returns
80+ // true if the view was not already on top.
81+ func (gui * Gui ) moveToTopOfWindow (context types.Context ) bool {
7482 view := context .GetView ()
7583 if view == nil {
76- return
84+ return false
7785 }
7886
7987 window := context .GetWindowName ()
8088
89+ topView := gui .topViewInWindow (window )
90+
91+ if view .Name () == topView .Name () {
92+ return false
93+ } else {
94+ if err := gui .g .SetViewOnTopOf (view .Name (), topView .Name ()); err != nil {
95+ gui .Log .Error (err )
96+ }
97+
98+ return true
99+ }
100+ }
101+
102+ func (gui * Gui ) topViewInWindow (windowName string ) * gocui.View {
81103 // now I need to find all views in that same window, via contexts. And I guess then I need to find the index of the highest view in that list.
82- viewNamesInWindow := gui .viewNamesInWindow (window )
104+ viewNamesInWindow := gui .viewNamesInWindow (windowName )
83105
84106 // The views list is ordered highest-last, so we're grabbing the last view of the window
85- topView := view
107+ var topView * gocui. View
86108 for _ , currentView := range gui .g .Views () {
87109 if lo .Contains (viewNamesInWindow , currentView .Name ()) {
88110 topView = currentView
89111 }
90112 }
91113
92- if err := gui .g .SetViewOnTopOf (view .Name (), topView .Name ()); err != nil {
93- gui .Log .Error (err )
94- }
114+ return topView
95115}
96116
97117func (gui * Gui ) viewNamesInWindow (windowName string ) []string {
0 commit comments