Files
gio-patched/widget/material/label.go
T
Elias Naur 3af01a3f43 layout: change Widget to take explicit Context and return explicit Dimensions
Change the definition of Widget from the implicit

        type Widget func()

to the explicit functional

        type Widget func(gtx layout.Context) layout.Dimensions

The advantages are numerous:

- Clearer connection between the incoming context and the output dimensions.
- Returning the Dimensions are impossible to omit.
- Contexts passed by value, so its fields can be exported
and freely mutated by the program.

The only disadvantage is the longer function literals and the many "returns".
What tipped the scales in favour of the explicit Widget variant is that type
aliases can dramatically shorten the literals:

	type (
		C = layout.Context
		D = layout.Dimensions
	)

	widget := func(gtx C) D {
		...
	}

Note that the aliases are not part of the Gio API and it is up to each user
whether they want to use them.

Finally the Go proposal for lightweight function literals,
https://github.com/golang/go/issues/21498, may remove the disadvantage
completely in future.

Context becomes a plain struct with only public fields, and its Reset is
replaced by a NewContext convenience constructor.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-05-23 22:28:49 +02:00

80 lines
1.8 KiB
Go

// SPDX-License-Identifier: Unlicense OR MIT
package material
import (
"image/color"
"gioui.org/layout"
"gioui.org/op/paint"
"gioui.org/text"
"gioui.org/unit"
"gioui.org/widget"
)
type LabelStyle struct {
// Face defines the text style.
Font text.Font
// Color is the text color.
Color color.RGBA
// Alignment specify the text alignment.
Alignment text.Alignment
// MaxLines limits the number of lines. Zero means no limit.
MaxLines int
Text string
TextSize unit.Value
shaper text.Shaper
}
func H1(th *Theme, txt string) LabelStyle {
return Label(th, th.TextSize.Scale(96.0/16.0), txt)
}
func H2(th *Theme, txt string) LabelStyle {
return Label(th, th.TextSize.Scale(60.0/16.0), txt)
}
func H3(th *Theme, txt string) LabelStyle {
return Label(th, th.TextSize.Scale(48.0/16.0), txt)
}
func H4(th *Theme, txt string) LabelStyle {
return Label(th, th.TextSize.Scale(34.0/16.0), txt)
}
func H5(th *Theme, txt string) LabelStyle {
return Label(th, th.TextSize.Scale(24.0/16.0), txt)
}
func H6(th *Theme, txt string) LabelStyle {
return Label(th, th.TextSize.Scale(20.0/16.0), txt)
}
func Body1(th *Theme, txt string) LabelStyle {
return Label(th, th.TextSize, txt)
}
func Body2(th *Theme, txt string) LabelStyle {
return Label(th, th.TextSize.Scale(14.0/16.0), txt)
}
func Caption(th *Theme, txt string) LabelStyle {
return Label(th, th.TextSize.Scale(12.0/16.0), txt)
}
func Label(th *Theme, size unit.Value, txt string) LabelStyle {
return LabelStyle{
Text: txt,
Color: th.Color.Text,
TextSize: size,
shaper: th.Shaper,
}
}
func (l LabelStyle) Layout(gtx layout.Context) layout.Dimensions {
paint.ColorOp{Color: l.Color}.Add(gtx.Ops)
tl := widget.Label{Alignment: l.Alignment, MaxLines: l.MaxLines}
return tl.Layout(gtx, l.shaper, l.Font, l.TextSize, l.Text)
}