Select line that is in the middle of the screen

TODO: doesn't work the very first time
This commit is contained in:
Stefan Haller 2024-10-15 16:36:19 +02:00
parent e031f437e9
commit 50426cda3a
3 changed files with 25 additions and 7 deletions

View File

@ -190,8 +190,8 @@ func (gui *Gui) resetHelpersAndControllers() {
patchExplorerControllerFactory := controllers.NewPatchExplorerControllerFactory(common)
stagingController := controllers.NewStagingController(common, gui.State.Contexts.Staging, gui.State.Contexts.StagingSecondary, false)
stagingSecondaryController := controllers.NewStagingController(common, gui.State.Contexts.StagingSecondary, gui.State.Contexts.Staging, true)
diffController := controllers.NewDiffController(common, gui.State.Contexts.Diff, gui.State.Contexts.DiffSecondary)
diffSecondaryController := controllers.NewDiffController(common, gui.State.Contexts.DiffSecondary, gui.State.Contexts.Diff)
diffController := controllers.NewDiffController(common, gui.State.Contexts.Diff, gui.State.Contexts.DiffSecondary, &gui.viewBufferManagerMap)
diffSecondaryController := controllers.NewDiffController(common, gui.State.Contexts.DiffSecondary, gui.State.Contexts.Diff, &gui.viewBufferManagerMap)
patchBuildingController := controllers.NewPatchBuildingController(common)
snakeController := controllers.NewSnakeController(common)
reflogCommitsController := controllers.NewReflogCommitsController(common)

View File

@ -3,6 +3,7 @@ package controllers
import (
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/jesseduffield/lazygit/pkg/tasks"
)
type DiffController struct {
@ -11,6 +12,8 @@ type DiffController struct {
context types.Context
otherContext types.Context
viewBufferManagerMap *map[string]*tasks.ViewBufferManager
}
var _ types.IController = &DiffController{}
@ -19,12 +22,14 @@ func NewDiffController(
c *ControllerCommon,
context types.Context,
otherContext types.Context,
viewBufferManagerMap *map[string]*tasks.ViewBufferManager,
) *DiffController {
return &DiffController{
baseController: baseController{},
c: c,
context: context,
otherContext: otherContext,
baseController: baseController{},
c: c,
context: context,
otherContext: otherContext,
viewBufferManagerMap: viewBufferManagerMap,
}
}
@ -56,6 +61,15 @@ func (self *DiffController) GetMouseKeybindings(opts types.KeybindingsOpts) []*g
func (self *DiffController) GetOnFocus() func(types.OnFocusOpts) {
return func(opts types.OnFocusOpts) {
self.c.Helpers().Diff.RenderFilesViewDiff(self.c.MainViewPairs().Diff)
if opts.ClickedWindowName == "main" {
if manager, ok := (*self.viewBufferManagerMap)[self.context.GetViewName()]; ok {
// TODO: doesn't work the first time after launching. Need to
// find a way to construct the ViewBufferManager for this view
// earlier.
manager.ReadLines(opts.ClickedViewLineIdx - self.context.GetView().LinesHeight() + 1)
}
self.context.GetView().FocusPoint(0, opts.ClickedViewLineIdx)
}
}
}

View File

@ -621,7 +621,11 @@ func (self *FilesController) refresh() error {
}
func (self *FilesController) focusMainView() error {
self.c.Context().Push(self.c.Contexts().Diff, types.OnFocusOpts{})
mainView := self.c.Helpers().Window.TopViewInWindow("main", false)
lineIdx := mainView.OriginY() + mainView.Height()/2
lineIdx = lo.Clamp(lineIdx, 0, mainView.LinesHeight()-1)
self.c.Context().Push(self.c.Contexts().Diff,
types.OnFocusOpts{ClickedWindowName: "main", ClickedViewLineIdx: lineIdx})
return nil
}