Skip to content

Commit cacc574

Browse files
committed
Select line that is in the middle of the screen
1 parent 2951461 commit cacc574

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
@@ -91,6 +91,14 @@ func (self *MainViewController) Context() types.Context {
9191
return self.context
9292
}
9393

94+
func (self *MainViewController) GetOnFocus() func(types.OnFocusOpts) {
95+
return func(opts types.OnFocusOpts) {
96+
if opts.ClickedWindowName != "" {
97+
self.context.GetView().FocusPoint(0, opts.ClickedViewLineIdx)
98+
}
99+
}
100+
}
101+
94102
func (self *MainViewController) togglePanel() error {
95103
if self.otherContext.GetView().Visible {
96104
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, ok := mainViewContext.(types.ISearchableContext); ok {
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)