From 4cf8d1178369bb5e1c0f1a42628c4fc4376d606f Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Tue, 25 Mar 2025 15:01:16 +0100 Subject: [PATCH] Select line that is in the middle of the screen --- pkg/gui/controllers/main_view_controller.go | 8 ++++++++ .../switch_to_focused_main_view_controller.go | 19 ++++++++++++++----- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/pkg/gui/controllers/main_view_controller.go b/pkg/gui/controllers/main_view_controller.go index 5e2e1caab..289e2e101 100644 --- a/pkg/gui/controllers/main_view_controller.go +++ b/pkg/gui/controllers/main_view_controller.go @@ -91,6 +91,14 @@ func (self *MainViewController) Context() types.Context { return self.context } +func (self *MainViewController) GetOnFocus() func(types.OnFocusOpts) { + return func(opts types.OnFocusOpts) { + if opts.ClickedWindowName != "" { + self.context.GetView().FocusPoint(0, opts.ClickedViewLineIdx) + } + } +} + func (self *MainViewController) togglePanel() error { if self.otherContext.GetView().Visible { self.otherContext.SetParentContext(self.context.GetParentContext()) diff --git a/pkg/gui/controllers/switch_to_focused_main_view_controller.go b/pkg/gui/controllers/switch_to_focused_main_view_controller.go index cb03f5e15..27d35e90b 100644 --- a/pkg/gui/controllers/switch_to_focused_main_view_controller.go +++ b/pkg/gui/controllers/switch_to_focused_main_view_controller.go @@ -3,6 +3,7 @@ package controllers import ( "github.com/jesseduffield/gocui" "github.com/jesseduffield/lazygit/pkg/gui/types" + "github.com/samber/lo" ) // This controller is for all contexts that can focus their main view. @@ -60,23 +61,31 @@ func (self *SwitchToFocusedMainViewController) Context() types.Context { } func (self *SwitchToFocusedMainViewController) onClickMain(opts gocui.ViewMouseBindingOpts) error { - return self.focusMainView("main") + return self.focusMainView("main", opts.Y) } func (self *SwitchToFocusedMainViewController) onClickSecondary(opts gocui.ViewMouseBindingOpts) error { - return self.focusMainView("secondary") + return self.focusMainView("secondary", opts.Y) } func (self *SwitchToFocusedMainViewController) handleFocusMainView() error { - return self.focusMainView("main") + return self.focusMainView("main", -1) } -func (self *SwitchToFocusedMainViewController) focusMainView(mainViewName string) error { +func (self *SwitchToFocusedMainViewController) focusMainView(mainViewName string, clickedViewLineIdx int) error { mainViewContext := self.c.Helpers().Window.GetContextForWindow(mainViewName) mainViewContext.SetParentContext(self.context) if context, ok := mainViewContext.(types.ISearchableContext); ok { context.ClearSearchString() } - self.c.Context().Push(mainViewContext, types.OnFocusOpts{}) + onFocusOpts := types.OnFocusOpts{ClickedWindowName: mainViewName} + if clickedViewLineIdx >= 0 { + onFocusOpts.ClickedViewLineIdx = clickedViewLineIdx + } else { + mainView := mainViewContext.GetView() + lineIdx := mainView.OriginY() + mainView.Height()/2 + onFocusOpts.ClickedViewLineIdx = lo.Clamp(lineIdx, 0, mainView.LinesHeight()-1) + } + self.c.Context().Push(mainViewContext, onFocusOpts) return nil }