1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-01-10 04:07:18 +02:00

introduce new approach to handling tab states

This commit is contained in:
Jesse Duffield 2020-08-19 09:05:43 +10:00
parent 9a2dc3fe15
commit 2fdadd383a
6 changed files with 91 additions and 29 deletions

View File

@ -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 {

View File

@ -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
}

View File

@ -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()

View File

@ -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})

View File

@ -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 {

View File

@ -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 {