diff options
Diffstat (limited to 'pkg/gui/window.go')
-rw-r--r-- | pkg/gui/window.go | 73 |
1 files changed, 66 insertions, 7 deletions
diff --git a/pkg/gui/window.go b/pkg/gui/window.go index d347081ab..efee847e1 100644 --- a/pkg/gui/window.go +++ b/pkg/gui/window.go @@ -1,7 +1,11 @@ package gui import ( + "fmt" + + "github.com/jesseduffield/lazygit/pkg/gui/context" "github.com/jesseduffield/lazygit/pkg/gui/types" + "github.com/samber/lo" ) // A window refers to a place on the screen which can hold one or more views. @@ -10,21 +14,38 @@ import ( // space. Right now most windows are 1:1 with views, except for commitFiles which // is a view that moves between windows +func (gui *Gui) initialWindowViewNameMap(contextTree *context.ContextTree) map[string]string { + result := map[string]string{} + + for _, context := range contextTree.Flatten() { + result[context.GetWindowName()] = context.GetViewName() + } + + return result +} + func (gui *Gui) getViewNameForWindow(window string) string { viewName, ok := gui.State.WindowViewNameMap[window] if !ok { - return window + panic(fmt.Sprintf("Viewname not found for window: %s", window)) } return viewName } -// for now all we actually care about is the context's view so we're storing that -func (gui *Gui) setWindowContext(c types.Context) { - if gui.State.WindowViewNameMap == nil { - gui.State.WindowViewNameMap = map[string]string{} +func (gui *Gui) getContextForWindow(window string) types.Context { + viewName := gui.getViewNameForWindow(window) + + context, ok := gui.contextForView(viewName) + if !ok { + panic("TODO: fix this") } + return context +} + +// for now all we actually care about is the context's view so we're storing that +func (gui *Gui) setWindowContext(c types.Context) { if c.IsTransient() { gui.resetWindowContext(c) } @@ -40,8 +61,46 @@ func (gui *Gui) currentWindow() string { func (gui *Gui) resetWindowContext(c types.Context) { for windowName, viewName := range gui.State.WindowViewNameMap { if viewName == c.GetViewName() && windowName != c.GetWindowName() { - // we assume here that the window contains as its default view a view with the same name as the window - gui.State.WindowViewNameMap[windowName] = windowName + for _, context := range gui.State.Contexts.Flatten() { + if context.GetKey() != c.GetKey() && context.GetWindowName() == windowName { + gui.State.WindowViewNameMap[windowName] = context.GetViewName() + } + } } } } + +func (gui *Gui) moveToTopOfWindow(context types.Context) { + view := context.GetView() + if view == nil { + return + } + + window := context.GetWindowName() + + // 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. + viewNamesInWindow := gui.viewNamesInWindow(window) + + // The views list is ordered highest-last, so we're grabbing the last view of the window + topView := view + for _, currentView := range gui.g.Views() { + if lo.Contains(viewNamesInWindow, currentView.Name()) { + topView = currentView + } + } + + if err := gui.g.SetViewOnTopOf(view.Name(), topView.Name()); err != nil { + gui.Log.Error(err) + } +} + +func (gui *Gui) viewNamesInWindow(windowName string) []string { + result := []string{} + for _, context := range gui.State.Contexts.Flatten() { + if context.GetWindowName() == windowName { + result = append(result, context.GetViewName()) + } + } + + return result +} |