summaryrefslogtreecommitdiffstats
path: root/pkg/gui
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2019-03-23 11:42:19 +1100
committerJesse Duffield <jessedduffield@gmail.com>2019-03-23 11:54:25 +1100
commitd84dfc23e7fe4e70ffd129a769d641c917972738 (patch)
tree91993749fd23b972b0aee1a101f3c2830de76295 /pkg/gui
parent9d8fd3594eac927504de2be3ff8dbf32cf6b84b6 (diff)
Rely on model rather than view to focus a point
Currently when we want to focus a point on a view (i.e. highlight a line and ensure it's within the bounds of a view's box, we use the LinesHeight method on the view to work out how many lines in total there are. This is bad because for example if we come back from editing a file, the view will have no contents so LinesHeight == 0, but we might be trying to select line 10 because there are actual ten things we expect to be rendered already. This causes a crash when e.g. 10 is greater than the height of the view. So we need to pass in to our FocusPoint method the actual number of items we want to render, rather than having the method rely on the LinesHeight, so that the method knows to scroll a bit before setting the cursor's y position. Unfortunately this makes for some awkward code with our current setup. We don't have a good interface type on these state objects so we now need to explicitly obtain the len() of whatever array we're rendering. In the case of the menu panel this is even more awkward because the items list is just an interface{} and it's not easy to get the list of that, so now when we instantiate a menu we need to pass in the count of items as well. The better solution would be to define an interface with a getItems and getLength method and have all these item arrays become structs implementing the interface, but I am too lazy to do this right now :)
Diffstat (limited to 'pkg/gui')
-rw-r--r--pkg/gui/branches_panel.go2
-rw-r--r--pkg/gui/commit_files_panel.go2
-rw-r--r--pkg/gui/commits_panel.go2
-rw-r--r--pkg/gui/files_panel.go2
-rw-r--r--pkg/gui/gui.go22
-rw-r--r--pkg/gui/menu_panel.go5
-rw-r--r--pkg/gui/options_menu_panel.go2
-rw-r--r--pkg/gui/rebase_options_panel.go2
-rw-r--r--pkg/gui/recent_repos_panel.go2
-rw-r--r--pkg/gui/stash_panel.go2
-rw-r--r--pkg/gui/view_helpers.go6
11 files changed, 28 insertions, 21 deletions
diff --git a/pkg/gui/branches_panel.go b/pkg/gui/branches_panel.go
index 8ee1b451d..4ce53bebd 100644
--- a/pkg/gui/branches_panel.go
+++ b/pkg/gui/branches_panel.go
@@ -34,7 +34,7 @@ func (gui *Gui) handleBranchSelect(g *gocui.Gui, v *gocui.View) error {
return gui.renderString(g, "main", gui.Tr.SLocalize("NoBranchesThisRepo"))
}
branch := gui.getSelectedBranch()
- if err := gui.focusPoint(0, gui.State.Panels.Branches.SelectedLine, v); err != nil {
+ if err := gui.focusPoint(0, gui.State.Panels.Branches.SelectedLine, len(gui.State.Branches), v); err != nil {
return err
}
go func() {
diff --git a/pkg/gui/commit_files_panel.go b/pkg/gui/commit_files_panel.go
index e8c20e0e7..2cdda6815 100644
--- a/pkg/gui/commit_files_panel.go
+++ b/pkg/gui/commit_files_panel.go
@@ -20,7 +20,7 @@ func (gui *Gui) handleCommitFileSelect(g *gocui.Gui, v *gocui.View) error {
return gui.renderString(g, "commitFiles", gui.Tr.SLocalize("NoCommiteFiles"))
}
- if err := gui.focusPoint(0, gui.State.Panels.CommitFiles.SelectedLine, v); err != nil {
+ if err := gui.focusPoint(0, gui.State.Panels.CommitFiles.SelectedLine, len(gui.State.CommitFiles), v); err != nil {
return err
}
commitText, err := gui.GitCommand.ShowCommitFile(commitFile.Sha, commitFile.Name)
diff --git a/pkg/gui/commits_panel.go b/pkg/gui/commits_panel.go
index 627b35835..1bd8be4a1 100644
--- a/pkg/gui/commits_panel.go
+++ b/pkg/gui/commits_panel.go
@@ -36,7 +36,7 @@ func (gui *Gui) handleCommitSelect(g *gocui.Gui, v *gocui.View) error {
return gui.renderString(g, "main", gui.Tr.SLocalize("NoCommitsThisBranch"))
}
- if err := gui.focusPoint(0, gui.State.Panels.Commits.SelectedLine, v); err != nil {
+ if err := gui.focusPoint(0, gui.State.Panels.Commits.SelectedLine, len(gui.State.Commits), v); err != nil {
return err
}
commitText, err := gui.GitCommand.Show(commit.Sha)
diff --git a/pkg/gui/files_panel.go b/pkg/gui/files_panel.go
index 45b3f4dd7..728ad2f97 100644
--- a/pkg/gui/files_panel.go
+++ b/pkg/gui/files_panel.go
@@ -63,7 +63,7 @@ func (gui *Gui) handleFileSelect(g *gocui.Gui, v *gocui.View, alreadySelected bo
return gui.renderString(g, "main", gui.Tr.SLocalize("NoChangedFiles"))
}
- if err := gui.focusPoint(0, gui.State.Panels.Files.SelectedLine, v); err != nil {
+ if err := gui.focusPoint(0, gui.State.Panels.Files.SelectedLine, len(gui.State.Files), v); err != nil {
return err
}
diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go
index d4ce0b3dc..07d51e2bc 100644
--- a/pkg/gui/gui.go
+++ b/pkg/gui/gui.go
@@ -135,6 +135,7 @@ type guiState struct {
Commits []*commands.Commit
StashEntries []*commands.StashEntry
CommitFiles []*commands.CommitFile
+ MenuItemCount int // can't store the actual list because it's of interface{} type
PreviousView string
Platform commands.Platform
Updating bool
@@ -480,20 +481,25 @@ func (gui *Gui) layout(g *gocui.Gui) error {
}
}
- listViews := map[*gocui.View]int{
- filesView: gui.State.Panels.Files.SelectedLine,
- branchesView: gui.State.Panels.Branches.SelectedLine,
- commitsView: gui.State.Panels.Commits.SelectedLine,
- stashView: gui.State.Panels.Stash.SelectedLine,
+ type listViewState struct {
+ selectedLine int
+ lineCount int
+ }
+
+ listViews := map[*gocui.View]listViewState{
+ filesView: {selectedLine: gui.State.Panels.Files.SelectedLine, lineCount: len(gui.State.Files)},
+ branchesView: {selectedLine: gui.State.Panels.Branches.SelectedLine, lineCount: len(gui.State.Branches)},
+ commitsView: {selectedLine: gui.State.Panels.Commits.SelectedLine, lineCount: len(gui.State.Commits)},
+ stashView: {selectedLine: gui.State.Panels.Stash.SelectedLine, lineCount: len(gui.State.StashEntries)},
}
// menu view might not exist so we check to be safe
if menuView, err := gui.g.View("menu"); err == nil {
- listViews[menuView] = gui.State.Panels.Menu.SelectedLine
+ listViews[menuView] = listViewState{selectedLine: gui.State.Panels.Menu.SelectedLine, lineCount: gui.State.MenuItemCount}
}
- for view, selectedLine := range listViews {
+ for view, state := range listViews {
// check if the selected line is now out of view and if so refocus it
- if err := gui.focusPoint(0, selectedLine, view); err != nil {
+ if err := gui.focusPoint(0, state.selectedLine, state.lineCount, view); err != nil {
return err
}
}
diff --git a/pkg/gui/menu_panel.go b/pkg/gui/menu_panel.go
index dbfa0cf01..d78a67199 100644
--- a/pkg/gui/menu_panel.go
+++ b/pkg/gui/menu_panel.go
@@ -10,7 +10,7 @@ import (
// list panel functions
func (gui *Gui) handleMenuSelect(g *gocui.Gui, v *gocui.View) error {
- return gui.focusPoint(0, gui.State.Panels.Menu.SelectedLine, v)
+ return gui.focusPoint(0, gui.State.Panels.Menu.SelectedLine, gui.State.MenuItemCount, v)
}
func (gui *Gui) handleMenuNextLine(g *gocui.Gui, v *gocui.View) error {
@@ -51,8 +51,9 @@ func (gui *Gui) handleMenuClose(g *gocui.Gui, v *gocui.View) error {
return gui.returnFocus(g, v)
}
-func (gui *Gui) createMenu(title string, items interface{}, handlePress func(int) error) error {
+func (gui *Gui) createMenu(title string, items interface{}, itemCount int, handlePress func(int) error) error {
isFocused := gui.g.CurrentView().Name() == "menu"
+ gui.State.MenuItemCount = itemCount
list, err := utils.RenderList(items, isFocused)
if err != nil {
return err
diff --git a/pkg/gui/options_menu_panel.go b/pkg/gui/options_menu_panel.go
index d217fb8f2..dfd354839 100644
--- a/pkg/gui/options_menu_panel.go
+++ b/pkg/gui/options_menu_panel.go
@@ -49,5 +49,5 @@ func (gui *Gui) handleCreateOptionsMenu(g *gocui.Gui, v *gocui.View) error {
return bindings[index].Handler(g, v)
}
- return gui.createMenu(strings.Title(gui.Tr.SLocalize("menu")), bindings, handleMenuPress)
+ return gui.createMenu(strings.Title(gui.Tr.SLocalize("menu")), bindings, len(bindings), handleMenuPress)
}
diff --git a/pkg/gui/rebase_options_panel.go b/pkg/gui/rebase_options_panel.go
index 337d81692..633574275 100644
--- a/pkg/gui/rebase_options_panel.go
+++ b/pkg/gui/rebase_options_panel.go
@@ -38,7 +38,7 @@ func (gui *Gui) handleCreateRebaseOptionsMenu(g *gocui.Gui, v *gocui.View) error
title = gui.Tr.SLocalize("RebaseOptionsTitle")
}
- return gui.createMenu(title, options, handleMenuPress)
+ return gui.createMenu(title, options, len(options), handleMenuPress)
}
func (gui *Gui) genericMergeCommand(command string) error {
diff --git a/pkg/gui/recent_repos_panel.go b/pkg/gui/recent_repos_panel.go
index 3f92431ea..a9ea5d481 100644
--- a/pkg/gui/recent_repos_panel.go
+++ b/pkg/gui/recent_repos_panel.go
@@ -44,7 +44,7 @@ func (gui *Gui) handleCreateRecentReposMenu(g *gocui.Gui, v *gocui.View) error {
return gui.Errors.ErrSwitchRepo
}
- return gui.createMenu(gui.Tr.SLocalize("RecentRepos"), recentRepos, handleMenuPress)
+ return gui.createMenu(gui.Tr.SLocalize("RecentRepos"), recentRepos, len(recentRepos), handleMenuPress)
}
// updateRecentRepoList registers the fact that we opened lazygit in this repo,
diff --git a/pkg/gui/stash_panel.go b/pkg/gui/stash_panel.go
index 05844f0ac..9c1851971 100644
--- a/pkg/gui/stash_panel.go
+++ b/pkg/gui/stash_panel.go
@@ -31,7 +31,7 @@ func (gui *Gui) handleStashEntrySelect(g *gocui.Gui, v *gocui.View) error {
if stashEntry == nil {
return gui.renderString(g, "main", gui.Tr.SLocalize("NoStashEntries"))
}
- if err := gui.focusPoint(0, gui.State.Panels.Stash.SelectedLine, v); err != nil {
+ if err := gui.focusPoint(0, gui.State.Panels.Stash.SelectedLine, len(gui.State.StashEntries), v); err != nil {
return err
}
go func() {
diff --git a/pkg/gui/view_helpers.go b/pkg/gui/view_helpers.go
index a258ca7e8..c7ee8dc88 100644
--- a/pkg/gui/view_helpers.go
+++ b/pkg/gui/view_helpers.go
@@ -180,8 +180,8 @@ func (gui *Gui) resetOrigin(v *gocui.View) error {
}
// if the cursor down past the last item, move it to the last line
-func (gui *Gui) focusPoint(cx int, cy int, v *gocui.View) error {
- if cy < 0 {
+func (gui *Gui) focusPoint(cx int, cy int, lineCount int, v *gocui.View) error {
+ if cy < 0 || cy > lineCount {
return nil
}
ox, oy := v.Origin()
@@ -191,7 +191,7 @@ func (gui *Gui) focusPoint(cx int, cy int, v *gocui.View) error {
// if line is above origin, move origin and set cursor to zero
// if line is below origin + height, move origin and set cursor to max
// otherwise set cursor to value - origin
- if ly > v.LinesHeight() {
+ if ly > lineCount {
if err := v.SetCursor(cx, cy); err != nil {
return err
}