2017-12-15 22:29:21 +08:00
|
|
|
package tview
|
|
|
|
|
2017-12-17 05:48:26 +08:00
|
|
|
import (
|
|
|
|
"github.com/gdamore/tcell"
|
|
|
|
)
|
2017-12-15 22:29:21 +08:00
|
|
|
|
2017-12-21 03:54:49 +08:00
|
|
|
// Box implements Primitive with a background and optional elements such as a
|
|
|
|
// border and a title. Most subclasses keep their content contained in the box
|
|
|
|
// but don't necessarily have to.
|
2018-01-07 23:39:06 +08:00
|
|
|
//
|
2018-01-15 04:29:34 +08:00
|
|
|
// Note that all classes which subclass from Box will also have access to its
|
|
|
|
// functions.
|
|
|
|
//
|
2018-01-07 23:39:06 +08:00
|
|
|
// See https://github.com/rivo/tview/wiki/Box for an example.
|
2017-12-15 22:29:21 +08:00
|
|
|
type Box struct {
|
|
|
|
// The position of the rect.
|
|
|
|
x, y, width, height int
|
|
|
|
|
2018-02-22 19:33:11 +08:00
|
|
|
// The inner rect reserved for the box's content. This is only used if the
|
|
|
|
// "draw" callback is not nil.
|
|
|
|
innerX, innerY, innerWidth, innerHeight int
|
|
|
|
|
2017-12-22 01:08:53 +08:00
|
|
|
// Border padding.
|
|
|
|
paddingTop, paddingBottom, paddingLeft, paddingRight int
|
|
|
|
|
2017-12-15 22:29:21 +08:00
|
|
|
// The box's background color.
|
|
|
|
backgroundColor tcell.Color
|
|
|
|
|
|
|
|
// Whether or not a border is drawn, reducing the box's space for content by
|
|
|
|
// two in width and height.
|
|
|
|
border bool
|
|
|
|
|
|
|
|
// The color of the border.
|
|
|
|
borderColor tcell.Color
|
|
|
|
|
|
|
|
// The title. Only visible if there is a border, too.
|
|
|
|
title string
|
|
|
|
|
|
|
|
// The color of the title.
|
|
|
|
titleColor tcell.Color
|
2017-12-17 05:48:26 +08:00
|
|
|
|
2017-12-26 08:07:30 +08:00
|
|
|
// The alignment of the title.
|
|
|
|
titleAlign int
|
|
|
|
|
2017-12-17 05:48:26 +08:00
|
|
|
// Provides a way to find out if this box has focus. We always go through
|
2018-01-07 05:49:12 +08:00
|
|
|
// this interface because it may be overridden by implementing classes.
|
2017-12-17 05:48:26 +08:00
|
|
|
focus Focusable
|
|
|
|
|
|
|
|
// Whether or not this box has focus.
|
|
|
|
hasFocus bool
|
2018-01-15 04:29:34 +08:00
|
|
|
|
|
|
|
// An optional capture function which receives a key event and returns the
|
|
|
|
// event to be forwarded to the primitive's default input handler (nil if
|
|
|
|
// nothing should be forwarded).
|
|
|
|
inputCapture func(event *tcell.EventKey) *tcell.EventKey
|
2018-02-22 19:33:11 +08:00
|
|
|
|
|
|
|
// An optional function which is called before the box is drawn.
|
|
|
|
draw func(screen tcell.Screen, x, y, width, height int) (int, int, int, int)
|
2017-12-15 22:29:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewBox returns a Box without a border.
|
|
|
|
func NewBox() *Box {
|
2017-12-17 05:48:26 +08:00
|
|
|
b := &Box{
|
2018-01-10 16:43:32 +08:00
|
|
|
width: 15,
|
|
|
|
height: 10,
|
|
|
|
backgroundColor: Styles.PrimitiveBackgroundColor,
|
|
|
|
borderColor: Styles.BorderColor,
|
|
|
|
titleColor: Styles.TitleColor,
|
|
|
|
titleAlign: AlignCenter,
|
2017-12-15 22:29:21 +08:00
|
|
|
}
|
2017-12-17 05:48:26 +08:00
|
|
|
b.focus = b
|
|
|
|
return b
|
2017-12-15 22:29:21 +08:00
|
|
|
}
|
|
|
|
|
2017-12-24 07:08:52 +08:00
|
|
|
// SetBorderPadding sets the size of the borders around the box content.
|
2017-12-22 01:08:53 +08:00
|
|
|
func (b *Box) SetBorderPadding(top, bottom, left, right int) *Box {
|
|
|
|
b.paddingTop, b.paddingBottom, b.paddingLeft, b.paddingRight = top, bottom, left, right
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRect returns the current position of the rectangle, x, y, width, and
|
|
|
|
// height.
|
|
|
|
func (b *Box) GetRect() (int, int, int, int) {
|
|
|
|
return b.x, b.y, b.width, b.height
|
|
|
|
}
|
|
|
|
|
2018-02-20 03:23:01 +08:00
|
|
|
// GetInnerRect returns the position of the inner rectangle (x, y, width,
|
|
|
|
// height), without the border and without any padding.
|
2017-12-22 01:08:53 +08:00
|
|
|
func (b *Box) GetInnerRect() (int, int, int, int) {
|
2018-02-22 19:33:11 +08:00
|
|
|
if b.draw != nil {
|
|
|
|
return b.innerX, b.innerY, b.innerWidth, b.innerHeight
|
|
|
|
}
|
2017-12-22 01:08:53 +08:00
|
|
|
x, y, width, height := b.GetRect()
|
|
|
|
if b.border {
|
|
|
|
x++
|
|
|
|
y++
|
|
|
|
width -= 2
|
|
|
|
height -= 2
|
|
|
|
}
|
|
|
|
return x + b.paddingLeft,
|
|
|
|
y + b.paddingTop,
|
|
|
|
width - b.paddingLeft - b.paddingRight,
|
|
|
|
height - b.paddingTop - b.paddingBottom
|
|
|
|
}
|
|
|
|
|
2018-01-15 17:04:03 +08:00
|
|
|
// SetRect sets a new position of the primitive.
|
2017-12-22 01:08:53 +08:00
|
|
|
func (b *Box) SetRect(x, y, width, height int) {
|
|
|
|
b.x = x
|
|
|
|
b.y = y
|
|
|
|
b.width = width
|
|
|
|
b.height = height
|
|
|
|
}
|
|
|
|
|
2018-02-22 19:33:11 +08:00
|
|
|
// SetDrawFunc sets a callback function which is invoked after the box primitive
|
|
|
|
// has been drawn. This allows you to add a more individual style to the box
|
|
|
|
// (and all primitives which extend it).
|
|
|
|
//
|
|
|
|
// The function is provided with the box's dimensions (set via SetRect()). It
|
|
|
|
// must return the box's inner dimensions (x, y, width, height) which will be
|
|
|
|
// returned by GetInnerRect(), used by descendent primitives to draw their own
|
|
|
|
// content.
|
|
|
|
func (b *Box) SetDrawFunc(handler func(screen tcell.Screen, x, y, width, height int) (int, int, int, int)) *Box {
|
|
|
|
b.draw = handler
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2018-03-11 06:30:42 +08:00
|
|
|
// GetDrawFunc returns the callback function which was installed with
|
|
|
|
// SetDrawFunc() or nil if no such function has been installed.
|
|
|
|
func (b *Box) GetDrawFunc() func(screen tcell.Screen, x, y, width, height int) (int, int, int, int) {
|
|
|
|
return b.draw
|
|
|
|
}
|
|
|
|
|
2018-01-15 04:29:34 +08:00
|
|
|
// wrapInputHandler wraps an input handler (see InputHandler()) with the
|
|
|
|
// functionality to capture input (see SetInputCapture()) before passing it
|
|
|
|
// on to the provided (default) input handler.
|
|
|
|
func (b *Box) wrapInputHandler(inputHandler func(*tcell.EventKey, func(p Primitive))) func(*tcell.EventKey, func(p Primitive)) {
|
|
|
|
return func(event *tcell.EventKey, setFocus func(p Primitive)) {
|
|
|
|
if b.inputCapture != nil {
|
|
|
|
event = b.inputCapture(event)
|
|
|
|
}
|
|
|
|
if event != nil && inputHandler != nil {
|
|
|
|
inputHandler(event, setFocus)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-22 01:08:53 +08:00
|
|
|
// InputHandler returns nil.
|
|
|
|
func (b *Box) InputHandler() func(event *tcell.EventKey, setFocus func(p Primitive)) {
|
2018-01-15 04:29:34 +08:00
|
|
|
return b.wrapInputHandler(nil)
|
|
|
|
}
|
|
|
|
|
2018-01-15 04:54:05 +08:00
|
|
|
// SetInputCapture installs a function which captures key events before they are
|
2018-01-15 04:29:34 +08:00
|
|
|
// forwarded to the primitive's default key event handler. This function can
|
|
|
|
// then choose to forward that key event (or a different one) to the default
|
|
|
|
// handler by returning it. If nil is returned, the default handler will not
|
|
|
|
// be called.
|
2018-01-15 04:54:05 +08:00
|
|
|
//
|
|
|
|
// Providing a nil handler will remove a previously existing handler.
|
2018-01-15 04:29:34 +08:00
|
|
|
func (b *Box) SetInputCapture(capture func(event *tcell.EventKey) *tcell.EventKey) *Box {
|
|
|
|
b.inputCapture = capture
|
|
|
|
return b
|
2017-12-22 01:08:53 +08:00
|
|
|
}
|
|
|
|
|
2018-03-11 06:30:42 +08:00
|
|
|
// GetInputCapture returns the function installed with SetInputCapture() or nil
|
|
|
|
// if no such function has been installed.
|
|
|
|
func (b *Box) GetInputCapture() func(event *tcell.EventKey) *tcell.EventKey {
|
|
|
|
return b.inputCapture
|
|
|
|
}
|
|
|
|
|
2017-12-22 01:08:53 +08:00
|
|
|
// SetBackgroundColor sets the box's background color.
|
|
|
|
func (b *Box) SetBackgroundColor(color tcell.Color) *Box {
|
|
|
|
b.backgroundColor = color
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetBorder sets the flag indicating whether or not the box should have a
|
|
|
|
// border.
|
|
|
|
func (b *Box) SetBorder(show bool) *Box {
|
|
|
|
b.border = show
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetBorderColor sets the box's border color.
|
|
|
|
func (b *Box) SetBorderColor(color tcell.Color) *Box {
|
|
|
|
b.borderColor = color
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTitle sets the box's title.
|
|
|
|
func (b *Box) SetTitle(title string) *Box {
|
|
|
|
b.title = title
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTitleColor sets the box's title color.
|
|
|
|
func (b *Box) SetTitleColor(color tcell.Color) *Box {
|
|
|
|
b.titleColor = color
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2017-12-26 08:07:30 +08:00
|
|
|
// SetTitleAlign sets the alignment of the title, one of AlignLeft, AlignCenter,
|
|
|
|
// or AlignRight.
|
|
|
|
func (b *Box) SetTitleAlign(align int) *Box {
|
|
|
|
b.titleAlign = align
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2017-12-15 22:29:21 +08:00
|
|
|
// Draw draws this primitive onto the screen.
|
|
|
|
func (b *Box) Draw(screen tcell.Screen) {
|
|
|
|
// Don't draw anything if there is no space.
|
|
|
|
if b.width <= 0 || b.height <= 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
def := tcell.StyleDefault
|
|
|
|
|
|
|
|
// Fill background.
|
|
|
|
background := def.Background(b.backgroundColor)
|
|
|
|
for y := b.y; y < b.y+b.height; y++ {
|
|
|
|
for x := b.x; x < b.x+b.width; x++ {
|
|
|
|
screen.SetContent(x, y, ' ', nil, background)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw border.
|
|
|
|
if b.border && b.width >= 2 && b.height >= 2 {
|
|
|
|
border := background.Foreground(b.borderColor)
|
2017-12-17 05:48:26 +08:00
|
|
|
var vertical, horizontal, topLeft, topRight, bottomLeft, bottomRight rune
|
|
|
|
if b.focus.HasFocus() {
|
2017-12-26 08:07:30 +08:00
|
|
|
vertical = GraphicsDbVertBar
|
|
|
|
horizontal = GraphicsDbHorBar
|
|
|
|
topLeft = GraphicsDbTopLeftCorner
|
|
|
|
topRight = GraphicsDbTopRightCorner
|
|
|
|
bottomLeft = GraphicsDbBottomLeftCorner
|
|
|
|
bottomRight = GraphicsDbBottomRightCorner
|
2017-12-17 05:48:26 +08:00
|
|
|
} else {
|
2017-12-26 08:07:30 +08:00
|
|
|
vertical = GraphicsHoriBar
|
|
|
|
horizontal = GraphicsVertBar
|
|
|
|
topLeft = GraphicsTopLeftCorner
|
|
|
|
topRight = GraphicsTopRightCorner
|
|
|
|
bottomLeft = GraphicsBottomLeftCorner
|
|
|
|
bottomRight = GraphicsBottomRightCorner
|
2017-12-15 22:29:21 +08:00
|
|
|
}
|
|
|
|
for x := b.x + 1; x < b.x+b.width-1; x++ {
|
2017-12-17 05:48:26 +08:00
|
|
|
screen.SetContent(x, b.y, vertical, nil, border)
|
|
|
|
screen.SetContent(x, b.y+b.height-1, vertical, nil, border)
|
2017-12-15 22:29:21 +08:00
|
|
|
}
|
|
|
|
for y := b.y + 1; y < b.y+b.height-1; y++ {
|
2017-12-17 05:48:26 +08:00
|
|
|
screen.SetContent(b.x, y, horizontal, nil, border)
|
|
|
|
screen.SetContent(b.x+b.width-1, y, horizontal, nil, border)
|
2017-12-15 22:29:21 +08:00
|
|
|
}
|
2017-12-17 05:48:26 +08:00
|
|
|
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, b.y+b.height-1, bottomLeft, nil, border)
|
|
|
|
screen.SetContent(b.x+b.width-1, b.y+b.height-1, bottomRight, nil, border)
|
2017-12-15 22:29:21 +08:00
|
|
|
|
|
|
|
// Draw title.
|
|
|
|
if b.title != "" && b.width >= 4 {
|
2018-01-18 00:13:36 +08:00
|
|
|
_, printed := Print(screen, b.title, b.x+1, b.y, b.width-2, b.titleAlign, b.titleColor)
|
|
|
|
if StringWidth(b.title)-printed > 0 && printed > 0 {
|
|
|
|
_, _, style, _ := screen.GetContent(b.x+b.width-2, b.y)
|
|
|
|
fg, _, _ := style.Decompose()
|
|
|
|
Print(screen, string(GraphicsEllipsis), b.x+b.width-2, b.y, 1, AlignLeft, fg)
|
2017-12-15 22:29:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-22 19:33:11 +08:00
|
|
|
|
|
|
|
// Call custom draw function.
|
|
|
|
if b.draw != nil {
|
|
|
|
b.innerX, b.innerY, b.innerWidth, b.innerHeight = b.draw(screen, b.x, b.y, b.width, b.height)
|
|
|
|
}
|
2017-12-15 22:29:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Focus is called when this primitive receives focus.
|
2017-12-19 03:04:52 +08:00
|
|
|
func (b *Box) Focus(delegate func(p Primitive)) {
|
2017-12-15 22:29:21 +08:00
|
|
|
b.hasFocus = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Blur is called when this primitive loses focus.
|
|
|
|
func (b *Box) Blur() {
|
|
|
|
b.hasFocus = false
|
|
|
|
}
|
2017-12-17 05:48:26 +08:00
|
|
|
|
|
|
|
// HasFocus returns whether or not this primitive has focus.
|
|
|
|
func (b *Box) HasFocus() bool {
|
|
|
|
return b.hasFocus
|
|
|
|
}
|
2017-12-21 03:54:49 +08:00
|
|
|
|
|
|
|
// GetFocusable returns the item's Focusable.
|
|
|
|
func (b *Box) GetFocusable() Focusable {
|
|
|
|
return b.focus
|
|
|
|
}
|