text: make text size implicit

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2019-10-05 11:22:26 +02:00
parent f230036cad
commit b4a52d3010
4 changed files with 65 additions and 55 deletions
+14 -3
View File
@@ -12,14 +12,17 @@ import (
"gioui.org/layout"
"gioui.org/op"
"gioui.org/op/paint"
"gioui.org/unit"
"golang.org/x/image/math/fixed"
)
// Label is a widget for laying out and drawing text.
type Label struct {
// Face define the font and style of the text.
// Face defines the font and style of the text.
Face Face
// Size is the text size. If zero, a default size is used.
Size unit.Value
// Material is a macro recording the material to draw the
// text. Use a ColorOp for colored text.
Material op.MacroOp
@@ -93,7 +96,8 @@ func (l *lineIterator) Next() (String, f32.Point, bool) {
func (l Label) Layout(gtx *layout.Context) {
cs := gtx.Constraints
textLayout := l.Face.Layout(l.Text, LayoutOptions{MaxWidth: cs.Width.Max})
tsize := textSize(gtx, l.Size)
textLayout := l.Face.Layout(tsize, l.Text, LayoutOptions{MaxWidth: cs.Width.Max})
lines := textLayout.Lines
if max := l.MaxLines; max > 0 && len(lines) > max {
lines = lines[:max]
@@ -120,7 +124,7 @@ func (l Label) Layout(gtx *layout.Context) {
var stack op.StackOp
stack.Push(gtx.Ops)
op.TransformOp{}.Offset(off).Add(gtx.Ops)
l.Face.Path(str).Add(gtx.Ops)
l.Face.Path(tsize, str).Add(gtx.Ops)
// Set a default color in case the material is empty.
paint.ColorOp{Color: color.RGBA{A: 0xff}}.Add(gtx.Ops)
l.Material.Add(gtx.Ops)
@@ -150,3 +154,10 @@ func textPadding(lines []Line) (padTop int, padBottom int) {
}
return
}
func textSize(c unit.Converter, s unit.Value) float32 {
if s.V == 0 {
s = unit.Sp(12)
}
return float32(c.Px(s))
}