mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-04-23 12:18:51 +02:00
In d5b4f7bb3e and 58a83b0862 we introduced a combined mechanism for rerendering views when either their width changes (needed for the branches view which truncates long branch names), or the screen mode (needed for those views that display more information in half or full screen mode, e.g. the commits view). This was a bad idea, because it unnecessarily rerenders too many views when just their width changes, which causes a noticable lag. This is a problem, for example, when selecting a file in the files panel that has only unstaged changes, and then going to one that has both staged and unstaged changes; this splits the main view, causing the side panels to become a bit narrower, and rerendering all those views took almost 500ms on my machine. Another similar example is entering or leaving staging mode. Fix this by being more specific about which views need rerendering under what conditions; this improves the time it takes to rerender in the above scenarios from 450-500s down to about 20ms. This reintroduces the code that was removed in 58a83b0862, but in a slightly different way.
215 lines
5.6 KiB
Go
215 lines
5.6 KiB
Go
package context
|
|
|
|
import (
|
|
"github.com/jesseduffield/gocui"
|
|
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
|
)
|
|
|
|
type BaseContext struct {
|
|
kind types.ContextKind
|
|
key types.ContextKey
|
|
view *gocui.View
|
|
viewTrait types.IViewTrait
|
|
windowName string
|
|
onGetOptionsMap func() map[string]string
|
|
|
|
keybindingsFns []types.KeybindingsFn
|
|
mouseKeybindingsFns []types.MouseKeybindingsFn
|
|
onClickFn func() error
|
|
onRenderToMainFn func() error
|
|
onFocusFn onFocusFn
|
|
onFocusLostFn onFocusLostFn
|
|
|
|
focusable bool
|
|
transient bool
|
|
hasControlledBounds bool
|
|
needsRerenderOnWidthChange types.NeedsRerenderOnWidthChangeLevel
|
|
needsRerenderOnHeightChange bool
|
|
highlightOnFocus bool
|
|
|
|
*ParentContextMgr
|
|
}
|
|
|
|
type (
|
|
onFocusFn = func(types.OnFocusOpts) error
|
|
onFocusLostFn = func(types.OnFocusLostOpts) error
|
|
)
|
|
|
|
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
|
|
NeedsRerenderOnWidthChange types.NeedsRerenderOnWidthChangeLevel
|
|
NeedsRerenderOnHeightChange 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,
|
|
needsRerenderOnWidthChange: opts.NeedsRerenderOnWidthChange,
|
|
needsRerenderOnHeightChange: opts.NeedsRerenderOnHeightChange,
|
|
ParentContextMgr: &ParentContextMgr{},
|
|
viewTrait: viewTrait,
|
|
}
|
|
}
|
|
|
|
func (self *BaseContext) GetOptionsMap() map[string]string {
|
|
if self.onGetOptionsMap != nil {
|
|
return self.onGetOptionsMap()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (self *BaseContext) SetWindowName(windowName string) {
|
|
self.windowName = windowName
|
|
}
|
|
|
|
func (self *BaseContext) GetWindowName() string {
|
|
return self.windowName
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
func (self *BaseContext) GetKind() types.ContextKind {
|
|
return self.kind
|
|
}
|
|
|
|
func (self *BaseContext) GetKey() types.ContextKey {
|
|
return self.key
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
func (self *BaseContext) AddMouseKeybindingsFn(fn types.MouseKeybindingsFn) {
|
|
self.mouseKeybindingsFns = append(self.mouseKeybindingsFns, fn)
|
|
}
|
|
|
|
func (self *BaseContext) ClearAllBindingsFn() {
|
|
self.keybindingsFns = []types.KeybindingsFn{}
|
|
self.mouseKeybindingsFns = []types.MouseKeybindingsFn{}
|
|
}
|
|
|
|
func (self *BaseContext) AddOnClickFn(fn func() error) {
|
|
if fn != nil {
|
|
self.onClickFn = fn
|
|
}
|
|
}
|
|
|
|
func (self *BaseContext) GetOnClick() func() error {
|
|
return self.onClickFn
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
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) NeedsRerenderOnWidthChange() types.NeedsRerenderOnWidthChangeLevel {
|
|
return self.needsRerenderOnWidthChange
|
|
}
|
|
|
|
func (self *BaseContext) NeedsRerenderOnHeightChange() bool {
|
|
return self.needsRerenderOnHeightChange
|
|
}
|
|
|
|
func (self *BaseContext) Title() string {
|
|
return ""
|
|
}
|