simplify how the context system works

This commit is contained in:
Jesse Duffield 2019-11-10 16:33:31 +11:00
parent e85310c0a9
commit 131113b065
11 changed files with 362 additions and 374 deletions

View file

@ -29,6 +29,7 @@ func (gui *Gui) handleCommitFileSelect(g *gocui.Gui, v *gocui.View) error {
} }
gui.getMainView().Title = "Patch" gui.getMainView().Title = "Patch"
gui.State.Panels.LineByLine = nil
commitFile := gui.getSelectedCommitFile(g) commitFile := gui.getSelectedCommitFile(g)
if commitFile == nil { if commitFile == nil {
@ -207,10 +208,7 @@ func (gui *Gui) enterCommitFile(selectedLineIdx int) error {
} }
} }
if err := gui.changeContext("main", "patch-building"); err != nil { if err := gui.changeContext("patch-building"); err != nil {
return err
}
if err := gui.changeContext("secondary", "patch-building"); err != nil {
return err return err
} }
if err := gui.switchFocus(gui.g, gui.getCommitFilesView(), gui.getMainView()); err != nil { if err := gui.switchFocus(gui.g, gui.getCommitFilesView(), gui.getMainView()); err != nil {

View file

@ -47,6 +47,7 @@ func (gui *Gui) handleCommitSelect(g *gocui.Gui, v *gocui.View) error {
gui.getMainView().Title = "Patch" gui.getMainView().Title = "Patch"
gui.getSecondaryView().Title = "Custom Patch" gui.getSecondaryView().Title = "Custom Patch"
gui.State.Panels.LineByLine = nil
commit := gui.getSelectedCommit(g) commit := gui.getSelectedCommit(g)
if commit == nil { if commit == nil {
@ -97,7 +98,7 @@ func (gui *Gui) refreshCommits(g *gocui.Gui) error {
if g.CurrentView() == v { if g.CurrentView() == v {
gui.handleCommitSelect(g, v) gui.handleCommitSelect(g, v)
} }
if g.CurrentView() == gui.getCommitFilesView() || (g.CurrentView() == gui.getMainView() || gui.State.Contexts["main"] == "patch-building") { if g.CurrentView() == gui.getCommitFilesView() || (g.CurrentView() == gui.getMainView() || gui.State.Context == "patch-building") {
return gui.refreshCommitFilesView() return gui.refreshCommitFilesView()
} }
return nil return nil

View file

@ -1,42 +1,45 @@
package gui package gui
func (gui *Gui) changeContext(viewName, context string) error { func (gui *Gui) changeContext(context string) error {
if gui.State.Contexts[viewName] == context { oldContext := gui.State.Context
if gui.State.Context == context {
return nil return nil
} }
contextMap := gui.GetContextMap() contextMap := gui.GetContextMap()
gui.g.DeleteKeybindings(viewName) oldBindings := contextMap[oldContext]
for _, binding := range oldBindings {
if err := gui.g.DeleteKeybinding(binding.ViewName, binding.Key, binding.Modifier); err != nil {
return err
}
}
bindings := contextMap[viewName][context] bindings := contextMap[context]
for _, binding := range bindings { for _, binding := range bindings {
if err := gui.g.SetKeybinding(binding.ViewName, binding.Key, binding.Modifier, binding.Handler); err != nil { if err := gui.g.SetKeybinding(binding.ViewName, binding.Key, binding.Modifier, binding.Handler); err != nil {
return err return err
} }
} }
gui.State.Contexts[viewName] = context
gui.State.Context = context
return nil return nil
} }
func (gui *Gui) setInitialContexts() error { func (gui *Gui) setInitialContext() error {
contextMap := gui.GetContextMap() contextMap := gui.GetContextMap()
initialContexts := map[string]string{ initialContext := "normal"
"main": "normal",
"secondary": "normal",
}
for viewName, context := range initialContexts { bindings := contextMap[initialContext]
bindings := contextMap[viewName][context]
for _, binding := range bindings { for _, binding := range bindings {
if err := gui.g.SetKeybinding(binding.ViewName, binding.Key, binding.Modifier, binding.Handler); err != nil { if err := gui.g.SetKeybinding(binding.ViewName, binding.Key, binding.Modifier, binding.Handler); err != nil {
return err return err
} }
} }
}
gui.State.Contexts = initialContexts gui.State.Context = initialContext
return nil return nil
} }

View file

@ -207,10 +207,7 @@ func (gui *Gui) enterFile(forceSecondaryFocused bool, selectedLineIdx int) error
if file.HasMergeConflicts { if file.HasMergeConflicts {
return gui.createErrorPanel(gui.g, gui.Tr.SLocalize("FileStagingRequirements")) return gui.createErrorPanel(gui.g, gui.Tr.SLocalize("FileStagingRequirements"))
} }
if err := gui.changeContext("main", "staging"); err != nil { if err := gui.changeContext("staging"); err != nil {
return err
}
if err := gui.changeContext("secondary", "staging"); err != nil {
return err return err
} }
if err := gui.switchFocus(gui.g, gui.getFilesView(), gui.getMainView()); err != nil { if err := gui.switchFocus(gui.g, gui.getFilesView(), gui.getMainView()); err != nil {
@ -466,7 +463,7 @@ func (gui *Gui) handleSwitchToMerge(g *gocui.Gui, v *gocui.View) error {
if !file.HasInlineMergeConflicts { if !file.HasInlineMergeConflicts {
return gui.createErrorPanel(g, gui.Tr.SLocalize("FileNoMergeCons")) return gui.createErrorPanel(g, gui.Tr.SLocalize("FileNoMergeCons"))
} }
if err := gui.changeContext("main", "merging"); err != nil { if err := gui.changeContext("merging"); err != nil {
return err return err
} }
if err := gui.switchFocus(g, v, gui.getMainView()); err != nil { if err := gui.switchFocus(g, v, gui.getMainView()); err != nil {

View file

@ -155,7 +155,7 @@ type guiState struct {
Updating bool Updating bool
Panels *panelStates Panels *panelStates
WorkingTreeState string // one of "merging", "rebasing", "normal" WorkingTreeState string // one of "merging", "rebasing", "normal"
Contexts map[string]string Context string // important not to set this value directly but to use gui.changeContext("new context")
CherryPickedCommits []*commands.Commit CherryPickedCommits []*commands.Commit
SplitMainPanel bool SplitMainPanel bool
} }
@ -281,11 +281,11 @@ func (gui *Gui) onFocusLost(v *gocui.View, newView *gocui.View) error {
} }
case "main": case "main":
// if we have lost focus to a first-class panel, we need to do some cleanup // if we have lost focus to a first-class panel, we need to do some cleanup
if err := gui.changeContext("main", "normal"); err != nil { if err := gui.changeContext("normal"); err != nil {
return err return err
} }
case "commitFiles": case "commitFiles":
if gui.State.Contexts["main"] != "patch-building" { if gui.State.Context != "patch-building" {
if _, err := gui.g.SetViewOnBottom(v.Name()); err != nil { if _, err := gui.g.SetViewOnBottom(v.Name()); err != nil {
return err return err
} }

View file

@ -592,9 +592,8 @@ func (gui *Gui) GetInitialKeybindings() []*Binding {
// GetCurrentKeybindings gets the list of keybindings given the current context // GetCurrentKeybindings gets the list of keybindings given the current context
func (gui *Gui) GetCurrentKeybindings() []*Binding { func (gui *Gui) GetCurrentKeybindings() []*Binding {
bindings := gui.GetInitialKeybindings() bindings := gui.GetInitialKeybindings()
viewName := gui.currentViewName() currentContext := gui.State.Context
currentContext := gui.State.Contexts[viewName] contextBindings := gui.GetContextMap()[currentContext]
contextBindings := gui.GetContextMap()[viewName][currentContext]
return append(bindings, contextBindings...) return append(bindings, contextBindings...)
} }
@ -607,15 +606,14 @@ func (gui *Gui) keybindings(g *gocui.Gui) error {
return err return err
} }
} }
if err := gui.setInitialContexts(); err != nil { if err := gui.setInitialContext(); err != nil {
return err return err
} }
return nil return nil
} }
func (gui *Gui) GetContextMap() map[string]map[string][]*Binding { func (gui *Gui) GetContextMap() map[string][]*Binding {
return map[string]map[string][]*Binding{ return map[string][]*Binding{
"secondary": {
"normal": { "normal": {
{ {
ViewName: "secondary", ViewName: "secondary",
@ -623,18 +621,6 @@ func (gui *Gui) GetContextMap() map[string]map[string][]*Binding {
Modifier: gocui.ModNone, Modifier: gocui.ModNone,
Handler: gui.handleMouseDownSecondary, Handler: gui.handleMouseDownSecondary,
}, },
},
"staging": {
{
ViewName: "secondary",
Key: gocui.MouseLeft,
Modifier: gocui.ModNone,
Handler: gui.handleTogglePanelClick,
},
},
},
"main": {
"normal": {
{ {
ViewName: "main", ViewName: "main",
Key: gocui.MouseWheelDown, Key: gocui.MouseWheelDown,
@ -657,6 +643,12 @@ func (gui *Gui) GetContextMap() map[string]map[string][]*Binding {
}, },
}, },
"staging": { "staging": {
{
ViewName: "secondary",
Key: gocui.MouseLeft,
Modifier: gocui.ModNone,
Handler: gui.handleTogglePanelClick,
},
{ {
ViewName: "main", ViewName: "main",
Key: gocui.KeyEsc, Key: gocui.KeyEsc,
@ -949,6 +941,5 @@ func (gui *Gui) GetContextMap() map[string]map[string][]*Binding {
Description: gui.Tr.SLocalize("Undo"), Description: gui.Tr.SLocalize("Undo"),
}, },
}, },
},
} }
} }

View file

@ -226,7 +226,7 @@ func (gui *Gui) refreshMainView() error {
var includedLineIndices []int var includedLineIndices []int
// I'd prefer not to have knowledge of contexts using this file but I'm not sure // I'd prefer not to have knowledge of contexts using this file but I'm not sure
// how to get around this // how to get around this
if gui.State.Contexts["main"] == "patch-building" { if gui.State.Context == "patch-building" {
filename := gui.State.CommitFiles[gui.State.Panels.CommitFiles.SelectedLine].Name filename := gui.State.CommitFiles[gui.State.Panels.CommitFiles.SelectedLine].Name
includedLineIndices = gui.GitCommand.PatchManager.GetFileIncLineIndices(filename) includedLineIndices = gui.GitCommand.PatchManager.GetFileIncLineIndices(filename)
} }

View file

@ -88,7 +88,7 @@ func (gui *Gui) handleRemoveSelectionFromPatch(g *gocui.Gui, v *gocui.View) erro
func (gui *Gui) handleEscapePatchBuildingPanel(g *gocui.Gui, v *gocui.View) error { func (gui *Gui) handleEscapePatchBuildingPanel(g *gocui.Gui, v *gocui.View) error {
gui.State.Panels.LineByLine = nil gui.State.Panels.LineByLine = nil
gui.State.Contexts["main"] = "normal" gui.changeContext("normal")
if gui.GitCommand.PatchManager.IsEmpty() { if gui.GitCommand.PatchManager.IsEmpty() {
gui.GitCommand.PatchManager.Reset() gui.GitCommand.PatchManager.Reset()

View file

@ -67,7 +67,7 @@ func (gui *Gui) validateNormalWorkingTreeState() (bool, error) {
} }
func (gui *Gui) returnFocusFromLineByLinePanelIfNecessary() error { func (gui *Gui) returnFocusFromLineByLinePanelIfNecessary() error {
if gui.State.Contexts["main"] == "patch-building" { if gui.State.Context == "patch-building" {
return gui.handleEscapePatchBuildingPanel(gui.g, nil) return gui.handleEscapePatchBuildingPanel(gui.g, nil)
} }
return nil return nil

View file

@ -117,7 +117,7 @@ func (gui *Gui) newLineFocused(g *gocui.Gui, v *gocui.View) error {
case "credentials": case "credentials":
return gui.handleCredentialsViewFocused(g, v) return gui.handleCredentialsViewFocused(g, v)
case "main": case "main":
if gui.State.Contexts["main"] == "merging" { if gui.State.Context == "merging" {
return gui.refreshMergePanel() return gui.refreshMergePanel()
} }
v.Highlight = false v.Highlight = false
@ -406,7 +406,7 @@ func (gui *Gui) renderPanelOptions() error {
case "menu": case "menu":
return gui.renderMenuOptions() return gui.renderMenuOptions()
case "main": case "main":
if gui.State.Contexts["main"] == "merging" { if gui.State.Context == "merging" {
return gui.renderMergeOptions() return gui.renderMergeOptions()
} }
} }

View file

@ -83,9 +83,8 @@ func getBindingSections(mApp *app.App) []*bindingSection {
bindingSections = addBinding(title, bindingSections, binding) bindingSections = addBinding(title, bindingSections, binding)
} }
for view, contexts := range mApp.Gui.GetContextMap() { for contextName, contextBindings := range mApp.Gui.GetContextMap() {
for contextName, contextBindings := range contexts { translatedView := localisedTitle(mApp, contextBindings[0].ViewName)
translatedView := localisedTitle(mApp, view)
translatedContextName := localisedTitle(mApp, contextName) translatedContextName := localisedTitle(mApp, contextName)
title := fmt.Sprintf("%s (%s)", translatedView, translatedContextName) title := fmt.Sprintf("%s (%s)", translatedView, translatedContextName)
@ -93,7 +92,6 @@ func getBindingSections(mApp *app.App) []*bindingSection {
bindingSections = addBinding(title, bindingSections, binding) bindingSections = addBinding(title, bindingSections, binding)
} }
} }
}
return bindingSections return bindingSections
} }