zntg/ui/label.go

49 lines
1.2 KiB
Go

package ui
import (
"opslag.de/schobers/geom"
)
type Label struct {
ControlBase
Text string
size desiredSizeCache
}
func BuildLabel(text string, fn func(*Label)) *Label {
var l = &Label{Text: text}
if fn != nil {
fn(l)
}
return l
}
func (l *Label) DesiredSize(ctx Context) geom.PointF32 {
var fontName = l.FontName(ctx)
return l.size.Update(ctx, fontName+l.Text, func(ctx Context) geom.PointF32 {
var font = ctx.Renderer().Font(fontName)
var width = font.WidthOf(l.Text)
var height = font.Height()
var pad = ctx.Style().Dimensions.TextPadding
return geom.PtF32(width+pad*2, height+pad*2)
})
}
func (l *Label) Render(ctx Context) {
l.RenderBackground(ctx)
var c = l.FontColor(ctx)
var f = l.FontName(ctx)
var pad = ctx.Style().Dimensions.TextPadding
var bounds = l.bounds.Inset(pad)
switch l.TextAlignment {
case AlignLeft:
ctx.Renderer().TextAlign(bounds.Min, f, c, l.Text, l.TextAlignment)
case AlignRight:
ctx.Renderer().TextAlign(geom.PtF32(bounds.Max.X, bounds.Min.Y), f, c, l.Text, l.TextAlignment)
case AlignCenter:
ctx.Renderer().TextAlign(geom.PtF32(.5*(bounds.Min.X+bounds.Max.X), bounds.Min.Y), f, c, l.Text, l.TextAlignment)
}
}