Fix input component overflow runewidth > 1

This commit is contained in:
erroneousboat 2017-12-16 15:10:16 +01:00
parent 23388070de
commit e596093f19
2 changed files with 33 additions and 3 deletions

View File

@ -135,17 +135,39 @@ func (i *Input) ScrollLeft() {
func (i *Input) ScrollRight() {
// Is the cursor at the far right of the Input component, cursor
// isn't at the end of the text
if i.CursorPositionScreen == i.Par.InnerBounds().Dx()-1 {
if (i.CursorPositionScreen + i.GetRuneWidthLeft()) > i.Par.InnerBounds().Dx()-1 {
// Increase offset to show what is on the right side
if i.Offset < len(i.Text) {
i.Offset++
i.Offset = i.CalculateOffset()
i.CursorPositionScreen = i.GetRuneWidthOffsetToCursor()
}
} else {
i.CursorPositionScreen += i.GetRuneWidthLeft()
}
}
// CalculateOffset will, based on the width of the runes on the
// left of the text cursor, calculate the offset that needs to
// be used by the Inpute Component
func (i *Input) CalculateOffset() int {
var offset int
var currentRuneWidth int
for j := (i.CursorPositionText - 1); currentRuneWidth < i.GetMaxWidth()-1; j-- {
currentRuneWidth += runewidth.RuneWidth(i.Text[j])
offset = j
}
return offset
}
// GetRunWidthOffsetToCursor will get the rune width of all
// the runes from the offset until the text cursor
func (i *Input) GetRuneWidthOffsetToCursor() int {
return runewidth.StringWidth(string(i.Text[i.Offset:i.CursorPositionText]))
}
// GetRuneWidthLeft will get the width of a rune on the left side
// of the CursorPositionText
func (i *Input) GetRuneWidthLeft() int {
@ -178,3 +200,9 @@ func (i *Input) Clear() {
func (i *Input) GetText() string {
return i.Par.Text
}
// GetMaxWidth returns the maximum number of positions
// the Input component can display
func (i *Input) GetMaxWidth() int {
return i.Par.InnerBounds().Dx() - 1
}

View File

@ -60,7 +60,9 @@ func eventHandler(ctx *context.AppContext) {
// Place your debugging statements here
if ctx.Debug {
ctx.View.Debug.Println("event received")
ctx.View.Debug.Println(
"event received",
)
}
}
}()