1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-10-08 22:52:12 +02:00
Files
lazygit/pkg/gui/context/simple_context.go
Stefan Haller 4dfa4e8aa2 Allow more than one controller to attach OnFocus/OnFocusLost functions
Trying to do this would previously have the second one silently overwrite the
first one's.

We don't currently have this in lazygit, but I ran into the situation once
during development, and it can lead to bugs that are hard to diagnose.

Instead of holding a list of functions, we could also have added a panic in case
the function was set already; this would have been good enough for the current
state, and enough to catch mistakes early in the future. However, I decided to
allow multiple controllers to attach these functions, because I can't see a
reason not to.
2025-07-11 10:51:44 +02:00

75 lines
1.5 KiB
Go

package context
import (
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/gui/types"
)
type SimpleContext struct {
*BaseContext
handleRenderFunc func()
}
func NewSimpleContext(baseContext *BaseContext) *SimpleContext {
return &SimpleContext{
BaseContext: baseContext,
}
}
var _ types.Context = &SimpleContext{}
// A Display context only renders a view. It has no keybindings and is not focusable.
func NewDisplayContext(key types.ContextKey, view *gocui.View, windowName string) types.Context {
return NewSimpleContext(
NewBaseContext(NewBaseContextOpts{
Kind: types.DISPLAY_CONTEXT,
Key: key,
View: view,
WindowName: windowName,
Focusable: false,
Transient: false,
}),
)
}
func (self *SimpleContext) HandleFocus(opts types.OnFocusOpts) {
if self.highlightOnFocus {
self.GetViewTrait().SetHighlight(true)
}
for _, fn := range self.onFocusFns {
fn(opts)
}
if self.onRenderToMainFn != nil {
self.onRenderToMainFn()
}
}
func (self *SimpleContext) HandleFocusLost(opts types.OnFocusLostOpts) {
self.GetViewTrait().SetHighlight(false)
self.view.SetOriginX(0)
for _, fn := range self.onFocusLostFns {
fn(opts)
}
}
func (self *SimpleContext) FocusLine() {
}
func (self *SimpleContext) HandleRender() {
if self.handleRenderFunc != nil {
self.handleRenderFunc()
}
}
func (self *SimpleContext) SetHandleRenderFunc(f func()) {
self.handleRenderFunc = f
}
func (self *SimpleContext) HandleRenderToMain() {
if self.onRenderToMainFn != nil {
self.onRenderToMainFn()
}
}