label.go 4.4 KB

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