doodle/ui/label.go

60 lines
978 B
Go
Raw Normal View History

package ui
2018-08-01 00:18:13 +00:00
import (
"fmt"
"git.kirsle.net/apps/doodle/render"
)
// Label is a simple text label widget.
type Label struct {
BaseWidget
width int32
height int32
Text render.Text
}
// NewLabel creates a new label.
func NewLabel(t render.Text) *Label {
2018-08-01 00:18:13 +00:00
w := &Label{
Text: t,
}
2018-08-01 00:18:13 +00:00
w.Configure(Config{
Padding: 4,
})
w.IDFunc(func() string {
return fmt.Sprintf("Label<%s>", w.Text.Text)
})
return w
}
// Compute the size of the label widget.
func (w *Label) Compute(e render.Engine) {
2018-08-01 00:18:13 +00:00
rect, _ := e.ComputeTextRect(w.Text)
if !w.FixedSize() {
w.resizeAuto(render.Rect{
W: rect.W + w.Padding(),
H: rect.H + w.Padding(),
})
}
2018-08-01 00:18:13 +00:00
w.MoveTo(render.Point{
X: rect.X + w.BoxThickness(1),
Y: rect.Y + w.BoxThickness(1),
})
}
// Present the label widget.
func (w *Label) Present(e render.Engine) {
2018-08-01 00:18:13 +00:00
var (
P = w.Point()
border = w.BoxThickness(1)
)
w.DrawBox(e)
e.DrawText(w.Text, render.Point{
X: P.X + border,
Y: P.Y + border,
})
}