mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-04 08:55:35 +00:00
theme/material: add the material theme
Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
// 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 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: unit.Sp(16),
|
||||
},
|
||||
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()
|
||||
}
|
||||
Reference in New Issue
Block a user