Files
gio/widget/material/editor.go
T
Elias Naur 7bf3265ccd layout,widget: transpose Constraints to use image.Points for limits
Instead of

    type Contraints struct {
	    Width, Height Constraint
    }

use

    type Constraints struct {
	    Min, Max image.Point
    }

which leads to simpler use. For example, the Min method is trivally replaced by
the field, and the RigidConstraints constructor is no longer a net win.

API Change. Rewrites:

    gofmt -r 'gtx.Constraints.Min() -> gtx.Constraints.Min'
    gofmt -r 'gtx.Constraints.Width.Min -> gtx.Constraints.Min.X'
    gofmt -r 'gtx.Constraints.Height.Min -> gtx.Constraints.Min.Y'
    gofmt -r 'gtx.Constraints.Height.Max -> gtx.Constraints.Max.Y'
    gofmt -r 'gtx.Constraints.Width.Max -> gtx.Constraints.Max.X'

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-05-19 09:58:07 +02: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/unit"
"gioui.org/widget"
)
type EditorStyle struct {
Font text.Font
TextSize unit.Value
// 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 Editor(th *Theme, hint string) EditorStyle {
return EditorStyle{
TextSize: th.TextSize,
Color: th.Color.Text,
shaper: th.Shaper,
Hint: hint,
HintColor: th.Color.Hint,
}
}
func (e EditorStyle) 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.TextSize, e.Hint)
macro.Stop()
if w := gtx.Dimensions.Size.X; gtx.Constraints.Min.X < w {
gtx.Constraints.Min.X = w
}
if h := gtx.Dimensions.Size.Y; gtx.Constraints.Min.Y < h {
gtx.Constraints.Min.Y = h
}
editor.Layout(gtx, e.shaper, e.Font, e.TextSize)
if editor.Len() > 0 {
paint.ColorOp{Color: e.Color}.Add(gtx.Ops)
editor.PaintText(gtx)
} else {
macro.Add()
}
paint.ColorOp{Color: e.Color}.Add(gtx.Ops)
editor.PaintCaret(gtx)
stack.Pop()
}