From c3aee42db8de6f63b8b124367db1fbaec60ca172 Mon Sep 17 00:00:00 2001 From: icechen Date: Tue, 15 Mar 2022 19:58:21 +0800 Subject: [PATCH] Add borderFocusStyle to box --- box.go | 67 ++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 51 insertions(+), 16 deletions(-) diff --git a/box.go b/box.go index d1c1c69..091d15c 100644 --- a/box.go +++ b/box.go @@ -36,6 +36,9 @@ type Box struct { // The border style. borderStyle tcell.Style + // The border focus style. + borderFocusStyle tcell.Style + // The title. Only visible if there is a border, too. title string @@ -71,13 +74,14 @@ type Box struct { // NewBox returns a Box without a border. func NewBox() *Box { b := &Box{ - width: 15, - height: 10, - innerX: -1, // Mark as uninitialized. - backgroundColor: Styles.PrimitiveBackgroundColor, - borderStyle: tcell.StyleDefault.Foreground(Styles.BorderColor).Background(Styles.PrimitiveBackgroundColor), - titleColor: Styles.TitleColor, - titleAlign: AlignCenter, + width: 15, + height: 10, + innerX: -1, // Mark as uninitialized. + backgroundColor: Styles.PrimitiveBackgroundColor, + borderStyle: tcell.StyleDefault.Foreground(Styles.BorderColor).Background(Styles.PrimitiveBackgroundColor), + borderFocusStyle: tcell.StyleDefault.Foreground(Styles.BorderColor).Background(Styles.PrimitiveBackgroundColor), + titleColor: Styles.TitleColor, + titleAlign: AlignCenter, } return b } @@ -253,10 +257,11 @@ func (b *Box) GetMouseCapture() func(action MouseAction, event *tcell.EventMouse func (b *Box) SetBackgroundColor(color tcell.Color) *Box { b.backgroundColor = color b.borderStyle = b.borderStyle.Background(color) + b.borderFocusStyle = b.borderFocusStyle.Background(color) return b } -// SetBorder sets the flag indicating whether or not the box should have a +// SetBorder sets the flag indicating whether the box should have a // border. func (b *Box) SetBorder(show bool) *Box { b.border = show @@ -290,6 +295,33 @@ func (b *Box) GetBorderColor() tcell.Color { return color } +// SetBorderFocusColor sets the box's border focus color. +func (b *Box) SetBorderFocusColor(color tcell.Color) *Box { + b.borderFocusStyle = b.borderFocusStyle.Foreground(color) + return b +} + +// SetBorderFocusAttributes sets the border's focus style attributes. You can combine +// different attributes using bitmask operations: +// +// box.SetBorderFocusAttributes(tcell.AttrUnderline | tcell.AttrBold) +func (b *Box) SetBorderFocusAttributes(attr tcell.AttrMask) *Box { + b.borderFocusStyle = b.borderFocusStyle.Attributes(attr) + return b +} + +// GetBorderFocusAttributes returns the border's focus style attributes. +func (b *Box) GetBorderFocusAttributes() tcell.AttrMask { + _, _, attr := b.borderFocusStyle.Decompose() + return attr +} + +// GetBorderFocusColor returns the box's border focus color. +func (b *Box) GetBorderFocusColor() tcell.Color { + color, _, _ := b.borderFocusStyle.Decompose() + return color +} + // GetBackgroundColor returns the box's background color. func (b *Box) GetBackgroundColor() tcell.Color { return b.backgroundColor @@ -351,7 +383,9 @@ func (b *Box) DrawForSubclass(screen tcell.Screen, p Primitive) { // Draw border. if b.border && b.width >= 2 && b.height >= 2 { var vertical, horizontal, topLeft, topRight, bottomLeft, bottomRight rune + var borderStyle tcell.Style if p.HasFocus() { + borderStyle = b.borderFocusStyle horizontal = Borders.HorizontalFocus vertical = Borders.VerticalFocus topLeft = Borders.TopLeftFocus @@ -359,6 +393,7 @@ func (b *Box) DrawForSubclass(screen tcell.Screen, p Primitive) { bottomLeft = Borders.BottomLeftFocus bottomRight = Borders.BottomRightFocus } else { + borderStyle = b.borderStyle horizontal = Borders.Horizontal vertical = Borders.Vertical topLeft = Borders.TopLeft @@ -367,17 +402,17 @@ func (b *Box) DrawForSubclass(screen tcell.Screen, p Primitive) { bottomRight = Borders.BottomRight } for x := b.x + 1; x < b.x+b.width-1; x++ { - screen.SetContent(x, b.y, horizontal, nil, b.borderStyle) - screen.SetContent(x, b.y+b.height-1, horizontal, nil, b.borderStyle) + screen.SetContent(x, b.y, horizontal, nil, borderStyle) + screen.SetContent(x, b.y+b.height-1, horizontal, nil, borderStyle) } for y := b.y + 1; y < b.y+b.height-1; y++ { - screen.SetContent(b.x, y, vertical, nil, b.borderStyle) - screen.SetContent(b.x+b.width-1, y, vertical, nil, b.borderStyle) + screen.SetContent(b.x, y, vertical, nil, borderStyle) + screen.SetContent(b.x+b.width-1, y, vertical, nil, borderStyle) } - screen.SetContent(b.x, b.y, topLeft, nil, b.borderStyle) - screen.SetContent(b.x+b.width-1, b.y, topRight, nil, b.borderStyle) - screen.SetContent(b.x, b.y+b.height-1, bottomLeft, nil, b.borderStyle) - screen.SetContent(b.x+b.width-1, b.y+b.height-1, bottomRight, nil, b.borderStyle) + screen.SetContent(b.x, b.y, topLeft, nil, borderStyle) + screen.SetContent(b.x+b.width-1, b.y, topRight, nil, borderStyle) + screen.SetContent(b.x, b.y+b.height-1, bottomLeft, nil, borderStyle) + screen.SetContent(b.x+b.width-1, b.y+b.height-1, bottomRight, nil, borderStyle) // Draw title. if b.title != "" && b.width >= 4 {