gui/views/view_text.go
2023-01-22 12:38:03 +01:00

31 lines
628 B
Go

package views
import (
"git.milar.in/milarin/gui"
)
// TextView is a gui.View which prints text
type TextView struct {
gui.ViewTmpl
Text string
}
var _ gui.View = &TextView{}
func NewTextView(text string) *TextView {
return &TextView{
Text: text,
}
}
func (v *TextView) Layout(ctx gui.AppContext) (prefWidth, prefHeight int) {
s := ctx.MeasureString(v.Text, v.FontFace(ctx)).Size()
return s.X, s.Y
}
func (v *TextView) Draw(img *gui.Image, ctx gui.AppContext) {
v.ViewTmpl.Draw(img, ctx)
s := ctx.MeasureString(v.Text, v.FontFace(ctx))
img.DrawString(v.Text, v.FontFace(ctx), 0, -s.Min.Y, v.Foreground(ctx))
}