#63 - added password mode for the edit field

This commit is contained in:
Vladimir Markelov 2018-01-02 11:15:05 -08:00
parent 2e2184713b
commit 8d7474f23b
8 changed files with 102 additions and 13 deletions

View File

@ -1 +1 @@
0.6.2
0.6.3

View File

@ -1,4 +1,13 @@
2017-12-01 - version 0.6.2
2018-01-02 - version 0.6.3
[+] Add a new boolean property for EditField - PasswordMode. If PasswordMode is
true then the editfield text is hidden with 'stars'.
Please see demo demos/editfield for details
[+] Change a theme for object 'Edit' - added non-obligatory forth character
that is used as replacement for any character inside EditField when Password
mode is on. By default it is '*' in all included themes. Old themes that
miss forth character use '*' as well.
2017-12-01 - version 0.6.2
[*] Fix selecting the next control with TAB key
2017-11-28 - version 0.6.2

View File

@ -0,0 +1,45 @@
package main
import (
ui "github.com/VladimirMarkelov/clui"
)
func createView() {
view := ui.AddWindow(0, 0, 10, 7, "EditField Demo")
frmChk := ui.CreateFrame(view, 8, 5, ui.BorderNone, ui.Fixed)
frmChk.SetPack(ui.Vertical)
frmChk.SetPaddings(1, 1)
frmChk.SetGaps(1, 1)
ui.CreateLabel(frmChk, ui.AutoSize, ui.AutoSize, "Enter password:", ui.Fixed)
edFld := ui.CreateEditField(frmChk, 20, "", ui.Fixed)
chkPass := ui.CreateCheckBox(frmChk, ui.AutoSize, "Show Password", ui.Fixed)
ui.ActivateControl(view, edFld)
chkPass.OnChange(func(state int) {
if state == 0 {
edFld.SetPasswordMode(false)
ui.PutEvent(ui.Event{Type: ui.EventRedraw})
} else if state == 1 {
edFld.SetPasswordMode(true)
ui.PutEvent(ui.Event{Type: ui.EventRedraw})
}
})
}
func mainLoop() {
// Every application must create a single Composer and
// call its intialize method
ui.InitLibrary()
defer ui.DeinitLibrary()
createView()
// start event processing loop - the main core of the library
ui.MainLoop()
}
func main() {
mainLoop()
}

View File

@ -15,7 +15,7 @@ ProgressActiveText = white
//----------------- Objects -----------------
SingleBorder=-|++++
DoubleBorder==|++++
Edit=<>V
Edit=<>V*
ScrollBar=|O^V<>
ViewButtons=^_X[]
CheckBox=[] X?

View File

@ -81,7 +81,7 @@ TableHeaderBack=black
//----------------- Objects -----------------
SingleBorder=─│┌┐└┘
DoubleBorder=═║╔╗╚╝
Edit=←→V
Edit=←→V*
ScrollBar=░■▲▼◄►
ViewButtons=^↓○[]
CheckBox=[] X?

49
edit.go
View File

@ -4,6 +4,7 @@ import (
"github.com/atotto/clipboard"
xs "github.com/huandu/xstrings"
term "github.com/nsf/termbox-go"
"strings"
)
/*
@ -20,9 +21,10 @@ type EditField struct {
// cursor position in edit text
cursorPos int
// the number of the first displayed text character - it is used in case of text is longer than edit width
offset int
readonly bool
maxWidth int
offset int
readonly bool
maxWidth int
showStars bool
onChange func(Event)
onKeyPress func(term.Key) bool
@ -97,26 +99,46 @@ func (e *EditField) Draw() {
parts := []rune(SysObject(ObjEdit))
chLeft, chRight := string(parts[0]), string(parts[1])
chStar := "*"
if len(parts) > 3 {
chStar = string(parts[3])
}
var textOut string
curOff := 0
if e.offset == 0 && xs.Len(e.title) < e.width {
textOut = e.title
if e.showStars {
textOut = strings.Repeat(chStar, xs.Len(e.title))
} else {
textOut = e.title
}
} else {
fromIdx := 0
toIdx := 0
if e.offset == 0 {
toIdx = e.width - 1
textOut = xs.Slice(e.title, 0, toIdx) + chRight
if e.showStars {
textOut = strings.Repeat(chStar, toIdx) + chRight
} else {
textOut = xs.Slice(e.title, 0, toIdx) + chRight
}
curOff = -e.offset
} else {
curOff = 1 - e.offset
fromIdx = e.offset
if e.width-1 <= xs.Len(e.title)-e.offset {
toIdx = e.offset + e.width - 2
textOut = chLeft + xs.Slice(e.title, fromIdx, toIdx) + chRight
if e.showStars {
textOut = chLeft + strings.Repeat(chStar, toIdx-fromIdx) + chRight
} else {
textOut = chLeft + xs.Slice(e.title, fromIdx, toIdx) + chRight
}
} else {
textOut = chLeft + xs.Slice(e.title, fromIdx, -1)
if e.showStars {
textOut = chLeft + strings.Repeat(chStar, xs.Len(e.title)-fromIdx)
} else {
textOut = chLeft + xs.Slice(e.title, fromIdx, -1)
}
}
}
}
@ -361,3 +383,16 @@ func (e *EditField) SetSize(width, height int) {
e.height = 1
}
// PasswordMode returns whether password mode is enabled for the control
func (e *EditField) PasswordMode() bool {
return e.showStars
}
// SetPasswordMode changes the way an EditField displays it content.
// If PasswordMode is false then the EditField works as regular text entry
// control. If PasswordMode is true then the EditField shows its content hidden
// with star characters ('*' by default)
func (e *EditField) SetPasswordMode(pass bool) {
e.showStars = pass
}

View File

@ -136,7 +136,7 @@ func ThemeReset() {
defTheme.objects[ObjSingleBorder] = "─│┌┐└┘"
defTheme.objects[ObjDoubleBorder] = "═║╔╗╚╝"
defTheme.objects[ObjEdit] = "←→V"
defTheme.objects[ObjEdit] = "←→V*"
defTheme.objects[ObjScrollBar] = "░■▲▼◄►"
defTheme.objects[ObjViewButtons] = "^↓○[]"
defTheme.objects[ObjCheckBox] = "[] X?"

View File

@ -81,7 +81,7 @@ TableHeaderBack=black
//----------------- Objects -----------------
SingleBorder=─│┌┐└┘
DoubleBorder=═║╔╗╚╝
Edit=←→V
Edit=←→V*
ScrollBar=░■▲▼◄►
ViewButtons=^↓○[]
CheckBox=[] X?