2018-02-18 02:11:58 +01:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
2018-06-01 09:58:00 +02:00
|
|
|
"github.com/gdamore/tcell"
|
2018-02-18 02:11:58 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
BORDER_LEFT = 1 << iota
|
|
|
|
BORDER_TOP = 1 << iota
|
|
|
|
BORDER_RIGHT = 1 << iota
|
|
|
|
BORDER_BOTTOM = 1 << iota
|
|
|
|
)
|
|
|
|
|
|
|
|
type Bordered struct {
|
|
|
|
borders uint
|
|
|
|
content Drawable
|
|
|
|
onInvalidate func(d Drawable)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBordered(content Drawable, borders uint) *Bordered {
|
|
|
|
b := &Bordered{
|
|
|
|
borders: borders,
|
|
|
|
content: content,
|
|
|
|
}
|
|
|
|
content.OnInvalidate(b.contentInvalidated)
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bordered *Bordered) contentInvalidated(d Drawable) {
|
|
|
|
bordered.Invalidate()
|
|
|
|
}
|
|
|
|
|
2019-01-20 21:06:44 +01:00
|
|
|
func (bordered *Bordered) Children() []Drawable {
|
|
|
|
return []Drawable{bordered.content}
|
|
|
|
}
|
|
|
|
|
2018-02-18 02:11:58 +01:00
|
|
|
func (bordered *Bordered) Invalidate() {
|
|
|
|
if bordered.onInvalidate != nil {
|
|
|
|
bordered.onInvalidate(bordered)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bordered *Bordered) OnInvalidate(onInvalidate func(d Drawable)) {
|
|
|
|
bordered.onInvalidate = onInvalidate
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bordered *Bordered) Draw(ctx *Context) {
|
|
|
|
x := 0
|
|
|
|
y := 0
|
|
|
|
width := ctx.Width()
|
|
|
|
height := ctx.Height()
|
2019-03-30 17:59:18 +01:00
|
|
|
style := tcell.StyleDefault.Reverse(true)
|
2018-02-18 02:11:58 +01:00
|
|
|
if bordered.borders&BORDER_LEFT != 0 {
|
2018-06-01 09:58:00 +02:00
|
|
|
ctx.Fill(0, 0, 1, ctx.Height(), ' ', style)
|
2018-02-18 02:11:58 +01:00
|
|
|
x += 1
|
|
|
|
width -= 1
|
|
|
|
}
|
|
|
|
if bordered.borders&BORDER_TOP != 0 {
|
2018-06-01 09:58:00 +02:00
|
|
|
ctx.Fill(0, 0, ctx.Width(), 1, ' ', style)
|
2018-02-18 02:11:58 +01:00
|
|
|
y += 1
|
|
|
|
height -= 1
|
|
|
|
}
|
|
|
|
if bordered.borders&BORDER_RIGHT != 0 {
|
2018-06-01 09:58:00 +02:00
|
|
|
ctx.Fill(ctx.Width()-1, 0, 1, ctx.Height(), ' ', style)
|
2018-02-18 02:11:58 +01:00
|
|
|
width -= 1
|
|
|
|
}
|
|
|
|
if bordered.borders&BORDER_BOTTOM != 0 {
|
2018-06-01 09:58:00 +02:00
|
|
|
ctx.Fill(0, ctx.Height()-1, ctx.Width(), 1, ' ', style)
|
2018-02-18 02:11:58 +01:00
|
|
|
height -= 1
|
|
|
|
}
|
|
|
|
subctx := ctx.Subcontext(x, y, width, height)
|
|
|
|
bordered.content.Draw(subctx)
|
|
|
|
}
|