1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-04-21 12:16:54 +02:00
lazygit/pkg/gui/context/simple_context.go
Stefan Haller 4e441127f3 Clear highlight in HandleFocusLost
Remove the old mechanism of clearing the highlight in Layout.

This fixes a problem with a wrong highlight showing up in the staging panel when
entering a file with only staged changes.

Reproduction recipe:
1. stage all changes in a file by pressing space on it in the files panel
2. enter the staged changes panel by pressing enter
3. unstage one of the changes
This makes the unstaged changes panel visible, but keeps the focus in the staged
changes panel. However, the highlight in the unstaged changes view becomes
visible, as if it were focused.

To explain why this happens, you need to know how the selection highlighting of
a view is turned on or off. It is turned on when it gains the focus, i.e. when
ActivateFocus is called on it, which in turn happens when PushContext is called.
It is turned off in Layout when gocui sees that the current view is no longer
the same as last time, in which case it calls onViewFocusLost on the previous
current view.

This mechanism only works reliably when there is at most one PushContext call
per event handler. If there is more than one, then the first one gets its
highlight turned on, then the second one, but since gocui has never seen the
first one as the active view in Layout, it doesn't get the highlight turned off
again even though it should.

And this happens in the above scenario. When pressing enter on a file with only
staged changes, we first push the staging context (in
FilesController.EnterFile), and then later we push the stagingSecondary context
when we realize we only have staged changes. This leaves the highlight of the
staging context on.
2024-06-23 13:21:49 +02:00

74 lines
1.5 KiB
Go

package context
import (
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/gui/types"
)
type SimpleContext struct {
*BaseContext
}
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) 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 {
self.GetViewTrait().SetHighlight(false)
_ = self.view.SetOriginX(0)
if self.onFocusLostFn != nil {
return self.onFocusLostFn(opts)
}
return nil
}
func (self *SimpleContext) HandleRender() error {
return nil
}
func (self *SimpleContext) HandleRenderToMain() error {
if self.onRenderToMainFn != nil {
return self.onRenderToMainFn()
}
return nil
}