From b76656416e6bf1cf5268dc670c354254d8f48f99 Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 9 Nov 2021 18:56:15 +0100 Subject: [PATCH] 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. --- inputfield.go | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/inputfield.go b/inputfield.go index 7bbc99c..0191fe9 100644 --- a/inputfield.go +++ b/inputfield.go @@ -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 } - 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) + if inputBg != tcell.ColorDefault { + for index := 0; index < fieldWidth; index++ { + 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) } }