Ignoring background colors of styles in InputField when set to ColorDefault. This makes it work when the primitive's background colour is not the default.

This commit is contained in:
Oliver 2021-11-09 18:56:15 +01:00
parent 2c1d4e4d73
commit b76656416e
1 changed files with 14 additions and 10 deletions

View File

@ -348,15 +348,16 @@ func (i *InputField) Draw(screen tcell.Screen) {
}
// Draw label.
_, labelBg, _ := i.labelStyle.Decompose()
if i.labelWidth > 0 {
labelWidth := i.labelWidth
if labelWidth > rightLimit-x {
labelWidth = rightLimit - x
}
printWithStyle(screen, i.label, x, y, 0, labelWidth, AlignLeft, i.labelStyle, false)
printWithStyle(screen, i.label, x, y, 0, labelWidth, AlignLeft, i.labelStyle, labelBg == tcell.ColorDefault)
x += labelWidth
} else {
_, drawnWidth, _, _ := printWithStyle(screen, i.label, x, y, 0, rightLimit-x, AlignLeft, i.labelStyle, false)
_, drawnWidth, _, _ := printWithStyle(screen, i.label, x, y, 0, rightLimit-x, AlignLeft, i.labelStyle, labelBg == tcell.ColorDefault)
x += drawnWidth
}
@ -364,18 +365,21 @@ func (i *InputField) Draw(screen tcell.Screen) {
i.fieldX = x
fieldWidth := i.fieldWidth
text := i.text
inputStyle := i.fieldStyle
placeholder := text == "" && i.placeholder != ""
if placeholder {
inputStyle = i.placeholderStyle
}
_, inputBg, _ := inputStyle.Decompose()
if fieldWidth == 0 {
fieldWidth = math.MaxInt32
}
if rightLimit-x < fieldWidth {
fieldWidth = rightLimit - x
}
if inputBg != tcell.ColorDefault {
for index := 0; index < fieldWidth; index++ {
if placeholder {
screen.SetContent(x+index, y, ' ', nil, i.placeholderStyle)
} else {
screen.SetContent(x+index, y, ' ', nil, i.fieldStyle)
screen.SetContent(x+index, y, ' ', nil, inputStyle)
}
}
@ -383,7 +387,7 @@ func (i *InputField) Draw(screen tcell.Screen) {
var cursorScreenPos int
if placeholder {
// Draw placeholder text.
printWithStyle(screen, Escape(i.placeholder), x, y, 0, fieldWidth, AlignLeft, i.placeholderStyle, false)
printWithStyle(screen, Escape(i.placeholder), x, y, 0, fieldWidth, AlignLeft, i.placeholderStyle, true)
i.offset = 0
} else {
// Draw entered text.
@ -392,7 +396,7 @@ func (i *InputField) Draw(screen tcell.Screen) {
}
if fieldWidth >= stringWidth(text) {
// We have enough space for the full text.
printWithStyle(screen, Escape(text), x, y, 0, fieldWidth, AlignLeft, i.fieldStyle, false)
printWithStyle(screen, Escape(text), x, y, 0, fieldWidth, AlignLeft, i.fieldStyle, true)
i.offset = 0
iterateString(text, func(main rune, comb []rune, textPos, textWidth, screenPos, screenWidth int) bool {
if textPos >= i.cursorPos {
@ -430,7 +434,7 @@ func (i *InputField) Draw(screen tcell.Screen) {
}
return false
})
printWithStyle(screen, Escape(text[i.offset:]), x, y, 0, fieldWidth, AlignLeft, i.fieldStyle, false)
printWithStyle(screen, Escape(text[i.offset:]), x, y, 0, fieldWidth, AlignLeft, i.fieldStyle, true)
}
}