github.com/gop9/olt@v0.0.0-20200202132135-d956aad50b08/gio/widget/material/label.go (about)

     1  // SPDX-License-Identifier: Unlicense OR MIT
     2  
     3  package material
     4  
     5  import (
     6  	"image/color"
     7  
     8  	"github.com/gop9/olt/gio/layout"
     9  	"github.com/gop9/olt/gio/op/paint"
    10  	"github.com/gop9/olt/gio/text"
    11  	"github.com/gop9/olt/gio/unit"
    12  	"github.com/gop9/olt/gio/widget"
    13  )
    14  
    15  type Label struct {
    16  	// Face defines the text style.
    17  	Font text.Font
    18  	// Color is the text color.
    19  	Color color.RGBA
    20  	// Alignment specify the text alignment.
    21  	Alignment text.Alignment
    22  	// MaxLines limits the number of lines. Zero means no limit.
    23  	MaxLines int
    24  	Text     string
    25  
    26  	shaper *text.Shaper
    27  }
    28  
    29  func (t *Theme) H1(txt string) Label {
    30  	return t.Label(t.TextSize.Scale(96.0/16.0), txt)
    31  }
    32  
    33  func (t *Theme) H2(txt string) Label {
    34  	return t.Label(t.TextSize.Scale(60.0/16.0), txt)
    35  }
    36  
    37  func (t *Theme) H3(txt string) Label {
    38  	return t.Label(t.TextSize.Scale(48.0/16.0), txt)
    39  }
    40  
    41  func (t *Theme) H4(txt string) Label {
    42  	return t.Label(t.TextSize.Scale(34.0/16.0), txt)
    43  }
    44  
    45  func (t *Theme) H5(txt string) Label {
    46  	return t.Label(t.TextSize.Scale(24.0/16.0), txt)
    47  }
    48  
    49  func (t *Theme) H6(txt string) Label {
    50  	return t.Label(t.TextSize.Scale(20.0/16.0), txt)
    51  }
    52  
    53  func (t *Theme) Body1(txt string) Label {
    54  	return t.Label(t.TextSize, txt)
    55  }
    56  
    57  func (t *Theme) Body2(txt string) Label {
    58  	return t.Label(t.TextSize.Scale(14.0/16.0), txt)
    59  }
    60  
    61  func (t *Theme) Caption(txt string) Label {
    62  	return t.Label(t.TextSize.Scale(12.0/16.0), txt)
    63  }
    64  
    65  func (t *Theme) Label(size unit.Value, txt string) Label {
    66  	return Label{
    67  		Text:  txt,
    68  		Color: t.Color.Text,
    69  		Font: text.Font{
    70  			Size: size,
    71  		},
    72  		shaper: t.Shaper,
    73  	}
    74  }
    75  
    76  func (l Label) Layout(gtx *layout.Context) {
    77  	paint.ColorOp{Color: l.Color}.Add(gtx.Ops)
    78  	tl := widget.Label{Alignment: l.Alignment, MaxLines: l.MaxLines}
    79  	tl.Layout(gtx, l.shaper, l.Font, l.Text)
    80  }