Skip to content

Commit 947ffde

Browse files
committed
Allow clicking in focused main view to go to staging
Only works if a file is selected.
1 parent a640b1d commit 947ffde

File tree

7 files changed

+70
-9
lines changed

7 files changed

+70
-9
lines changed

pkg/gui/context/base_context.go

+20-8
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ type BaseContext struct {
1313
windowName string
1414
onGetOptionsMap func() map[string]string
1515

16-
keybindingsFns []types.KeybindingsFn
17-
mouseKeybindingsFns []types.MouseKeybindingsFn
18-
onClickFn func() error
19-
onRenderToMainFn func()
20-
onFocusFn onFocusFn
21-
onFocusLostFn onFocusLostFn
16+
keybindingsFns []types.KeybindingsFn
17+
mouseKeybindingsFns []types.MouseKeybindingsFn
18+
onClickFn func() error
19+
onClickFocusedMainViewFn onClickFocusedMainViewFn
20+
onRenderToMainFn func()
21+
onFocusFn onFocusFn
22+
onFocusLostFn onFocusLostFn
2223

2324
focusable bool
2425
transient bool
@@ -31,8 +32,9 @@ type BaseContext struct {
3132
}
3233

3334
type (
34-
onFocusFn = func(types.OnFocusOpts)
35-
onFocusLostFn = func(types.OnFocusLostOpts)
35+
onFocusFn = func(types.OnFocusOpts)
36+
onFocusLostFn = func(types.OnFocusLostOpts)
37+
onClickFocusedMainViewFn = func(mainViewName string, clickedLineIdx int) error
3638
)
3739

3840
var _ types.IBaseContext = &BaseContext{}
@@ -144,10 +146,20 @@ func (self *BaseContext) AddOnClickFn(fn func() error) {
144146
}
145147
}
146148

149+
func (self *BaseContext) AddOnClickFocusedMainViewFn(fn onClickFocusedMainViewFn) {
150+
if fn != nil {
151+
self.onClickFocusedMainViewFn = fn
152+
}
153+
}
154+
147155
func (self *BaseContext) GetOnClick() func() error {
148156
return self.onClickFn
149157
}
150158

159+
func (self *BaseContext) GetOnClickFocusedMainView() onClickFocusedMainViewFn {
160+
return self.onClickFocusedMainViewFn
161+
}
162+
151163
func (self *BaseContext) AddOnRenderToMainFn(fn func()) {
152164
if fn != nil {
153165
self.onRenderToMainFn = fn

pkg/gui/controllers/attach.go

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ func AttachControllers(context types.Context, controllers ...types.IController)
77
context.AddKeybindingsFn(controller.GetKeybindings)
88
context.AddMouseKeybindingsFn(controller.GetMouseKeybindings)
99
context.AddOnClickFn(controller.GetOnClick())
10+
context.AddOnClickFocusedMainViewFn(controller.GetOnClickFocusedMainView())
1011
context.AddOnRenderToMainFn(controller.GetOnRenderToMain())
1112
context.AddOnFocusFn(controller.GetOnFocus())
1213
context.AddOnFocusLostFn(controller.GetOnFocusLost())

pkg/gui/controllers/base_controller.go

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ func (self *baseController) GetOnClick() func() error {
1919
return nil
2020
}
2121

22+
func (self *baseController) GetOnClickFocusedMainView() func(mainViewName string, clickedLineIdx int) error {
23+
return nil
24+
}
25+
2226
func (self *baseController) GetOnRenderToMain() func() {
2327
return nil
2428
}

pkg/gui/controllers/commits_files_controller.go

+10
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,16 @@ func (self *CommitFilesController) expandAll() error {
531531
return nil
532532
}
533533

534+
func (self *CommitFilesController) GetOnClickFocusedMainView() func(mainViewName string, clickedLineIdx int) error {
535+
return func(mainViewName string, clickedLineIdx int) error {
536+
node := self.getSelectedItem()
537+
if node != nil && node.File != nil {
538+
return self.enterCommitFile(node, types.OnFocusOpts{ClickedWindowName: mainViewName, ClickedViewLineIdx: clickedLineIdx})
539+
}
540+
return nil
541+
}
542+
}
543+
534544
// NOTE: these functions are identical to those in files_controller.go (except for types) and
535545
// could also be cleaned up with some generics
536546
func normalisedSelectedCommitFileNodes(selectedNodes []*filetree.CommitFileNode) []*filetree.CommitFileNode {

pkg/gui/controllers/files_controller.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,16 @@ func (self *FilesController) GetOnClick() func() error {
290290
})
291291
}
292292

293+
func (self *FilesController) GetOnClickFocusedMainView() func(mainViewName string, clickedLineIdx int) error {
294+
return func(mainViewName string, clickedLineIdx int) error {
295+
node := self.getSelectedItem()
296+
if node != nil && node.File != nil {
297+
return self.EnterFile(types.OnFocusOpts{ClickedWindowName: mainViewName, ClickedViewLineIdx: clickedLineIdx})
298+
}
299+
return nil
300+
}
301+
}
302+
293303
// if we are dealing with a status for which there is no key in this map,
294304
// then we won't optimistically render: we'll just let `git status` tell
295305
// us what the new status is.
@@ -512,7 +522,8 @@ func (self *FilesController) EnterFile(opts types.OnFocusOpts) error {
512522
return errors.New(self.c.Tr.FileStagingRequirements)
513523
}
514524

515-
self.c.Context().Push(self.c.Contexts().Staging, opts)
525+
context := lo.Ternary(opts.ClickedWindowName == "secondary", self.c.Contexts().StagingSecondary, self.c.Contexts().Staging)
526+
self.c.Context().Push(context, opts)
516527
return nil
517528
}
518529

