From 6fb83b740b9b3030d6b6e6ed09f895a7c6a4839d Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sun, 16 Aug 2020 14:16:28 +1000 Subject: [PATCH] WIP --- pkg/gui/context.go | 5 ++++- pkg/gui/keybindings.go | 24 ++++++++++++------------ pkg/gui/layout.go | 8 ++++---- pkg/gui/line_by_line_panel.go | 2 +- pkg/gui/list_view.go | 24 ++++++++++++------------ pkg/gui/side_window.go | 6 +++--- 6 files changed, 36 insertions(+), 33 deletions(-) diff --git a/pkg/gui/context.go b/pkg/gui/context.go index b209156da..85e653438 100644 --- a/pkg/gui/context.go +++ b/pkg/gui/context.go @@ -176,6 +176,7 @@ func (gui *Gui) activateContext(c Context) error { } newView := gui.g.CurrentView() + newView.Context = c.GetKey() gui.g.Cursor = newView.Editable @@ -190,6 +191,7 @@ func (gui *Gui) activateContext(c Context) error { return err } + // TODO: consider removing this and instead depending on the .Context field of views gui.State.ViewContextMap[c.GetViewName()] = c return nil @@ -228,6 +230,7 @@ func (gui *Gui) createContextTree() { OnFocus: gui.handleStatusSelect, Kind: SIDE_CONTEXT, ViewName: "status", + Key: "status", }, }, Files: SimpleContextNode{ @@ -314,7 +317,7 @@ func (gui *Gui) createContextTree() { OnFocus: func() error { return gui.handleCommitMessageFocused() }, Kind: PERSISTENT_POPUP, ViewName: "commitMessage", - Key: "commit-message", + Key: "commit-message", // admittedly awkward to have view names in camelCase and contexts in kebab-case }, }, Search: SimpleContextNode{ diff --git a/pkg/gui/keybindings.go b/pkg/gui/keybindings.go index a1d51d356..3231be031 100644 --- a/pkg/gui/keybindings.go +++ b/pkg/gui/keybindings.go @@ -1408,16 +1408,16 @@ func (gui *Gui) GetInitialKeybindings() []*Binding { for _, listView := range gui.getListViews() { bindings = append(bindings, []*Binding{ - {ViewName: listView.ViewName, Contexts: []string{listView.Context}, Key: gui.getKey("universal.prevItem-alt"), Modifier: gocui.ModNone, Handler: listView.handlePrevLine}, - {ViewName: listView.ViewName, Contexts: []string{listView.Context}, Key: gui.getKey("universal.prevItem"), Modifier: gocui.ModNone, Handler: listView.handlePrevLine}, - {ViewName: listView.ViewName, Contexts: []string{listView.Context}, Key: gocui.MouseWheelUp, Modifier: gocui.ModNone, Handler: listView.handlePrevLine}, - {ViewName: listView.ViewName, Contexts: []string{listView.Context}, Key: gui.getKey("universal.nextItem-alt"), Modifier: gocui.ModNone, Handler: listView.handleNextLine}, - {ViewName: listView.ViewName, Contexts: []string{listView.Context}, Key: gui.getKey("universal.nextItem"), Modifier: gocui.ModNone, Handler: listView.handleNextLine}, - {ViewName: listView.ViewName, Contexts: []string{listView.Context}, Key: gui.getKey("universal.prevPage"), Modifier: gocui.ModNone, Handler: listView.handlePrevPage, Description: gui.Tr.SLocalize("prevPage")}, - {ViewName: listView.ViewName, Contexts: []string{listView.Context}, Key: gui.getKey("universal.nextPage"), Modifier: gocui.ModNone, Handler: listView.handleNextPage, Description: gui.Tr.SLocalize("nextPage")}, - {ViewName: listView.ViewName, Contexts: []string{listView.Context}, Key: gui.getKey("universal.gotoTop"), Modifier: gocui.ModNone, Handler: listView.handleGotoTop, Description: gui.Tr.SLocalize("gotoTop")}, - {ViewName: listView.ViewName, Contexts: []string{listView.Context}, Key: gocui.MouseWheelDown, Modifier: gocui.ModNone, Handler: listView.handleNextLine}, - {ViewName: listView.ViewName, Contexts: []string{listView.Context}, Key: gocui.MouseLeft, Modifier: gocui.ModNone, Handler: listView.handleClick}, + {ViewName: listView.ViewName, Contexts: []string{listView.ContextKey}, Key: gui.getKey("universal.prevItem-alt"), Modifier: gocui.ModNone, Handler: listView.handlePrevLine}, + {ViewName: listView.ViewName, Contexts: []string{listView.ContextKey}, Key: gui.getKey("universal.prevItem"), Modifier: gocui.ModNone, Handler: listView.handlePrevLine}, + {ViewName: listView.ViewName, Contexts: []string{listView.ContextKey}, Key: gocui.MouseWheelUp, Modifier: gocui.ModNone, Handler: listView.handlePrevLine}, + {ViewName: listView.ViewName, Contexts: []string{listView.ContextKey}, Key: gui.getKey("universal.nextItem-alt"), Modifier: gocui.ModNone, Handler: listView.handleNextLine}, + {ViewName: listView.ViewName, Contexts: []string{listView.ContextKey}, Key: gui.getKey("universal.nextItem"), Modifier: gocui.ModNone, Handler: listView.handleNextLine}, + {ViewName: listView.ViewName, Contexts: []string{listView.ContextKey}, Key: gui.getKey("universal.prevPage"), Modifier: gocui.ModNone, Handler: listView.handlePrevPage, Description: gui.Tr.SLocalize("prevPage")}, + {ViewName: listView.ViewName, Contexts: []string{listView.ContextKey}, Key: gui.getKey("universal.nextPage"), Modifier: gocui.ModNone, Handler: listView.handleNextPage, Description: gui.Tr.SLocalize("nextPage")}, + {ViewName: listView.ViewName, Contexts: []string{listView.ContextKey}, Key: gui.getKey("universal.gotoTop"), Modifier: gocui.ModNone, Handler: listView.handleGotoTop, Description: gui.Tr.SLocalize("gotoTop")}, + {ViewName: listView.ViewName, Contexts: []string{listView.ContextKey}, Key: gocui.MouseWheelDown, Modifier: gocui.ModNone, Handler: listView.handleNextLine}, + {ViewName: listView.ViewName, Contexts: []string{listView.ContextKey}, Key: gocui.MouseLeft, Modifier: gocui.ModNone, Handler: listView.handleClick}, }...) // the commits panel needs to lazyload things so it has a couple of its own handlers @@ -1431,14 +1431,14 @@ func (gui *Gui) GetInitialKeybindings() []*Binding { bindings = append(bindings, []*Binding{ { ViewName: listView.ViewName, - Contexts: []string{listView.Context}, + Contexts: []string{listView.ContextKey}, Key: gui.getKey("universal.startSearch"), Handler: openSearchHandler, Description: gui.Tr.SLocalize("startSearch"), }, { ViewName: listView.ViewName, - Contexts: []string{listView.Context}, + Contexts: []string{listView.ContextKey}, Key: gui.getKey("universal.gotoBottom"), Handler: gotoBottomHandler, Description: gui.Tr.SLocalize("gotoBottom"), diff --git a/pkg/gui/layout.go b/pkg/gui/layout.go index 66f2eca5e..220f16cf5 100644 --- a/pkg/gui/layout.go +++ b/pkg/gui/layout.go @@ -286,20 +286,20 @@ func (gui *Gui) layout(g *gocui.Gui) error { } listViewStates := []listViewState{ - {view: filesView, context: "", selectedLine: gui.State.Panels.Files.SelectedLine, lineCount: len(gui.State.Files), listView: gui.filesListView()}, + {view: filesView, context: "files", selectedLine: gui.State.Panels.Files.SelectedLine, lineCount: len(gui.State.Files), listView: gui.filesListView()}, {view: branchesView, context: "local-branches", selectedLine: gui.State.Panels.Branches.SelectedLine, lineCount: len(gui.State.Branches), listView: gui.branchesListView()}, {view: branchesView, context: "remotes", selectedLine: gui.State.Panels.Remotes.SelectedLine, lineCount: len(gui.State.Remotes), listView: gui.remotesListView()}, {view: branchesView, context: "remote-branches", selectedLine: gui.State.Panels.RemoteBranches.SelectedLine, lineCount: len(gui.State.Remotes), listView: gui.remoteBranchesListView()}, {view: branchesView, context: "tags", selectedLine: gui.State.Panels.Tags.SelectedLine, lineCount: len(gui.State.Tags), listView: gui.tagsListView()}, {view: commitsView, context: "branch-commits", selectedLine: gui.State.Panels.Commits.SelectedLine, lineCount: len(gui.State.Commits), listView: gui.branchCommitsListView()}, {view: commitsView, context: "reflog-commits", selectedLine: gui.State.Panels.ReflogCommits.SelectedLine, lineCount: len(gui.State.FilteredReflogCommits), listView: gui.reflogCommitsListView()}, - {view: stashView, context: "", selectedLine: gui.State.Panels.Stash.SelectedLine, lineCount: len(gui.State.StashEntries), listView: gui.stashListView()}, - {view: commitFilesView, context: "", selectedLine: gui.State.Panels.CommitFiles.SelectedLine, lineCount: len(gui.State.CommitFiles), listView: gui.commitFilesListView()}, + {view: stashView, context: "stash", selectedLine: gui.State.Panels.Stash.SelectedLine, lineCount: len(gui.State.StashEntries), listView: gui.stashListView()}, + {view: commitFilesView, context: "commit-files", selectedLine: gui.State.Panels.CommitFiles.SelectedLine, lineCount: len(gui.State.CommitFiles), listView: gui.commitFilesListView()}, } // menu view might not exist so we check to be safe if menuView, err := gui.g.View("menu"); err == nil { - listViewStates = append(listViewStates, listViewState{view: menuView, context: "", selectedLine: gui.State.Panels.Menu.SelectedLine, lineCount: gui.State.MenuItemCount, listView: gui.menuListView()}) + listViewStates = append(listViewStates, listViewState{view: menuView, context: "menu", selectedLine: gui.State.Panels.Menu.SelectedLine, lineCount: gui.State.MenuItemCount, listView: gui.menuListView()}) } for _, listViewState := range listViewStates { // ignore views where the context doesn't match up with the selected line we're trying to focus diff --git a/pkg/gui/line_by_line_panel.go b/pkg/gui/line_by_line_panel.go index b090103cb..6fa3c8b45 100644 --- a/pkg/gui/line_by_line_panel.go +++ b/pkg/gui/line_by_line_panel.go @@ -332,7 +332,7 @@ func (gui *Gui) handleEscapeLineByLinePanel() { func (gui *Gui) handleOpenFileAtLine() error { // again, would be good to use inheritance here (or maybe even composition) var filename string - switch gui.State.MainContext { + switch gui.currentContext().GetKey() { case "patch-building": filename = gui.getSelectedCommitFileName() case "staging": diff --git a/pkg/gui/list_view.go b/pkg/gui/list_view.go index 715676ca6..d0efe544d 100644 --- a/pkg/gui/list_view.go +++ b/pkg/gui/list_view.go @@ -4,7 +4,7 @@ import "github.com/jesseduffield/gocui" type ListView struct { ViewName string - Context string + ContextKey string GetItemsLength func() int GetSelectedLineIdxPtr func() *int OnFocus func() error @@ -14,11 +14,10 @@ type ListView struct { Gui *Gui RendersToMainView bool Kind int - Key string } func (lv *ListView) GetKey() string { - return lv.Key + return lv.ContextKey } func (lv *ListView) GetKind() int { @@ -152,6 +151,7 @@ func (lv *ListView) onSearchSelect(selectedLineIdx int) error { func (gui *Gui) menuListView() *ListView { return &ListView{ ViewName: "menu", + ContextKey: "menu", GetItemsLength: func() int { return gui.getMenuView().LinesHeight() }, GetSelectedLineIdxPtr: func() *int { return &gui.State.Panels.Menu.SelectedLine }, OnFocus: gui.handleMenuSelect, @@ -161,13 +161,13 @@ func (gui *Gui) menuListView() *ListView { Gui: gui, RendersToMainView: false, Kind: PERSISTENT_POPUP, - Key: "menu", } } func (gui *Gui) filesListView() *ListView { return &ListView{ ViewName: "files", + ContextKey: "files", GetItemsLength: func() int { return len(gui.State.Files) }, GetSelectedLineIdxPtr: func() *int { return &gui.State.Panels.Files.SelectedLine }, OnFocus: gui.focusAndSelectFile, @@ -176,14 +176,13 @@ func (gui *Gui) filesListView() *ListView { Gui: gui, RendersToMainView: false, Kind: SIDE_CONTEXT, - Key: "files", } } func (gui *Gui) branchesListView() *ListView { return &ListView{ ViewName: "branches", - Context: "local-branches", + ContextKey: "local-branches", GetItemsLength: func() int { return len(gui.State.Branches) }, GetSelectedLineIdxPtr: func() *int { return &gui.State.Panels.Branches.SelectedLine }, OnFocus: gui.handleBranchSelect, @@ -191,14 +190,13 @@ func (gui *Gui) branchesListView() *ListView { Gui: gui, RendersToMainView: true, Kind: SIDE_CONTEXT, - Key: "menu", } } func (gui *Gui) remotesListView() *ListView { return &ListView{ ViewName: "branches", - Context: "remotes", + ContextKey: "remotes", GetItemsLength: func() int { return len(gui.State.Remotes) }, GetSelectedLineIdxPtr: func() *int { return &gui.State.Panels.Remotes.SelectedLine }, OnFocus: gui.renderRemotesWithSelection, @@ -213,7 +211,7 @@ func (gui *Gui) remotesListView() *ListView { func (gui *Gui) remoteBranchesListView() *ListView { return &ListView{ ViewName: "branches", - Context: "remote-branches", + ContextKey: "remote-branches", GetItemsLength: func() int { return len(gui.State.RemoteBranches) }, GetSelectedLineIdxPtr: func() *int { return &gui.State.Panels.RemoteBranches.SelectedLine }, OnFocus: gui.handleRemoteBranchSelect, @@ -227,7 +225,7 @@ func (gui *Gui) remoteBranchesListView() *ListView { func (gui *Gui) tagsListView() *ListView { return &ListView{ ViewName: "branches", - Context: "tags", + ContextKey: "tags", GetItemsLength: func() int { return len(gui.State.Tags) }, GetSelectedLineIdxPtr: func() *int { return &gui.State.Panels.Tags.SelectedLine }, OnFocus: gui.handleTagSelect, @@ -241,7 +239,7 @@ func (gui *Gui) tagsListView() *ListView { func (gui *Gui) branchCommitsListView() *ListView { return &ListView{ ViewName: "commits", - Context: "branch-commits", + ContextKey: "branch-commits", GetItemsLength: func() int { return len(gui.State.Commits) }, GetSelectedLineIdxPtr: func() *int { return &gui.State.Panels.Commits.SelectedLine }, OnFocus: gui.handleCommitSelect, @@ -256,7 +254,7 @@ func (gui *Gui) branchCommitsListView() *ListView { func (gui *Gui) reflogCommitsListView() *ListView { return &ListView{ ViewName: "commits", - Context: "reflog-commits", + ContextKey: "reflog-commits", GetItemsLength: func() int { return len(gui.State.FilteredReflogCommits) }, GetSelectedLineIdxPtr: func() *int { return &gui.State.Panels.ReflogCommits.SelectedLine }, OnFocus: gui.handleReflogCommitSelect, @@ -270,6 +268,7 @@ func (gui *Gui) reflogCommitsListView() *ListView { func (gui *Gui) stashListView() *ListView { return &ListView{ ViewName: "stash", + ContextKey: "stash", GetItemsLength: func() int { return len(gui.State.StashEntries) }, GetSelectedLineIdxPtr: func() *int { return &gui.State.Panels.Stash.SelectedLine }, OnFocus: gui.handleStashEntrySelect, @@ -283,6 +282,7 @@ func (gui *Gui) stashListView() *ListView { func (gui *Gui) commitFilesListView() *ListView { return &ListView{ ViewName: "commitFiles", + ContextKey: "commitFiles", GetItemsLength: func() int { return len(gui.State.CommitFiles) }, GetSelectedLineIdxPtr: func() *int { return &gui.State.Panels.CommitFiles.SelectedLine }, OnFocus: gui.handleCommitFileSelect, diff --git a/pkg/gui/side_window.go b/pkg/gui/side_window.go index 5d50984bb..f3ae24b3a 100644 --- a/pkg/gui/side_window.go +++ b/pkg/gui/side_window.go @@ -25,7 +25,7 @@ func (gui *Gui) nextSideWindow() error { viewName := gui.getViewNameForWindow(newWindow) - return gui.switchContext(gui.State.ViewContextMap[viewName]) + return gui.switchContextToView(viewName) } func (gui *Gui) previousSideWindow() error { @@ -51,7 +51,7 @@ func (gui *Gui) previousSideWindow() error { viewName := gui.getViewNameForWindow(newWindow) - return gui.switchContext(gui.State.ViewContextMap[viewName]) + return gui.switchContextToView(viewName) } func (gui *Gui) goToSideWindow(sideViewName string) func(g *gocui.Gui, v *gocui.View) error { @@ -61,6 +61,6 @@ func (gui *Gui) goToSideWindow(sideViewName string) func(g *gocui.Gui, v *gocui. gui.Log.Error(err) return nil } - return gui.switchContext(gui.State.ViewContextMap[sideViewName]) + return gui.switchContextToView(sideViewName) } }