diff --git a/pkg/gui/branches_panel.go b/pkg/gui/branches_panel.go index 17af8005e..358b094e8 100644 --- a/pkg/gui/branches_panel.go +++ b/pkg/gui/branches_panel.go @@ -221,22 +221,6 @@ func (gui *Gui) getCheckedOutBranch() *commands.Branch { return gui.State.Branches[0] } -func (gui *Gui) handleNewBranch(g *gocui.Gui, v *gocui.View) error { - branch := gui.getSelectedBranch() - if branch == nil { - return nil - } - message := gui.Tr.TemplateLocalize( - "NewBranchNameBranchOff", - Teml{ - "branchName": branch.Name, - }, - ) - return gui.prompt(v, message, "", func(response string) error { - return gui.createNewBranchWithName(response) - }) -} - func (gui *Gui) createNewBranchWithName(newBranchName string) error { branch := gui.getSelectedBranch() if branch == nil { diff --git a/pkg/gui/commits_panel.go b/pkg/gui/commits_panel.go index db3aea4df..5e1c55c58 100644 --- a/pkg/gui/commits_panel.go +++ b/pkg/gui/commits_panel.go @@ -675,25 +675,35 @@ func (gui *Gui) handleClipboardCopyCommit(g *gocui.Gui, v *gocui.View) error { return gui.OSCommand.CopyToClipboard(commit.Sha) } -func (gui *Gui) handleNewBranchOffCommit() error { - commit := gui.getSelectedLocalCommit() - if commit == nil { - return nil - } +func (gui *Gui) handleNewBranchOffCurrentItem() error { + context := gui.currentSideContext() + + itemId := context.GetSelectedItemId() message := gui.Tr.TemplateLocalize( "NewBranchNameBranchOff", Teml{ - "branchName": commit.NameWithSha(), + "branchName": itemId, // TODO: add 'long name' field on ListItem interface (right now we just get an ugly commit SHA for commits) }, ) return gui.prompt(gui.getCurrentSideView(), message, "", func(response string) error { - if err := gui.GitCommand.NewBranch(response, commit.Sha); err != nil { + if err := gui.GitCommand.NewBranch(response, itemId); err != nil { return err } - gui.State.Panels.Commits.SelectedLineIdx = 0 - gui.State.Panels.Branches.SelectedLineIdx = 0 + + // if we're currently in the branch commits context then the selected commit + // is about to go to the top of the list + if context.GetKey() == BRANCH_COMMITS_CONTEXT_KEY { + context.GetPanelState().SetSelectedLineIdx(0) + } + + if context.GetKey() != gui.Contexts.Branches.Context.GetKey() { + if err := gui.switchContext(gui.Contexts.Branches.Context); err != nil { + return err + } + gui.State.Panels.Branches.SelectedLineIdx = 0 + } return gui.refreshSidePanels(refreshOptions{mode: ASYNC}) }) diff --git a/pkg/gui/context.go b/pkg/gui/context.go index 87d2fba3b..e5018b26b 100644 --- a/pkg/gui/context.go +++ b/pkg/gui/context.go @@ -46,6 +46,7 @@ type Context interface { SetWindowName(string) GetKey() string GetSelectedItemId() string + GetSelectedItem() ListItem SetParentContext(Context) GetParentContext() Context } @@ -63,6 +64,10 @@ func (c BasicContext) SetWindowName(windowName string) { panic("can't set window name on basic context") } +func (c BasicContext) GetSelectedItem() ListItem { + return nil +} + func (c BasicContext) GetWindowName() string { // TODO: fix this up return c.GetViewName() @@ -677,3 +682,14 @@ func (gui *Gui) rerenderView(viewName string) error { return context.HandleRender() } + +func (gui *Gui) getCurrentSideView() *gocui.View { + currentSideContext := gui.currentSideContext() + if currentSideContext == nil { + return nil + } + + view, _ := gui.g.View(currentSideContext.GetViewName()) + + return view +} diff --git a/pkg/gui/keybindings.go b/pkg/gui/keybindings.go index 76e28fdf5..51fcdd2a2 100644 --- a/pkg/gui/keybindings.go +++ b/pkg/gui/keybindings.go @@ -496,7 +496,7 @@ func (gui *Gui) GetInitialKeybindings() []*Binding { ViewName: "branches", Contexts: []string{LOCAL_BRANCHES_CONTEXT_KEY}, Key: gui.getKey("universal.new"), - Handler: gui.handleNewBranch, + Handler: gui.wrappedHandler(gui.handleNewBranchOffCurrentItem), Description: gui.Tr.SLocalize("newBranch"), }, { @@ -801,7 +801,7 @@ func (gui *Gui) GetInitialKeybindings() []*Binding { Contexts: []string{BRANCH_COMMITS_CONTEXT_KEY}, Key: gui.getKey("universal.new"), Modifier: gocui.ModNone, - Handler: gui.wrappedHandler(gui.handleNewBranchOffCommit), + Handler: gui.wrappedHandler(gui.handleNewBranchOffCurrentItem), Description: gui.Tr.SLocalize("createNewBranchFromCommit"), }, { @@ -860,6 +860,13 @@ func (gui *Gui) GetInitialKeybindings() []*Binding { Handler: gui.wrappedHandler(gui.handleCreateSubCommitResetMenu), Description: gui.Tr.SLocalize("viewResetOptions"), }, + { + ViewName: "branches", + Contexts: []string{SUB_COMMITS_CONTEXT_KEY}, + Key: gui.getKey("universal.new"), + Handler: gui.wrappedHandler(gui.handleNewBranchOffCurrentItem), + Description: gui.Tr.SLocalize("newBranch"), + }, { ViewName: "stash", Key: gui.getKey("universal.goInto"), @@ -1358,10 +1365,9 @@ func (gui *Gui) GetInitialKeybindings() []*Binding { ViewName: "branches", Contexts: []string{REMOTE_BRANCHES_CONTEXT_KEY}, Key: gui.getKey("universal.new"), - Handler: gui.handleNewBranchOffRemote, + Handler: gui.wrappedHandler(gui.handleNewBranchOffCurrentItem), Description: gui.Tr.SLocalize("newBranch"), }, - { ViewName: "branches", Contexts: []string{REMOTE_BRANCHES_CONTEXT_KEY}, diff --git a/pkg/gui/list_context.go b/pkg/gui/list_context.go index 1edaf410f..bf6aaeaaf 100644 --- a/pkg/gui/list_context.go +++ b/pkg/gui/list_context.go @@ -21,7 +21,7 @@ type ListContext struct { OnFocus func() error OnFocusLost func() error OnClickSelectedItem func() error - GetSelectedItem func() ListItem + SelectedItem func() ListItem GetPanelState func() IListPanelState Gui *Gui @@ -36,6 +36,10 @@ type ListItem interface { ID() string } +func (lc *ListContext) GetSelectedItem() ListItem { + return lc.SelectedItem() +} + func (lc *ListContext) GetContains() int { return lc.Contains } @@ -64,7 +68,7 @@ func (lc *ListContext) GetParentContext() Context { } func (lc *ListContext) GetSelectedItemId() string { - item := lc.GetSelectedItem() + item := lc.SelectedItem() if item == nil { return "" @@ -269,8 +273,8 @@ func (gui *Gui) filesListContext() *ListContext { GetDisplayStrings: func() [][]string { return presentation.GetFileListDisplayStrings(gui.State.Files, gui.State.Diff.Ref) }, - Contains: CONTAINS_NOTHING, - GetSelectedItem: func() ListItem { return gui.getSelectedFile() }, + Contains: CONTAINS_NOTHING, + SelectedItem: func() ListItem { return gui.getSelectedFile() }, } } @@ -287,8 +291,8 @@ func (gui *Gui) branchesListContext() *ListContext { GetDisplayStrings: func() [][]string { return presentation.GetBranchListDisplayStrings(gui.State.Branches, gui.State.ScreenMode != SCREEN_NORMAL, gui.State.Diff.Ref) }, - Contains: CONTAINS_COMMITS, - GetSelectedItem: func() ListItem { return gui.getSelectedBranch() }, + Contains: CONTAINS_COMMITS, + SelectedItem: func() ListItem { return gui.getSelectedBranch() }, } } @@ -306,8 +310,8 @@ func (gui *Gui) remotesListContext() *ListContext { GetDisplayStrings: func() [][]string { return presentation.GetRemoteListDisplayStrings(gui.State.Remotes, gui.State.Diff.Ref) }, - Contains: CONTAINS_BRANCHES, - GetSelectedItem: func() ListItem { return gui.getSelectedRemote() }, + Contains: CONTAINS_BRANCHES, + SelectedItem: func() ListItem { return gui.getSelectedRemote() }, } } @@ -324,8 +328,8 @@ func (gui *Gui) remoteBranchesListContext() *ListContext { GetDisplayStrings: func() [][]string { return presentation.GetRemoteBranchListDisplayStrings(gui.State.RemoteBranches, gui.State.Diff.Ref) }, - Contains: CONTAINS_COMMITS, - GetSelectedItem: func() ListItem { return gui.getSelectedRemoteBranch() }, + Contains: CONTAINS_COMMITS, + SelectedItem: func() ListItem { return gui.getSelectedRemoteBranch() }, } } @@ -342,8 +346,8 @@ func (gui *Gui) tagsListContext() *ListContext { GetDisplayStrings: func() [][]string { return presentation.GetTagListDisplayStrings(gui.State.Tags, gui.State.Diff.Ref) }, - Contains: CONTAINS_COMMITS, - GetSelectedItem: func() ListItem { return gui.getSelectedTag() }, + Contains: CONTAINS_COMMITS, + SelectedItem: func() ListItem { return gui.getSelectedTag() }, } } @@ -361,8 +365,8 @@ func (gui *Gui) branchCommitsListContext() *ListContext { GetDisplayStrings: func() [][]string { return presentation.GetCommitListDisplayStrings(gui.State.Commits, gui.State.ScreenMode != SCREEN_NORMAL, gui.cherryPickedCommitShaMap(), gui.State.Diff.Ref) }, - Contains: CONTAINS_FILES, - GetSelectedItem: func() ListItem { return gui.getSelectedLocalCommit() }, + Contains: CONTAINS_FILES, + SelectedItem: func() ListItem { return gui.getSelectedLocalCommit() }, } } @@ -379,8 +383,8 @@ func (gui *Gui) reflogCommitsListContext() *ListContext { GetDisplayStrings: func() [][]string { return presentation.GetReflogCommitListDisplayStrings(gui.State.FilteredReflogCommits, gui.State.ScreenMode != SCREEN_NORMAL, gui.State.Diff.Ref) }, - Contains: CONTAINS_FILES, - GetSelectedItem: func() ListItem { return gui.getSelectedReflogCommit() }, + Contains: CONTAINS_FILES, + SelectedItem: func() ListItem { return gui.getSelectedReflogCommit() }, } } @@ -397,8 +401,8 @@ func (gui *Gui) subCommitsListContext() *ListContext { GetDisplayStrings: func() [][]string { return presentation.GetCommitListDisplayStrings(gui.State.SubCommits, gui.State.ScreenMode != SCREEN_NORMAL, gui.cherryPickedCommitShaMap(), gui.State.Diff.Ref) }, - Contains: CONTAINS_COMMITS, - GetSelectedItem: func() ListItem { return gui.getSelectedSubCommit() }, + Contains: CONTAINS_COMMITS, + SelectedItem: func() ListItem { return gui.getSelectedSubCommit() }, } } @@ -415,8 +419,8 @@ func (gui *Gui) stashListContext() *ListContext { GetDisplayStrings: func() [][]string { return presentation.GetStashEntryListDisplayStrings(gui.State.StashEntries, gui.State.Diff.Ref) }, - Contains: CONTAINS_FILES, - GetSelectedItem: func() ListItem { return gui.getSelectedStashEntry() }, + Contains: CONTAINS_FILES, + SelectedItem: func() ListItem { return gui.getSelectedStashEntry() }, } } @@ -434,8 +438,8 @@ func (gui *Gui) commitFilesListContext() *ListContext { GetDisplayStrings: func() [][]string { return presentation.GetCommitFileListDisplayStrings(gui.State.CommitFiles, gui.State.Diff.Ref) }, - Contains: CONTAINS_NOTHING, - GetSelectedItem: func() ListItem { return gui.getSelectedCommitFile() }, + Contains: CONTAINS_NOTHING, + SelectedItem: func() ListItem { return gui.getSelectedCommitFile() }, } } diff --git a/pkg/gui/remote_branches_panel.go b/pkg/gui/remote_branches_panel.go index 62040c917..55027084e 100644 --- a/pkg/gui/remote_branches_panel.go +++ b/pkg/gui/remote_branches_panel.go @@ -122,27 +122,3 @@ func (gui *Gui) handleCreateResetToRemoteBranchMenu(g *gocui.Gui, v *gocui.View) return gui.createResetMenu(selectedBranch.FullName()) } - -func (gui *Gui) handleNewBranchOffRemote(g *gocui.Gui, v *gocui.View) error { - branch := gui.getSelectedRemoteBranch() - if branch == nil { - return nil - } - message := gui.Tr.TemplateLocalize( - "NewBranchNameBranchOff", - Teml{ - "branchName": branch.FullName(), - }, - ) - return gui.prompt(v, message, branch.FullName(), func(response string) error { - if err := gui.GitCommand.NewBranch(response, branch.FullName()); err != nil { - return gui.surfaceError(err) - } - gui.State.Panels.Branches.SelectedLineIdx = 0 - - if err := gui.switchContext(gui.Contexts.Branches.Context); err != nil { - return err - } - return gui.refreshSidePanels(refreshOptions{mode: ASYNC}) - }) -}