1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-06-21 00:30:00 +02:00

add contexts to views

This commit is contained in:
Jesse Duffield
2019-11-16 12:41:04 +11:00
parent 44bbc106a9
commit 7e0a8f235e
13 changed files with 531 additions and 486 deletions

View File

@ -208,7 +208,7 @@ func (gui *Gui) enterCommitFile(selectedLineIdx int) error {
} }
} }
if err := gui.changeContext("patch-building"); err != nil { if err := gui.changeMainViewsContext("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

@ -111,7 +111,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.Context == "patch-building") { if g.CurrentView() == gui.getCommitFilesView() || (g.CurrentView() == gui.getMainView() || gui.State.MainContext == "patch-building") {
return gui.refreshCommitFilesView() return gui.refreshCommitFilesView()
} }
return nil return nil

View File

@ -153,10 +153,10 @@ func (gui *Gui) setKeyBindings(g *gocui.Gui, handleConfirm, handleClose func(*go
if err := gui.renderString(g, "options", actions); err != nil { if err := gui.renderString(g, "options", actions); err != nil {
return err return err
} }
if err := g.SetKeybinding("confirmation", gocui.KeyEnter, gocui.ModNone, gui.wrappedConfirmationFunction(handleConfirm, returnFocusOnClose)); err != nil { if err := g.SetKeybinding("confirmation", nil, gocui.KeyEnter, gocui.ModNone, gui.wrappedConfirmationFunction(handleConfirm, returnFocusOnClose)); err != nil {
return err return err
} }
return g.SetKeybinding("confirmation", gocui.KeyEsc, gocui.ModNone, gui.wrappedConfirmationFunction(handleClose, returnFocusOnClose)) return g.SetKeybinding("confirmation", nil, gocui.KeyEsc, gocui.ModNone, gui.wrappedConfirmationFunction(handleClose, returnFocusOnClose))
} }
func (gui *Gui) createMessagePanel(g *gocui.Gui, currentView *gocui.View, title, prompt string) error { func (gui *Gui) createMessagePanel(g *gocui.Gui, currentView *gocui.View, title, prompt string) error {

View File

@ -1,45 +1,20 @@
package gui package gui
func (gui *Gui) changeContext(context string) error { // changeContext is a helper function for when we want to change a 'main' context
oldContext := gui.State.Context // which currently just means a context that affects both the main and secondary views
// other views can have their context changed directly but this function helps
if gui.State.Context == context { // keep the main and secondary views in sync
func (gui *Gui) changeMainViewsContext(context string) error {
if gui.State.MainContext == context {
return nil return nil
} }
contextMap := gui.GetContextMap() switch context {
case "normal", "patch-building", "staging", "merging":
oldBindings := contextMap[oldContext] gui.getMainView().Context = context
for _, binding := range oldBindings { gui.getSecondaryView().Context = context
if err := gui.g.DeleteKeybinding(binding.ViewName, binding.Key, binding.Modifier); err != nil {
return err
}
} }
bindings := contextMap[context] gui.State.MainContext = context
for _, binding := range bindings {
if err := gui.g.SetKeybinding(binding.ViewName, binding.Key, binding.Modifier, binding.Handler); err != nil {
return err
}
}
gui.State.Context = context
return nil
}
func (gui *Gui) setInitialContext() error {
contextMap := gui.GetContextMap()
initialContext := "normal"
bindings := contextMap[initialContext]
for _, binding := range bindings {
if err := gui.g.SetKeybinding(binding.ViewName, binding.Key, binding.Modifier, binding.Handler); err != nil {
return err
}
}
gui.State.Context = initialContext
return nil return nil
} }

View File

@ -216,7 +216,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("staging"); err != nil { if err := gui.changeMainViewsContext("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 {
@ -510,7 +510,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("merging"); err != nil { if err := gui.changeMainViewsContext("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

@ -167,7 +167,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"
Context string // important not to set this value directly but to use gui.changeContext("new context") MainContext string // used to keep the main and secondary views' contexts in sync
CherryPickedCommits []*commands.Commit CherryPickedCommits []*commands.Commit
SplitMainPanel bool SplitMainPanel bool
RetainOriginalDir bool RetainOriginalDir bool
@ -315,11 +315,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("normal"); err != nil { if err := gui.changeMainViewsContext("normal"); err != nil {
return err return err
} }
case "commitFiles": case "commitFiles":
if gui.State.Context != "patch-building" { if gui.State.MainContext != "patch-building" {
if _, err := gui.g.SetViewOnBottom(v.Name()); err != nil { if _, err := gui.g.SetViewOnBottom(v.Name()); err != nil {
return err return err
} }
@ -582,7 +582,7 @@ func (gui *Gui) layout(g *gocui.Gui) error {
} }
// doing this here because it'll only happen once // doing this here because it'll only happen once
if err := gui.loadNewRepo(); err != nil { if err := gui.onInitialViewsCreation(); err != nil {
return err return err
} }
} }
@ -627,6 +627,14 @@ func (gui *Gui) layout(g *gocui.Gui) error {
return gui.resizeCurrentPopupPanel(g) return gui.resizeCurrentPopupPanel(g)
} }
func (gui *Gui) onInitialViewsCreation() error {
if err := gui.changeMainViewsContext("normal"); err != nil {
return err
}
return gui.loadNewRepo()
}
func (gui *Gui) loadNewRepo() error { func (gui *Gui) loadNewRepo() error {
gui.Updater.CheckForNewUpdate(gui.onBackgroundUpdateCheckFinish, false) gui.Updater.CheckForNewUpdate(gui.onBackgroundUpdateCheckFinish, false)
if err := gui.updateRecentRepoList(); err != nil { if err := gui.updateRecentRepoList(); err != nil {

File diff suppressed because it is too large Load Diff

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.Context == "patch-building" { if gui.State.MainContext == "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

@ -87,7 +87,7 @@ func (gui *Gui) createMenu(title string, items interface{}, itemCount int, handl
for _, key := range []gocui.Key{gocui.KeySpace, gocui.KeyEnter, 'y'} { for _, key := range []gocui.Key{gocui.KeySpace, gocui.KeyEnter, 'y'} {
_ = gui.g.DeleteKeybinding("menu", key, gocui.ModNone) _ = gui.g.DeleteKeybinding("menu", key, gocui.ModNone)
if err := gui.g.SetKeybinding("menu", key, gocui.ModNone, wrappedHandlePress); err != nil { if err := gui.g.SetKeybinding("menu", nil, key, gocui.ModNone, wrappedHandlePress); err != nil {
return err return err
} }
} }

View File

@ -6,6 +6,7 @@ import (
"github.com/go-errors/errors" "github.com/go-errors/errors"
"github.com/jesseduffield/gocui" "github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/utils"
) )
func (gui *Gui) getBindings(v *gocui.View) []*Binding { func (gui *Gui) getBindings(v *gocui.View) []*Binding {
@ -13,7 +14,7 @@ func (gui *Gui) getBindings(v *gocui.View) []*Binding {
bindingsGlobal, bindingsPanel []*Binding bindingsGlobal, bindingsPanel []*Binding
) )
bindings := gui.GetCurrentKeybindings() bindings := gui.GetInitialKeybindings()
for _, binding := range bindings { for _, binding := range bindings {
if binding.GetKey() != "" && binding.Description != "" { if binding.GetKey() != "" && binding.Description != "" {
@ -21,10 +22,12 @@ func (gui *Gui) getBindings(v *gocui.View) []*Binding {
case "": case "":
bindingsGlobal = append(bindingsGlobal, binding) bindingsGlobal = append(bindingsGlobal, binding)
case v.Name(): case v.Name():
if len(binding.Contexts) == 0 || utils.IncludesString(binding.Contexts, v.Context) {
bindingsPanel = append(bindingsPanel, binding) bindingsPanel = append(bindingsPanel, binding)
} }
} }
} }
}
// append dummy element to have a separator between // append dummy element to have a separator between
// panel and global keybindings // panel and global keybindings

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.changeContext("normal") gui.changeMainViewsContext("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.Context == "patch-building" { if gui.State.MainContext == "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.Context == "merging" { if gui.State.MainContext == "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.Context == "merging" { if gui.State.MainContext == "merging" {
return gui.renderMergeOptions() return gui.renderMergeOptions()
} }
} }