|
|
@ -1,6 +1,8 @@
|
|
|
|
package tview
|
|
|
|
package tview
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
|
|
|
|
"math/rand"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/gdamore/tcell"
|
|
|
|
"github.com/gdamore/tcell"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
@ -13,9 +15,12 @@ import (
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// See https://github.com/rivo/tview/wiki/Box for an example.
|
|
|
|
// See https://github.com/rivo/tview/wiki/Box for an example.
|
|
|
|
type Box struct {
|
|
|
|
type Box struct {
|
|
|
|
|
|
|
|
id int
|
|
|
|
// The position of the rect.
|
|
|
|
// The position of the rect.
|
|
|
|
x, y, width, height int
|
|
|
|
x, y, width, height int
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
maxWidth, maxHeight, minWidth, minHeight int
|
|
|
|
|
|
|
|
|
|
|
|
// The inner rect reserved for the box's content.
|
|
|
|
// The inner rect reserved for the box's content.
|
|
|
|
innerX, innerY, innerWidth, innerHeight int
|
|
|
|
innerX, innerY, innerWidth, innerHeight int
|
|
|
|
|
|
|
|
|
|
|
@ -67,6 +72,7 @@ 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{
|
|
|
|
|
|
|
|
id: rand.Int(),
|
|
|
|
width: 15,
|
|
|
|
width: 15,
|
|
|
|
height: 10,
|
|
|
|
height: 10,
|
|
|
|
innerX: -1, // Mark as uninitialized.
|
|
|
|
innerX: -1, // Mark as uninitialized.
|
|
|
@ -80,15 +86,29 @@ func NewBox() *Box {
|
|
|
|
return b
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// SetHeight sets height of the box
|
|
|
|
// GetID returns unique id of box
|
|
|
|
func (b *Box) SetHeight(height int) *Box {
|
|
|
|
func (b *Box) GetID() int {
|
|
|
|
|
|
|
|
return b.id
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// SetSize sets size of the box
|
|
|
|
|
|
|
|
func (b *Box) SetSize(width, height int) *Box {
|
|
|
|
|
|
|
|
b.width = width
|
|
|
|
b.height = height
|
|
|
|
b.height = height
|
|
|
|
return b
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// SetWidth sets width of the box
|
|
|
|
// SetMinSize sets min size of the box
|
|
|
|
func (b *Box) SetWidth(width int) *Box {
|
|
|
|
func (b *Box) SetMinSize(width, height int) *Box {
|
|
|
|
b.width = width
|
|
|
|
b.minWidth = width
|
|
|
|
|
|
|
|
b.minHeight = height
|
|
|
|
|
|
|
|
return b
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// SetMaxSize sets max size of the box
|
|
|
|
|
|
|
|
func (b *Box) SetMaxSize(width, height int) *Box {
|
|
|
|
|
|
|
|
b.maxWidth = width
|
|
|
|
|
|
|
|
b.maxHeight = height
|
|
|
|
return b
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -114,7 +134,23 @@ func (b *Box) GetBorderPadding() (top, bottom, left, right int) {
|
|
|
|
// GetRect returns the current position of the rectangle, x, y, width, and
|
|
|
|
// GetRect returns the current position of the rectangle, x, y, width, and
|
|
|
|
// height.
|
|
|
|
// height.
|
|
|
|
func (b *Box) GetRect() (int, int, int, int) {
|
|
|
|
func (b *Box) GetRect() (int, int, int, int) {
|
|
|
|
return b.x, b.y, b.width, b.height
|
|
|
|
width := b.width
|
|
|
|
|
|
|
|
switch {
|
|
|
|
|
|
|
|
case b.maxWidth > 0 && width > b.maxWidth:
|
|
|
|
|
|
|
|
width = b.maxWidth
|
|
|
|
|
|
|
|
case b.minWidth > 0 && width < b.minWidth:
|
|
|
|
|
|
|
|
width = b.minWidth
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
height := b.height
|
|
|
|
|
|
|
|
switch {
|
|
|
|
|
|
|
|
case b.maxHeight > 0 && height > b.maxHeight:
|
|
|
|
|
|
|
|
height = b.maxHeight
|
|
|
|
|
|
|
|
case b.minHeight > 0 && height < b.minHeight:
|
|
|
|
|
|
|
|
height = b.minHeight
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return b.x, b.y, width, height
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GetInnerRect returns the position of the inner rectangle (x, y, width,
|
|
|
|
// GetInnerRect returns the position of the inner rectangle (x, y, width,
|
|
|
@ -256,8 +292,9 @@ func (b *Box) SetTitleAlign(align int) *Box {
|
|
|
|
|
|
|
|
|
|
|
|
// Draw draws this primitive onto the screen.
|
|
|
|
// Draw draws this primitive onto the screen.
|
|
|
|
func (b *Box) Draw(screen tcell.Screen) {
|
|
|
|
func (b *Box) Draw(screen tcell.Screen) {
|
|
|
|
|
|
|
|
_, _, width, height := b.GetRect()
|
|
|
|
// Don't draw anything if there is no space.
|
|
|
|
// Don't draw anything if there is no space.
|
|
|
|
if b.width <= 0 || b.height <= 0 {
|
|
|
|
if width <= 0 || height <= 0 {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -265,14 +302,14 @@ func (b *Box) Draw(screen tcell.Screen) {
|
|
|
|
|
|
|
|
|
|
|
|
// Fill background.
|
|
|
|
// Fill background.
|
|
|
|
background := def.Background(b.backgroundColor)
|
|
|
|
background := def.Background(b.backgroundColor)
|
|
|
|
for y := b.y; y < b.y+b.height; y++ {
|
|
|
|
for y := b.y; y < b.y+height; y++ {
|
|
|
|
for x := b.x; x < b.x+b.width; x++ {
|
|
|
|
for x := b.x; x < b.x+width; x++ {
|
|
|
|
screen.SetContent(x, y, ' ', nil, background)
|
|
|
|
screen.SetContent(x, y, ' ', nil, background)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Draw border.
|
|
|
|
// Draw border.
|
|
|
|
if b.border && b.width >= 2 && b.height >= 2 {
|
|
|
|
if b.border && width >= 2 && height >= 2 {
|
|
|
|
border := background.Foreground(b.borderColor)
|
|
|
|
border := background.Foreground(b.borderColor)
|
|
|
|
var vertical, horizontal, topLeft, topRight, bottomLeft, bottomRight rune
|
|
|
|
var vertical, horizontal, topLeft, topRight, bottomLeft, bottomRight rune
|
|
|
|
if b.focus.HasFocus() {
|
|
|
|
if b.focus.HasFocus() {
|
|
|
@ -290,34 +327,34 @@ func (b *Box) Draw(screen tcell.Screen) {
|
|
|
|
bottomLeft = Styles.GraphicsBottomLeftCorner
|
|
|
|
bottomLeft = Styles.GraphicsBottomLeftCorner
|
|
|
|
bottomRight = Styles.GraphicsBottomRightCorner
|
|
|
|
bottomRight = Styles.GraphicsBottomRightCorner
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for x := b.x + 1; x < b.x+b.width-1; x++ {
|
|
|
|
for x := b.x + 1; x < b.x+width-1; x++ {
|
|
|
|
screen.SetContent(x, b.y, vertical, nil, border)
|
|
|
|
screen.SetContent(x, b.y, vertical, nil, border)
|
|
|
|
screen.SetContent(x, b.y+b.height-1, vertical, nil, border)
|
|
|
|
screen.SetContent(x, b.y+height-1, vertical, nil, border)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for y := b.y + 1; y < b.y+b.height-1; y++ {
|
|
|
|
for y := b.y + 1; y < b.y+height-1; y++ {
|
|
|
|
screen.SetContent(b.x, y, horizontal, nil, border)
|
|
|
|
screen.SetContent(b.x, y, horizontal, nil, border)
|
|
|
|
screen.SetContent(b.x+b.width-1, y, horizontal, nil, border)
|
|
|
|
screen.SetContent(b.x+width-1, y, horizontal, nil, border)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
screen.SetContent(b.x, b.y, topLeft, nil, border)
|
|
|
|
screen.SetContent(b.x, b.y, topLeft, nil, border)
|
|
|
|
screen.SetContent(b.x+b.width-1, b.y, topRight, nil, border)
|
|
|
|
screen.SetContent(b.x+width-1, b.y, topRight, nil, border)
|
|
|
|
screen.SetContent(b.x, b.y+b.height-1, bottomLeft, nil, border)
|
|
|
|
screen.SetContent(b.x, b.y+height-1, bottomLeft, nil, border)
|
|
|
|
screen.SetContent(b.x+b.width-1, b.y+b.height-1, bottomRight, nil, border)
|
|
|
|
screen.SetContent(b.x+width-1, b.y+height-1, bottomRight, nil, border)
|
|
|
|
|
|
|
|
|
|
|
|
// Draw title.
|
|
|
|
// Draw title.
|
|
|
|
if b.title != "" && b.width >= 4 {
|
|
|
|
if b.title != "" && width >= 4 {
|
|
|
|
title := " " + b.title + " "
|
|
|
|
title := " " + b.title + " "
|
|
|
|
_, printed := Print(screen, title, b.x+1, b.y, b.width-2, b.titleAlign, b.titleColor)
|
|
|
|
_, printed := Print(screen, title, b.x+1, b.y, width-2, b.titleAlign, b.titleColor)
|
|
|
|
if StringWidth(title)-printed > 0 && printed > 0 {
|
|
|
|
if StringWidth(title)-printed > 0 && printed > 0 {
|
|
|
|
_, _, style, _ := screen.GetContent(b.x+b.width-2, b.y)
|
|
|
|
_, _, style, _ := screen.GetContent(b.x+width-2, b.y)
|
|
|
|
fg, _, _ := style.Decompose()
|
|
|
|
fg, _, _ := style.Decompose()
|
|
|
|
Print(screen, string(Styles.GraphicsEllipsis), b.x+b.width-2, b.y, 1, AlignLeft, fg)
|
|
|
|
Print(screen, string(Styles.GraphicsEllipsis), b.x+width-2, b.y, 1, AlignLeft, fg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Call custom draw function.
|
|
|
|
// Call custom draw function.
|
|
|
|
if b.draw != nil {
|
|
|
|
if b.draw != nil {
|
|
|
|
b.innerX, b.innerY, b.innerWidth, b.innerHeight = b.draw(screen, b.x, b.y, b.width, b.height)
|
|
|
|
b.innerX, b.innerY, b.innerWidth, b.innerHeight = b.draw(screen, b.x, b.y, width, height)
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
// Remember the inner rect.
|
|
|
|
// Remember the inner rect.
|
|
|
|
b.innerX = -1
|
|
|
|
b.innerX = -1
|
|
|
|