Skip to content

Commit

Permalink
Add a secure option for text inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
hovatterz committed Dec 27, 2020
1 parent ee48700 commit 0e9d995
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions widget/textinput.go
Expand Up @@ -4,6 +4,7 @@ import (
img "image"
"image/color"
"math"
"strings"
"sync/atomic"
"time"

Expand Down Expand Up @@ -42,6 +43,7 @@ type TextInput struct {
scrollOffset int
focused bool
lastInputText string
secure bool
}

type TextInputOpt func(t *TextInput)
Expand Down Expand Up @@ -186,6 +188,12 @@ func (o TextInputOptions) Placeholder(s string) TextInputOpt {
}
}

func (o TextInputOptions) Secure(b bool) TextInputOpt {
return func(t *TextInput) {
t.secure = b
}
}

func (t *TextInput) GetWidget() *Widget {
t.init.Do()
return t.widget
Expand Down Expand Up @@ -426,9 +434,14 @@ func (t *TextInput) drawTextAndCaret(screen *ebiten.Image, def DeferredRenderFun
tr := rect
tr = tr.Add(img.Point{t.padding.Left, t.padding.Top})

inputStr := t.InputText
if t.secure {
inputStr = strings.Repeat("*", len(t.InputText))
}

cx := 0
if t.focused {
sub := string([]rune(t.InputText)[:t.cursorPosition])
sub := string([]rune(inputStr)[:t.cursorPosition])
cx = fontAdvance(sub, t.face)

dx := tr.Min.X + t.scrollOffset + cx + t.caret.Width + t.padding.Right - rect.Max.X
Expand All @@ -446,7 +459,7 @@ func (t *TextInput) drawTextAndCaret(screen *ebiten.Image, def DeferredRenderFun

t.text.SetLocation(tr)
if len([]rune(t.InputText)) > 0 {
t.text.Label = t.InputText
t.text.Label = inputStr
} else {
t.text.Label = t.placeholderText
}
Expand Down

0 comments on commit 0e9d995

Please sign in to comment.