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

more refactoring

This commit is contained in:
Jesse Duffield 2021-04-05 01:10:23 +10:00
parent 16b9b9725e
commit 41747b5b34
7 changed files with 203 additions and 204 deletions

View File

@ -257,7 +257,7 @@ func (gui *Gui) handleToggleCommitFileDirCollapsed() error {
func (gui *Gui) switchToCommitFilesContext(refName string, canRebase bool, context Context, windowName string) error { func (gui *Gui) switchToCommitFilesContext(refName string, canRebase bool, context Context, windowName string) error {
// sometimes the commitFiles view is already shown in another window, so we need to ensure that window // sometimes the commitFiles view is already shown in another window, so we need to ensure that window
// no longer considers the commitFiles view as its main view. // no longer considers the commitFiles view as its main view.
gui.resetWindowForView("commitFiles") gui.resetWindowForView(gui.Views.CommitFiles)
gui.State.Panels.CommitFiles.SelectedLineIdx = 0 gui.State.Panels.CommitFiles.SelectedLineIdx = 0
gui.State.Panels.CommitFiles.refName = refName gui.State.Panels.CommitFiles.refName = refName
@ -287,14 +287,11 @@ func (gui *Gui) handleToggleCommitFileTreeView() error {
} }
} }
// TODO: pretty sure this view only ever has this context. Is this if condition necessary? if err := gui.State.Contexts.CommitFiles.HandleRender(); err != nil {
if gui.Views.CommitFiles.Context == COMMIT_FILES_CONTEXT_KEY { return err
if err := gui.State.Contexts.CommitFiles.HandleRender(); err != nil { }
return err if err := gui.State.Contexts.CommitFiles.HandleFocus(); err != nil {
} return err
if err := gui.State.Contexts.CommitFiles.HandleFocus(); err != nil {
return err
}
} }
return nil return nil

View File

