editor.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. type EditorStyle struct {
  15. Font font.Font
  16. // LineHeight controls the distance between the baselines of lines of text.
  17. // If zero, a sensible default will be used.
  18. LineHeight unit.Sp
  19. // LineHeightScale applies a scaling factor to the LineHeight. If zero, a
  20. // sensible default will be used.
  21. LineHeightScale float32
  22. TextSize unit.Sp
  23. // Color is the text color.
  24. Color color.NRGBA
  25. // Hint contains the text displayed when the editor is empty.
  26. Hint string
  27. // HintColor is the color of hint text.
  28. HintColor color.NRGBA
  29. // SelectionColor is the color of the background for selected text.
  30. SelectionColor color.NRGBA
  31. Editor *widget.Editor
  32. shaper *text.Shaper
  33. }
  34. func Editor(th *Theme, editor *widget.Editor, hint string) EditorStyle {
  35. return EditorStyle{
  36. Editor: editor,
  37. Font: font.Font{
  38. Typeface: th.Face,
  39. },
  40. TextSize: th.TextSize,
  41. Color: th.Palette.Fg,
  42. shaper: th.Shaper,
  43. Hint: hint,
  44. HintColor: f32color.MulAlpha(th.Palette.Fg, 0xbb),
  45. SelectionColor: f32color.MulAlpha(th.Palette.ContrastBg, 0x60),
  46. }
  47. }
  48. func (e EditorStyle) Layout(gtx layout.Context) layout.Dimensions {
  49. // Choose colors.
  50. textColorMacro := op.Record(gtx.Ops)
  51. paint.ColorOp{Color: e.Color}.Add(gtx.Ops)
  52. textColor := textColorMacro.Stop()
  53. hintColorMacro := op.Record(gtx.Ops)
  54. paint.ColorOp{Color: e.HintColor}.Add(gtx.Ops)
  55. hintColor := hintColorMacro.Stop()
  56. selectionColorMacro := op.Record(gtx.Ops)
  57. paint.ColorOp{Color: blendDisabledColor(!gtx.Enabled(), e.SelectionColor)}.Add(gtx.Ops)
  58. selectionColor := selectionColorMacro.Stop()
  59. var maxlines int
  60. if e.Editor.SingleLine {
  61. maxlines = 1
  62. }
  63. macro := op.Record(gtx.Ops)
  64. tl := widget.Label{
  65. Alignment: e.Editor.Alignment,
  66. MaxLines: maxlines,
  67. LineHeight: e.LineHeight,
  68. LineHeightScale: e.LineHeightScale,
  69. }
  70. dims := tl.Layout(gtx, e.shaper, e.Font, e.TextSize, e.Hint, hintColor)
  71. call := macro.Stop()
  72. if w := dims.Size.X; gtx.Constraints.Min.X < w {
  73. gtx.Constraints.Min.X = w
  74. }
  75. if h := dims.Size.Y; gtx.Constraints.Min.Y < h {
  76. gtx.Constraints.Min.Y = h
  77. }
  78. e.Editor.LineHeight = e.LineHeight
  79. e.Editor.LineHeightScale = e.LineHeightScale
  80. dims = e.Editor.Layout(gtx, e.shaper, e.Font, e.TextSize, textColor, selectionColor)
  81. if e.Editor.Len() == 0 {
  82. call.Add(gtx.Ops)
  83. }
  84. return dims
  85. }
  86. func blendDisabledColor(disabled bool, c color.NRGBA) color.NRGBA {
  87. if disabled {
  88. return f32color.Disabled(c)
  89. }
  90. return c
  91. }