mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-02-25 18:55:28 -06:00
This begins a big refactor of moving more code out of the Gui struct into contexts, controllers, and helpers. We also move some code into structs in the gui package purely for the sake of better encapsulation
83 lines
1.6 KiB
Go
83 lines
1.6 KiB
Go
package context
|
|
|
|
import (
|
|
"github.com/jesseduffield/gocui"
|
|
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
|
)
|
|
|
|
type SimpleContext struct {
|
|
OnRender func() error
|
|
|
|
*BaseContext
|
|
}
|
|
|
|
type ContextCallbackOpts struct {
|
|
OnRender func() error
|
|
}
|
|
|
|
func NewSimpleContext(baseContext *BaseContext, opts ContextCallbackOpts) *SimpleContext {
|
|
return &SimpleContext{
|
|
OnRender: opts.OnRender,
|
|
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,
|
|
}),
|
|
ContextCallbackOpts{},
|
|
)
|
|
}
|
|
|
|
func (self *SimpleContext) HandleFocus(opts types.OnFocusOpts) error {
|
|
if self.highlightOnFocus {
|
|
self.GetViewTrait().SetHighlight(true)
|
|
}
|
|
|
|
if self.onFocusFn != nil {
|
|
if err := self.onFocusFn(opts); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if self.onRenderToMainFn != nil {
|
|
if err := self.onRenderToMainFn(); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (self *SimpleContext) HandleFocusLost(opts types.OnFocusLostOpts) error {
|
|
if self.onFocusLostFn != nil {
|
|
return self.onFocusLostFn(opts)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (self *SimpleContext) HandleRender() error {
|
|
if self.OnRender != nil {
|
|
return self.OnRender()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (self *SimpleContext) HandleRenderToMain() error {
|
|
if self.onRenderToMainFn != nil {
|
|
return self.onRenderToMainFn()
|
|
}
|
|
|
|
return nil
|
|
}
|