From 4daa9edccd125ff16e8f1fbde3afb03404c92134 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Sun, 9 Aug 2026 19:18:04 +0200 Subject: [PATCH] Fold ViewSelectionController into MainViewController The focused main view is about to get a real diff selection, which makes its up/down/page/top/bottom keys mode-aware: what they do depends on the select state the main view controller owns. Keeping them in a separate controller would mean either duplicating that state or reaching across controllers for it, so move them to where the state will live. The two controllers were attached to the same pair of contexts and nothing else, so nothing else can notice. Co-authored-by: Claude Opus 5 (1M context) --- pkg/gui/controllers.go | 3 - pkg/gui/controllers/main_view_controller.go | 56 ++++++++++ .../controllers/view_selection_controller.go | 100 ------------------ 3 files changed, 56 insertions(+), 103 deletions(-) delete mode 100644 pkg/gui/controllers/view_selection_controller.go diff --git a/pkg/gui/controllers.go b/pkg/gui/controllers.go index dc5a77834..8668277c6 100644 --- a/pkg/gui/controllers.go +++ b/pkg/gui/controllers.go @@ -178,7 +178,6 @@ func (gui *Gui) resetHelpersAndControllers() { contextLinesController := controllers.NewContextLinesController(common) renameSimilarityThresholdController := controllers.NewRenameSimilarityThresholdController(common) verticalScrollControllerFactory := controllers.NewVerticalScrollControllerFactory(common) - viewSelectionControllerFactory := controllers.NewViewSelectionControllerFactory(common) branchesController := controllers.NewBranchesController(common) gitFlowController := controllers.NewGitFlowController(common) @@ -315,13 +314,11 @@ func (gui *Gui) resetHelpersAndControllers() { controllers.AttachControllers(gui.State.Contexts.Normal, mainViewController, verticalScrollControllerFactory.Create(gui.State.Contexts.Normal), - viewSelectionControllerFactory.Create(gui.State.Contexts.Normal), ) controllers.AttachControllers(gui.State.Contexts.NormalSecondary, secondaryViewController, verticalScrollControllerFactory.Create(gui.State.Contexts.NormalSecondary), - viewSelectionControllerFactory.Create(gui.State.Contexts.NormalSecondary), ) controllers.AttachControllers(gui.State.Contexts.Files, diff --git a/pkg/gui/controllers/main_view_controller.go b/pkg/gui/controllers/main_view_controller.go index 5bde8c5ff..648a96b98 100644 --- a/pkg/gui/controllers/main_view_controller.go +++ b/pkg/gui/controllers/main_view_controller.go @@ -51,6 +51,12 @@ func (self *MainViewController) GetKeybindings(opts types.KeybindingsOpts) []*ty Description: self.c.Tr.StartSearch, Tag: "navigation", }, + {Tag: "navigation", Keys: opts.GetKeys(opts.Config.Universal.PrevItem), Handler: self.handlePrevLine}, + {Tag: "navigation", Keys: opts.GetKeys(opts.Config.Universal.NextItem), Handler: self.handleNextLine}, + {Tag: "navigation", Keys: opts.GetKeys(opts.Config.Universal.PrevPage), Handler: self.handlePrevPage, Description: self.c.Tr.PrevPage}, + {Tag: "navigation", Keys: opts.GetKeys(opts.Config.Universal.NextPage), Handler: self.handleNextPage, Description: self.c.Tr.NextPage}, + {Tag: "navigation", Keys: opts.GetKeys(opts.Config.Universal.GotoTop), Handler: self.handleGotoTop, Description: self.c.Tr.GotoTop}, + {Tag: "navigation", Keys: opts.GetKeys(opts.Config.Universal.GotoBottom), Handler: self.handleGotoBottom, Description: self.c.Tr.GotoBottom}, } } @@ -105,6 +111,56 @@ func (self *MainViewController) onClickInOtherViewOfMainViewPair(opts gocui.View return nil } +func (self *MainViewController) handleLineChange(delta int) { + v := self.context.GetView() + if delta < 0 { + v.ScrollUp(-delta) + } else { + v.ScrollDown(delta) + self.c.ReadLinesToFillView(v) + } +} + +func (self *MainViewController) handlePrevLine() error { + self.handleLineChange(-1) + return nil +} + +func (self *MainViewController) handleNextLine() error { + self.handleLineChange(1) + return nil +} + +func (self *MainViewController) handlePrevPage() error { + self.handleLineChange(-self.context.GetViewTrait().PageDelta()) + return nil +} + +func (self *MainViewController) handleNextPage() error { + self.handleLineChange(self.context.GetViewTrait().PageDelta()) + return nil +} + +func (self *MainViewController) handleGotoTop() error { + v := self.context.GetView() + self.handleLineChange(-v.ViewLinesHeight()) + return nil +} + +func (self *MainViewController) handleGotoBottom() error { + if manager := self.c.GetViewBufferManagerForView(self.context.GetView()); manager != nil { + manager.ReadToEnd(func() { + self.c.OnUIThread(func() error { + v := self.context.GetView() + self.handleLineChange(v.ViewLinesHeight()) + return nil + }) + }) + } + + return nil +} + func (self *MainViewController) openSearch() error { if manager := self.c.GetViewBufferManagerForView(self.context.GetView()); manager != nil { manager.ReadToEnd(func() { diff --git a/pkg/gui/controllers/view_selection_controller.go b/pkg/gui/controllers/view_selection_controller.go deleted file mode 100644 index 1a97a9a30..000000000 --- a/pkg/gui/controllers/view_selection_controller.go +++ /dev/null @@ -1,100 +0,0 @@ -package controllers - -import ( - "github.com/jesseduffield/lazygit/pkg/gocui" - "github.com/jesseduffield/lazygit/pkg/gui/types" -) - -type ViewSelectionControllerFactory struct { - c *ControllerCommon -} - -func NewViewSelectionControllerFactory(c *ControllerCommon) *ViewSelectionControllerFactory { - return &ViewSelectionControllerFactory{ - c: c, - } -} - -func (self *ViewSelectionControllerFactory) Create(context types.Context) types.IController { - return &ViewSelectionController{ - baseController: baseController{}, - c: self.c, - context: context, - } -} - -type ViewSelectionController struct { - baseController - c *ControllerCommon - - context types.Context -} - -func (self *ViewSelectionController) Context() types.Context { - return self.context -} - -func (self *ViewSelectionController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding { - return []*types.Binding{ - {Tag: "navigation", Keys: opts.GetKeys(opts.Config.Universal.PrevItem), Handler: self.handlePrevLine}, - {Tag: "navigation", Keys: opts.GetKeys(opts.Config.Universal.NextItem), Handler: self.handleNextLine}, - {Tag: "navigation", Keys: opts.GetKeys(opts.Config.Universal.PrevPage), Handler: self.handlePrevPage, Description: self.c.Tr.PrevPage}, - {Tag: "navigation", Keys: opts.GetKeys(opts.Config.Universal.NextPage), Handler: self.handleNextPage, Description: self.c.Tr.NextPage}, - {Tag: "navigation", Keys: opts.GetKeys(opts.Config.Universal.GotoTop), Handler: self.handleGotoTop, Description: self.c.Tr.GotoTop}, - {Tag: "navigation", Keys: opts.GetKeys(opts.Config.Universal.GotoBottom), Handler: self.handleGotoBottom, Description: self.c.Tr.GotoBottom}, - } -} - -func (self *ViewSelectionController) GetMouseKeybindings(opts types.KeybindingsOpts) []*gocui.ViewMouseBinding { - return []*gocui.ViewMouseBinding{} -} - -func (self *ViewSelectionController) handleLineChange(delta int) { - v := self.Context().GetView() - if delta < 0 { - v.ScrollUp(-delta) - } else { - v.ScrollDown(delta) - self.c.ReadLinesToFillView(v) - } -} - -func (self *ViewSelectionController) handlePrevLine() error { - self.handleLineChange(-1) - return nil -} - -func (self *ViewSelectionController) handleNextLine() error { - self.handleLineChange(1) - return nil -} - -func (self *ViewSelectionController) handlePrevPage() error { - self.handleLineChange(-self.context.GetViewTrait().PageDelta()) - return nil -} - -func (self *ViewSelectionController) handleNextPage() error { - self.handleLineChange(self.context.GetViewTrait().PageDelta()) - return nil -} - -func (self *ViewSelectionController) handleGotoTop() error { - v := self.Context().GetView() - self.handleLineChange(-v.ViewLinesHeight()) - return nil -} - -func (self *ViewSelectionController) handleGotoBottom() error { - if manager := self.c.GetViewBufferManagerForView(self.context.GetView()); manager != nil { - manager.ReadToEnd(func() { - self.c.OnUIThread(func() error { - v := self.Context().GetView() - self.handleLineChange(v.ViewLinesHeight()) - return nil - }) - }) - } - - return nil -}