diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2022-02-05 17:04:10 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2022-03-17 19:13:40 +1100 |
commit | d82f175e79f18756769d91de94458b095130297c (patch) | |
tree | 63c0c5b17a698a5a202a85b930edd0cf9e85ebf7 /pkg/gui | |
parent | 145c69d9ae32ec8fbdd6d1e6116efec466a0a709 (diff) |
refactor contexts
Diffstat (limited to 'pkg/gui')
54 files changed, 1557 insertions, 1243 deletions
diff --git a/pkg/gui/branches_panel.go b/pkg/gui/branches_panel.go index f295c9470..072ee257b 100644 --- a/pkg/gui/branches_panel.go +++ b/pkg/gui/branches_panel.go @@ -13,22 +13,9 @@ import ( // list panel functions -func (gui *Gui) getSelectedBranch() *models.Branch { - if len(gui.State.Model.Branches) == 0 { - return nil - } - - selectedLine := gui.State.Panels.Branches.SelectedLineIdx - if selectedLine == -1 { - return nil - } - - return gui.State.Model.Branches[selectedLine] -} - func (gui *Gui) branchesRenderToMain() error { var task updateTask - branch := gui.getSelectedBranch() + branch := gui.State.Contexts.Branches.GetSelected() if branch == nil { task = NewRenderStringTask(gui.c.Tr.NoBranchesThisRepo) } else { @@ -48,24 +35,26 @@ func (gui *Gui) branchesRenderToMain() error { // specific functions func (gui *Gui) handleBranchPress() error { - if gui.State.Panels.Branches.SelectedLineIdx == -1 { + branch := gui.State.Contexts.Branches.GetSelected() + if branch == nil { return nil } - if gui.State.Panels.Branches.SelectedLineIdx == 0 { + + if branch == gui.getCheckedOutBranch() { return gui.c.ErrorMsg(gui.c.Tr.AlreadyCheckedOutBranch) } - branch := gui.getSelectedBranch() + gui.c.LogAction(gui.c.Tr.Actions.CheckoutBranch) return gui.helpers.Refs.CheckoutRef(branch.Name, types.CheckoutRefOptions{}) } func (gui *Gui) handleCreatePullRequestPress() error { - branch := gui.getSelectedBranch() + branch := gui.State.Contexts.Branches.GetSelected() return gui.createPullRequest(branch.Name, "") } func (gui *Gui) handleCreatePullRequestMenu() error { - selectedBranch := gui.getSelectedBranch() + selectedBranch := gui.State.Contexts.Branches.GetSelected() if selectedBranch == nil { return nil } @@ -77,7 +66,7 @@ func (gui *Gui) handleCreatePullRequestMenu() error { func (gui *Gui) handleCopyPullRequestURLPress() error { hostingServiceMgr := gui.getHostingServiceMgr() - branch := gui.getSelectedBranch() + branch := gui.State.Contexts.Branches.GetSelected() branchExistsOnRemote := gui.git.Remote.CheckRemoteBranchExists(branch.Name) @@ -109,7 +98,7 @@ func (gui *Gui) handleGitFetch() error { } func (gui *Gui) handleForceCheckout() error { - branch := gui.getSelectedBranch() + branch := gui.State.Contexts.Branches.GetSelected() message := gui.c.Tr.SureForceCheckout title := gui.c.Tr.ForceCheckoutBranch @@ -156,7 +145,7 @@ func (gui *Gui) getCheckedOutBranch() *models.Branch { } func (gui *Gui) createNewBranchWithName(newBranchName string) error { - branch := gui.getSelectedBranch() + branch := gui.State.Contexts.Branches.GetSelected() if branch == nil { return nil } @@ -165,7 +154,7 @@ func (gui *Gui) createNewBranchWithName(newBranchName string) error { return gui.c.Error(err) } - gui.State.Panels.Branches.SelectedLineIdx = 0 + gui.State.Contexts.Branches.SetSelectedLineIdx(0) return gui.c.Refresh(types.RefreshOptions{Mode: types.ASYNC}) } @@ -174,7 +163,7 @@ func (gui *Gui) handleDeleteBranch() error { } func (gui *Gui) deleteBranch(force bool) error { - selectedBranch := gui.getSelectedBranch() + selectedBranch := gui.State.Contexts.Branches.GetSelected() if selectedBranch == nil { return nil } @@ -245,12 +234,12 @@ func (gui *Gui) mergeBranchIntoCheckedOutBranch(branchName string) error { } func (gui *Gui) handleMerge() error { - selectedBranchName := gui.getSelectedBranch().Name + selectedBranchName := gui.State.Contexts.Branches.GetSelected().Name return gui.mergeBranchIntoCheckedOutBranch(selectedBranchName) } func (gui *Gui) handleRebaseOntoLocalBranch() error { - selectedBranchName := gui.getSelectedBranch().Name + selectedBranchName := gui.State.Contexts.Branches.GetSelected().Name return gui.handleRebaseOntoBranch(selectedBranchName) } @@ -279,7 +268,7 @@ func (gui *Gui) handleRebaseOntoBranch(selectedBranchName string) error { } func (gui *Gui) handleFastForward() error { - branch := gui.getSelectedBranch() + branch := gui.State.Contexts.Branches.GetSelected() if branch == nil || !branch.IsRealBranch() { return nil } @@ -305,7 +294,7 @@ func (gui *Gui) handleFastForward() error { ) return gui.c.WithLoaderPanel(message, func() error { - if gui.State.Panels.Branches.SelectedLineIdx == 0 { + if branch == gui.getCheckedOutBranch() { gui.c.LogAction(action) err := gui.git.Sync.Pull( @@ -334,7 +323,7 @@ func (gui *Gui) handleFastForward() error { } func (gui *Gui) handleCreateResetToBranchMenu() error { - branch := gui.getSelectedBranch() + branch := gui.State.Contexts.Branches.GetSelected() if branch == nil { return nil } @@ -343,7 +332,7 @@ func (gui *Gui) handleCreateResetToBranchMenu() error { } func (gui *Gui) handleRenameBranch() error { - branch := gui.getSelectedBranch() + branch := gui.State.Contexts.Branches.GetSelected() if branch == nil || !branch.IsRealBranch() { return nil } @@ -364,7 +353,7 @@ func (gui *Gui) handleRenameBranch() error { // now that we've got our stuff again we need to find that branch and reselect it. for i, newBranch := range gui.State.Model.Branches { if newBranch.Name == newBranchName { - gui.State.Panels.Branches.SetSelectedLineIdx(i) + gui.State.Contexts.Branches.SetSelectedLineIdx(i) if err := gui.State.Contexts.Branches.HandleRender(); err != nil { return err } @@ -391,7 +380,7 @@ func (gui *Gui) handleRenameBranch() error { } func (gui *Gui) handleEnterBranch() error { - branch := gui.getSelectedBranch() + branch := gui.State.Contexts.Branches.GetSelected() if branch == nil { return nil } @@ -400,7 +389,7 @@ func (gui *Gui) handleEnterBranch() error { } func (gui *Gui) handleNewBranchOffBranch() error { - selectedBranch := gui.getSelectedBranch() + selectedBranch := gui.State.Contexts.Branches.GetSelected() if selectedBranch == nil { return nil } diff --git a/pkg/gui/commit_files_panel.go b/pkg/gui/commit_files_panel.go index d4cef7b14..fbbedcb6f 100644 --- a/pkg/gui/commit_files_panel.go +++ b/pkg/gui/commit_files_panel.go @@ -5,16 +5,11 @@ import ( "github.com/jesseduffield/lazygit/pkg/commands/patch" "github.com/jesseduffield/lazygit/pkg/gui/context" "github.com/jesseduffield/lazygit/pkg/gui/controllers" - "github.com/jesseduffield/lazygit/pkg/gui/filetree" "github.com/jesseduffield/lazygit/pkg/gui/types" ) -func (gui *Gui) getSelectedCommitFileNode() *filetree.CommitFileNode { - return gui.State.Contexts.CommitFiles.GetSelectedFileNode() -} - func (gui *Gui) getSelectedCommitFile() *models.CommitFile { - node := gui.getSelectedCommitFileNode() + node := gui.State.Contexts.CommitFiles.GetSelectedFileNode() if node == nil { return nil } @@ -22,20 +17,21 @@ func (gui *Gui) getSelectedCommitFile() *models.CommitFile { } func (gui *Gui) getSelectedCommitFilePath() string { - node := gui.getSelectedCommitFileNode() + node := gui.State.Contexts.CommitFiles.GetSelectedFileNode() if node == nil { return "" } return node.GetPath() } +// TODO: do we need this? func (gui *Gui) onCommitFileFocus() error { gui.escapeLineByLinePanel() return nil } func (gui *Gui) commitFilesRenderToMain() error { - node := gui.getSelectedCommitFileNode() + node := gui.State.Contexts.CommitFiles.GetSelectedFileNode() if node == nil { return nil } @@ -62,7 +58,7 @@ func (gui *Gui) commitFilesRenderToMain() error { } func (gui *Gui) handleCheckoutCommitFile() error { - node := gui.getSelectedCommitFileNode() + node := gui.State.Contexts.CommitFiles.GetSelectedFileNode() if node == nil { return nil } @@ -88,7 +84,7 @@ func (gui *Gui) handleDiscardOldFileChange() error { HandleConfirm: func() error { return gui.c.WithWaitingStatus(gui.c.Tr.RebasingStatus, func() error { gui.c.LogAction(gui.c.Tr.Actions.DiscardOldFileChange) - if err := gui.git.Rebase.DiscardOldFileChanges(gui.State.Model.Commits, gui.State.Panels.Commits.SelectedLineIdx, fileName); err != nil { + if err := gui.git.Rebase.DiscardOldFileChanges(gui.State.Model.Commits, gui.State.Contexts.BranchCommits.GetSelectedLineIdx(), fileName); err != nil { if err := gui.helpers.Rebase.CheckMergeOrRebase(err); err != nil { return err } @@ -122,7 +118,7 @@ func (gui *Gui) refreshCommitFilesView() error { } func (gui *Gui) handleOpenOldCommitFile() error { - node := gui.getSelectedCommitFileNode() + node := gui.State.Contexts.CommitFiles.GetSelectedFileNode() if node == nil { return nil } @@ -131,7 +127,7 @@ func (gui *Gui) handleOpenOldCommitFile() error { } func (gui *Gui) handleEditCommitFile() error { - node := gui.getSelectedCommitFileNode() + node := gui.State.Contexts.CommitFiles.GetSelectedFileNode() if node == nil { return nil } @@ -144,7 +140,7 @@ func (gui *Gui) handleEditCommitFile() error { } func (gui *Gui) handleToggleFileForPatch() error { - node := gui.getSelectedCommitFileNode() + node := gui.State.Contexts.CommitFiles.GetSelectedFileNode() if node == nil { return nil } @@ -212,7 +208,7 @@ func (gui *Gui) handleEnterCommitFile() error { } func (gui *Gui) enterCommitFile(opts types.OnFocusOpts) error { - node := gui.getSelectedCommitFileNode() + node := gui.State.Contexts.CommitFiles.GetSelectedFileNode() if node == nil { return nil } @@ -246,7 +242,7 @@ func (gui *Gui) enterCommitFile(opts types.OnFocusOpts) error { } func (gui *Gui) handleToggleCommitFileDirCollapsed() error { - node := gui.getSelectedCommitFileNode() + node := gui.State.Contexts.CommitFiles.GetSelectedFileNode() if node == nil { return nil } diff --git a/pkg/gui/commits_panel.go b/pkg/gui/commits_panel.go index 4175918ea..37e234e82 100644 --- a/pkg/gui/commits_panel.go +++ b/pkg/gui/commits_panel.go @@ -11,18 +11,12 @@ const COMMIT_THRESHOLD = 200 // list panel functions func (gui *Gui) getSelectedLocalCommit() *models.Commit { - selectedLine := gui.State.Panels.Commits.SelectedLineIdx - if selectedLine == -1 || selectedLine > len(gui.State.Model.Commits)-1 { - return nil - } - - return gui.State.Model.Commits[selectedLine] + return gui.State.Contexts.BranchCommits.GetSelected() } func (gui *Gui) onCommitFocus() error { - state := gui.State.Panels.Commits - if state.SelectedLineIdx > COMMIT_THRESHOLD && state.LimitCommits { - state.LimitCommits = false + if gui.State.Contexts.BranchCommits.GetSelectedLineIdx() > COMMIT_THRESHOLD && gui.State.LimitCommits { + gui.State.LimitCommits = false go utils.Safe(func() { if err := gui.refreshCommitsWithLimit(); err != nil { _ = gui.c.Error(err) @@ -37,7 +31,7 @@ func (gui *Gui) onCommitFocus() error { func (gui *Gui) branchCommitsRenderToMain() error { var task updateTask - commit := gui.getSelectedLocalCommit() + commit := gui.State.Contexts.BranchCommits.GetSelected() if commit == nil { task = NewRenderStringTask(gui.c.Tr.NoCommitsThisBranch) } else { diff --git a/pkg/gui/confirmation_panel.go b/pkg/gui/confirmation_panel.go index 8ed16ea39..b2cfabfab 100644 --- a/pkg/gui/confirmation_panel.go +++ b/pkg/gui/confirmation_panel.go @@ -77,7 +77,29 @@ func (gui *Gui) getMessageHeight(wrap bool, message string, width int) int { } func (gui *Gui) getConfirmationPanelDimensions(wrap bool, prompt string) (int, int, int, int) { + panelWidth := gui.getConfirmationPanelWidth() + panelHeight := gui.getMessageHeight(wrap, prompt, panelWidth) + return gui.getConfirmationPanelDimensionsAux(panelWidth, panelHeight) +} + +func (gui *Gui) getConfirmationPanelDimensionsForContentHeight(contentHeight int) (int, int, int, int) { + panelWidth := gui.getConfirmationPanelWidth() + return gui.getConfirmationPanelDimensionsAux(panelWidth, contentHeight) +} + +func (gui *Gui) getConfirmationPanelDimensionsAux(panelWidth int, panelHeight int) (int, int, int, int) { width, height := gui.g.Size() + if panelHeight > height*3/4 { + panelHeight = height * 3 / 4 + } + return width/2 - panelWidth/2, + height/2 - panelHeight/2 - panelHeight%2 - 1, + width/2 + panelWidth/2, + height/2 + panelHeight/2 +} + +func (gui *Gui) getConfirmationPanelWidth() int { + width, _ := gui.g.Size() // we want a minimum width up to a point, then we do it based on ratio. panelWidth := 4 * width / 7 minWidth := 80 @@ -88,14 +110,8 @@ func (gui *Gui) getConfirmationPanelDimensions(wrap bool, prompt string) (int, i panelWidth = minWidth } } - panelHeight := gui.getMessageHeight(wrap, prompt, panelWidth) - if panelHeight > height*3/4 { - panelHeight = height * 3 / 4 - } - return width/2 - panelWidth/2, - height/2 - panelHeight/2 - panelHeight%2 - 1, - width/2 + panelWidth/2, - height/2 + panelHeight/2 + + return panelWidth } func (gui *Gui) prepareConfirmationPanel( diff --git a/pkg/gui/context.go b/pkg/gui/context.go index f8aa9134e..b59f0a448 100644 --- a/pkg/gui/context.go +++ b/pkg/gui/context.go @@ -65,7 +65,6 @@ func (gui *Gui) pushContext(c types.Context, opts ...types.OnFocusOpts) error { } if !c.IsFocusable() { - panic(c.GetKey()) return nil } diff --git a/pkg/gui/context/branches_context.go b/pkg/gui/context/branches_context.go new file mode 100644 index 000000000..0be6e1dce --- /dev/null +++ b/pkg/gui/context/branches_context.go @@ -0,0 +1,86 @@ +package context + +import ( + "github.com/jesseduffield/gocui" + "github.com/jesseduffield/lazygit/pkg/commands/models" + "github.com/jesseduffield/lazygit/pkg/gui/context/traits" + "github.com/jesseduffield/lazygit/pkg/gui/types" +) + +type BranchesContext struct { + *BranchesViewModel + *ListContextTrait +} + +var _ types.IListContext = (*BranchesContext)(nil) + +func NewBranchesContext( + getModel func() []*models.Branch, + view *gocui.View, + getDisplayStrings func(startIdx int, length int) [][]string, + + onFocus func(...types.OnFocusOpts) error, + onRenderToMain func(...types.OnFocusOpts) error, + onFocusLost func() error, + + c *types.ControllerCommon, +) *BranchesContext { + viewModel := NewBranchesViewModel(getModel) + + return &BranchesContext{ + BranchesViewModel: viewModel, + ListContextTrait: &ListContextTrait{ + Context: NewSimpleContext(NewBaseContext(NewBaseContextOpts{ + ViewName: "branches", + WindowName: "branches", + Key: LOCAL_BRANCHES_CONTEXT_KEY, + Kind: types.SIDE_CONTEXT, + Focusable: true, + }), ContextCallbackOpts{ + OnFocus: onFocus, + OnFocusLost: onFocusLost, + OnRenderToMain: onRenderToMain, + }), + list: viewModel, + viewTrait: NewViewTrait(view), + getDisplayStrings: getDisplayStrings, + c: c, + }, + } +} + +func (self *BranchesContext) GetSelectedItemId() string { + item := self.GetSelected() + if item == nil { + return "" + } + + return item.ID() +} + +type BranchesViewModel struct { + *traits.ListCursor + getModel func() []*models.Branch +} + +func NewBranchesViewModel(getModel func() []*models.Branch) *BranchesViewModel { + self := &BranchesViewModel{ + getModel: getModel, + } + + self.ListCursor = traits.NewListCursor(self) + + return self +} + +func (self *BranchesViewModel) GetItemsLength() int { + return len(self.getModel()) +} + +func (self *BranchesViewModel) GetSelected() *models.Branch { + if self.GetItemsLength() == 0 { + return nil + } + + return self.getModel()[self.GetSelectedLineIdx()] +} diff --git a/pkg/gui/context/commit_files_context.go b/pkg/gui/context/commit_files_context.go index e729fb3c1..1c555387b 100644 --- a/pkg/gui/context/commit_files_context.go +++ b/pkg/gui/context/commit_files_context.go @@ -9,7 +9,6 @@ import ( type CommitFilesContext struct { *filetree.CommitFileTreeViewModel - *BaseContext *ListContextTrait } @@ -17,7 +16,7 @@ var _ types.IListContext = (*CommitFilesContext)(nil) func NewCommitFilesContext( getModel func() []*models.CommitFile, - getView func() *gocui.View, + view *gocui.View, getDisplayStrings func(startIdx int, length int) [][]string, onFocus func(...types.OnFocusOpts) error, @@ -26,43 +25,30 @@ func NewCommitFilesContext( c *types.ControllerCommon, ) *CommitFilesContext { - baseContext := NewBaseContext(NewBaseContextOpts{ - ViewName: "commitFiles", - WindowName: "commits", - Key: COMMIT_FILES_CONTEXT_KEY, - Kind: types.SIDE_CONTEXT, - Focusable: true, - }) - - self := &CommitFilesContext{} - takeFocus := func() error { return c.PushContext(self) } - viewModel := filetree.NewCommitFileTreeViewModel(getModel, c.Log, c.UserConfig.Gui.ShowFileTree) - viewTrait := NewViewTrait(getView) - listContextTrait := &ListContextTrait{ - base: baseContext, - list: viewModel, - viewTrait: viewTrait, - - GetDisplayStrings: getDisplayStrings, - OnFocus: onFocus, - OnRenderToMain: onRenderToMain, - OnFocusLost: onFocusLost, - takeFocus: takeFocus, - - // TODO: handle this in a trait - RenderSelection: false, - c: c, + return &CommitFilesContext{ + CommitFileTreeViewModel: viewModel, + ListContextTrait: &ListContextTrait{ + Context: NewSimpleContext( + NewBaseContext(NewBaseContextOpts{ + ViewName: "commitFiles", + WindowName: "commits", + Key: COMMIT_FILES_CONTEXT_KEY, + Kind: types.SIDE_CONTEXT, + Focusable: true, + }), + ContextCallbackOpts{ + OnFocus: onFocus, + OnFocusLost: onFocusLost, + OnRenderToMain: onRenderToMain, + }), + list: viewModel, + viewTrait: NewViewTrait(view), + getDisplayStrings: getDisplayStrings, + c: c, + }, } - - baseContext.AddKeybindingsFn(listContextTrait.keybindings) - - self.BaseContext = baseContext - self.ListContextTrait = listContextTrait - self.CommitFileTreeViewModel = viewModel - - return self } func (self *CommitFilesContext) GetSelectedItemId() string { diff --git a/pkg/gui/context/context.go b/pkg/gui/context/context.go index 710e9a590..5f7c8f163 100644 --- a/pkg/gui/context/context.go +++ b/pkg/gui/context/context.go @@ -1,6 +1,10 @@ package context -import "github.com/jesseduffield/lazygit/pkg/gui/types" +import ( + "sync" |