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
|
|
|
|
2018-08-11 00:19:47 +00:00
|
|
|
// DefaultFont is the default font settings used for a Label.
|
|
|
|
var DefaultFont = render.Text{
|
|
|
|
Size: 12,
|
|
|
|
Color: render.Black,
|
|
|
|
}
|
|
|
|
|
2018-07-25 16:03:49 +00:00
|
|
|
// Label is a simple text label widget.
|
|
|
|
type Label struct {
|
|
|
|
BaseWidget
|
2018-08-05 19:54:57 +00:00
|
|
|
|
|
|
|
// Configurable fields for the constructor.
|
|
|
|
Text string
|
|
|
|
TextVariable *string
|
|
|
|
Font render.Text
|
|
|
|
|
2018-07-25 16:03:49 +00:00
|
|
|
width int32
|
|
|
|
height int32
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewLabel creates a new label.
|
2018-08-05 19:54:57 +00:00
|
|
|
func NewLabel(c Label) *Label {
|
2018-08-01 00:18:13 +00:00
|
|
|
w := &Label{
|
2018-08-05 19:54:57 +00:00
|
|
|
Text: c.Text,
|
|
|
|
TextVariable: c.TextVariable,
|
2018-08-11 00:19:47 +00:00
|
|
|
Font: DefaultFont,
|
|
|
|
}
|
|
|
|
if !c.Font.IsZero() {
|
|
|
|
w.Font = c.Font
|
2018-07-25 16:03:49 +00:00
|
|
|
}
|
2018-08-01 00:18:13 +00:00
|
|
|
w.IDFunc(func() string {
|
2018-08-11 00:19:47 +00:00
|
|
|
return fmt.Sprintf(`Label<"%s">`, w.text().Text)
|
2018-08-01 00:18:13 +00:00
|
|
|
})
|
|
|
|
return w
|
2018-07-25 16:03:49 +00:00
|
|
|
}
|
|
|
|
|
2018-08-05 19:54:57 +00:00
|
|
|
// text returns the label's displayed text, coming from the TextVariable if
|
|
|
|
// available or else the Text attribute instead.
|
|
|
|
func (w *Label) text() render.Text {
|
|
|
|
if w.TextVariable != nil {
|
|
|
|
w.Font.Text = *w.TextVariable
|
|
|
|
return w.Font
|
|
|
|
}
|
|
|
|
w.Font.Text = w.Text
|
|
|
|
return w.Font
|
|
|
|
}
|
|
|
|
|
2018-08-11 00:19:47 +00:00
|
|
|
// Value returns the current text value displayed in the widget, whether it was
|
|
|
|
// the hardcoded value or a TextVariable.
|
|
|
|
func (w *Label) Value() string {
|
|
|
|
return w.text().Text
|
|
|
|
}
|
|
|
|
|
2018-07-25 16:03:49 +00:00
|
|
|
// Compute the size of the label widget.
|
|
|
|
func (w *Label) Compute(e render.Engine) {
|
2018-08-11 00:19:47 +00:00
|
|
|
rect, err := e.ComputeTextRect(w.text())
|
|
|
|
if err != nil {
|
|
|
|
log.Error("%s: failed to compute text rect: %s", w, err)
|
|
|
|
return
|
|
|
|
}
|
2018-08-02 01:52:52 +00:00
|
|
|
|
|
|
|
if !w.FixedSize() {
|
|
|
|
w.resizeAuto(render.Rect{
|
2018-08-05 19:54:57 +00:00
|
|
|
W: rect.W + (w.Font.Padding * 2),
|
|
|
|
H: rect.H + (w.Font.Padding * 2),
|
2018-08-02 01:52:52 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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.
|
2018-08-05 19:54:57 +00:00
|
|
|
func (w *Label) Present(e render.Engine, P render.Point) {
|
|
|
|
border := w.BoxThickness(1)
|
|
|
|
|
|
|
|
w.DrawBox(e, P)
|
|
|
|
e.DrawText(w.text(), render.Point{
|
|
|
|
X: P.X + border + w.Font.Padding,
|
|
|
|
Y: P.Y + border + w.Font.Padding,
|
2018-08-01 00:18:13 +00:00
|
|
|
})
|
2018-07-25 16:03:49 +00:00
|
|
|
}
|