Sander Schobers
75fce53716
Extended DesiredSize method with extra arguments that tells the control how much space is available for the parent control (note: this might not be the actual given size when Arrange is called on the control).
55 lines
1.3 KiB
Go
55 lines
1.3 KiB
Go
package ui
|
|
|
|
import (
|
|
"opslag.de/schobers/geom"
|
|
)
|
|
|
|
type Label struct {
|
|
ControlBase
|
|
|
|
Text string
|
|
|
|
desired CachedValue
|
|
}
|
|
|
|
func BuildLabel(text string, fn func(*Label)) *Label {
|
|
var l = &Label{Text: text}
|
|
if fn != nil {
|
|
fn(l)
|
|
}
|
|
return l
|
|
}
|
|
|
|
func (l *Label) hashDesiredSize(ctx Context) string {
|
|
return l.FontName(ctx) + l.Text
|
|
}
|
|
|
|
func (l *Label) desiredSize(ctx Context) interface{} {
|
|
fontName := l.FontName(ctx)
|
|
font := ctx.Fonts().Font(fontName)
|
|
width := font.WidthOf(l.Text)
|
|
height := font.Height()
|
|
pad := ctx.Style().Dimensions.TextPadding
|
|
return geom.PtF32(width+pad*2, height+pad*2)
|
|
}
|
|
|
|
func (l *Label) DesiredSize(ctx Context, _ geom.PointF32) geom.PointF32 {
|
|
return l.desired.GetContext(ctx, l.desiredSize, l.hashDesiredSize).(geom.PointF32)
|
|
}
|
|
|
|
func (l *Label) Render(ctx Context) {
|
|
l.RenderBackground(ctx)
|
|
fontColor := l.TextColor(ctx)
|
|
fontName := l.FontName(ctx)
|
|
pad := ctx.Style().Dimensions.TextPadding
|
|
bounds := l.bounds.Inset(pad)
|
|
switch l.TextAlignment {
|
|
case AlignLeft:
|
|
ctx.Fonts().TextAlign(fontName, bounds.Min, fontColor, l.Text, l.TextAlignment)
|
|
case AlignRight:
|
|
ctx.Fonts().TextAlign(fontName, geom.PtF32(bounds.Max.X, bounds.Min.Y), fontColor, l.Text, l.TextAlignment)
|
|
case AlignCenter:
|
|
ctx.Fonts().TextAlign(fontName, geom.PtF32(.5*(bounds.Min.X+bounds.Max.X), bounds.Min.Y), fontColor, l.Text, l.TextAlignment)
|
|
}
|
|
}
|