Skip to content

Commit 7e1be80

Browse files
committed
Select line that is in the middle of the screen
1 parent 2b26805 commit 7e1be80

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

pkg/gui/controllers/main_view_controller.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ func (self *MainViewController) Context() types.Context {
8383
return self.context
8484
}
8585

86+
func (self *MainViewController) GetOnFocus() func(types.OnFocusOpts) {
87+
return func(opts types.OnFocusOpts) {
88+
if opts.ClickedWindowName != "" {
89+
self.context.GetView().FocusPoint(0, opts.ClickedViewLineIdx)
90+
}
91+
}
92+
}
93+
8694
func (self *MainViewController) togglePanel() error {
8795
if self.otherContext.GetView().Visible {
8896
self.otherContext.SetParentContext(self.context.GetParentContext())

pkg/gui/controllers/switch_to_focused_main_view_controller.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package controllers
33
import (
44
"github.com/jesseduffield/gocui"
55
"github.com/jesseduffield/lazygit/pkg/gui/types"
6+
"github.com/samber/lo"
67
)
78

89
// This controller is for all contexts that can focus their main view.
@@ -60,23 +61,31 @@ func (self *SwitchToFocusedMainViewController) Context() types.Context {
6061
}
6162

6263
func (self *SwitchToFocusedMainViewController) onClickMain(opts gocui.ViewMouseBindingOpts) error {
63-
return self.focusMainView("main")
64+
return self.focusMainView("main", opts.Y)
6465
}
6566

6667
func (self *SwitchToFocusedMainViewController) onClickSecondary(opts gocui.ViewMouseBindingOpts) error {
67-
return self.focusMainView("secondary")
68+
return self.focusMainView("secondary", opts.Y)
6869
}
6970

7071
func (self *SwitchToFocusedMainViewController) handleFocusMainView() error {
71-
return self.focusMainView("main")
72+
return self.focusMainView("main", -1)
7273
}
7374

74-
func (self *SwitchToFocusedMainViewController) focusMainView(mainViewName string) error {
75+
func (self *SwitchToFocusedMainViewController) focusMainView(mainViewName string, clickedViewLineIdx int) error {
7576
mainViewContext := self.c.Helpers().Window.GetContextForWindow(mainViewName)
7677
mainViewContext.SetParentContext(self.context)
7778
if context := mainViewContext.(types.ISearchableContext); context != nil {
7879
context.ClearSearchString()
7980
}
80-
self.c.Context().Push(mainViewContext, types.OnFocusOpts{})
81+
onFocusOpts := types.OnFocusOpts{ClickedWindowName: mainViewName}
82+
if clickedViewLineIdx >= 0 {
83+
onFocusOpts.ClickedViewLineIdx = clickedViewLineIdx
84+
} else {
85+
mainView := mainViewContext.GetView()
86+
lineIdx := mainView.OriginY() + mainView.Height()/2
87+
onFocusOpts.ClickedViewLineIdx = lo.Clamp(lineIdx, 0, mainView.LinesHeight()-1)
88+
}
89+
self.c.Context().Push(mainViewContext, onFocusOpts)
8190
return nil
8291
}

0 commit comments

Comments
 (0)