jesseduffield.lazygit/pkg/gui/context/simple_context.go
Stefan Haller ec3f681ebf Derive the selection highlight from the context stack
A view drew a selection because something told it to, from four places on
three different schedules: a context being focused, a context losing focus,
a context being activated over another one, and a list being re-rendered.
Whether the flags ended up describing the state of the app depended on
which of those had run last, and the last one to run was often none of
them: a refresh only re-focuses the view that has the focus, so a list
whose contents changed underneath an unfocused panel kept whichever
highlight it happened to have.

Derive both flags instead, in one place, from the two things they mean: a
view shows a selection while its context is on the stack and has something
to select, and the context the user is in shows an active one where the
ones behind it show inactive ones. Nothing else needs to say anything about
highlighting, so nothing else can leave a view saying something untrue
about where the focus is.

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-03 21:19:04 +02:00

76 lines
1.6 KiB
Go

package context
import (
"github.com/jesseduffield/lazygit/pkg/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) {
for _, fn := range self.onFocusFns {
fn(opts)
}
if self.onRenderToMainFn != nil && !opts.SkipMainViewUpdate {
self.onRenderToMainFn()
}
}
func (self *SimpleContext) HandleFocusLost(opts types.OnFocusLostOpts) {
self.view.SetOriginX(0)
for _, fn := range self.onFocusLostFns {
fn(opts)
}
}
func (self *SimpleContext) HandleQuit() {
for _, fn := range self.onQuitFns {
fn()
}
}
func (self *SimpleContext) FocusLine(scrollIntoView bool) {
}
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()
}
}