Browse Source

Added a Draw callback to Box which exposes tcell.Screen. Resolves #57

tablepad
Oliver 5 years ago
parent
commit
8c2cd21162
  1. 2
      README.md
  2. 28
      box.go

2
README.md

@ -64,6 +64,8 @@ Add your issue here on GitHub. Feel free to get in touch if you have any questio
(There are no corresponding tags in the project. I only keep such a history in this README.)
- v0.10 (2018-02-22)
- Direct access to the `screen` object through callback in `Box` (i.e. for all primitives).
- v0.9 (2018-02-20)
- Introduced `Grid` layout.
- Direct access to the `screen` object through callbacks in `Application`.

28
box.go

@ -16,6 +16,10 @@ type Box struct {
// The position of the rect.
x, y, width, height int
// 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
// Border padding.
paddingTop, paddingBottom, paddingLeft, paddingRight int
@ -49,6 +53,9 @@ type Box struct {
// event to be forwarded to the primitive's default input handler (nil if
// nothing should be forwarded).
inputCapture func(event *tcell.EventKey) *tcell.EventKey
// 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)
}
// NewBox returns a Box without a border.
@ -80,6 +87,9 @@ func (b *Box) GetRect() (int, int, int, int) {
// GetInnerRect returns the position of the inner rectangle (x, y, width,
// height), without the border and without any padding.
func (b *Box) GetInnerRect() (int, int, int, int) {
if b.draw != nil {
return b.innerX, b.innerY, b.innerWidth, b.innerHeight
}
x, y, width, height := b.GetRect()
if b.border {
x++
@ -101,6 +111,19 @@ func (b *Box) SetRect(x, y, width, height int) {
b.height = height
}
// 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
}
// 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.
@ -229,6 +252,11 @@ func (b *Box) Draw(screen tcell.Screen) {
}
}
}
// 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)
}
}
// Focus is called when this primitive receives focus.

Loading…
Cancel
Save