diff --git a/pkg/gui/context/base_context.go b/pkg/gui/context/base_context.go index a21e95e3f..e4993828c 100644 --- a/pkg/gui/context/base_context.go +++ b/pkg/gui/context/base_context.go @@ -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 } diff --git a/pkg/gui/controllers/attach.go b/pkg/gui/controllers/attach.go index b703c28c4..c67c415a3 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.AddOnDoubleClickFn(controller.GetOnDoubleClick()) + context.AddOnClickFn(controller.GetOnClick()) context.AddOnClickFocusedMainViewFn(controller.GetOnClickFocusedMainView()) context.AddOnRenderToMainFn(controller.GetOnRenderToMain()) context.AddOnFocusFn(controller.GetOnFocus()) diff --git a/pkg/gui/controllers/base_controller.go b/pkg/gui/controllers/base_controller.go index d85f7f052..afd6cf210 100644 --- a/pkg/gui/controllers/base_controller.go +++ b/pkg/gui/controllers/base_controller.go @@ -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 } diff --git a/pkg/gui/controllers/list_controller.go b/pkg/gui/controllers/list_controller.go index f07c5994d..a56860bab 100644 --- a/pkg/gui/controllers/list_controller.go +++ b/pkg/gui/controllers/list_controller.go @@ -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 } diff --git a/pkg/gui/types/context.go b/pkg/gui/types/context.go index 42c7ff293..afc8e11eb 100644 --- a/pkg/gui/types/context.go +++ b/pkg/gui/types/context.go @@ -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