1
0
mirror of https://github.com/rivo/tview.git synced 2025-04-26 13:49:06 +08:00

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

View File

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