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:
parent
9a2dc3fe15
commit
2fdadd383a
@ -446,30 +446,24 @@ func (gui *Gui) handleFastForward(g *gocui.Gui, v *gocui.View) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (gui *Gui) onBranchesTabClick(tabIndex int) error {
|
func (gui *Gui) onBranchesTabClick(tabIndex int) error {
|
||||||
contexts := []string{"local-branches", "remotes", "tags"}
|
|
||||||
branchesView := gui.getBranchesView()
|
branchesView := gui.getBranchesView()
|
||||||
branchesView.TabIndex = tabIndex
|
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 {
|
func (gui *Gui) tabIndexForContext(c Context, tabContexts tabContexts) int {
|
||||||
branchesView := gui.getBranchesView()
|
for i, tabContext := range tabContexts {
|
||||||
branchesView.Context = context
|
for _, context := range tabContext.contexts {
|
||||||
if err := gui.onSearchEscape(); err != nil {
|
if context.GetKey() == c.GetKey() {
|
||||||
return err
|
return i
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
gui.Log.Errorf("tab not found for context %s", c.GetKey())
|
||||||
contextTabIndexMap := map[string]int{
|
return 0
|
||||||
"local-branches": 0,
|
|
||||||
"remotes": 1,
|
|
||||||
"remote-branches": 1,
|
|
||||||
"tags": 2,
|
|
||||||
}
|
|
||||||
|
|
||||||
branchesView.TabIndex = contextTabIndexMap[context]
|
|
||||||
|
|
||||||
return gui.refreshBranchesViewWithSelection()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gui *Gui) refreshBranchesViewWithSelection() error {
|
func (gui *Gui) refreshBranchesViewWithSelection() error {
|
||||||
|
@ -106,10 +106,6 @@ func (gui *Gui) switchContext(c Context) error {
|
|||||||
gui.State.ContextStack = append(gui.State.ContextStack, c)
|
gui.State.ContextStack = append(gui.State.ContextStack, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.GetViewName() == "main" {
|
|
||||||
gui.changeMainViewsContext(c.GetKey())
|
|
||||||
}
|
|
||||||
|
|
||||||
return gui.activateContext(c)
|
return gui.activateContext(c)
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -172,6 +168,12 @@ func (gui *Gui) activateContext(c Context) error {
|
|||||||
return gui.returnFromContext()
|
return gui.returnFromContext()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if viewName == "main" {
|
||||||
|
gui.changeMainViewsContext(c.GetKey())
|
||||||
|
}
|
||||||
|
|
||||||
|
gui.setViewTabForContext(c)
|
||||||
|
|
||||||
if _, err := gui.g.SetCurrentView(viewName); err != nil {
|
if _, err := gui.g.SetCurrentView(viewName); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -443,3 +445,66 @@ func (gui *Gui) changeMainViewsContext(context string) {
|
|||||||
|
|
||||||
gui.State.MainContext = context
|
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
|
||||||
|
}
|
||||||
|
@ -96,8 +96,9 @@ type Gui struct {
|
|||||||
|
|
||||||
// when lazygit is opened outside a git directory we want to open to the most
|
// when lazygit is opened outside a git directory we want to open to the most
|
||||||
// recent repo with the recent repos popup showing
|
// recent repo with the recent repos popup showing
|
||||||
showRecentRepos bool
|
showRecentRepos bool
|
||||||
Contexts ContextTree
|
Contexts ContextTree
|
||||||
|
ViewTabContextMap map[string]tabContexts
|
||||||
}
|
}
|
||||||
|
|
||||||
// for now the staging panel state, unlike the other panel states, is going to be
|
// 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.resetState()
|
||||||
gui.State.FilterPath = filterPath
|
gui.State.FilterPath = filterPath
|
||||||
gui.Contexts = gui.contextTree()
|
gui.Contexts = gui.contextTree()
|
||||||
|
gui.ViewTabContextMap = gui.viewTabContextMap()
|
||||||
|
|
||||||
gui.watchFilesForChanges()
|
gui.watchFilesForChanges()
|
||||||
|
|
||||||
|
@ -48,7 +48,7 @@ func (gui *Gui) handleRemoteBranchSelect() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (gui *Gui) handleRemoteBranchesEscape(g *gocui.Gui, v *gocui.View) 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 {
|
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 {
|
if err := gui.handleCheckoutRef(remoteBranch.FullName(), handleCheckoutRefOptions{}); err != nil {
|
||||||
return err
|
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 {
|
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)
|
return gui.surfaceError(err)
|
||||||
}
|
}
|
||||||
gui.State.Panels.Branches.SelectedLine = 0
|
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 err
|
||||||
}
|
}
|
||||||
return gui.refreshSidePanels(refreshOptions{mode: ASYNC})
|
return gui.refreshSidePanels(refreshOptions{mode: ASYNC})
|
||||||
|
@ -110,7 +110,7 @@ func (gui *Gui) handleRemoteEnter() error {
|
|||||||
}
|
}
|
||||||
gui.State.Panels.RemoteBranches.SelectedLine = newSelectedLine
|
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 {
|
func (gui *Gui) handleAddRemote(g *gocui.Gui, v *gocui.View) error {
|
||||||
|
@ -83,7 +83,7 @@ func (gui *Gui) handleCheckoutTag(g *gocui.Gui, v *gocui.View) error {
|
|||||||
if err := gui.handleCheckoutRef(tag.Name, handleCheckoutRefOptions{}); err != nil {
|
if err := gui.handleCheckoutRef(tag.Name, handleCheckoutRefOptions{}); err != nil {
|
||||||
return err
|
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 {
|
func (gui *Gui) handleDeleteTag(g *gocui.Gui, v *gocui.View) error {
|
||||||
|
Loading…
Reference in New Issue
Block a user