From fd8a455aff852bdda6daaea82287a10fe882c97c Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sun, 16 Aug 2020 18:17:16 +1000 Subject: [PATCH] small things WIP --- pkg/gui/confirmation_panel.go | 6 +- pkg/gui/context.go | 131 ++++++++++++++-------------------- pkg/gui/line_by_line_panel.go | 2 +- 3 files changed, 58 insertions(+), 81 deletions(-) diff --git a/pkg/gui/confirmation_panel.go b/pkg/gui/confirmation_panel.go index 3ead76fc1..42f630422 100644 --- a/pkg/gui/confirmation_panel.go +++ b/pkg/gui/confirmation_panel.go @@ -168,9 +168,11 @@ func (gui *Gui) prepareConfirmationPanel(currentView *gocui.View, title, prompt } func (gui *Gui) onNewPopupPanel() { - viewNames := []string{"commitMessage", + viewNames := []string{ + "commitMessage", "credentials", - "menu"} + "menu", + } for _, viewName := range viewNames { _, _ = gui.g.SetViewOnBottom(viewName) } diff --git a/pkg/gui/context.go b/pkg/gui/context.go index c44cc7fc1..199047da4 100644 --- a/pkg/gui/context.go +++ b/pkg/gui/context.go @@ -1,60 +1,9 @@ package gui import ( - "github.com/golang-collections/collections/stack" "github.com/jesseduffield/gocui" ) -// changeContext is a helper function for when we want to change a 'main' context -// which currently just means a context that affects both the main and secondary views -// other views can have their context changed directly but this function helps -// keep the main and secondary views in sync -func (gui *Gui) changeMainViewsContext(context string) { - if gui.State.MainContext == context { - return - } - - switch context { - case "normal", "patch-building", "staging", "merging": - gui.getMainView().Context = context - gui.getSecondaryView().Context = context - } - - gui.State.MainContext = context -} - -type Stack struct { - stack []Context -} - -func (s *Stack) Push(contextKey Context) { - s.stack = append(s.stack, contextKey) -} - -func (s *Stack) Pop() (Context, bool) { - if len(s.stack) == 0 { - return nil, false - } - - n := len(s.stack) - 1 - value := s.stack[n] - s.stack = s.stack[:n] - - return value, true -} - -// the context manager maintains a stack of contexts so that we can easily switch focus back and forth -type contextManager struct { - gui *Gui - stack stack.Stack -} - -func (c *contextManager) push(contextKey string) { - c.stack.Push(contextKey) -} - -// push focus, pop focus. - const ( SIDE_CONTEXT int = iota MAIN_CONTEXT @@ -156,22 +105,43 @@ func (gui *Gui) switchContextToView(viewName string) error { return gui.switchContext(gui.State.ViewContextMap[viewName]) } -func (gui *Gui) renderContextStack() string { - result := "" - for _, context := range gui.State.ContextStack { - result += context.GetViewName() + "\n" +func (gui *Gui) returnFromContext() error { + // TODO: add mutexes + + if len(gui.State.ContextStack) == 1 { + // cannot escape from bottommost context + return nil } - return result + + n := len(gui.State.ContextStack) - 1 + + currentContext := gui.State.ContextStack[n] + newContext := gui.State.ContextStack[n-1] + + gui.State.ContextStack = gui.State.ContextStack[:n] + + if err := currentContext.HandleFocusLost(); err != nil { + return err + } + + return gui.activateContext(newContext) } func (gui *Gui) activateContext(c Context) error { gui.Log.Warn(gui.renderContextStack()) - if _, err := gui.g.SetCurrentView(c.GetViewName()); err != nil { + viewName := c.GetViewName() + _, err := gui.g.View(viewName) + // if view no longer exists, pop again + if err != nil { + return gui.returnFromContext() + } + + if _, err := gui.g.SetCurrentView(viewName); err != nil { return err } - if _, err := gui.g.SetViewOnTop(c.GetViewName()); err != nil { + if _, err := gui.g.SetViewOnTop(viewName); err != nil { return err } @@ -197,26 +167,12 @@ func (gui *Gui) activateContext(c Context) error { return nil } -func (gui *Gui) returnFromContext() error { - // TODO: add mutexes - - if len(gui.State.ContextStack) == 1 { - // cannot escape from bottommost context - return nil +func (gui *Gui) renderContextStack() string { + result := "" + for _, context := range gui.State.ContextStack { + result += context.GetViewName() + "\n" } - - n := len(gui.State.ContextStack) - 1 - - currentContext := gui.State.ContextStack[n] - newContext := gui.State.ContextStack[n-1] - - gui.State.ContextStack = gui.State.ContextStack[:n] - - if err := currentContext.HandleFocusLost(); err != nil { - return err - } - - return gui.activateContext(newContext) + return result } func (gui *Gui) currentContext() Context { @@ -267,7 +223,8 @@ func (gui *Gui) createContextTree() { Context: BasicContext{ // TODO: think about different situations where this arises OnFocus: func() error { - return gui.refreshStagingPanel(false, -1) + return nil + // return gui.refreshStagingPanel(false, -1) }, Kind: MAIN_CONTEXT, ViewName: "main", @@ -400,3 +357,21 @@ func (gui *Gui) onViewFocus(newView *gocui.View) error { return nil } + +// changeContext is a helper function for when we want to change a 'main' context +// which currently just means a context that affects both the main and secondary views +// other views can have their context changed directly but this function helps +// keep the main and secondary views in sync +func (gui *Gui) changeMainViewsContext(context string) { + if gui.State.MainContext == context { + return + } + + switch context { + case "normal", "patch-building", "staging", "merging": + gui.getMainView().Context = context + gui.getSecondaryView().Context = context + } + + gui.State.MainContext = context +} diff --git a/pkg/gui/line_by_line_panel.go b/pkg/gui/line_by_line_panel.go index 79b4d780a..1aedcabc1 100644 --- a/pkg/gui/line_by_line_panel.go +++ b/pkg/gui/line_by_line_panel.go @@ -332,7 +332,7 @@ func (gui *Gui) handleEscapeLineByLinePanel() { func (gui *Gui) handleOpenFileAtLine() error { // again, would be good to use inheritance here (or maybe even composition) var filename string - switch gui.currentContext().GetKey() { + switch gui.State.MainContext { case "patch-building": filename = gui.getSelectedCommitFileName() case "staging":