summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Haller <stefan@haller-berlin.de>2023-03-13 21:23:08 +0100
committerJesse Duffield <jessedduffield@gmail.com>2023-03-20 20:14:13 +1100
commit40f6767cfc77931e35bc932b3adae943e7e521f0 (patch)
tree65fb895db4175a8e0517c6067ae18cc0e49f6e56
parent776d8f4d2e4afff3c80a16c33ad28780e41169e6 (diff)
Avoid deactivating and activating when pushing the current context again
When calling PushContext, do nothing if the context to be pushed is already on top of the stack. Avoids flicker in certain situations.
-rw-r--r--pkg/gui/context.go22
1 files changed, 17 insertions, 5 deletions
diff --git a/pkg/gui/context.go b/pkg/gui/context.go
index e4719ab13..bb69cc3ce 100644
--- a/pkg/gui/context.go
+++ b/pkg/gui/context.go
@@ -52,7 +52,7 @@ func (gui *Gui) pushContext(c types.Context, opts types.OnFocusOpts) error {
return nil
}
- contextsToDeactivate := gui.pushToContextStack(c)
+ contextsToDeactivate, contextToActivate := gui.pushToContextStack(c)
for _, contextToDeactivate := range contextsToDeactivate {
if err := gui.deactivateContext(contextToDeactivate, types.OnFocusLostOpts{NewContextKey: c.GetKey()}); err != nil {
@@ -60,16 +60,28 @@ func (gui *Gui) pushContext(c types.Context, opts types.OnFocusOpts) error {
}
}
- return gui.activateContext(c, opts)
+ if contextToActivate == nil {
+ return nil
+ }
+
+ return gui.activateContext(contextToActivate, opts)
}
-// Adjusts the context stack based on the context that's being pushed and returns contexts to deactivate
-func (gui *Gui) pushToContextStack(c types.Context) []types.Context {
+// Adjusts the context stack based on the context that's being pushed and
+// returns (contexts to deactivate, context to activate)
+func (gui *Gui) pushToContextStack(c types.Context) ([]types.Context, types.Context) {
contextsToDeactivate := []types.Context{}
gui.State.ContextManager.Lock()
defer gui.State.ContextManager.Unlock()
+ if len(gui.State.ContextManager.ContextStack) > 0 &&
+ c == gui.State.ContextManager.ContextStack[len(gui.State.ContextManager.ContextStack)-1] {
+ // Context being pushed is already on top of the stack: nothing to
+ // deactivate or activate
+ return contextsToDeactivate, nil
+ }
+
if len(gui.State.ContextManager.ContextStack) == 0 {
gui.State.ContextManager.ContextStack = append(gui.State.ContextManager.ContextStack, c)
} else if c.GetKind() == types.SIDE_CONTEXT {
@@ -105,7 +117,7 @@ func (gui *Gui) pushToContextStack(c types.Context) []types.Context {
}
}
- return contextsToDeactivate
+ return contextsToDeactivate, c
}
func (gui *Gui) popContext() error {