label.go raw

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