e768fe347a732056031100f2c66987d6db258ea4 — Chris Waldon 8 months ago e0ceb9f
widget/material: export LabelStyle.Shaper and document fields

We panic when someone constructs a literal LabelStyle because they cannot possibly
populate the shaper field. The resulting error is cryptic, and unusual within Gio
because most style types are safe to construct literally. This commit enables
creating literal LabelStyles by exporting the Shaper field, and also documents
the purposes of all of the fields.

Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
1 files changed, 15 insertions(+), 8 deletions(-)

M widget/material/label.go
M widget/material/label.go => widget/material/label.go +15 -8
@@ 31,11 31,18 @@ type LabelStyle struct {
	// Truncator is the text that will be shown at the end of the final
	// line if MaxLines is exceeded. Defaults to "…" if empty.
	Truncator string
	Text      string
	TextSize  unit.Sp

	shaper *text.Shaper
	State  *widget.Selectable
	// Text is the content displayed by the label.
	Text string
	// TextSize determines the size of the text glyphs.
	TextSize unit.Sp

	// Shaper is the text shaper used to display this labe. This field is automatically
	// set using by all constructor functions. If constructing a LabelStyle literal, you
	// must provide a Shaper or displaying text will panic.
	Shaper *text.Shaper
	// State provides text selection state for the label. If not set, the label cannot
	// be selected or copied interactively.
	State *widget.Selectable

func H1(th *Theme, txt string) LabelStyle {

@@ 100,7 107,7 @@ func Label(th *Theme, size unit.Sp, txt string) LabelStyle {
		Color:          th.Palette.Fg,
		SelectionColor: f32color.MulAlpha(th.Palette.ContrastBg, 0x60),
		TextSize:       size,
		shaper:         th.Shaper,
		Shaper:         th.Shaper,

@@ 119,12 126,12 @@ func (l LabelStyle) Layout(gtx layout.Context) layout.Dimensions {
		l.State.Alignment = l.Alignment
		l.State.MaxLines = l.MaxLines
		l.State.Truncator = l.Truncator
		return l.State.Layout(gtx, l.shaper, l.Font, l.TextSize, textColor, selectColor)
		return l.State.Layout(gtx, l.Shaper, l.Font, l.TextSize, textColor, selectColor)
	tl := widget.Label{
		Alignment: l.Alignment,
		MaxLines:  l.MaxLines,
		Truncator: l.Truncator,
	return tl.Layout(gtx, l.shaper, l.Font, l.TextSize, l.Text, textColor)
	return tl.Layout(gtx, l.Shaper, l.Font, l.TextSize, l.Text, textColor)