material: make theme constructors stand-alone functions

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>
This commit is contained in:
Elias Naur
2020-05-03 12:29:41 +02:00
parent e460e4f4bf
commit 060cff257f
7 changed files with 82 additions and 82 deletions
+8 -8
View File
@@ -13,7 +13,7 @@ import (
"gioui.org/widget"
)
type Editor struct {
type EditorStyle struct {
Font text.Font
TextSize unit.Value
// Color is the text color.
@@ -26,17 +26,17 @@ type Editor struct {
shaper text.Shaper
}
func (t *Theme) Editor(hint string) Editor {
return Editor{
TextSize: t.TextSize,
Color: t.Color.Text,
shaper: t.Shaper,
func Editor(th *Theme, hint string) EditorStyle {
return EditorStyle{
TextSize: th.TextSize,
Color: th.Color.Text,
shaper: th.Shaper,
Hint: hint,
HintColor: t.Color.Hint,
HintColor: th.Color.Hint,
}
}
func (e Editor) Layout(gtx *layout.Context, editor *widget.Editor) {
func (e EditorStyle) Layout(gtx *layout.Context, editor *widget.Editor) {
var stack op.StackOp
stack.Push(gtx.Ops)
var macro op.MacroOp