2022-01-29 10:09:20 +02:00
|
|
|
package context
|
|
|
|
|
2022-02-05 01:31:07 +02:00
|
|
|
import (
|
2022-02-05 05:42:56 +02:00
|
|
|
"github.com/jesseduffield/gocui"
|
2022-02-05 01:31:07 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
|
|
|
)
|
2022-01-29 10:09:20 +02:00
|
|
|
|
|
|
|
type BaseContext struct {
|
2022-01-30 00:53:28 +02:00
|
|
|
kind types.ContextKind
|
|
|
|
key types.ContextKey
|
2022-06-13 03:01:26 +02:00
|
|
|
view *gocui.View
|
|
|
|
viewTrait types.IViewTrait
|
2022-01-30 00:53:28 +02:00
|
|
|
windowName string
|
|
|
|
onGetOptionsMap func() map[string]string
|
2022-01-29 10:09:20 +02:00
|
|
|
|
2022-02-05 05:42:56 +02:00
|
|
|
keybindingsFns []types.KeybindingsFn
|
|
|
|
mouseKeybindingsFns []types.MouseKeybindingsFn
|
2022-02-27 02:42:22 +02:00
|
|
|
onClickFn func() error
|
2022-12-30 14:24:24 +02:00
|
|
|
onRenderToMainFn func() error
|
|
|
|
onFocusFn onFocusFn
|
|
|
|
onFocusLostFn onFocusLostFn
|
2022-02-05 01:31:07 +02:00
|
|
|
|
2022-06-13 03:01:26 +02:00
|
|
|
focusable bool
|
|
|
|
transient bool
|
|
|
|
hasControlledBounds bool
|
2023-02-16 12:31:10 +02:00
|
|
|
highlightOnFocus bool
|
2022-02-05 07:56:36 +02:00
|
|
|
|
2022-01-29 10:09:20 +02:00
|
|
|
*ParentContextMgr
|
|
|
|
}
|
|
|
|
|
2022-12-30 14:24:24 +02:00
|
|
|
type (
|
|
|
|
onFocusFn = func(types.OnFocusOpts) error
|
|
|
|
onFocusLostFn = func(types.OnFocusLostOpts) error
|
|
|
|
)
|
|
|
|
|
2022-02-05 01:31:07 +02:00
|
|
|
var _ types.IBaseContext = &BaseContext{}
|
|
|
|
|
2022-01-30 00:53:28 +02:00
|
|
|
type NewBaseContextOpts struct {
|
2022-06-13 03:01:26 +02:00
|
|
|
Kind types.ContextKind
|
|
|
|
Key types.ContextKey
|
|
|
|
View *gocui.View
|
|
|
|
WindowName string
|
|
|
|
Focusable bool
|
|
|
|
Transient bool
|
|
|
|
HasUncontrolledBounds bool // negating for the sake of making false the default
|
2023-02-16 12:31:10 +02:00
|
|
|
HighlightOnFocus bool
|
2022-01-30 00:53:28 +02:00
|
|
|
|
|
|
|
OnGetOptionsMap func() map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBaseContext(opts NewBaseContextOpts) *BaseContext {
|
2022-06-13 03:01:26 +02:00
|
|
|
viewTrait := NewViewTrait(opts.View)
|
|
|
|
|
|
|
|
hasControlledBounds := !opts.HasUncontrolledBounds
|
|
|
|
|
2022-01-30 00:53:28 +02:00
|
|
|
return &BaseContext{
|
2022-06-13 03:01:26 +02:00
|
|
|
kind: opts.Kind,
|
|
|
|
key: opts.Key,
|
|
|
|
view: opts.View,
|
|
|
|
windowName: opts.WindowName,
|
|
|
|
onGetOptionsMap: opts.OnGetOptionsMap,
|
|
|
|
focusable: opts.Focusable,
|
|
|
|
transient: opts.Transient,
|
|
|
|
hasControlledBounds: hasControlledBounds,
|
2023-02-16 12:31:10 +02:00
|
|
|
highlightOnFocus: opts.HighlightOnFocus,
|
2022-06-13 03:01:26 +02:00
|
|
|
ParentContextMgr: &ParentContextMgr{},
|
|
|
|
viewTrait: viewTrait,
|
2022-01-30 00:53:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-29 10:09:20 +02:00
|
|
|
func (self *BaseContext) GetOptionsMap() map[string]string {
|
2022-01-30 00:53:28 +02:00
|
|
|
if self.onGetOptionsMap != nil {
|
|
|
|
return self.onGetOptionsMap()
|
2022-01-29 10:09:20 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *BaseContext) SetWindowName(windowName string) {
|
2022-01-30 00:53:28 +02:00
|
|
|
self.windowName = windowName
|
2022-01-29 10:09:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *BaseContext) GetWindowName() string {
|
2022-01-30 00:53:28 +02:00
|
|
|
return self.windowName
|
2022-01-29 10:09:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *BaseContext) GetViewName() string {
|
2022-06-13 03:01:26 +02:00
|
|
|
// for the sake of the global context which has no view
|
|
|
|
if self.view == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return self.view.Name()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *BaseContext) GetView() *gocui.View {
|
|
|
|
return self.view
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *BaseContext) GetViewTrait() types.IViewTrait {
|
|
|
|
return self.viewTrait
|
2022-01-29 10:09:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *BaseContext) GetKind() types.ContextKind {
|
2022-01-30 00:53:28 +02:00
|
|
|
return self.kind
|
2022-01-29 10:09:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *BaseContext) GetKey() types.ContextKey {
|
2022-01-30 00:53:28 +02:00
|
|
|
return self.key
|
2022-01-29 10:09:20 +02:00
|
|
|
}
|
2022-02-05 01:31:07 +02:00
|
|
|
|
|
|
|
func (self *BaseContext) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
|
|
|
|
bindings := []*types.Binding{}
|
|
|
|
for i := range self.keybindingsFns {
|
|
|
|
// the first binding in the bindings array takes precedence but we want the
|
|
|
|
// last keybindingsFn to take precedence to we add them in reverse
|
|
|
|
bindings = append(bindings, self.keybindingsFns[len(self.keybindingsFns)-1-i](opts)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return bindings
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *BaseContext) AddKeybindingsFn(fn types.KeybindingsFn) {
|
|
|
|
self.keybindingsFns = append(self.keybindingsFns, fn)
|
|
|
|
}
|
2022-02-05 05:42:56 +02:00
|
|
|
|
|
|
|
func (self *BaseContext) AddMouseKeybindingsFn(fn types.MouseKeybindingsFn) {
|
|
|
|
self.mouseKeybindingsFns = append(self.mouseKeybindingsFns, fn)
|
|
|
|
}
|
|
|
|
|
2022-02-27 02:42:22 +02:00
|
|
|
func (self *BaseContext) AddOnClickFn(fn func() error) {
|
|
|
|
if fn != nil {
|
|
|
|
self.onClickFn = fn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *BaseContext) GetOnClick() func() error {
|
|
|
|
return self.onClickFn
|
|
|
|
}
|
|
|
|
|
2022-12-30 14:24:24 +02:00
|
|
|
func (self *BaseContext) AddOnRenderToMainFn(fn func() error) {
|
|
|
|
if fn != nil {
|
|
|
|
self.onRenderToMainFn = fn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *BaseContext) GetOnRenderToMain() func() error {
|
|
|
|
return self.onRenderToMainFn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *BaseContext) AddOnFocusFn(fn onFocusFn) {
|
|
|
|
if fn != nil {
|
|
|
|
self.onFocusFn = fn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *BaseContext) GetOnFocus() onFocusFn {
|
|
|
|
return self.onFocusFn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *BaseContext) AddOnFocusLostFn(fn onFocusLostFn) {
|
|
|
|
if fn != nil {
|
|
|
|
self.onFocusLostFn = fn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *BaseContext) GetOnFocusLost() onFocusLostFn {
|
|
|
|
return self.onFocusLostFn
|
|
|
|
}
|
|
|
|
|
2022-02-05 05:42:56 +02:00
|
|
|
func (self *BaseContext) GetMouseKeybindings(opts types.KeybindingsOpts) []*gocui.ViewMouseBinding {
|
|
|
|
bindings := []*gocui.ViewMouseBinding{}
|
|
|
|
for i := range self.mouseKeybindingsFns {
|
|
|
|
// the first binding in the bindings array takes precedence but we want the
|
|
|
|
// last keybindingsFn to take precedence to we add them in reverse
|
|
|
|
bindings = append(bindings, self.mouseKeybindingsFns[len(self.mouseKeybindingsFns)-1-i](opts)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return bindings
|
|
|
|
}
|
2022-02-05 07:56:36 +02:00
|
|
|
|
|
|
|
func (self *BaseContext) IsFocusable() bool {
|
|
|
|
return self.focusable
|
|
|
|
}
|
2022-03-24 13:07:30 +02:00
|
|
|
|
|
|
|
func (self *BaseContext) IsTransient() bool {
|
|
|
|
return self.transient
|
|
|
|
}
|
|
|
|
|
2022-06-13 03:01:26 +02:00
|
|
|
func (self *BaseContext) HasControlledBounds() bool {
|
|
|
|
return self.hasControlledBounds
|
|
|
|
}
|
|
|
|
|
2022-03-24 13:07:30 +02:00
|
|
|
func (self *BaseContext) Title() string {
|
|
|
|
return ""
|
|
|
|
}
|