diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2020-08-18 22:41:27 +1000 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2020-08-23 14:29:18 +1000 |
commit | 2488e0044d2ca22f463291f6db06a61c79bd5ff0 (patch) | |
tree | 7816fcf390f06358c66fd036211a60ad896f0de4 /pkg/gui/context.go | |
parent | 9c866fd49c537dffe445e2cba26d8f0649551c64 (diff) |
concurrent-safe handling of context state
Diffstat (limited to 'pkg/gui/context.go')
-rw-r--r-- | pkg/gui/context.go | 60 |
1 files changed, 34 insertions, 26 deletions
diff --git a/pkg/gui/context.go b/pkg/gui/context.go index 59fb4965b..1e466151e 100644 --- a/pkg/gui/context.go +++ b/pkg/gui/context.go @@ -87,23 +87,27 @@ type ContextTree struct { } func (gui *Gui) switchContext(c Context) error { - // push onto stack - // if we are switching to a side context, remove all other contexts in the stack - if c.GetKind() == SIDE_CONTEXT { - for _, stackContext := range gui.State.ContextStack { - if stackContext.GetKey() != c.GetKey() { - if err := gui.deactivateContext(stackContext); err != nil { - return err + gui.g.Update(func(*gocui.Gui) error { + // push onto stack + // if we are switching to a side context, remove all other contexts in the stack + if c.GetKind() == SIDE_CONTEXT { + for _, stackContext := range gui.State.ContextStack { + if stackContext.GetKey() != c.GetKey() { + if err := gui.deactivateContext(stackContext); err != nil { + return err + } } } + gui.State.ContextStack = []Context{c} + } else { + // TODO: think about other exceptional cases + gui.State.ContextStack = append(gui.State.ContextStack, c) } - gui.State.ContextStack = []Context{c} - } else { - // TODO: think about other exceptional cases - gui.State.ContextStack = append(gui.State.ContextStack, c) - } - return gui.activateContext(c) + return gui.activateContext(c) + }) + + return nil } // switchContextToView is to be used when you don't know which context you @@ -114,25 +118,29 @@ func (gui *Gui) switchContextToView(viewName string) error { } func (gui *Gui) returnFromContext() error { - // TODO: add mutexes + gui.g.Update(func(*gocui.Gui) error { + // TODO: add mutexes - if len(gui.State.ContextStack) == 1 { - // cannot escape from bottommost context - return nil - } + if len(gui.State.ContextStack) == 1 { + // cannot escape from bottommost context + return nil + } - n := len(gui.State.ContextStack) - 1 + n := len(gui.State.ContextStack) - 1 - currentContext := gui.State.ContextStack[n] - newContext := gui.State.ContextStack[n-1] + currentContext := gui.State.ContextStack[n] + newContext := gui.State.ContextStack[n-1] - gui.State.ContextStack = gui.State.ContextStack[:n] + gui.State.ContextStack = gui.State.ContextStack[:n] - if err := gui.deactivateContext(currentContext); err != nil { - return err - } + if err := gui.deactivateContext(currentContext); err != nil { + return err + } + + return gui.activateContext(newContext) + }) - return gui.activateContext(newContext) + return nil } func (gui *Gui) deactivateContext(c Context) error { |