forked from joejulian/gio
060cff257f
The multitude of widget methods on Theme is unnecessary coupling in that all possible widgets either have to be included in package material, or be different than 3rd party widgets: var th *Theme // Core widget, calling a method on Theme. th.Button(...).Layout(...) // 3rd party widget, calling a function taking a Theme. datepicker.New(th, ...).Layout(...) Another reason for the Theme methods was to enable a poor man's theme replacement, so that you could use the same code for compatible themes. For example, mat.Button(...).Layout(...) would not need to change if the type of mat changed, as long as the new type had a compatible method Button. However, that point misses the fact that the mat variable had to be declared somewhere, naming the theme package: var mat *material.Theme (or, say, *cocoa.Theme) A better and complete way to replace a theme is to use import renaming. For example, to replace the material theme with a hypothetical Windows theme, replace import theme "gioui.org/widget/material" with import theme "github.com/somebody/windows This change moves all Theme widget methods to be standalone functions, and renames the widget style types accordingly. For example, instead of the method func (t *Theme) Button(...) Button there is now a function func Button(t *Theme, ...) ButtonStyle Signed-off-by: Elias Naur <mail@eliasnaur.com>
65 lines
1.4 KiB
Go
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.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, 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()
|
|
}
|