@ -19,27 +19,27 @@ type ContextKey string
const ( const (
STATUS_CONTEXT_KEY ContextKey = "status" STATUS_CONTEXT_KEY ContextKey = "status"
FILES_CONTEXT_KEY = "files" FILES_CONTEXT_KEY ContextKey = "files"
LOCAL_BRANCHES_CONTEXT_KEY = "localBranches" LOCAL_BRANCHES_CONTEXT_KEY ContextKey = "localBranches"
REMOTES_CONTEXT_KEY = "remotes" REMOTES_CONTEXT_KEY ContextKey = "remotes"
REMOTE_BRANCHES_CONTEXT_KEY = "remoteBranches" REMOTE_BRANCHES_CONTEXT_KEY ContextKey = "remoteBranches"
TAGS_CONTEXT_KEY = "tags" TAGS_CONTEXT_KEY ContextKey = "tags"
BRANCH_COMMITS_CONTEXT_KEY = "commits" BRANCH_COMMITS_CONTEXT_KEY ContextKey = "commits"
REFLOG_COMMITS_CONTEXT_KEY = "reflogCommits" REFLOG_COMMITS_CONTEXT_KEY ContextKey = "reflogCommits"
SUB_COMMITS_CONTEXT_KEY = "subCommits" SUB_COMMITS_CONTEXT_KEY ContextKey = "subCommits"
COMMIT_FILES_CONTEXT_KEY = "commitFiles" COMMIT_FILES_CONTEXT_KEY ContextKey = "commitFiles"
STASH_CONTEXT_KEY = "stash" STASH_CONTEXT_KEY ContextKey = "stash"
MAIN_NORMAL_CONTEXT_KEY = "normal" MAIN_NORMAL_CONTEXT_KEY ContextKey = "normal"
MAIN_MERGING_CONTEXT_KEY = "merging" MAIN_MERGING_CONTEXT_KEY ContextKey = "merging"
MAIN_PATCH_BUILDING_CONTEXT_KEY = "patchBuilding" MAIN_PATCH_BUILDING_CONTEXT_KEY ContextKey = "patchBuilding"
MAIN_STAGING_CONTEXT_KEY = "staging" MAIN_STAGING_CONTEXT_KEY ContextKey = "staging"
MENU_CONTEXT_KEY = "menu" MENU_CONTEXT_KEY ContextKey = "menu"
CREDENTIALS_CONTEXT_KEY = "credentials" CREDENTIALS_CONTEXT_KEY ContextKey = "credentials"
CONFIRMATION_CONTEXT_KEY = "confirmation" CONFIRMATION_CONTEXT_KEY ContextKey = "confirmation"
SEARCH_CONTEXT_KEY = "search" SEARCH_CONTEXT_KEY ContextKey = "search"
COMMIT_MESSAGE_CONTEXT_KEY = "commitMessage" COMMIT_MESSAGE_CONTEXT_KEY ContextKey = "commitMessage"
SUBMODULES_CONTEXT_KEY = "submodules" SUBMODULES_CONTEXT_KEY ContextKey = "submodules"
SUGGESTIONS_CONTEXT_KEY = "suggestions" SUGGESTIONS_CONTEXT_KEY ContextKey = "suggestions"
) )
var allContextKeys = []ContextKey{ var allContextKeys = []ContextKey{
@ -512,7 +512,7 @@ func (gui *Gui) activateContext(c Context) error {
originalViewContextKey := ContextKey(v.Context) originalViewContextKey := ContextKey(v.Context)
// ensure that any other window for which this view was active is now set to the default for that window. // ensure that any other window for which this view was active is now set to the default for that window.
gui.setViewAsActiveForWindow(viewName) gui.setViewAsActiveForWindow(v)
if viewName == "main" { if viewName == "main" {
gui.changeMainViewsContext(c.GetKey()) gui.changeMainViewsContext(c.GetKey())
@ -677,7 +677,7 @@ func (gui *Gui) onViewFocusLost(oldView *gocui.View, newView *gocui.View) error
} }
if oldView == gui.Views.CommitFiles && newView != gui.Views.Main && newView != gui.Views.Secondary && newView != gui.Views.Search { if oldView == gui.Views.CommitFiles && newView != gui.Views.Main && newView != gui.Views.Secondary && newView != gui.Views.Search {
gui.resetWindowForView("commitFiles") gui.resetWindowForView(gui.Views.CommitFiles)
if err := gui.deactivateContext(gui.State.Contexts.CommitFiles); err != nil { if err := gui.deactivateContext(gui.State.Contexts.CommitFiles); err != nil {
return err return err
} }

View File

@ -117,14 +117,14 @@ func (gui *Gui) refreshFilesAndSubmodules() error {
gui.Log.Error(err) gui.Log.Error(err)
} }
if gui.Views.Files.Context == FILES_CONTEXT_KEY { if ContextKey(gui.Views.Files.Context) == FILES_CONTEXT_KEY {
// doing this a little custom (as opposed to using gui.postRefreshUpdate) because we handle selecting the file explicitly below // doing this a little custom (as opposed to using gui.postRefreshUpdate) because we handle selecting the file explicitly below
if err := gui.State.Contexts.Files.HandleRender(); err != nil { if err := gui.State.Contexts.Files.HandleRender(); err != nil {
return err return err
} }
} }
if gui.currentContext().GetKey() == FILES_CONTEXT_KEY || (g.CurrentView() == gui.Views.Main && g.CurrentView().Context == MAIN_MERGING_CONTEXT_KEY) { if gui.currentContext().GetKey() == FILES_CONTEXT_KEY || (g.CurrentView() == gui.Views.Main && ContextKey(g.CurrentView().Context) == MAIN_MERGING_CONTEXT_KEY) {
newSelectedPath := gui.getSelectedPath() newSelectedPath := gui.getSelectedPath()
alreadySelected := selectedPath != "" && newSelectedPath == selectedPath alreadySelected := selectedPath != "" && newSelectedPath == selectedPath
if err := gui.selectFile(alreadySelected); err != nil { if err := gui.selectFile(alreadySelected); err != nil {
@ -856,7 +856,7 @@ func (gui *Gui) handleToggleFileTreeView() error {
} }
} }
if gui.Views.Files.Context == FILES_CONTEXT_KEY { if ContextKey(gui.Views.Files.Context) == FILES_CONTEXT_KEY {
if err := gui.State.Contexts.Files.HandleRender(); err != nil { if err := gui.State.Contexts.Files.HandleRender(); err != nil {
return err return err
} }

View File

@ -148,13 +148,13 @@ func (gui *Gui) handleMouseDownMain() error {
return nil return nil
} }
switch gui.g.CurrentView().Name() { switch gui.g.CurrentView() {
case "files": case gui.Views.Files:
// set filename, set primary/secondary selected, set line number, then switch context // set filename, set primary/secondary selected, set line number, then switch context
// I'll need to know it was changed though. // I'll need to know it was changed though.
// Could I pass something along to the context change? // Could I pass something along to the context change?
return gui.enterFile(false, gui.Views.Main.SelectedLineIdx()) return gui.enterFile(false, gui.Views.Main.SelectedLineIdx())
case "commitFiles": case gui.Views.CommitFiles:
return gui.enterCommitFile(gui.Views.Main.SelectedLineIdx()) return gui.enterCommitFile(gui.Views.Main.SelectedLineIdx())
} }
@ -166,8 +166,8 @@ func (gui *Gui) handleMouseDownSecondary() error {
return nil return nil
} }
switch gui.g.CurrentView().Name() { switch gui.g.CurrentView() {
case "files": case gui.Views.Files:
return gui.enterFile(true, gui.Views.Secondary.SelectedLineIdx()) return gui.enterFile(true, gui.Views.Secondary.SelectedLineIdx())
} }

File diff suppressed because it is too large Load Diff

View File

@ -317,8 +317,8 @@ func (gui *Gui) promptToContinueRebase() error {
} }
func (gui *Gui) canScrollMergePanel() bool { func (gui *Gui) canScrollMergePanel() bool {
currentViewName := gui.currentViewName() currentView := gui.g.CurrentView()
if currentViewName != "main" && currentViewName != "files" { if currentView != gui.Views.Main && currentView != gui.Views.Files {
return false return false
} }

View File

@ -1,5 +1,7 @@
package gui package gui
import "github.com/jesseduffield/gocui"
// A window refers to a place on the screen which can hold one or more views. // A window refers to a place on the screen which can hold one or more views.
// A view is a box that renders content, and within a window only one view will // A view is a box that renders content, and within a window only one view will
// appear at a time. When a view appears within a window, it occupies the whole // appear at a time. When a view appears within a window, it occupies the whole
@ -15,28 +17,28 @@ func (gui *Gui) getViewNameForWindow(window string) string {
return viewName return viewName
} }
func (gui *Gui) getWindowForViewName(viewName string) string { func (gui *Gui) getWindowForView(view *gocui.View) string {
if viewName == "commitFiles" { if view == gui.Views.CommitFiles {
return gui.State.Contexts.CommitFiles.GetWindowName() return gui.State.Contexts.CommitFiles.GetWindowName()
} }
return viewName return view.Name()
} }
func (gui *Gui) setViewAsActiveForWindow(viewName string) { func (gui *Gui) setViewAsActiveForWindow(view *gocui.View) {
if gui.State.WindowViewNameMap == nil { if gui.State.WindowViewNameMap == nil {
gui.State.WindowViewNameMap = map[string]string{} gui.State.WindowViewNameMap = map[string]string{}
} }
gui.State.WindowViewNameMap[gui.getWindowForViewName(viewName)] = viewName gui.State.WindowViewNameMap[gui.getWindowForView(view)] = view.Name()
} }
func (gui *Gui) currentWindow() string { func (gui *Gui) currentWindow() string {
return gui.getWindowForViewName(gui.currentViewName()) return gui.getWindowForView(gui.g.CurrentView())
} }
func (gui *Gui) resetWindowForView(viewName string) { func (gui *Gui) resetWindowForView(view *gocui.View) {
window := gui.getWindowForViewName(viewName) window := gui.getWindowForView(view)
// we assume here that the window contains as its default view a view with the same name as the window // we assume here that the window contains as its default view a view with the same name as the window
gui.State.WindowViewNameMap[window] = window gui.State.WindowViewNameMap[window] = window
} }