1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00
lazygit/pkg/gui/context/base_context.go

158 lines
3.9 KiB
Go
Raw Normal View History

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 {
kind types.ContextKind
key types.ContextKey
view *gocui.View
viewTrait types.IViewTrait
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-02-05 01:31:07 +02:00
focusable bool
transient bool
hasControlledBounds bool
highlightOnFocus bool
2022-02-05 07:56:36 +02:00
2022-01-29 10:09:20 +02:00
*ParentContextMgr
}
2022-02-05 01:31:07 +02:00
var _ types.IBaseContext = &BaseContext{}
type NewBaseContextOpts struct {
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
HighlightOnFocus bool
OnGetOptionsMap func() map[string]string
}
func NewBaseContext(opts NewBaseContextOpts) *BaseContext {
viewTrait := NewViewTrait(opts.View)
hasControlledBounds := !opts.HasUncontrolledBounds
return &BaseContext{
kind: opts.Kind,
key: opts.Key,
view: opts.View,
windowName: opts.WindowName,
onGetOptionsMap: opts.OnGetOptionsMap,
focusable: opts.Focusable,
transient: opts.Transient,
hasControlledBounds: hasControlledBounds,
highlightOnFocus: opts.HighlightOnFocus,
ParentContextMgr: &ParentContextMgr{},
viewTrait: viewTrait,
}
}
2022-01-29 10:09:20 +02:00
func (self *BaseContext) GetOptionsMap() map[string]string {
if self.onGetOptionsMap != nil {
return self.onGetOptionsMap()
2022-01-29 10:09:20 +02:00
}
return nil
}
func (self *BaseContext) SetWindowName(windowName string) {
self.windowName = windowName
2022-01-29 10:09:20 +02:00
}
func (self *BaseContext) GetWindowName() string {
return self.windowName
2022-01-29 10:09:20 +02:00
}
func (self *BaseContext) GetViewName() string {
// 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 {
return self.kind
2022-01-29 10:09:20 +02:00
}
func (self *BaseContext) GetKey() types.ContextKey {
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-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
}
func (self *BaseContext) IsTransient() bool {
return self.transient
}
func (self *BaseContext) HasControlledBounds() bool {
return self.hasControlledBounds
}
func (self *BaseContext) Title() string {
return ""
}