diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2022-06-13 11:01:26 +1000 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2022-08-06 13:49:11 +1000 |
commit | 524bf83a4a681408c3fb57818f6968cab632e0ae (patch) | |
tree | 8858b4ee8d4670dcdd1637fe5fedf00ff080c154 /pkg/gui/main_panels.go | |
parent | 6dfef08efc5c7f262194c0af35fd777428f33a1a (diff) |
refactor to only have one context per view
Diffstat (limited to 'pkg/gui/main_panels.go')
-rw-r--r-- | pkg/gui/main_panels.go | 91 |
1 files changed, 73 insertions, 18 deletions
diff --git a/pkg/gui/main_panels.go b/pkg/gui/main_panels.go index cbb478446..ea9bfc3b3 100644 --- a/pkg/gui/main_panels.go +++ b/pkg/gui/main_panels.go @@ -10,18 +10,11 @@ import ( type viewUpdateOpts struct { title string - // awkwardly calling this noWrap because of how hard Go makes it to have - // a boolean option that defaults to true - noWrap bool - - highlight bool - task updateTask - - context types.Context } type refreshMainOpts struct { + pair MainContextPair main *viewUpdateOpts secondary *viewUpdateOpts } @@ -99,15 +92,21 @@ func (gui *Gui) runTaskForView(view *gocui.View, task updateTask) error { return nil } -func (gui *Gui) refreshMainView(opts *viewUpdateOpts, view *gocui.View) error { - view.Title = opts.title - view.Wrap = !opts.noWrap - view.Highlight = opts.highlight - context := opts.context - if context == nil { - context = gui.State.Contexts.Normal +func (gui *Gui) moveMainContextPairToTop(pair MainContextPair) { + gui.setWindowContext(pair.main) + gui.moveToTopOfWindow(pair.main) + if pair.secondary != nil { + gui.setWindowContext(pair.secondary) + gui.moveToTopOfWindow(pair.secondary) + } +} + +func (gui *Gui) refreshMainView(opts *viewUpdateOpts, context types.Context) error { + view := context.GetView() + + if opts.title != "" { + view.Title = opts.title } - gui.ViewContextMapSet(view.Name(), context) if err := gui.runTaskForView(view, opts.task); err != nil { gui.c.Log.Error(err) @@ -117,19 +116,75 @@ func (gui *Gui) refreshMainView(opts *viewUpdateOpts, view *gocui.View) error { return nil } +type MainContextPair struct { + main types.Context + secondary types.Context +} + +func (gui *Gui) normalMainContextPair() MainContextPair { + return MainContextPair{ + main: gui.State.Contexts.Normal, + secondary: gui.State.Contexts.NormalSecondary, + } +} + +func (gui *Gui) stagingMainContextPair() MainContextPair { + return MainContextPair{ + main: gui.State.Contexts.Staging, + secondary: gui.State.Contexts.StagingSecondary, + } +} + +func (gui *Gui) patchBuildingMainContextPair() MainContextPair { + return MainContextPair{ + main: gui.State.Contexts.CustomPatchBuilder, + secondary: gui.State.Contexts.CustomPatchBuilderSecondary, + } +} + +func (gui *Gui) mergingMainContextPair() MainContextPair { + return MainContextPair{ + main: gui.State.Contexts.Merging, + secondary: nil, + } +} + +func (gui *Gui) allMainContextPairs() []MainContextPair { + return []MainContextPair{ + gui.normalMainContextPair(), + gui.stagingMainContextPair(), + gui.patchBuildingMainContextPair(), + gui.mergingMainContextPair(), + } +} + func (gui *Gui) refreshMainViews(opts refreshMainOpts) error { + // need to reset scroll positions of all other main views + for _, pair := range gui.allMainContextPairs() { + if pair.main != opts.pair.main { + _ = pair.main.GetView().SetOrigin(0, 0) + } + if pair.secondary != nil && pair.secondary != opts.pair.secondary { + _ = pair.secondary.GetView().SetOrigin(0, 0) + } + } + if opts.main != nil { - if err := gui.refreshMainView(opts.main, gui.Views.Main); err != nil { + if err := gui.refreshMainView(opts.main, opts.pair.main); err != nil { return err } } if opts.secondary != nil { - if err := gui.refreshMainView(opts.secondary, gui.Views.Secondary); err != nil { + if err := gui.refreshMainView(opts.secondary, opts.pair.secondary); err != nil { return err } + } else if opts.pair.secondary != nil { + opts.pair.secondary.GetView().Clear() } + gui.moveMainContextPairToTop(opts.pair) + gui.splitMainPanel(opts.secondary != nil) return nil |