Files
gio-patched/widget/material/editor.go
T
Elias Naur 9c0fc631bd widget/material: use theme TextSize for Editors
Thanks to Werner Laurensse for noticing.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2019-11-07 18:45:53 +01:00

65 lines
1.4 KiB
Go

// SPDX-License-Identifier: Unlicense OR MIT
package material
import (
"image/color"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/op/paint"
"gioui.org/text"
"gioui.org/widget"
)
type Editor struct {
Font text.Font
// Color is the text color.
Color color.RGBA
// Hint contains the text displayed when the editor is empty.
Hint string
// HintColor is the color of hint text.
HintColor color.RGBA
shaper *text.Shaper
}
func (t *Theme) Editor(hint string) Editor {
return Editor{
Font: text.Font{
Size: t.TextSize,
},
Color: t.Color.Text,
shaper: t.Shaper,
Hint: hint,
HintColor: t.Color.Hint,
}
}
func (e Editor) Layout(gtx *layout.Context, editor *widget.Editor) {
var stack op.StackOp
stack.Push(gtx.Ops)
var macro op.MacroOp
macro.Record(gtx.Ops)
paint.ColorOp{Color: e.HintColor}.Add(gtx.Ops)
tl := widget.Label{Alignment: editor.Alignment}
tl.Layout(gtx, e.shaper, e.Font, e.Hint)
macro.Stop()
if w := gtx.Dimensions.Size.X; gtx.Constraints.Width.Min < w {
gtx.Constraints.Width.Min = w
}
if h := gtx.Dimensions.Size.Y; gtx.Constraints.Height.Min < h {
gtx.Constraints.Height.Min = h
}
editor.Layout(gtx, e.shaper, e.Font)
if editor.Len() > 0 {
paint.ColorOp{Color: e.Color}.Add(gtx.Ops)
editor.PaintText(gtx)
} else {
macro.Add(gtx.Ops)
}
paint.ColorOp{Color: e.Color}.Add(gtx.Ops)
editor.PaintCaret(gtx)
stack.Pop()
}