diff --git a/pkg/gui/context/base_context.go b/pkg/gui/context/base_context.go index 76482ad0f..b72374392 100644 --- a/pkg/gui/context/base_context.go +++ b/pkg/gui/context/base_context.go @@ -13,12 +13,13 @@ type BaseContext struct { windowName string onGetOptionsMap func() map[string]string - keybindingsFns []types.KeybindingsFn - mouseKeybindingsFns []types.MouseKeybindingsFn - onClickFn func() error - onRenderToMainFn func() - onFocusFn onFocusFn - onFocusLostFn onFocusLostFn + keybindingsFns []types.KeybindingsFn + mouseKeybindingsFns []types.MouseKeybindingsFn + onClickFn func() error + onClickFocusedMainViewFn onClickFocusedMainViewFn + onRenderToMainFn func() + onFocusFn onFocusFn + onFocusLostFn onFocusLostFn focusable bool transient bool @@ -31,8 +32,9 @@ type BaseContext struct { } type ( - onFocusFn = func(types.OnFocusOpts) - onFocusLostFn = func(types.OnFocusLostOpts) + onFocusFn = func(types.OnFocusOpts) + onFocusLostFn = func(types.OnFocusLostOpts) + onClickFocusedMainViewFn = func(mainViewName string, clickedLineIdx int) error ) var _ types.IBaseContext = &BaseContext{} @@ -144,10 +146,20 @@ func (self *BaseContext) AddOnClickFn(fn func() error) { } } +func (self *BaseContext) AddOnClickFocusedMainViewFn(fn onClickFocusedMainViewFn) { + if fn != nil { + self.onClickFocusedMainViewFn = fn + } +} + func (self *BaseContext) GetOnClick() func() error { return self.onClickFn } +func (self *BaseContext) GetOnClickFocusedMainView() onClickFocusedMainViewFn { + return self.onClickFocusedMainViewFn +} + func (self *BaseContext) AddOnRenderToMainFn(fn func()) { if fn != nil { self.onRenderToMainFn = fn diff --git a/pkg/gui/controllers/attach.go b/pkg/gui/controllers/attach.go index 6a24fd7d3..8c1dbb91e 100644 --- a/pkg/gui/controllers/attach.go +++ b/pkg/gui/controllers/attach.go @@ -7,6 +7,7 @@ func AttachControllers(context types.Context, controllers ...types.IController) context.AddKeybindingsFn(controller.GetKeybindings) context.AddMouseKeybindingsFn(controller.GetMouseKeybindings) context.AddOnClickFn(controller.GetOnClick()) + context.AddOnClickFocusedMainViewFn(controller.GetOnClickFocusedMainView()) context.AddOnRenderToMainFn(controller.GetOnRenderToMain()) context.AddOnFocusFn(controller.GetOnFocus()) context.AddOnFocusLostFn(controller.GetOnFocusLost()) diff --git a/pkg/gui/controllers/base_controller.go b/pkg/gui/controllers/base_controller.go index adff1927a..0e1229222 100644 --- a/pkg/gui/controllers/base_controller.go +++ b/pkg/gui/controllers/base_controller.go @@ -19,6 +19,10 @@ func (self *baseController) GetOnClick() func() error { return nil } +func (self *baseController) GetOnClickFocusedMainView() func(mainViewName string, clickedLineIdx int) error { + return nil +} + func (self *baseController) GetOnRenderToMain() func() { return nil } diff --git a/pkg/gui/controllers/commits_files_controller.go b/pkg/gui/controllers/commits_files_controller.go index 1ea916695..e1818789c 100644 --- a/pkg/gui/controllers/commits_files_controller.go +++ b/pkg/gui/controllers/commits_files_controller.go @@ -531,6 +531,16 @@ func (self *CommitFilesController) expandAll() error { return nil } +func (self *CommitFilesController) GetOnClickFocusedMainView() func(mainViewName string, clickedLineIdx int) error { + return func(mainViewName string, clickedLineIdx int) error { + node := self.getSelectedItem() + if node != nil && node.File != nil { + return self.enterCommitFile(node, types.OnFocusOpts{ClickedWindowName: mainViewName, ClickedViewLineIdx: clickedLineIdx}) + } + return nil + } +} + // NOTE: these functions are identical to those in files_controller.go (except for types) and // could also be cleaned up with some generics func normalisedSelectedCommitFileNodes(selectedNodes []*filetree.CommitFileNode) []*filetree.CommitFileNode { diff --git a/pkg/gui/controllers/files_controller.go b/pkg/gui/controllers/files_controller.go index 22bccf6c6..1240e3cf7 100644 --- a/pkg/gui/controllers/files_controller.go +++ b/pkg/gui/controllers/files_controller.go @@ -323,6 +323,16 @@ func (self *FilesController) GetOnClick() func() error { }) } +func (self *FilesController) GetOnClickFocusedMainView() func(mainViewName string, clickedLineIdx int) error { + return func(mainViewName string, clickedLineIdx int) error { + node := self.getSelectedItem() + if node != nil && node.File != nil { + return self.EnterFile(types.OnFocusOpts{ClickedWindowName: mainViewName, ClickedViewLineIdx: clickedLineIdx}) + } + return nil + } +} + // if we are dealing with a status for which there is no key in this map, // then we won't optimistically render: we'll just let `git status` tell // us what the new status is. @@ -545,7 +555,8 @@ func (self *FilesController) EnterFile(opts types.OnFocusOpts) error { return self.handleNonInlineConflict(file) } - self.c.Context().Push(self.c.Contexts().Staging, opts) + context := lo.Ternary(opts.ClickedWindowName == "secondary", self.c.Contexts().StagingSecondary, self.c.Contexts().Staging) + self.c.Context().Push(context, opts) return nil } diff --git a/pkg/gui/controllers/main_view_controller.go b/pkg/gui/controllers/main_view_controller.go index b48868677..b5a6722a5 100644 --- a/pkg/gui/controllers/main_view_controller.go +++ b/pkg/gui/controllers/main_view_controller.go @@ -1,6 +1,7 @@ package controllers import ( + "github.com/jesseduffield/gocui" "github.com/jesseduffield/lazygit/pkg/gui/context" "github.com/jesseduffield/lazygit/pkg/gui/types" ) @@ -52,6 +53,16 @@ func (self *MainViewController) GetKeybindings(opts types.KeybindingsOpts) []*ty } } +func (self *MainViewController) GetMouseKeybindings(opts types.KeybindingsOpts) []*gocui.ViewMouseBinding { + return []*gocui.ViewMouseBinding{ + { + ViewName: self.context.GetViewName(), + Key: gocui.MouseLeft, + Handler: self.onClick, + }, + } +} + func (self *MainViewController) Context() types.Context { return self.context } @@ -70,6 +81,14 @@ func (self *MainViewController) escape() error { return nil } +func (self *MainViewController) onClick(opts gocui.ViewMouseBindingOpts) error { + parentCtx := self.context.GetParentContext() + if parentCtx.GetOnClickFocusedMainView() != nil { + return parentCtx.GetOnClickFocusedMainView()(self.context.GetViewName(), opts.Y) + } + 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/types/context.go b/pkg/gui/types/context.go index 8a9bbf25b..f76bedeb6 100644 --- a/pkg/gui/types/context.go +++ b/pkg/gui/types/context.go @@ -94,6 +94,9 @@ type IBaseContext interface { // our list controller can come along and wrap it in a list-specific click handler. // We'll need to think of a better way to do this. AddOnClickFn(func() error) + // Likewise for the focused main view: we need this to communicate between a + // side panel controller and the focused main view controller. + AddOnClickFocusedMainViewFn(func(mainViewName string, clickedLineIdx int) error) AddOnRenderToMainFn(func()) AddOnFocusFn(func(OnFocusOpts)) @@ -240,6 +243,7 @@ type HasKeybindings interface { GetKeybindings(opts KeybindingsOpts) []*Binding GetMouseKeybindings(opts KeybindingsOpts) []*gocui.ViewMouseBinding GetOnClick() func() error + GetOnClickFocusedMainView() func(mainViewName string, clickedLineIdx int) error GetOnRenderToMain() func() GetOnFocus() func(OnFocusOpts) GetOnFocusLost() func(OnFocusLostOpts)