Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/goki/gi
Browse files Browse the repository at this point in the history
  • Loading branch information
rcoreilly committed Aug 23, 2018
2 parents 5a50544 + e90047f commit 0f6c8e6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 deletions.
8 changes: 4 additions & 4 deletions complete/complete.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (
"unicode"
)

// CompletionFunc is the function called to get the list of possible completions
// Func is the function called to get the list of possible completions
// and also determines the correct seed based on the text passed as a parameter of CompletionFunc
type Func func(text string) (matches []string, seed string)

// Matches returns a list of matches given a list of possibilities and a seed.
// MatchSeed returns a list of matches given a list of possibilities and a seed.
// The list must be presorted. The seed is basically a prefix.
func MatchSeed(completions []string, seed string) (matches []string) {
completions = completions
Expand Down Expand Up @@ -54,10 +54,10 @@ func MatchSeed(completions []string, seed string) (matches []string) {
return matches
}

// Extend tries to extend the current seed checking possible completions for a longer common seed
// ExtendSeed tries to extend the current seed checking possible completions for a longer common seed
// e.g. if the current seed is "ab" and the completions are "abcde" and "abcdf" then Extend returns "cd"
// but if the possible completions are "abcde" and "abz" then Extend returns ""
func Extend(matches []string, seed string) (extension string) {
func ExtendSeed(matches []string, seed string) (extension string) {
keep_looking := true
new_seed := seed
potential_seed := new_seed
Expand Down
31 changes: 14 additions & 17 deletions textfield.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,11 +529,19 @@ func (tf *TextField) OfferCompletions() {

var completions []string
completions, tf.Seed = tf.CompleteFunc(string(tf.EditTxt[0:tf.CursorPos]))
if len(completions) > 0 {
if len(completions) == 1 && completions[0] == tf.Seed { // don't show if only one and it completions current text
count := len(completions)
if count > 0 {
if count == 1 && completions[0] == tf.Seed { // don't show if only one and it completions current text
return
}
m := tf.MakeCompletionMenu(completions)
var m Menu
for i := 0; i < count; i++ {
s := completions[i]
m.AddMenuText(s, "", tf.This, nil, func(recv, send ki.Ki, sig int64, data interface{}) {
tff := recv.Embed(KiT_TextField).(*TextField)
tff.Complete(s)
})
}
cpos := tf.CharStartPos(tf.CursorPos).ToPoint()
// todo: figure popup placement using font and line height
vp := PopupMenu(m, cpos.X+15, cpos.Y+50, tf.Viewport, "tf-completion-menu")
Expand All @@ -542,19 +550,6 @@ func (tf *TextField) OfferCompletions() {
}
}

func (tf *TextField) MakeCompletionMenu(matches []string) Menu {
var m Menu
count := len(matches)
for i := 0; i < count; i++ {
s := matches[i]
m.AddMenuText(s, "", tf.This, nil, func(recv, send ki.Ki, sig int64, data interface{}) {
tff := recv.Embed(KiT_TextField).(*TextField)
tff.Complete(s)
})
}
return m
}

// Complete edits the text field using the string chosen from the completion menu
func (tf *TextField) Complete(str string) {
s1 := string(tf.EditTxt[0:tf.CursorPos])
Expand Down Expand Up @@ -663,7 +658,7 @@ func (tf *TextField) KeyInput(kt *key.ChordEvent) {
return
}
// try to extend the seed
s := complete.Extend(matches, tf.Seed)
s := complete.ExtendSeed(matches, tf.Seed)
if s != "" {
// todo: get currently selected menu item and set selected when new menu is offered
win.ClosePopup(win.Popup)
Expand All @@ -687,9 +682,11 @@ func (tf *TextField) KeyInput(kt *key.ChordEvent) {
case KeyFunMoveRight:
kt.SetProcessed()
tf.CursorForward(1)
tf.OfferCompletions()
case KeyFunMoveLeft:
kt.SetProcessed()
tf.CursorBackward(1)
tf.OfferCompletions()
case KeyFunHome:
kt.SetProcessed()
tf.CursorStart()
Expand Down

0 comments on commit 0f6c8e6

Please sign in to comment.