diff --git a/pkg/gui/branches_panel.go b/pkg/gui/branches_panel.go index 926c589f0..4efbe6397 100644 --- a/pkg/gui/branches_panel.go +++ b/pkg/gui/branches_panel.go @@ -446,30 +446,24 @@ func (gui *Gui) handleFastForward(g *gocui.Gui, v *gocui.View) error { } func (gui *Gui) onBranchesTabClick(tabIndex int) error { - contexts := []string{"local-branches", "remotes", "tags"} branchesView := gui.getBranchesView() branchesView.TabIndex = tabIndex - return gui.switchBranchesPanelContext(contexts[tabIndex]) + context := gui.ViewTabContextMap["branches"][tabIndex].contexts[0] + + return gui.switchContext(context) } -func (gui *Gui) switchBranchesPanelContext(context string) error { - branchesView := gui.getBranchesView() - branchesView.Context = context - if err := gui.onSearchEscape(); err != nil { - return err +func (gui *Gui) tabIndexForContext(c Context, tabContexts tabContexts) int { + for i, tabContext := range tabContexts { + for _, context := range tabContext.contexts { + if context.GetKey() == c.GetKey() { + return i + } + } } - - contextTabIndexMap := map[string]int{ - "local-branches": 0, - "remotes": 1, - "remote-branches": 1, - "tags": 2, - } - - branchesView.TabIndex = contextTabIndexMap[context] - - return gui.refreshBranchesViewWithSelection() + gui.Log.Errorf("tab not found for context %s", c.GetKey()) + return 0 } func (gui *Gui) refreshBranchesViewWithSelection() error { diff --git a/pkg/gui/context.go b/pkg/gui/context.go index 3c5dedde5..ef3139f48 100644 --- a/pkg/gui/context.go +++ b/pkg/gui/context.go @@ -106,10 +106,6 @@ func (gui *Gui) switchContext(c Context) error { gui.State.ContextStack = append(gui.State.ContextStack, c) } - if c.GetViewName() == "main" { - gui.changeMainViewsContext(c.GetKey()) - } - return gui.activateContext(c) }) @@ -172,6 +168,12 @@ func (gui *Gui) activateContext(c Context) error { return gui.returnFromContext() } + if viewName == "main" { + gui.changeMainViewsContext(c.GetKey()) + } + + gui.setViewTabForContext(c) + if _, err := gui.g.SetCurrentView(viewName); err != nil { return err } @@ -443,3 +445,66 @@ func (gui *Gui) changeMainViewsContext(context string) { gui.State.MainContext = context } + +func (gui *Gui) viewTabContextMap() map[string]tabContexts { + return map[string]tabContexts{ + "branches": tabContexts{ + { + tab: "Branches", + contexts: []Context{gui.Contexts.Branches.Context}, + }, + { + tab: "Remotes", + contexts: []Context{ + gui.Contexts.Remotes.Context, + gui.Contexts.Remotes.Branches.Context, + }, + }, + { + tab: "Tags", + contexts: []Context{gui.Contexts.Tags.Context}, + }, + }, + "commits": tabContexts{ + { + tab: "Commits", + contexts: []Context{gui.Contexts.BranchCommits.Context}, + }, + { + tab: "Reflog", + contexts: []Context{ + gui.Contexts.ReflogCommits.Context, + }, + }, + }, + } +} + +func (gui *Gui) setViewTabForContext(c Context) { + // search for the context in our map and if we find it, set the tab for the corresponding view + + tabContexts, ok := gui.ViewTabContextMap[c.GetViewName()] + if !ok { + return + } + + for tabIndex, tabContext := range tabContexts { + for _, context := range tabContext.contexts { + if context.GetKey() == c.GetKey() { + // get the view, set the tab + v, err := gui.g.View(c.GetViewName()) + if err != nil { + gui.Log.Error(err) + return + } + v.TabIndex = tabIndex + return + } + } + } +} + +type tabContexts []struct { + tab string + contexts []Context +} diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index cc083b4de..818cfbf60 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -96,8 +96,9 @@ type Gui struct { // when lazygit is opened outside a git directory we want to open to the most // recent repo with the recent repos popup showing - showRecentRepos bool - Contexts ContextTree + showRecentRepos bool + Contexts ContextTree + ViewTabContextMap map[string]tabContexts } // for now the staging panel state, unlike the other panel states, is going to be @@ -308,6 +309,7 @@ func NewGui(log *logrus.Entry, gitCommand *commands.GitCommand, oSCommand *comma gui.resetState() gui.State.FilterPath = filterPath gui.Contexts = gui.contextTree() + gui.ViewTabContextMap = gui.viewTabContextMap() gui.watchFilesForChanges() diff --git a/pkg/gui/remote_branches_panel.go b/pkg/gui/remote_branches_panel.go index 01f801de1..f6c213de8 100644 --- a/pkg/gui/remote_branches_panel.go +++ b/pkg/gui/remote_branches_panel.go @@ -48,7 +48,7 @@ func (gui *Gui) handleRemoteBranchSelect() error { } func (gui *Gui) handleRemoteBranchesEscape(g *gocui.Gui, v *gocui.View) error { - return gui.switchBranchesPanelContext("remotes") + return gui.switchContext(gui.Contexts.Remotes.Context) } func (gui *Gui) renderRemoteBranchesWithSelection() error { @@ -74,7 +74,7 @@ func (gui *Gui) handleCheckoutRemoteBranch(g *gocui.Gui, v *gocui.View) error { if err := gui.handleCheckoutRef(remoteBranch.FullName(), handleCheckoutRefOptions{}); err != nil { return err } - return gui.switchBranchesPanelContext("local-branches") + return gui.switchContext(gui.Contexts.Branches.Context) } func (gui *Gui) handleMergeRemoteBranch(g *gocui.Gui, v *gocui.View) error { @@ -163,7 +163,8 @@ func (gui *Gui) handleNewBranchOffRemote(g *gocui.Gui, v *gocui.View) error { return gui.surfaceError(err) } gui.State.Panels.Branches.SelectedLine = 0 - if err := gui.switchBranchesPanelContext("local-branches"); err != nil { + + if err := gui.switchContext(gui.Contexts.Branches.Context); err != nil { return err } return gui.refreshSidePanels(refreshOptions{mode: ASYNC}) diff --git a/pkg/gui/remotes_panel.go b/pkg/gui/remotes_panel.go index 68f868a1b..24058ea84 100644 --- a/pkg/gui/remotes_panel.go +++ b/pkg/gui/remotes_panel.go @@ -110,7 +110,7 @@ func (gui *Gui) handleRemoteEnter() error { } gui.State.Panels.RemoteBranches.SelectedLine = newSelectedLine - return gui.switchBranchesPanelContext("remote-branches") + return gui.switchContext(gui.Contexts.Remotes.Branches.Context) } func (gui *Gui) handleAddRemote(g *gocui.Gui, v *gocui.View) error { diff --git a/pkg/gui/tags_panel.go b/pkg/gui/tags_panel.go index ce696a06c..eb27f916d 100644 --- a/pkg/gui/tags_panel.go +++ b/pkg/gui/tags_panel.go @@ -83,7 +83,7 @@ func (gui *Gui) handleCheckoutTag(g *gocui.Gui, v *gocui.View) error { if err := gui.handleCheckoutRef(tag.Name, handleCheckoutRefOptions{}); err != nil { return err } - return gui.switchBranchesPanelContext("local-branches") + return gui.switchContext(gui.Contexts.Branches.Context) } func (gui *Gui) handleDeleteTag(g *gocui.Gui, v *gocui.View) error {