2018-07-25 16:03:49 +00:00
|
|
|
package ui
|
|
|
|
|
2018-08-01 00:18:13 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"git.kirsle.net/apps/doodle/render"
|
|
|
|
)
|
2018-07-25 16:03:49 +00:00
|
|
|
|
|
|
|
// 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{
|
2018-07-25 16:03:49 +00:00
|
|
|
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
|
2018-07-25 16:03:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
2018-08-02 01:52:52 +00:00
|
|
|
|
|
|
|
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),
|
|
|
|
})
|
2018-07-25 16:03:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
})
|
2018-07-25 16:03:49 +00:00
|
|
|
}
|