1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00
lazygit/pkg/gui/context.go

321 lines
8.4 KiB
Go
Raw Normal View History

2019-02-16 12:01:17 +02:00
package gui
2020-08-16 02:05:45 +02:00
import (
"errors"
"sync"
2022-03-19 10:12:58 +02:00
"github.com/jesseduffield/generics/slices"
2022-01-29 10:15:46 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/context"
"github.com/jesseduffield/lazygit/pkg/gui/types"
2020-08-16 02:05:45 +02:00
)
2022-05-07 07:42:36 +02:00
// This file is for the management of contexts. There is a context stack such that
// for example you might start off in the commits context and then open a menu, putting
// you in the menu context. When contexts are activated/deactivated certain things need
// to happen like showing/hiding views and rendering content.
type ContextMgr struct {
ContextStack []types.Context
sync.RWMutex
gui *Gui
2023-03-23 13:05:25 +02:00
allContexts *context.ContextTree
}
2022-03-20 00:24:39 +02:00
2023-03-23 13:05:25 +02:00
func NewContextMgr(
initialContext types.Context,
gui *Gui,
allContexts *context.ContextTree,
) *ContextMgr {
2023-03-23 09:47:29 +02:00
return &ContextMgr{
ContextStack: []types.Context{initialContext},
RWMutex: sync.RWMutex{},
gui: gui,
2023-03-23 13:05:25 +02:00
allContexts: allContexts,
}
}
2023-03-23 09:47:29 +02:00
// use when you don't want to return to the original context upon
// hitting escape: you want to go that context's parent instead.
2023-03-23 09:47:29 +02:00
func (self *ContextMgr) Replace(c types.Context) error {
2022-02-05 07:56:36 +02:00
if !c.IsFocusable() {
return nil
}
self.Lock()
2022-02-13 03:47:15 +02:00
if len(self.ContextStack) == 0 {
self.ContextStack = []types.Context{c}
2022-01-15 03:04:00 +02:00
} else {
// replace the last item with the given item
self.ContextStack = append(self.ContextStack[0:len(self.ContextStack)-1], c)
2022-01-15 03:04:00 +02:00
}
defer self.Unlock()
2022-02-13 03:47:15 +02:00
return self.activateContext(c, types.OnFocusOpts{})
}
2023-03-23 09:47:29 +02:00
func (self *ContextMgr) Push(c types.Context, opts ...types.OnFocusOpts) error {
if len(opts) > 1 {
2023-03-23 09:47:29 +02:00
return errors.New("cannot pass multiple opts to Push")
}
singleOpts := types.OnFocusOpts{}
if len(opts) > 0 {
// using triple dot but you should only ever pass one of these opt structs
singleOpts = opts[0]
}
2022-02-05 07:56:36 +02:00
if !c.IsFocusable() {
return nil
2022-02-05 05:42:56 +02:00
}
contextsToDeactivate, contextToActivate := self.pushToContextStack(c)
for _, contextToDeactivate := range contextsToDeactivate {
if err := self.deactivateContext(contextToDeactivate, types.OnFocusLostOpts{NewContextKey: c.GetKey()}); err != nil {
return err
}
}
if contextToActivate == nil {
return nil
}
return self.activateContext(contextToActivate, singleOpts)
}
// Adjusts the context stack based on the context that's being pushed and
// returns (contexts to deactivate, context to activate)
func (self *ContextMgr) pushToContextStack(c types.Context) ([]types.Context, types.Context) {
contextsToDeactivate := []types.Context{}
self.Lock()
defer self.Unlock()
if len(self.ContextStack) > 0 &&
c == self.ContextStack[len(self.ContextStack)-1] {
// Context being pushed is already on top of the stack: nothing to
// deactivate or activate
return contextsToDeactivate, nil
}
if len(self.ContextStack) == 0 {
self.ContextStack = append(self.ContextStack, c)
} else if c.GetKind() == types.SIDE_CONTEXT {
// if we are switching to a side context, remove all other contexts in the stack
contextsToDeactivate = self.ContextStack
self.ContextStack = []types.Context{c}
} else if c.GetKind() == types.MAIN_CONTEXT {
// if we're switching to a main context, remove all other main contexts in the stack
contextsToKeep := []types.Context{}
for _, stackContext := range self.ContextStack {
if stackContext.GetKind() == types.MAIN_CONTEXT {
contextsToDeactivate = append(contextsToDeactivate, stackContext)
} else {
contextsToKeep = append(contextsToKeep, stackContext)
}
}
self.ContextStack = append(contextsToKeep, c)
} else {
topContext := self.currentContextWithoutLock()
// if we're pushing the same context on, we do nothing.
if topContext.GetKey() != c.GetKey() {
// if top one is a temporary popup, we remove it. Ideally you'd be able to
// escape back to previous temporary popups, but because we're currently reusing
// views for this, you might not be able to get back to where you previously were.
2022-08-02 01:16:01 +02:00
// The exception is when going to the search context e.g. for searching a menu.
if (topContext.GetKind() == types.TEMPORARY_POPUP && c.GetKey() != context.SEARCH_CONTEXT_KEY) ||
// we only ever want one main context on the stack at a time.
(topContext.GetKind() == types.MAIN_CONTEXT && c.GetKind() == types.MAIN_CONTEXT) {
contextsToDeactivate = append(contextsToDeactivate, topContext)
_, self.ContextStack = slices.Pop(self.ContextStack)
}
self.ContextStack = append(self.ContextStack, c)
}
}
return contextsToDeactivate, c
2020-08-16 05:58:29 +02:00
}
2023-03-23 09:47:29 +02:00
func (self *ContextMgr) Pop() error {
self.Lock()
2020-08-16 10:17:16 +02:00
if len(self.ContextStack) == 1 {
2021-09-25 05:21:28 +02:00
// cannot escape from bottommost context
self.Unlock()
2021-09-25 05:21:28 +02:00
return nil
}
2020-08-16 10:17:16 +02:00
var currentContext types.Context
currentContext, self.ContextStack = slices.Pop(self.ContextStack)
2020-08-16 10:17:16 +02:00
newContext := self.ContextStack[len(self.ContextStack)-1]
self.Unlock()
2020-08-16 10:17:16 +02:00
if err := self.deactivateContext(currentContext, types.OnFocusLostOpts{NewContextKey: newContext.GetKey()}); err != nil {
2021-09-25 05:21:28 +02:00
return err
}
return self.activateContext(newContext, types.OnFocusOpts{})
2020-08-16 05:58:29 +02:00
}
func (self *ContextMgr) deactivateContext(c types.Context, opts types.OnFocusLostOpts) error {
view, _ := self.gui.c.GocuiGui().View(c.GetViewName())
2021-04-06 02:26:18 +02:00
if view != nil && view.IsSearching() {
if err := self.gui.onSearchEscape(); err != nil {
2021-04-06 02:26:18 +02:00
return err
2021-04-04 15:51:59 +02:00
}
2020-08-17 13:58:30 +02:00
}
2021-04-06 02:26:18 +02:00
// if we are the kind of context that is sent to back upon deactivation, we should do that
if view != nil &&
(c.GetKind() == types.TEMPORARY_POPUP ||
c.GetKind() == types.PERSISTENT_POPUP) {
2021-04-06 02:26:18 +02:00
view.Visible = false
}
if err := c.HandleFocusLost(opts); err != nil {
2020-08-17 13:58:30 +02:00
return err
}
return nil
}
func (self *ContextMgr) activateContext(c types.Context, opts types.OnFocusOpts) error {
2020-08-16 10:17:16 +02:00
viewName := c.GetViewName()
v, err := self.gui.c.GocuiGui().View(viewName)
2020-08-16 10:17:16 +02:00
if err != nil {
return err
2020-08-16 10:17:16 +02:00
}
self.gui.helpers.Window.SetWindowContext(c)
2020-08-19 11:07:14 +02:00
self.gui.helpers.Window.MoveToTopOfWindow(c)
if _, err := self.gui.c.GocuiGui().SetCurrentView(viewName); err != nil {
return err
2020-08-16 05:58:29 +02:00
}
desiredTitle := c.Title()
if desiredTitle != "" {
v.Title = desiredTitle
}
2021-04-04 15:51:59 +02:00
v.Visible = true
2020-08-16 05:58:29 +02:00
self.gui.c.GocuiGui().Cursor = v.Editable
2020-08-16 05:58:29 +02:00
self.gui.renderContextOptionsMap(c)
2020-08-16 05:58:29 +02:00
if err := c.HandleFocus(opts); err != nil {
2020-08-16 05:58:29 +02:00
return err
}
return nil
}
2023-03-23 09:47:29 +02:00
func (self *ContextMgr) Current() types.Context {
self.RLock()
defer self.RUnlock()
return self.currentContextWithoutLock()
}
func (self *ContextMgr) currentContextWithoutLock() types.Context {
if len(self.ContextStack) == 0 {
return self.gui.defaultSideContext()
}
return self.ContextStack[len(self.ContextStack)-1]
2020-08-16 02:05:45 +02:00
}
// Note that this could return the 'status' context which is not itself a list context.
2023-03-23 09:47:29 +02:00
func (self *ContextMgr) CurrentSide() types.Context {
self.RLock()
defer self.RUnlock()
stack := self.ContextStack
2020-08-22 00:49:02 +02:00
// find the first context in the stack with the type of types.SIDE_CONTEXT
2020-08-22 00:49:02 +02:00
for i := range stack {
context := stack[len(stack)-1-i]
if context.GetKind() == types.SIDE_CONTEXT {
2021-04-04 15:51:59 +02:00
return context
2020-08-22 00:49:02 +02:00
}
}
return self.gui.defaultSideContext()
2020-08-22 00:49:02 +02:00
}
2021-04-11 07:01:49 +02:00
// static as opposed to popup
2023-03-23 09:47:29 +02:00
func (self *ContextMgr) CurrentStatic() types.Context {
self.RLock()
defer self.RUnlock()
2021-04-11 07:01:49 +02:00
return self.currentStaticContextWithoutLock()
}
func (self *ContextMgr) currentStaticContextWithoutLock() types.Context {
stack := self.ContextStack
2021-04-11 07:01:49 +02:00
if len(stack) == 0 {
return self.gui.defaultSideContext()
2021-04-11 07:01:49 +02:00
}
// find the first context in the stack without a popup type
for i := range stack {
context := stack[len(stack)-1-i]
if context.GetKind() != types.TEMPORARY_POPUP && context.GetKind() != types.PERSISTENT_POPUP {
2021-04-11 07:01:49 +02:00
return context
}
}
return self.gui.defaultSideContext()
2021-04-11 07:01:49 +02:00
}
2023-03-23 09:47:29 +02:00
func (self *ContextMgr) ForEach(f func(types.Context)) {
self.RLock()
defer self.RUnlock()
for _, context := range self.gui.State.ContextMgr.ContextStack {
f(context)
}
}
func (self *ContextMgr) IsCurrent(c types.Context) bool {
return self.Current().GetKey() == c.GetKey()
}
2023-03-23 13:05:25 +02:00
// all list contexts
func (self *ContextMgr) AllList() []types.IListContext {
var listContexts []types.IListContext
for _, context := range self.allContexts.Flatten() {
if listContext, ok := context.(types.IListContext); ok {
listContexts = append(listContexts, listContext)
}
}
return listContexts
}
func (self *ContextMgr) AllPatchExplorer() []types.IPatchExplorerContext {
var listContexts []types.IPatchExplorerContext
for _, context := range self.allContexts.Flatten() {
if listContext, ok := context.(types.IPatchExplorerContext); ok {
listContexts = append(listContexts, listContext)
}
}
return listContexts
}