Skip to content

Commit 020413e

Browse files
committed
Add navigation keybindings to focused main view
1 parent 2a6d0bf commit 020413e

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

pkg/gui/controllers.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ func (gui *Gui) resetHelpersAndControllers() {
181181
contextLinesController := controllers.NewContextLinesController(common)
182182
renameSimilarityThresholdController := controllers.NewRenameSimilarityThresholdController(common)
183183
verticalScrollControllerFactory := controllers.NewVerticalScrollControllerFactory(common)
184+
viewSelectionControllerFactory := controllers.NewViewSelectionControllerFactory(common)
184185

185186
branchesController := controllers.NewBranchesController(common)
186187
gitFlowController := controllers.NewGitFlowController(common)
@@ -327,11 +328,13 @@ func (gui *Gui) resetHelpersAndControllers() {
327328
controllers.AttachControllers(gui.State.Contexts.Normal,
328329
mainViewController,
329330
verticalScrollControllerFactory.Create(gui.State.Contexts.Normal),
331+
viewSelectionControllerFactory.Create(gui.State.Contexts.Normal),
330332
)
331333

332334
controllers.AttachControllers(gui.State.Contexts.NormalSecondary,
333335
secondaryViewController,
334336
verticalScrollControllerFactory.Create(gui.State.Contexts.NormalSecondary),
337+
viewSelectionControllerFactory.Create(gui.State.Contexts.NormalSecondary),
335338
)
336339

337340
controllers.AttachControllers(gui.State.Contexts.Files,
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package controllers
2+
3+
import (
4+
"github.com/jesseduffield/gocui"
5+
"github.com/jesseduffield/lazygit/pkg/gui/types"
6+
)
7+
8+
type ViewSelectionControllerFactory struct {
9+
c *ControllerCommon
10+
}
11+
12+
func NewViewSelectionControllerFactory(c *ControllerCommon) *ViewSelectionControllerFactory {
13+
return &ViewSelectionControllerFactory{
14+
c: c,
15+
}
16+
}
17+
18+
func (self *ViewSelectionControllerFactory) Create(context types.Context) types.IController {
19+
return &ViewSelectionController{
20+
baseController: baseController{},
21+
c: self.c,
22+
context: context,
23+
}
24+
}
25+
26+
type ViewSelectionController struct {
27+
baseController
28+
c *ControllerCommon
29+
30+
context types.Context
31+
}
32+
33+
func (self *ViewSelectionController) Context() types.Context {
34+
return self.context
35+
}
36+
37+
func (self *ViewSelectionController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
38+
return []*types.Binding{
39+
{Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.PrevItem), Handler: self.handlePrevLine},
40+
{Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.PrevItemAlt), Handler: self.handlePrevLine},
41+
{Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.NextItem), Handler: self.handleNextLine},
42+
{Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.NextItemAlt), Handler: self.handleNextLine},
43+
{Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.PrevPage), Handler: self.handlePrevPage, Description: self.c.Tr.PrevPage},
44+
{Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.NextPage), Handler: self.handleNextPage, Description: self.c.Tr.NextPage},
45+
{Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.GotoTop), Handler: self.handleGotoTop, Description: self.c.Tr.GotoTop, Alternative: "<home>"},
46+
{Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.GotoBottom), Handler: self.handleGotoBottom, Description: self.c.Tr.GotoBottom, Alternative: "<end>"},
47+
{Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.GotoTopAlt), Handler: self.handleGotoTop},
48+
{Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.GotoBottomAlt), Handler: self.handleGotoBottom},
49+
}
50+
}
51+
52+
func (self *ViewSelectionController) GetMouseKeybindings(opts types.KeybindingsOpts) []*gocui.ViewMouseBinding {
53+
return []*gocui.ViewMouseBinding{}
54+
}
55+
56+
func (self *ViewSelectionController) handleLineChange(delta int) {
57+
if delta > 0 {
58+
if manager := self.c.GetViewBufferManagerForView(self.context.GetView()); manager != nil {
59+
manager.ReadLines(delta)
60+
}
61+
}
62+
63+
v := self.Context().GetView()
64+
if delta < 0 {
65+
v.ScrollUp(-delta)
66+
} else {
67+
v.ScrollDown(delta)
68+
}
69+
}
70+
71+
func (self *ViewSelectionController) handlePrevLine() error {
72+
self.handleLineChange(-1)
73+
return nil
74+
}
75+
76+
func (self *ViewSelectionController) handleNextLine() error {
77+
self.handleLineChange(1)
78+
return nil
79+
}
80+
81+
func (self *ViewSelectionController) handlePrevPage() error {
82+
self.handleLineChange(-self.context.GetViewTrait().PageDelta())
83+
return nil
84+
}
85+
86+
func (self *ViewSelectionController) handleNextPage() error {
87+
self.handleLineChange(self.context.GetViewTrait().PageDelta())
88+
return nil
89+
}
90+
91+
func (self *ViewSelectionController) handleGotoTop() error {
92+
v := self.Context().GetView()
93+
self.handleLineChange(-v.ViewLinesHeight())
94+
return nil
95+
}
96+
97+
func (self *ViewSelectionController) handleGotoBottom() error {
98+
v := self.Context().GetView()
99+
self.handleLineChange(v.ViewLinesHeight())
100+
return nil
101+
}

0 commit comments

Comments
 (0)