Add GetOnClick to HasKeybindings

Can be used for doing additional click handling in list views.

Like the GetOnDoubleClick hook we should try to find a better design for this
than putting it in HasKeybindings and BaseContext, since it is only used by list
contexts.

Co-authored-by: Stefan Haller <stefan@haller-berlin.de>
This commit is contained in:
blakemckeany 2026-03-25 16:45:47 +01:00 committed by Stefan Haller
parent 61b72cef38
commit 4567840198
5 changed files with 36 additions and 0 deletions

View file

@ -16,6 +16,7 @@ type BaseContext struct {
keybindingsFns []types.KeybindingsFn
mouseKeybindingsFns []types.MouseKeybindingsFn
onDoubleClickFn func() error
onClickFn func(opts gocui.ViewMouseBindingOpts) error
onClickFocusedMainViewFn onClickFocusedMainViewFn
onRenderToMainFn func()
onFocusFns []onFocusFn
@ -141,6 +142,7 @@ func (self *BaseContext) ClearAllAttachedControllerFunctions() {
self.onFocusFns = nil
self.onFocusLostFns = nil
self.onDoubleClickFn = nil
self.onClickFn = nil
self.onClickFocusedMainViewFn = nil
self.onRenderToMainFn = nil
}
@ -154,6 +156,15 @@ func (self *BaseContext) AddOnDoubleClickFn(fn func() error) {
}
}
func (self *BaseContext) AddOnClickFn(fn func(opts gocui.ViewMouseBindingOpts) error) {
if fn != nil {
if self.onClickFn != nil {
panic("only one controller is allowed to set an onClickFn")
}
self.onClickFn = fn
}
}
func (self *BaseContext) AddOnClickFocusedMainViewFn(fn onClickFocusedMainViewFn) {
if fn != nil {
if self.onClickFocusedMainViewFn != nil {
@ -167,6 +178,10 @@ func (self *BaseContext) GetOnDoubleClick() func() error {
return self.onDoubleClickFn
}
func (self *BaseContext) GetOnClick() func(opts gocui.ViewMouseBindingOpts) error {
return self.onClickFn
}
func (self *BaseContext) GetOnClickFocusedMainView() onClickFocusedMainViewFn {
return self.onClickFocusedMainViewFn
}

View file

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

View file

@ -23,6 +23,10 @@ func (self *baseController) GetOnClickFocusedMainView() func(mainViewName string
return nil
}
func (self *baseController) GetOnClick() func(opts gocui.ViewMouseBindingOpts) error {
return nil
}
func (self *baseController) GetOnRenderToMain() func() {
return nil
}

View file

@ -246,7 +246,14 @@ func (self *ListController) HandleClick(opts gocui.ViewMouseBindingOpts) error {
if opts.IsDoubleClick && alreadyFocused && self.context.GetOnDoubleClick() != nil {
return self.context.GetOnDoubleClick()()
}
self.context.HandleFocus(types.OnFocusOpts{})
// Let view-specific controllers do additional click handling
if self.context.GetOnClick() != nil {
return self.context.GetOnClick()(opts)
}
return nil
}

View file

@ -98,6 +98,9 @@ type IBaseContext interface {
// 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)
// Adding on to the above, this is so that a list-specific handler can register
// a hook for doing additional click handling
AddOnClickFn(func(opts gocui.ViewMouseBindingOpts) error)
AddOnRenderToMainFn(func())
AddOnFocusFn(func(OnFocusOpts))
@ -251,6 +254,12 @@ type HasKeybindings interface {
// views currently. Will be called after the double-clicked list entry has been selected.
GetOnDoubleClick() func() error
// Implement this to get called for any non-double-click in the view. Only supported by list
// views currently. Will be called after the clicked list entry has been selected, and
// HandleFocus has already been called (so the main view is up to date). Should return nil if it
// decides not to do anything with the click.
GetOnClick() func(opts gocui.ViewMouseBindingOpts) error
// Implement this in a side-panel controller to get called when there's a click in the main view
// that belongs to your panel while the main view is already focused.
GetOnClickFocusedMainView() func(mainViewName string, clickedLineIdx int) error