github.com/gop9/olt@v0.0.0-20200202132135-d956aad50b08/gio/widget/material/editor.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" 10 "github.com/gop9/olt/gio/op/paint" 11 "github.com/gop9/olt/gio/text" 12 "github.com/gop9/olt/gio/widget" 13 ) 14 15 type Editor struct { 16 Font text.Font 17 // Color is the text color. 18 Color color.RGBA 19 // Hint contains the text displayed when the editor is empty. 20 Hint string 21 // HintColor is the color of hint text. 22 HintColor color.RGBA 23 24 shaper *text.Shaper 25 } 26 27 func (t *Theme) Editor(hint string) Editor { 28 return Editor{ 29 Font: text.Font{ 30 Size: t.TextSize, 31 }, 32 Color: t.Color.Text, 33 shaper: t.Shaper, 34 Hint: hint, 35 HintColor: t.Color.Hint, 36 } 37 } 38 39 func (e Editor) Layout(gtx *layout.Context, editor *widget.Editor) { 40 var stack op.StackOp 41 stack.Push(gtx.Ops) 42 var macro op.MacroOp 43 macro.Record(gtx.Ops) 44 paint.ColorOp{Color: e.HintColor}.Add(gtx.Ops) 45 tl := widget.Label{Alignment: editor.Alignment} 46 tl.Layout(gtx, e.shaper, e.Font, e.Hint) 47 macro.Stop() 48 if w := gtx.Dimensions.Size.X; gtx.Constraints.Width.Min < w { 49 gtx.Constraints.Width.Min = w 50 } 51 if h := gtx.Dimensions.Size.Y; gtx.Constraints.Height.Min < h { 52 gtx.Constraints.Height.Min = h 53 } 54 editor.Layout(gtx, e.shaper, e.Font) 55 if editor.Len() > 0 { 56 paint.ColorOp{Color: e.Color}.Add(gtx.Ops) 57 editor.PaintText(gtx) 58 } else { 59 macro.Add() 60 } 61 paint.ColorOp{Color: e.Color}.Add(gtx.Ops) 62 editor.PaintCaret(gtx) 63 stack.Pop() 64 }