diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2020-08-23 10:50:27 +1000 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2020-08-23 14:29:18 +1000 |
commit | fda9f4ea7a476a11d4a50cbbd9ce657fe69ae2cb (patch) | |
tree | 499299faa4d6a6d59f9b10ad989448d52dddcd28 | |
parent | f876d8fdc82235f75a11a5807f05b4d8b469d521 (diff) |
centralise logic for rendering options map
-rw-r--r-- | pkg/gui/context.go | 36 | ||||
-rw-r--r-- | pkg/gui/files_panel.go | 2 | ||||
-rw-r--r-- | pkg/gui/list_context.go | 9 | ||||
-rw-r--r-- | pkg/gui/menu_panel.go | 5 | ||||
-rw-r--r-- | pkg/gui/merge_panel.go | 6 | ||||
-rw-r--r-- | pkg/gui/view_helpers.go | 22 |
6 files changed, 43 insertions, 37 deletions
diff --git a/pkg/gui/context.go b/pkg/gui/context.go index 3bfbab089..945f815e8 100644 --- a/pkg/gui/context.go +++ b/pkg/gui/context.go @@ -47,15 +47,24 @@ type Context interface { GetKey() string SetParentContext(Context) GetParentContext() Context + GetOptionsMap() map[string]string } type BasicContext struct { - OnFocus func() error - OnFocusLost func() error - OnRender func() error - Kind int - Key string - ViewName string + OnFocus func() error + OnFocusLost func() error + OnRender func() error + OnGetOptionsMap func() map[string]string + Kind int + Key string + ViewName string +} + +func (c BasicContext) GetOptionsMap() map[string]string { + if c.OnGetOptionsMap != nil { + return c.OnGetOptionsMap() + } + return nil } func (c BasicContext) SetWindowName(windowName string) { @@ -244,9 +253,10 @@ func (gui *Gui) contextTree() ContextTree { // TODO: centralise the code here // return gui.refreshMergePanel() }, - Kind: MAIN_CONTEXT, - ViewName: "main", - Key: MAIN_MERGING_CONTEXT_KEY, + Kind: MAIN_CONTEXT, + ViewName: "main", + Key: MAIN_MERGING_CONTEXT_KEY, + OnGetOptionsMap: gui.getMergingOptions, }, }, Credentials: SimpleContextNode{ @@ -470,10 +480,12 @@ func (gui *Gui) activateContext(c Context) error { gui.g.Cursor = newView.Editable - // TODO: move this logic to the context - if err := gui.renderPanelOptions(); err != nil { - return err + // render the options available for the current context at the bottom of the screen + optionsMap := c.GetOptionsMap() + if optionsMap == nil { + optionsMap = gui.globalOptionsMap() } + gui.renderOptionsMap(optionsMap) if err := c.HandleFocus(); err != nil { return err diff --git a/pkg/gui/files_panel.go b/pkg/gui/files_panel.go index f9312930c..61f3815fb 100644 --- a/pkg/gui/files_panel.go +++ b/pkg/gui/files_panel.go @@ -101,7 +101,7 @@ func (gui *Gui) refreshFiles() error { return err } - if g.CurrentView() == filesView || (g.CurrentView() == gui.getMainView() && g.CurrentView().Context == "merging") { + if g.CurrentView() == filesView || (g.CurrentView() == gui.getMainView() && g.CurrentView().Context == MAIN_MERGING_CONTEXT_KEY) { newSelectedFile := gui.getSelectedFile() alreadySelected := selectedFile != nil && newSelectedFile != nil && newSelectedFile.Name == selectedFile.Name return gui.selectFile(alreadySelected) diff --git a/pkg/gui/list_context.go b/pkg/gui/list_context.go index 23a96952d..5b64aa435 100644 --- a/pkg/gui/list_context.go +++ b/pkg/gui/list_context.go @@ -13,6 +13,7 @@ type ListContext struct { OnFocus func() error OnFocusLost func() error OnClickSelectedItem func() error + OnGetOptionsMap func() map[string]string // the boolean here tells us whether the item is nil. This is needed because you can't work it out on the calling end once the pointer is wrapped in an interface (unless you want to use reflection) SelectedItem func() (ListItem, bool) @@ -70,6 +71,13 @@ func (lc *ListContext) GetSelectedItemId() string { return item.ID() } +func (lc *ListContext) GetOptionsMap() map[string]string { + if lc.OnGetOptionsMap != nil { + return lc.OnGetOptionsMap() + } + return nil +} + // OnFocus assumes that the content of the context has already been rendered to the view. OnRender is the function which actually renders the content to the view func (lc *ListContext) OnRender() error { view, err := lc.Gui.g.View(lc.ViewName) @@ -241,6 +249,7 @@ func (gui *Gui) menuListContext() *ListContext { Gui: gui, ResetMainViewOriginOnFocus: false, Kind: PERSISTENT_POPUP, + OnGetOptionsMap: gui.getMenuOptions, // no GetDisplayStrings field because we do a custom render on menu creation } diff --git a/pkg/gui/menu_panel.go b/pkg/gui/menu_panel.go index 101e7d0cf..7f84831b7 100644 --- a/pkg/gui/menu_panel.go +++ b/pkg/gui/menu_panel.go @@ -32,13 +32,12 @@ func (gui *Gui) handleMenuSelect() error { // specific functions -func (gui *Gui) renderMenuOptions() error { - optionsMap := map[string]string{ +func (gui *Gui) getMenuOptions() map[string]string { + return map[string]string{ gui.getKeyDisplay("universal.return"): gui.Tr.SLocalize("close"), fmt.Sprintf("%s %s", gui.getKeyDisplay("universal.prevItem"), gui.getKeyDisplay("universal.nextItem")): gui.Tr.SLocalize("navigate"), gui.getKeyDisplay("universal.select"): gui.Tr.SLocalize("execute"), } - return gui.renderOptionsMap(optionsMap) } func (gui *Gui) handleMenuClose(g *gocui.Gui, v *gocui.View) error { diff --git a/pkg/gui/merge_panel.go b/pkg/gui/merge_panel.go index c23549d39..da54b01ed 100644 --- a/pkg/gui/merge_panel.go +++ b/pkg/gui/merge_panel.go @@ -292,14 +292,14 @@ func (gui *Gui) scrollToConflict(g *gocui.Gui) error { return nil } -func (gui *Gui) renderMergeOptions() error { - return gui.renderOptionsMap(map[string]string{ +func (gui *Gui) getMergingOptions() map[string]string { + return map[string]string{ fmt.Sprintf("%s %s", gui.getKeyDisplay("universal.prevItem"), gui.getKeyDisplay("universal.nextItem")): gui.Tr.SLocalize("selectHunk"), fmt.Sprintf("%s %s", gui.getKeyDisplay("universal.prevBlock"), gui.getKeyDisplay("universal.nextBlock")): gui.Tr.SLocalize("navigateConflicts"), gui.getKeyDisplay("universal.select"): gui.Tr.SLocalize("pickHunk"), gui.getKeyDisplay("main.pickBothHunks"): gui.Tr.SLocalize("pickBothHunks"), gui.getKeyDisplay("universal.undo"): gui.Tr.SLocalize("undo"), - }) + } } func (gui *Gui) handleEscapeMerge() error { diff --git a/pkg/gui/view_helpers.go b/pkg/gui/view_helpers.go index 4b60c4c26..796b7dca6 100644 --- a/pkg/gui/view_helpers.go +++ b/pkg/gui/view_helpers.go @@ -185,9 +185,8 @@ func (gui *Gui) optionsMapToString(optionsMap map[string]string) string { return strings.Join(optionsArray, ", ") } -func (gui *Gui) renderOptionsMap(optionsMap map[string]string) error { +func (gui *Gui) renderOptionsMap(optionsMap map[string]string) { gui.renderString("options", gui.optionsMapToString(optionsMap)) - return nil } // TODO: refactor properly @@ -327,28 +326,15 @@ func (gui *Gui) renderDisplayStrings(v *gocui.View, displayStrings [][]string) { }) } -func (gui *Gui) renderPanelOptions() error { - currentView := gui.g.CurrentView() - switch currentView.Name() { - case "menu": - return gui.renderMenuOptions() - case "main": - if gui.State.MainContext == "merging" { - return gui.renderMergeOptions() - } - } - return gui.renderGlobalOptions() -} - -func (gui *Gui) renderGlobalOptions() error { - return gui.renderOptionsMap(map[string]string{ +func (gui *Gui) globalOptionsMap() map[string]string { + return map[string]string{ fmt.Sprintf("%s/%s", gui.getKeyDisplay("universal.scrollUpMain"), gui.getKeyDisplay("universal.scrollDownMain")): gui.Tr.SLocalize("scroll"), fmt.Sprintf("%s %s %s %s", gui.getKeyDisplay("universal.prevBlock"), gui.getKeyDisplay("universal.nextBlock"), gui.getKeyDisplay("universal.prevItem"), gui.getKeyDisplay("universal.nextItem")): gui.Tr.SLocalize("navigate"), gui.getKeyDisplay("universal.return"): gui.Tr.SLocalize("cancel"), gui.getKeyDisplay("universal.quit"): gui.Tr.SLocalize("quit"), gui.getKeyDisplay("universal.optionMenu"): gui.Tr.SLocalize("menu"), "1-5": gui.Tr.SLocalize("jump"), - }) + } } func (gui *Gui) isPopupPanel(viewName string) bool { |