1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-06-13 00:07:59 +02:00

Allow clicking in focused main view to go to staging

Only works if a file is selected.
This commit is contained in:
Stefan Haller 2025-03-26 12:39:39 +01:00
parent acfa024915
commit fbb8b2e17e
7 changed files with 70 additions and 9 deletions

View File

@ -16,6 +16,7 @@ type BaseContext struct {
keybindingsFns []types.KeybindingsFn keybindingsFns []types.KeybindingsFn
mouseKeybindingsFns []types.MouseKeybindingsFn mouseKeybindingsFns []types.MouseKeybindingsFn
onClickFn func() error onClickFn func() error
onClickFocusedMainViewFn onClickFocusedMainViewFn
onRenderToMainFn func() onRenderToMainFn func()
onFocusFn onFocusFn onFocusFn onFocusFn
onFocusLostFn onFocusLostFn onFocusLostFn onFocusLostFn
@ -33,6 +34,7 @@ type BaseContext struct {
type ( type (
onFocusFn = func(types.OnFocusOpts) onFocusFn = func(types.OnFocusOpts)
onFocusLostFn = func(types.OnFocusLostOpts) onFocusLostFn = func(types.OnFocusLostOpts)
onClickFocusedMainViewFn = func(mainViewName string, clickedLineIdx int) error
) )
var _ types.IBaseContext = &BaseContext{} 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 { func (self *BaseContext) GetOnClick() func() error {
return self.onClickFn return self.onClickFn
} }
func (self *BaseContext) GetOnClickFocusedMainView() onClickFocusedMainViewFn {
return self.onClickFocusedMainViewFn
}
func (self *BaseContext) AddOnRenderToMainFn(fn func()) { func (self *BaseContext) AddOnRenderToMainFn(fn func()) {
if fn != nil { if fn != nil {
self.onRenderToMainFn = fn self.onRenderToMainFn = fn

View File

@ -7,6 +7,7 @@ func AttachControllers(context types.Context, controllers ...types.IController)
context.AddKeybindingsFn(controller.GetKeybindings) context.AddKeybindingsFn(controller.GetKeybindings)
context.AddMouseKeybindingsFn(controller.GetMouseKeybindings) context.AddMouseKeybindingsFn(controller.GetMouseKeybindings)
context.AddOnClickFn(controller.GetOnClick()) context.AddOnClickFn(controller.GetOnClick())
context.AddOnClickFocusedMainViewFn(controller.GetOnClickFocusedMainView())
context.AddOnRenderToMainFn(controller.GetOnRenderToMain()) context.AddOnRenderToMainFn(controller.GetOnRenderToMain())
context.AddOnFocusFn(controller.GetOnFocus()) context.AddOnFocusFn(controller.GetOnFocus())
context.AddOnFocusLostFn(controller.GetOnFocusLost()) context.AddOnFocusLostFn(controller.GetOnFocusLost())

View File

@ -19,6 +19,10 @@ func (self *baseController) GetOnClick() func() error {
return nil return nil
} }
func (self *baseController) GetOnClickFocusedMainView() func(mainViewName string, clickedLineIdx int) error {
return nil
}
func (self *baseController) GetOnRenderToMain() func() { func (self *baseController) GetOnRenderToMain() func() {
return nil return nil
} }

View File

@ -531,6 +531,16 @@ func (self *CommitFilesController) expandAll() error {
return nil 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 // NOTE: these functions are identical to those in files_controller.go (except for types) and
// could also be cleaned up with some generics // could also be cleaned up with some generics
func normalisedSelectedCommitFileNodes(selectedNodes []*filetree.CommitFileNode) []*filetree.CommitFileNode { func normalisedSelectedCommitFileNodes(selectedNodes []*filetree.CommitFileNode) []*filetree.CommitFileNode {

View File

@ -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, // 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 // then we won't optimistically render: we'll just let `git status` tell
// us what the new status is. // us what the new status is.
@ -545,7 +555,8 @@ func (self *FilesController) EnterFile(opts types.OnFocusOpts) error {
return self.handleNonInlineConflict(file) 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 return nil
} }

View File

@ -1,6 +1,7 @@
package controllers package controllers
import ( import (
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/gui/context" "github.com/jesseduffield/lazygit/pkg/gui/context"
"github.com/jesseduffield/lazygit/pkg/gui/types" "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 { func (self *MainViewController) Context() types.Context {
return self.context return self.context
} }
@ -70,6 +81,14 @@ func (self *MainViewController) escape() error {
return nil 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 { func (self *MainViewController) openSearch() error {
if manager := self.c.GetViewBufferManagerForView(self.context.GetView()); manager != nil { if manager := self.c.GetViewBufferManagerForView(self.context.GetView()); manager != nil {
manager.ReadToEnd(func() { manager.ReadToEnd(func() {

View File

@ -94,6 +94,9 @@ type IBaseContext interface {
// our list controller can come along and wrap it in a list-specific click handler. // 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. // We'll need to think of a better way to do this.
AddOnClickFn(func() error) 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()) AddOnRenderToMainFn(func())
AddOnFocusFn(func(OnFocusOpts)) AddOnFocusFn(func(OnFocusOpts))
@ -240,6 +243,7 @@ type HasKeybindings interface {
GetKeybindings(opts KeybindingsOpts) []*Binding GetKeybindings(opts KeybindingsOpts) []*Binding
GetMouseKeybindings(opts KeybindingsOpts) []*gocui.ViewMouseBinding GetMouseKeybindings(opts KeybindingsOpts) []*gocui.ViewMouseBinding
GetOnClick() func() error GetOnClick() func() error
GetOnClickFocusedMainView() func(mainViewName string, clickedLineIdx int) error
GetOnRenderToMain() func() GetOnRenderToMain() func()
GetOnFocus() func(OnFocusOpts) GetOnFocus() func(OnFocusOpts)
GetOnFocusLost() func(OnFocusLostOpts) GetOnFocusLost() func(OnFocusLostOpts)