summaryrefslogtreecommitdiffstats
path: root/pkg/gui/context
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2024-01-14 09:50:52 +1100
committerJesse Duffield <jessedduffield@gmail.com>2024-01-19 10:47:21 +1100
commit54bd94ad24ca24ca12fab59e9dbf0d79fe7681da (patch)
tree0dd7c0819d29aa38828486f3e5c6b0a017790c5b /pkg/gui/context
parent8840c1a2b75749724b2dc6e329c2e150db93bc5a (diff)
Add SetSelection function for list contexts and use it in most places
The only time we should call SetSelectedLineIdx is when we are happy for a select range to be retained which means things like moving the selected line index to top top/bottom or up/down a page as the user navigates. But in every other case we should now call SetSelection because that will set the selected index and cancel the range which is almost always what we want.
Diffstat (limited to 'pkg/gui/context')
-rw-r--r--pkg/gui/context/commit_files_context.go2
-rw-r--r--pkg/gui/context/filtered_list_view_model.go2
-rw-r--r--pkg/gui/context/list_context_trait.go2
-rw-r--r--pkg/gui/context/local_commits_context.go2
-rw-r--r--pkg/gui/context/sub_commits_context.go2
-rw-r--r--pkg/gui/context/suggestions_context.go2
-rw-r--r--pkg/gui/context/traits/list_cursor.go15
-rw-r--r--pkg/gui/context/working_tree_context.go2
8 files changed, 22 insertions, 7 deletions
diff --git a/pkg/gui/context/commit_files_context.go b/pkg/gui/context/commit_files_context.go
index 037554c91..ad1ffa031 100644
--- a/pkg/gui/context/commit_files_context.go
+++ b/pkg/gui/context/commit_files_context.go
@@ -63,7 +63,7 @@ func NewCommitFilesContext(c *ContextCommon) *CommitFilesContext {
}
ctx.GetView().SetOnSelectItem(ctx.SearchTrait.onSelectItemWrapper(func(selectedLineIdx int) error {
- ctx.GetList().SetSelectedLineIdx(selectedLineIdx)
+ ctx.GetList().SetSelection(selectedLineIdx)
return ctx.HandleFocus(types.OnFocusOpts{})
}))
diff --git a/pkg/gui/context/filtered_list_view_model.go b/pkg/gui/context/filtered_list_view_model.go
index 1e649550a..c8abbe4a1 100644
--- a/pkg/gui/context/filtered_list_view_model.go
+++ b/pkg/gui/context/filtered_list_view_model.go
@@ -31,5 +31,5 @@ func (self *FilteredListViewModel[T]) ClearFilter() {
self.FilteredList.ClearFilter()
- self.SetSelectedLineIdx(unfilteredIndex)
+ self.SetSelection(unfilteredIndex)
}
diff --git a/pkg/gui/context/list_context_trait.go b/pkg/gui/context/list_context_trait.go
index c4f0e2549..d7315c9ec 100644
--- a/pkg/gui/context/list_context_trait.go
+++ b/pkg/gui/context/list_context_trait.go
@@ -102,7 +102,7 @@ func (self *ListContextTrait) HandleRender() error {
}
func (self *ListContextTrait) OnSearchSelect(selectedLineIdx int) error {
- self.GetList().SetSelectedLineIdx(selectedLineIdx)
+ self.GetList().SetSelection(selectedLineIdx)
return self.HandleFocus(types.OnFocusOpts{})
}
diff --git a/pkg/gui/context/local_commits_context.go b/pkg/gui/context/local_commits_context.go
index e0172638d..61a40b30b 100644
--- a/pkg/gui/context/local_commits_context.go
+++ b/pkg/gui/context/local_commits_context.go
@@ -85,7 +85,7 @@ func NewLocalCommitsContext(c *ContextCommon) *LocalCommitsContext {
}
ctx.GetView().SetOnSelectItem(ctx.SearchTrait.onSelectItemWrapper(func(selectedLineIdx int) error {
- ctx.GetList().SetSelectedLineIdx(selectedLineIdx)
+ ctx.GetList().SetSelection(selectedLineIdx)
return ctx.HandleFocus(types.OnFocusOpts{})
}))
diff --git a/pkg/gui/context/sub_commits_context.go b/pkg/gui/context/sub_commits_context.go
index 79b0d9781..1f795b44d 100644
--- a/pkg/gui/context/sub_commits_context.go
+++ b/pkg/gui/context/sub_commits_context.go
@@ -134,7 +134,7 @@ func NewSubCommitsContext(
}
ctx.GetView().SetOnSelectItem(ctx.SearchTrait.onSelectItemWrapper(func(selectedLineIdx int) error {
- ctx.GetList().SetSelectedLineIdx(selectedLineIdx)
+ ctx.GetList().SetSelection(selectedLineIdx)
return ctx.HandleFocus(types.OnFocusOpts{})
}))
diff --git a/pkg/gui/context/suggestions_context.go b/pkg/gui/context/suggestions_context.go
index e3c1f5f26..0921a7329 100644
--- a/pkg/gui/context/suggestions_context.go
+++ b/pkg/gui/context/suggestions_context.go
@@ -74,7 +74,7 @@ func (self *SuggestionsContext) GetSelectedItemId() string {
func (self *SuggestionsContext) SetSuggestions(suggestions []*types.Suggestion) {
self.State.Suggestions = suggestions
- self.SetSelectedLineIdx(0)
+ self.SetSelection(0)
self.c.ResetViewOrigin(self.GetView())
_ = self.HandleRender()
}
diff --git a/pkg/gui/context/traits/list_cursor.go b/pkg/gui/context/traits/list_cursor.go
index 647f2a36b..85cf22e30 100644
--- a/pkg/gui/context/traits/list_cursor.go
+++ b/pkg/gui/context/traits/list_cursor.go
@@ -45,10 +45,25 @@ func (self *ListCursor) GetSelectedLineIdx() int {
return self.selectedIdx
}
+// Sets the selected line index. Note, you probably don't want to use this directly,
+// because it doesn't affect the range select mode or range start index. You should only
+// use this for navigation situations where e.g. the user wants to jump to the top of
+// a list while in range select mode so that the selection ends up being between
+// the top of the list and the previous selection
func (self *ListCursor) SetSelectedLineIdx(value int) {
self.selectedIdx = self.clampValue(value)
}
+// Sets the selected index and cancels the range. You almost always want to use
+// this instead of SetSelectedLineIdx. For example, if you want to jump the cursor
+// to the top of a list after checking out a branch, you should use this method,
+// or you may end up with a large range selection from the previous cursor position
+// to the top of the list.
+func (self *ListCursor) SetSelection(value int) {
+ self.selectedIdx = self.clampValue(value)
+ self.CancelRangeSelect()
+}
+
func (self *ListCursor) clampValue(value int) int {
clampedValue := -1
if self.list.Len() > 0 {
diff --git a/pkg/gui/context/working_tree_context.go b/pkg/gui/context/working_tree_context.go
index 0e0b8d72b..72a991f76 100644
--- a/pkg/gui/context/working_tree_context.go
+++ b/pkg/gui/context/working_tree_context.go
@@ -50,7 +50,7 @@ func NewWorkingTreeContext(c *ContextCommon) *WorkingTreeContext {
}
ctx.GetView().SetOnSelectItem(ctx.SearchTrait.onSelectItemWrapper(func(selectedLineIdx int) error {
- ctx.GetList().SetSelectedLineIdx(selectedLineIdx)
+ ctx.GetList().SetSelection(selectedLineIdx)
return ctx.HandleFocus(types.OnFocusOpts{})
}))