github.com/utopiagio/gio@v0.0.8/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/utopiagio/gio/font" 9 "github.com/utopiagio/gio/internal/f32color" 10 "github.com/utopiagio/gio/layout" 11 "github.com/utopiagio/gio/op" 12 "github.com/utopiagio/gio/op/paint" 13 "github.com/utopiagio/gio/text" 14 "github.com/utopiagio/gio/unit" 15 "github.com/utopiagio/gio/widget" 16 ) 17 18 // LabelStyle configures the presentation of text. If the State field is set, the 19 // label will be laid out as interactive (able to be selected and copied). Otherwise, 20 // the label will be non-interactive. 21 type LabelStyle struct { 22 // Face defines the text style. 23 Font font.Font 24 // Color is the text color. 25 Color color.NRGBA 26 // SelectionColor is the color of the background for selected text. 27 SelectionColor color.NRGBA 28 // Alignment specify the text alignment. 29 Alignment text.Alignment 30 // MaxLines limits the number of lines. Zero means no limit. 31 MaxLines int 32 // WrapPolicy configures how displayed text will be broken into lines. 33 WrapPolicy text.WrapPolicy 34 // Truncator is the text that will be shown at the end of the final 35 // line if MaxLines is exceeded. Defaults to "…" if empty. 36 Truncator string 37 // Text is the content displayed by the label. 38 Text string 39 // TextSize determines the size of the text glyphs. 40 TextSize unit.Sp 41 // LineHeight controls the distance between the baselines of lines of text. 42 // If zero, a sensible default will be used. 43 LineHeight unit.Sp 44 // LineHeightScale applies a scaling factor to the LineHeight. If zero, a 45 // sensible default will be used. 46 LineHeightScale float32 47 // Shaper is the text shaper used to display this labe. This field is automatically 48 // set using by all constructor functions. If constructing a LabelStyle literal, you 49 // must provide a Shaper or displaying text will panic. 50 Shaper *text.Shaper 51 // State provides text selection state for the label. If not set, the label cannot 52 // be selected or copied interactively. 53 State *widget.Selectable 54 } 55 56 func H1(th *Theme, txt string) LabelStyle { 57 label := Label(th, th.TextSize*96.0/16.0, txt) 58 label.Font.Weight = font.Light 59 return label 60 } 61 62 func H2(th *Theme, txt string) LabelStyle { 63 label := Label(th, th.TextSize*60.0/16.0, txt) 64 label.Font.Weight = font.Light 65 return label 66 } 67 68 func H3(th *Theme, txt string) LabelStyle { 69 return Label(th, th.TextSize*48.0/16.0, txt) 70 } 71 72 func H4(th *Theme, txt string) LabelStyle { 73 return Label(th, th.TextSize*34.0/16.0, txt) 74 } 75 76 func H5(th *Theme, txt string) LabelStyle { 77 return Label(th, th.TextSize*24.0/16.0, txt) 78 } 79 80 func H6(th *Theme, txt string) LabelStyle { 81 label := Label(th, th.TextSize*20.0/16.0, txt) 82 label.Font.Weight = font.Medium 83 return label 84 } 85 86 func Subtitle1(th *Theme, txt string) LabelStyle { 87 return Label(th, th.TextSize*16.0/16.0, txt) 88 } 89 90 func Subtitle2(th *Theme, txt string) LabelStyle { 91 label := Label(th, th.TextSize*14.0/16.0, txt) 92 label.Font.Weight = font.Medium 93 return label 94 } 95 96 func Body1(th *Theme, txt string) LabelStyle { 97 return Label(th, th.TextSize, txt) 98 } 99 100 func Body2(th *Theme, txt string) LabelStyle { 101 return Label(th, th.TextSize*14.0/16.0, txt) 102 } 103 104 func Caption(th *Theme, txt string) LabelStyle { 105 return Label(th, th.TextSize*12.0/16.0, txt) 106 } 107 108 func Overline(th *Theme, txt string) LabelStyle { 109 return Label(th, th.TextSize*10.0/16.0, txt) 110 } 111 112 func Label(th *Theme, size unit.Sp, txt string) LabelStyle { 113 l := LabelStyle{ 114 Text: txt, 115 Color: th.Palette.Fg, 116 SelectionColor: f32color.MulAlpha(th.Palette.ContrastBg, 0x60), 117 TextSize: size, 118 Shaper: th.Shaper, 119 } 120 l.Font.Typeface = th.Face 121 return l 122 } 123 124 func (l LabelStyle) Layout(gtx layout.Context) layout.Dimensions { 125 textColorMacro := op.Record(gtx.Ops) 126 paint.ColorOp{Color: l.Color}.Add(gtx.Ops) 127 textColor := textColorMacro.Stop() 128 selectColorMacro := op.Record(gtx.Ops) 129 paint.ColorOp{Color: l.SelectionColor}.Add(gtx.Ops) 130 selectColor := selectColorMacro.Stop() 131 132 if l.State != nil { 133 if l.State.Text() != l.Text { 134 l.State.SetText(l.Text) 135 } 136 l.State.Alignment = l.Alignment 137 l.State.MaxLines = l.MaxLines 138 l.State.Truncator = l.Truncator 139 l.State.WrapPolicy = l.WrapPolicy 140 l.State.LineHeight = l.LineHeight 141 l.State.LineHeightScale = l.LineHeightScale 142 return l.State.Layout(gtx, l.Shaper, l.Font, l.TextSize, textColor, selectColor) 143 } 144 tl := widget.Label{ 145 Alignment: l.Alignment, 146 MaxLines: l.MaxLines, 147 Truncator: l.Truncator, 148 WrapPolicy: l.WrapPolicy, 149 LineHeight: l.LineHeight, 150 LineHeightScale: l.LineHeightScale, 151 } 152 return tl.Layout(gtx, l.Shaper, l.Font, l.TextSize, l.Text, textColor) 153 }