gui: implement PreviousViews stack

This commit is contained in:
Dawid Dziurla 2019-08-31 12:02:27 +02:00
parent b12414907b
commit 897d47d0cc
No known key found for this signature in database
GPG key ID: 7B6D8368172E9B0B
4 changed files with 15 additions and 8 deletions

View file

@ -1,6 +1,7 @@
package gui
import (
"github.com/golang-collections/collections/stack"
"strings"
"sync"
@ -117,7 +118,7 @@ type panelStates struct {
type guiState struct {
MenuItemCount int // can't store the actual list because it's of interface{} type
PreviousView string
PreviousViews *stack.Stack
Platform commands.Platform
Panels *panelStates
SubProcessOutput string
@ -131,6 +132,10 @@ type guiState struct {
// NewGui builds a new gui handler
func NewGui(log *logrus.Entry, dockerCommand *commands.DockerCommand, oSCommand *commands.OSCommand, tr *i18n.TranslationSet, config *config.AppConfig, errorChan chan error) (*Gui, error) {
previousViews := stack.New()
// push dummy initial string
previousViews.Push("")
initialState := guiState{
Platform: *oSCommand.Platform,
Panels: &panelStates{
@ -144,7 +149,8 @@ func NewGui(log *logrus.Entry, dockerCommand *commands.DockerCommand, oSCommand
},
Project: &projectState{ContextIndex: 0},
},
SessionIndex: 0,
SessionIndex: 0,
PreviousViews: previousViews,
}
cyclableViews := []string{"project", "containers", "images", "volumes"}

View file

@ -94,7 +94,7 @@ func (gui *Gui) layout(g *gocui.Gui) error {
}
currView := gui.g.CurrentView()
currentCyclebleView := gui.State.PreviousView
currentCyclebleView := gui.State.PreviousViews.Peek().(string)
if currView != nil {
viewName := currView.Name()
usePreviouseView := true
@ -106,7 +106,7 @@ func (gui *Gui) layout(g *gocui.Gui) error {
}
}
if usePreviouseView {
currentCyclebleView = gui.State.PreviousView
currentCyclebleView = gui.State.PreviousViews.Peek().(string)
}
}
@ -273,7 +273,7 @@ func (gui *Gui) layout(g *gocui.Gui) error {
}
if gui.g.CurrentView() == nil {
v, err := gui.g.View(gui.State.PreviousView)
v, err := gui.g.View(gui.State.PreviousViews.Peek().(string))
if err != nil {
viewName := gui.initiallyFocusedViewName()
v, err = gui.g.View(viewName)

View file

@ -22,7 +22,7 @@ func (gui *Gui) RunWithSubprocesses() error {
break
} else if err == gui.Errors.ErrSubProcess {
// preparing the state for when we return
gui.State.PreviousView = gui.currentViewName()
gui.State.PreviousViews.Push(gui.currentViewName())
// giving goEvery goroutines time to finish
gui.State.SessionIndex++

View file

@ -103,7 +103,8 @@ func (gui *Gui) newLineFocused(v *gocui.View) error {
}
func (gui *Gui) returnFocus(g *gocui.Gui, v *gocui.View) error {
previousView, err := g.View(gui.State.PreviousView)
previousViewName := gui.State.PreviousViews.Pop().(string)
previousView, err := g.View(previousViewName)
if err != nil {
// always fall back to services view if there's no 'previous' view stored
previousView, err = g.View(gui.initiallyFocusedViewName())
@ -120,7 +121,7 @@ func (gui *Gui) switchFocus(g *gocui.Gui, oldView, newView *gocui.View) error {
// we assume we'll never want to return focus to a popup panel i.e.
// we should never stack popup panels
if oldView != nil && !gui.isPopupPanel(oldView.Name()) {
gui.State.PreviousView = oldView.Name()
gui.State.PreviousViews.Push(oldView.Name())
}
gui.Log.Info("setting highlight to true for view " + newView.Name())