pkg/gui/controllers/main_view_controller.go

+19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package controllers
22

33
import (
4+
"github.com/jesseduffield/gocui"
45
"github.com/jesseduffield/lazygit/pkg/gui/context"
56
"github.com/jesseduffield/lazygit/pkg/gui/types"
67
"github.com/jesseduffield/lazygit/pkg/tasks"
@@ -56,6 +57,16 @@ func (self *MainViewController) GetKeybindings(opts types.KeybindingsOpts) []*ty
5657
}
5758
}
5859

60+
func (self *MainViewController) GetMouseKeybindings(opts types.KeybindingsOpts) []*gocui.ViewMouseBinding {
61+
return []*gocui.ViewMouseBinding{
62+
{
63+
ViewName: self.context.GetViewName(),
64+
Key: gocui.MouseLeft,
65+
Handler: self.onClick,
66+
},
67+
}
68+
}
69+
5970
func (self *MainViewController) Context() types.Context {
6071
return self.context
6172
}
@@ -74,6 +85,14 @@ func (self *MainViewController) escape() error {
7485
return nil
7586
}
7687

88+
func (self *MainViewController) onClick(opts gocui.ViewMouseBindingOpts) error {
89+
parentCtx := self.context.GetParentContext()
90+
if parentCtx.GetOnClickFocusedMainView() != nil {
91+
return parentCtx.GetOnClickFocusedMainView()(self.context.GetViewName(), opts.Y)
92+
}
93+
return nil
94+
}
95+
7796
func (self *MainViewController) openSearch() error {
7897
if manager, ok := (*self.viewBufferManagerMap)[self.context.GetViewName()]; ok {
7998
manager.ReadToEnd(func() {

pkg/gui/types/context.go

+4
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ type IBaseContext interface {
9494
// our list controller can come along and wrap it in a list-specific click handler.
9595
// We'll need to think of a better way to do this.
9696
AddOnClickFn(func() error)
97+
// Likewise for the focused main view: we need this to communicate between a
98+
// side panel controller and the focused main view controller.
99+
AddOnClickFocusedMainViewFn(func(mainViewName string, clickedLineIdx int) error)
97100

98101
AddOnRenderToMainFn(func())
99102
AddOnFocusFn(func(OnFocusOpts))
@@ -240,6 +243,7 @@ type HasKeybindings interface {
240243
GetKeybindings(opts KeybindingsOpts) []*Binding
241244
GetMouseKeybindings(opts KeybindingsOpts) []*gocui.ViewMouseBinding
242245
GetOnClick() func() error
246+
GetOnClickFocusedMainView() func(mainViewName string, clickedLineIdx int) error
243247
GetOnRenderToMain() func()
244248
GetOnFocus() func(OnFocusOpts)
245249
GetOnFocusLost() func(OnFocusLostOpts)

0 commit comments

Comments
 (0)