Skip to content

Commit

Permalink
typeahead: improve by also saving entry identifier and putting it on top
Browse files Browse the repository at this point in the history
  • Loading branch information
abenz1267 committed Aug 16, 2024
1 parent 79dac2b commit 33a0172
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
2 changes: 1 addition & 1 deletion cmd/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.7.5
0.7.6
10 changes: 10 additions & 0 deletions internal/ui/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ func (h *Handler) handle() {

func sortEntries(entries []util.Entry) {
slices.SortFunc(entries, func(a, b util.Entry) int {
if tahAcceptedIdentifier != "" {
if a.Identifier() == tahAcceptedIdentifier {
return -1
}

if b.Identifier() == tahAcceptedIdentifier {
return 1
}
}

text := elements.input.Text()

if text == "" {
Expand Down
22 changes: 15 additions & 7 deletions internal/ui/interactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ import (
)

var (
activationEnabled bool
amKey uint
cmdAltModifier gdk.ModifierType
amModifier gdk.ModifierType
amLabel string
commands map[string]func()
singleModule modules.Workable
activationEnabled bool
amKey uint
cmdAltModifier gdk.ModifierType
amModifier gdk.ModifierType
amLabel string
commands map[string]func()
singleModule modules.Workable
tahSuggestionIdentifier string
tahAcceptedIdentifier string
)

func setupCommands() {
Expand Down Expand Up @@ -250,6 +252,9 @@ func handleGlobalKeysPressed(val uint, code uint, modifier gdk.ModifierType) boo
return true
case gdk.KEY_Tab:
if elements.typeahead.Text() != "" {
tahAcceptedIdentifier = tahSuggestionIdentifier
tahSuggestionIdentifier = ""

elements.input.SetText(elements.typeahead.Text())
elements.input.SetPosition(-1)

Expand Down Expand Up @@ -752,6 +757,8 @@ func processAsync(ctx context.Context, text string) {
})
}

tahAcceptedIdentifier = ""

handler.mut.Unlock()

if !layout.Window.Box.Search.Spinner.Hide {
Expand All @@ -776,6 +783,7 @@ func setTypeahead(modules []modules.Workable) {
for _, v := range tah {
if strings.HasPrefix(v.Term, trimmed) {
toSet = v.Term
tahSuggestionIdentifier = v.Identifier
}
}

Expand Down

0 comments on commit 33a0172

Please sign in to comment.