Skip to content

Commit 1b98c1b

Browse files
committed
Add navigation keybindings to focused main view
1 parent 416ba98 commit 1b98c1b

File tree

2 files changed

+109
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)