2017-12-15 14:29:21 +00:00
|
|
|
package tview
|
|
|
|
|
2017-12-16 21:48:26 +00:00
|
|
|
import (
|
2020-10-18 12:15:57 +00:00
|
|
|
"github.com/gdamore/tcell/v2"
|
2017-12-16 21:48:26 +00:00
|
|
|
)
|
2017-12-15 14:29:21 +00:00
|
|
|
|
2020-02-19 20:46:40 +00:00
|
|
|
// Box implements the Primitive interface with an empty background and optional
|
|
|
|
// elements such as a border and a title. Box itself does not hold any content
|
|
|
|
// but serves as the superclass of all other primitives. Subclasses add their
|
|
|
|
// own content, typically (but not necessarily) keeping their content within the
|
|
|
|
// box's rectangle.
|
2018-01-07 15:39:06 +00:00
|
|
|
//
|
2020-02-19 20:46:40 +00:00
|
|
|
// Box provides a number of utility functions available to all primitives.
|
2018-01-14 20:29:34 +00:00
|
|
|
//
|
2018-01-07 15:39:06 +00:00
|
|
|
// See https://github.com/rivo/tview/wiki/Box for an example.
|
2017-12-15 14:29:21 +00:00
|
|
|
type Box struct {
|
|
|
|
// The position of the rect.
|
|
|
|
x, y, width, height int
|
|
|
|
|
2018-03-23 20:08:52 +00:00
|
|
|
// The inner rect reserved for the box's content.
|
2018-02-22 11:33:11 +00:00
|
|
|
innerX, innerY, innerWidth, innerHeight int
|
|
|
|
|
2017-12-21 17:08:53 +00:00
|
|
|
// Border padding.
|
|
|
|
paddingTop, paddingBottom, paddingLeft, paddingRight int
|
|
|
|
|
2017-12-15 14:29:21 +00:00
|
|
|
// The box's background color.
|
|
|
|
backgroundColor tcell.Color
|
|
|
|
|
2021-03-12 17:48:52 +00:00
|
|
|
// If set to true, the background of this box is not cleared while drawing.
|
|
|
|
dontClear bool
|
|
|
|
|
2017-12-15 14:29:21 +00:00
|
|
|
// Whether or not a border is drawn, reducing the box's space for content by
|
|
|
|
// two in width and height.
|
|
|
|
border bool
|
|
|
|
|
2020-10-18 12:15:57 +00:00
|
|
|
// The border style.
|
|
|
|
borderStyle tcell.Style
|
2018-08-07 07:23:19 +00:00
|
|
|
|
2017-12-15 14:29:21 +00:00
|
|
|
// The title. Only visible if there is a border, too.
|
|
|
|
title string
|
|
|
|
|
|
|
|
// The color of the title.
|
|
|
|
titleColor tcell.Color
|
2017-12-16 21:48:26 +00:00
|
|
|
|
2017-12-26 00:07:30 +00:00
|
|
|
// The alignment of the title.
|
|
|
|
titleAlign int
|
|
|
|
|
2021-11-08 16:05:49 +00:00
|
|
|
// Whether or not this box has focus. This is typically ignored for
|
|
|
|
// container primitives (e.g. Flex, Grid, Pages), as they will delegate
|
|
|
|
// focus to their children.
|
2017-12-16 21:48:26 +00:00
|
|
|
hasFocus bool
|
2018-01-14 20:29:34 +00:00
|
|
|
|
2021-11-08 16:05:49 +00:00
|
|
|
// Optional callback functions invoked when the primitive receives or loses
|
|
|
|
// focus.
|
|
|
|
focus, blur func()
|
|
|
|
|
2018-01-14 20:29:34 +00: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 11:33:11 +00: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)
|
2019-11-04 05:34:46 +00:00
|
|
|
|
|
|
|
// An optional capture function which receives a mouse event and returns the
|
2020-03-27 17:41:44 +00:00
|
|
|
// event to be forwarded to the primitive's default mouse event handler (at
|
|
|
|
// least one nil if nothing should be forwarded).
|
2020-02-14 02:09:09 +00:00
|
|
|
mouseCapture func(action MouseAction, event *tcell.EventMouse) (MouseAction, *tcell.EventMouse)
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewBox returns a Box without a border.
|
|
|
|
func NewBox() *Box {
|
2017-12-16 21:48:26 +00:00
|
|
|
b := &Box{
|
2018-01-10 08:43:32 +00:00
|
|
|
width: 15,
|
|
|
|
height: 10,
|
2018-03-23 20:08:52 +00:00
|
|
|
innerX: -1, // Mark as uninitialized.
|
2018-01-10 08:43:32 +00:00
|
|
|
backgroundColor: Styles.PrimitiveBackgroundColor,
|
2021-03-12 17:48:52 +00:00
|
|
|
borderStyle: tcell.StyleDefault.Foreground(Styles.BorderColor).Background(Styles.PrimitiveBackgroundColor),
|
2018-01-10 08:43:32 +00:00
|
|
|
titleColor: Styles.TitleColor,
|
|
|
|
titleAlign: AlignCenter,
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
2017-12-16 21:48:26 +00:00
|
|
|
return b
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
|
|
|
|
2017-12-23 23:08:52 +00:00
|
|
|
// SetBorderPadding sets the size of the borders around the box content.
|
2017-12-21 17:08:53 +00: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-19 19:23:01 +00:00
|
|
|
// GetInnerRect returns the position of the inner rectangle (x, y, width,
|
2019-07-09 13:01:41 +00:00
|
|
|
// height), without the border and without any padding. Width and height values
|
|
|
|
// will clamp to 0 and thus never be negative.
|
2017-12-21 17:08:53 +00:00
|
|
|
func (b *Box) GetInnerRect() (int, int, int, int) {
|
2018-03-23 20:08:52 +00:00
|
|
|
if b.innerX >= 0 {
|
2018-02-22 11:33:11 +00:00
|
|
|
return b.innerX, b.innerY, b.innerWidth, b.innerHeight
|
|
|
|
}
|
2017-12-21 17:08:53 +00:00
|
|
|
x, y, width, height := b.GetRect()
|
|
|
|
if b.border {
|
|
|
|
x++
|
|
|
|
y++
|
|
|
|
width -= 2
|
|
|
|
height -= 2
|
|
|
|
}
|
2019-07-09 13:01:41 +00:00
|
|
|
x, y, width, height = x+b.paddingLeft,
|
|
|
|
y+b.paddingTop,
|
|
|
|
width-b.paddingLeft-b.paddingRight,
|
|
|
|
height-b.paddingTop-b.paddingBottom
|
|
|
|
if width < 0 {
|
|
|
|
width = 0
|
|
|
|
}
|
|
|
|
if height < 0 {
|
|
|
|
height = 0
|
|
|
|
}
|
|
|
|
return x, y, width, height
|
2017-12-21 17:08:53 +00:00
|
|
|
}
|
|
|
|
|
2018-12-26 20:24:39 +00:00
|
|
|
// SetRect sets a new position of the primitive. Note that this has no effect
|
|
|
|
// if this primitive is part of a layout (e.g. Flex, Grid) or if it was added
|
|
|
|
// like this:
|
|
|
|
//
|
2022-09-02 15:49:12 +00:00
|
|
|
// application.SetRoot(p, true)
|
2017-12-21 17:08:53 +00:00
|
|
|
func (b *Box) SetRect(x, y, width, height int) {
|
|
|
|
b.x = x
|
|
|
|
b.y = y
|
|
|
|
b.width = width
|
|
|
|
b.height = height
|
2018-10-17 15:40:28 +00:00
|
|
|
b.innerX = -1 // Mark inner rect as uninitialized.
|
2017-12-21 17:08:53 +00:00
|
|
|
}
|
|
|
|
|
2018-02-22 11:33:11 +00: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-10 22:30:42 +00: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-03-19 20:25:30 +00:00
|
|
|
// WrapInputHandler wraps an input handler (see InputHandler()) with the
|
2018-01-14 20:29:34 +00:00
|
|
|
// functionality to capture input (see SetInputCapture()) before passing it
|
|
|
|
// on to the provided (default) input handler.
|
2018-03-19 20:25:30 +00:00
|
|
|
//
|
|
|
|
// This is only meant to be used by subclassing primitives.
|
|
|
|
func (b *Box) WrapInputHandler(inputHandler func(*tcell.EventKey, func(p Primitive))) func(*tcell.EventKey, func(p Primitive)) {
|
2018-01-14 20:29:34 +00:00
|
|
|
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-21 17:08:53 +00:00
|
|
|
// InputHandler returns nil.
|
|
|
|
func (b *Box) InputHandler() func(event *tcell.EventKey, setFocus func(p Primitive)) {
|
2018-03-19 20:25:30 +00:00
|
|
|
return b.WrapInputHandler(nil)
|
2018-01-14 20:29:34 +00:00
|
|
|
}
|
|
|
|
|
2018-01-14 20:54:05 +00:00
|
|
|
// SetInputCapture installs a function which captures key events before they are
|
2018-01-14 20:29:34 +00: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-14 20:54:05 +00:00
|
|
|
//
|
|
|
|
// Providing a nil handler will remove a previously existing handler.
|
2018-11-26 11:36:27 +00:00
|
|
|
//
|
2022-02-16 16:25:59 +00:00
|
|
|
// This function can also be used on container primitives (like Flex, Grid, or
|
|
|
|
// Form) as keyboard events will be handed down until they are handled.
|
2018-01-14 20:29:34 +00:00
|
|
|
func (b *Box) SetInputCapture(capture func(event *tcell.EventKey) *tcell.EventKey) *Box {
|
|
|
|
b.inputCapture = capture
|
|
|
|
return b
|
2017-12-21 17:08:53 +00:00
|
|
|
}
|
|
|
|
|
2018-03-10 22:30:42 +00: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
|
|
|
|
}
|
|
|
|
|
2019-11-04 05:34:46 +00:00
|
|
|
// WrapMouseHandler wraps a mouse event handler (see MouseHandler()) with the
|
2020-03-27 17:41:44 +00:00
|
|
|
// functionality to capture mouse events (see SetMouseCapture()) before passing
|
|
|
|
// them on to the provided (default) event handler.
|
2019-11-04 05:34:46 +00:00
|
|
|
//
|
|
|
|
// This is only meant to be used by subclassing primitives.
|
2020-02-14 02:09:09 +00:00
|
|
|
func (b *Box) WrapMouseHandler(mouseHandler func(MouseAction, *tcell.EventMouse, func(p Primitive)) (bool, Primitive)) func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
|
|
|
|
return func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
|
2019-11-04 05:34:46 +00:00
|
|
|
if b.mouseCapture != nil {
|
2020-02-14 02:09:09 +00:00
|
|
|
action, event = b.mouseCapture(action, event)
|
2019-11-04 05:34:46 +00:00
|
|
|
}
|
2019-11-05 09:19:40 +00:00
|
|
|
if event != nil && mouseHandler != nil {
|
2020-02-14 02:09:09 +00:00
|
|
|
consumed, capture = mouseHandler(action, event, setFocus)
|
2019-11-04 05:34:46 +00:00
|
|
|
}
|
2020-01-24 20:40:34 +00:00
|
|
|
return
|
2019-11-04 05:34:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MouseHandler returns nil.
|
2020-02-14 02:09:09 +00:00
|
|
|
func (b *Box) MouseHandler() func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
|
2020-03-27 17:41:44 +00:00
|
|
|
return b.WrapMouseHandler(func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
|
2022-09-02 15:49:12 +00:00
|
|
|
if action == MouseLeftDown && b.InRect(event.Position()) {
|
2020-03-27 17:41:44 +00:00
|
|
|
setFocus(b)
|
|
|
|
consumed = true
|
|
|
|
}
|
|
|
|
return
|
|
|
|
})
|
2019-11-04 05:34:46 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 17:41:44 +00:00
|
|
|
// SetMouseCapture sets a function which captures mouse events (consisting of
|
|
|
|
// the original tcell mouse event and the semantic mouse action) before they are
|
|
|
|
// forwarded to the primitive's default mouse event handler. This function can
|
|
|
|
// then choose to forward that event (or a different one) by returning it or
|
|
|
|
// returning a nil mouse event, in which case the default handler will not be
|
|
|
|
// called.
|
2019-11-04 05:34:46 +00:00
|
|
|
//
|
|
|
|
// Providing a nil handler will remove a previously existing handler.
|
2020-02-14 02:09:09 +00:00
|
|
|
func (b *Box) SetMouseCapture(capture func(action MouseAction, event *tcell.EventMouse) (MouseAction, *tcell.EventMouse)) *Box {
|
2019-11-04 05:34:46 +00:00
|
|
|
b.mouseCapture = capture
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2020-03-27 17:41:44 +00:00
|
|
|
// InRect returns true if the given coordinate is within the bounds of the box's
|
|
|
|
// rectangle.
|
|
|
|
func (b *Box) InRect(x, y int) bool {
|
|
|
|
rectX, rectY, width, height := b.GetRect()
|
|
|
|
return x >= rectX && x < rectX+width && y >= rectY && y < rectY+height
|
2020-01-24 20:40:34 +00:00
|
|
|
}
|
|
|
|
|
2019-11-04 05:34:46 +00:00
|
|
|
// GetMouseCapture returns the function installed with SetMouseCapture() or nil
|
|
|
|
// if no such function has been installed.
|
2020-02-14 02:09:09 +00:00
|
|
|
func (b *Box) GetMouseCapture() func(action MouseAction, event *tcell.EventMouse) (MouseAction, *tcell.EventMouse) {
|
2019-11-04 05:34:46 +00:00
|
|
|
return b.mouseCapture
|
|
|
|
}
|
|
|
|
|
2017-12-21 17:08:53 +00:00
|
|
|
// SetBackgroundColor sets the box's background color.
|
|
|
|
func (b *Box) SetBackgroundColor(color tcell.Color) *Box {
|
|
|
|
b.backgroundColor = color
|
2021-03-12 11:58:47 +00:00
|
|
|
b.borderStyle = b.borderStyle.Background(color)
|
2017-12-21 17:08:53 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-12-17 18:20:43 +00:00
|
|
|
// SetBorderStyle sets the box's border style.
|
|
|
|
func (b *Box) SetBorderStyle(style tcell.Style) *Box {
|
|
|
|
b.borderStyle = style
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2017-12-21 17:08:53 +00:00
|
|
|
// SetBorderColor sets the box's border color.
|
|
|
|
func (b *Box) SetBorderColor(color tcell.Color) *Box {
|
2020-10-18 12:15:57 +00:00
|
|
|
b.borderStyle = b.borderStyle.Foreground(color)
|
2017-12-21 17:08:53 +00:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2018-08-07 07:23:19 +00:00
|
|
|
// SetBorderAttributes sets the border's style attributes. You can combine
|
|
|
|
// different attributes using bitmask operations:
|
|
|
|
//
|
2022-09-02 15:49:12 +00:00
|
|
|
// box.SetBorderAttributes(tcell.AttrUnderline | tcell.AttrBold)
|
2018-08-07 07:23:19 +00:00
|
|
|
func (b *Box) SetBorderAttributes(attr tcell.AttrMask) *Box {
|
2020-10-18 12:15:57 +00:00
|
|
|
b.borderStyle = b.borderStyle.Attributes(attr)
|
2018-08-07 07:23:19 +00:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2020-04-14 11:29:54 +00:00
|
|
|
// GetBorderAttributes returns the border's style attributes.
|
|
|
|
func (b *Box) GetBorderAttributes() tcell.AttrMask {
|
2020-10-18 12:15:57 +00:00
|
|
|
_, _, attr := b.borderStyle.Decompose()
|
|
|
|
return attr
|
2020-04-14 11:29:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBorderColor returns the box's border color.
|
|
|
|
func (b *Box) GetBorderColor() tcell.Color {
|
2020-10-18 12:15:57 +00:00
|
|
|
color, _, _ := b.borderStyle.Decompose()
|
|
|
|
return color
|
2020-04-14 11:29:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBackgroundColor returns the box's background color.
|
|
|
|
func (b *Box) GetBackgroundColor() tcell.Color {
|
|
|
|
return b.backgroundColor
|
|
|
|
}
|
|
|
|
|
2017-12-21 17:08:53 +00:00
|
|
|
// SetTitle sets the box's title.
|
|
|
|
func (b *Box) SetTitle(title string) *Box {
|
|
|
|
b.title = title
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2019-06-04 21:14:24 +00:00
|
|
|
// GetTitle returns the box's current title.
|
|
|
|
func (b *Box) GetTitle() string {
|
|
|
|
return b.title
|
|
|
|
}
|
|
|
|
|
2017-12-21 17:08:53 +00:00
|
|
|
// SetTitleColor sets the box's title color.
|
|
|
|
func (b *Box) SetTitleColor(color tcell.Color) *Box {
|
|
|
|
b.titleColor = color
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2017-12-26 00:07:30 +00: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 14:29:21 +00:00
|
|
|
// Draw draws this primitive onto the screen.
|
|
|
|
func (b *Box) Draw(screen tcell.Screen) {
|
2020-11-17 18:33:25 +00:00
|
|
|
b.DrawForSubclass(screen, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DrawForSubclass draws this box under the assumption that primitive p is a
|
|
|
|
// subclass of this box. This is needed e.g. to draw proper box frames which
|
|
|
|
// depend on the subclass's focus.
|
|
|
|
//
|
|
|
|
// Only call this function from your own custom primitives. It is not needed in
|
|
|
|
// applications that have no custom primitives.
|
|
|
|
func (b *Box) DrawForSubclass(screen tcell.Screen, p Primitive) {
|
2017-12-15 14:29:21 +00:00
|
|
|
// Don't draw anything if there is no space.
|
|
|
|
if b.width <= 0 || b.height <= 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill background.
|
2022-11-15 14:33:49 +00:00
|
|
|
background := tcell.StyleDefault.Background(b.backgroundColor)
|
2021-03-12 17:48:52 +00:00
|
|
|
if !b.dontClear {
|
2018-05-02 15:02:52 +00:00
|
|
|
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)
|
|
|
|
}
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw border.
|
|
|
|
if b.border && b.width >= 2 && b.height >= 2 {
|
2017-12-16 21:48:26 +00:00
|
|
|
var vertical, horizontal, topLeft, topRight, bottomLeft, bottomRight rune
|
2020-11-17 18:33:25 +00:00
|
|
|
if p.HasFocus() {
|
2018-05-27 08:49:03 +00:00
|
|
|
horizontal = Borders.HorizontalFocus
|
|
|
|
vertical = Borders.VerticalFocus
|
|
|
|
topLeft = Borders.TopLeftFocus
|
|
|
|
topRight = Borders.TopRightFocus
|
|
|
|
bottomLeft = Borders.BottomLeftFocus
|
|
|
|
bottomRight = Borders.BottomRightFocus
|
2017-12-16 21:48:26 +00:00
|
|
|
} else {
|
2018-05-27 08:49:03 +00:00
|
|
|
horizontal = Borders.Horizontal
|
|
|
|
vertical = Borders.Vertical
|
|
|
|
topLeft = Borders.TopLeft
|
|
|
|
topRight = Borders.TopRight
|
|
|
|
bottomLeft = Borders.BottomLeft
|
|
|
|
bottomRight = Borders.BottomRight
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
|
|
|
for x := b.x + 1; x < b.x+b.width-1; x++ {
|
2020-10-18 12:15:57 +00:00
|
|
|
screen.SetContent(x, b.y, horizontal, nil, b.borderStyle)
|
|
|
|
screen.SetContent(x, b.y+b.height-1, horizontal, nil, b.borderStyle)
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
|
|
|
for y := b.y + 1; y < b.y+b.height-1; y++ {
|
2020-10-18 12:15:57 +00:00
|
|
|
screen.SetContent(b.x, y, vertical, nil, b.borderStyle)
|
|
|
|
screen.SetContent(b.x+b.width-1, y, vertical, nil, b.borderStyle)
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
2020-10-18 12:15:57 +00:00
|
|
|
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)
|
2017-12-15 14:29:21 +00:00
|
|
|
|
|
|
|
// Draw title.
|
|
|
|
if b.title != "" && b.width >= 4 {
|
2018-10-17 15:40:28 +00:00
|
|
|
printed, _ := Print(screen, b.title, b.x+1, b.y, b.width-2, b.titleAlign, b.titleColor)
|
|
|
|
if len(b.title)-printed > 0 && printed > 0 {
|
2023-08-22 20:16:59 +00:00
|
|
|
xEllipsis := b.x + b.width - 2
|
|
|
|
if b.titleAlign == AlignRight {
|
|
|
|
xEllipsis = b.x + 1
|
|
|
|
}
|
|
|
|
_, _, style, _ := screen.GetContent(xEllipsis, b.y)
|
2018-01-17 16:13:36 +00:00
|
|
|
fg, _, _ := style.Decompose()
|
2023-08-22 20:16:59 +00:00
|
|
|
Print(screen, string(SemigraphicsHorizontalEllipsis), xEllipsis, b.y, 1, AlignLeft, fg)
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-22 11:33:11 +00: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)
|
2018-03-23 20:08:52 +00:00
|
|
|
} else {
|
|
|
|
// Remember the inner rect.
|
|
|
|
b.innerX = -1
|
|
|
|
b.innerX, b.innerY, b.innerWidth, b.innerHeight = b.GetInnerRect()
|
|
|
|
}
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
|
|
|
|
2021-11-08 16:05:49 +00:00
|
|
|
// SetFocusFunc sets a callback function which is invoked when this primitive
|
|
|
|
// receives focus. Container primitives such as Flex or Grid may not be notified
|
|
|
|
// if one of their descendents receive focus directly.
|
|
|
|
//
|
|
|
|
// Set to nil to remove the callback function.
|
|
|
|
func (b *Box) SetFocusFunc(callback func()) *Box {
|
|
|
|
b.focus = callback
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetBlurFunc sets a callback function which is invoked when this primitive
|
|
|
|
// loses focus. This does not apply to container primitives such as Flex or
|
|
|
|
// Grid.
|
|
|
|
//
|
|
|
|
// Set to nil to remove the callback function.
|
|
|
|
func (b *Box) SetBlurFunc(callback func()) *Box {
|
|
|
|
b.blur = callback
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2017-12-15 14:29:21 +00:00
|
|
|
// Focus is called when this primitive receives focus.
|
2017-12-18 19:04:52 +00:00
|
|
|
func (b *Box) Focus(delegate func(p Primitive)) {
|
2017-12-15 14:29:21 +00:00
|
|
|
b.hasFocus = true
|
2021-11-08 16:05:49 +00:00
|
|
|
if b.focus != nil {
|
|
|
|
b.focus()
|
|
|
|
}
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Blur is called when this primitive loses focus.
|
|
|
|
func (b *Box) Blur() {
|
2021-11-08 16:05:49 +00:00
|
|
|
if b.blur != nil {
|
|
|
|
b.blur()
|
|
|
|
}
|
2017-12-15 14:29:21 +00:00
|
|
|
b.hasFocus = false
|
|
|
|
}
|
2017-12-16 21:48:26 +00:00
|
|
|
|
|
|
|
// HasFocus returns whether or not this primitive has focus.
|
|
|
|
func (b *Box) HasFocus() bool {
|
|
|
|
return b.hasFocus
|
|
|
|
}
|