Add borderFocusStyle to box

This commit is contained in:
icechen 2022-03-15 19:58:21 +08:00
parent 9994674d60
commit c3aee42db8
1 changed files with 51 additions and 16 deletions

67
box.go
View File

@ -36,6 +36,9 @@ type Box struct {
// The border style. // The border style.
borderStyle tcell.Style borderStyle tcell.Style
// The border focus style.
borderFocusStyle tcell.Style
// The title. Only visible if there is a border, too. // The title. Only visible if there is a border, too.
title string title string
@ -71,13 +74,14 @@ type Box struct {
// NewBox returns a Box without a border. // NewBox returns a Box without a border.
func NewBox() *Box { func NewBox() *Box {
b := &Box{ b := &Box{
width: 15, width: 15,
height: 10, height: 10,
innerX: -1, // Mark as uninitialized. innerX: -1, // Mark as uninitialized.
backgroundColor: Styles.PrimitiveBackgroundColor, backgroundColor: Styles.PrimitiveBackgroundColor,
borderStyle: tcell.StyleDefault.Foreground(Styles.BorderColor).Background(Styles.PrimitiveBackgroundColor), borderStyle: tcell.StyleDefault.Foreground(Styles.BorderColor).Background(Styles.PrimitiveBackgroundColor),
titleColor: Styles.TitleColor, borderFocusStyle: tcell.StyleDefault.Foreground(Styles.BorderColor).Background(Styles.PrimitiveBackgroundColor),
titleAlign: AlignCenter, titleColor: Styles.TitleColor,
titleAlign: AlignCenter,
} }
return b return b
} }
@ -253,10 +257,11 @@ func (b *Box) GetMouseCapture() func(action MouseAction, event *tcell.EventMouse
func (b *Box) SetBackgroundColor(color tcell.Color) *Box { func (b *Box) SetBackgroundColor(color tcell.Color) *Box {
b.backgroundColor = color b.backgroundColor = color
b.borderStyle = b.borderStyle.Background(color) b.borderStyle = b.borderStyle.Background(color)
b.borderFocusStyle = b.borderFocusStyle.Background(color)
return b 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. // border.
func (b *Box) SetBorder(show bool) *Box { func (b *Box) SetBorder(show bool) *Box {
b.border = show b.border = show
@ -290,6 +295,33 @@ func (b *Box) GetBorderColor() tcell.Color {
return 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. // GetBackgroundColor returns the box's background color.
func (b *Box) GetBackgroundColor() tcell.Color { func (b *Box) GetBackgroundColor() tcell.Color {
return b.backgroundColor return b.backgroundColor
@ -351,7 +383,9 @@ func (b *Box) DrawForSubclass(screen tcell.Screen, p Primitive) {
// Draw border. // Draw border.
if b.border && b.width >= 2 && b.height >= 2 { if b.border && b.width >= 2 && b.height >= 2 {
var vertical, horizontal, topLeft, topRight, bottomLeft, bottomRight rune var vertical, horizontal, topLeft, topRight, bottomLeft, bottomRight rune
var borderStyle tcell.Style
if p.HasFocus() { if p.HasFocus() {
borderStyle = b.borderFocusStyle
horizontal = Borders.HorizontalFocus horizontal = Borders.HorizontalFocus
vertical = Borders.VerticalFocus vertical = Borders.VerticalFocus
topLeft = Borders.TopLeftFocus topLeft = Borders.TopLeftFocus
@ -359,6 +393,7 @@ func (b *Box) DrawForSubclass(screen tcell.Screen, p Primitive) {
bottomLeft = Borders.BottomLeftFocus bottomLeft = Borders.BottomLeftFocus
bottomRight = Borders.BottomRightFocus bottomRight = Borders.BottomRightFocus
} else { } else {
borderStyle = b.borderStyle
horizontal = Borders.Horizontal horizontal = Borders.Horizontal
vertical = Borders.Vertical vertical = Borders.Vertical
topLeft = Borders.TopLeft topLeft = Borders.TopLeft
@ -367,17 +402,17 @@ func (b *Box) DrawForSubclass(screen tcell.Screen, p Primitive) {
bottomRight = Borders.BottomRight bottomRight = Borders.BottomRight
} }
for x := b.x + 1; x < b.x+b.width-1; x++ { 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, horizontal, nil, borderStyle)
screen.SetContent(x, b.y+b.height-1, horizontal, nil, b.borderStyle) screen.SetContent(x, b.y+b.height-1, horizontal, nil, borderStyle)
} }
for y := b.y + 1; y < b.y+b.height-1; y++ { 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, y, vertical, nil, borderStyle)
screen.SetContent(b.x+b.width-1, y, vertical, nil, b.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.y, topLeft, nil, borderStyle)
screen.SetContent(b.x+b.width-1, b.y, topRight, nil, b.borderStyle) screen.SetContent(b.x+b.width-1, b.y, topRight, nil, borderStyle)
screen.SetContent(b.x, b.y+b.height-1, bottomLeft, nil, b.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, b.borderStyle) screen.SetContent(b.x+b.width-1, b.y+b.height-1, bottomRight, nil, borderStyle)
// Draw title. // Draw title.
if b.title != "" && b.width >= 4 { if b.title != "" && b.width >= 4 {