widget/material: move widget state object from Layout methods to constructors

Instead of, say,

	var th *material.Theme
	var btn *widget.Clickable

	material.Button(th, "Click me").Layout(gtx, btn)

move the widget state objects to the constructor:

	material.Button(th, btn, "Click me").Layout(gtx)

The advatage is that several widgets can now be used without
wrapping them in function literals. For example,

	layout.Inset{}.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
		material.Button(th, "Click me").Layout(gtx, btn)
	})

collapses to just

	layout.Inset{}.Layout(gtx, material.Button(th, btn, "Click me").Layout)

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2020-05-23 20:09:08 +02:00
parent 3af01a3f43
commit 2451750782
6 changed files with 62 additions and 44 deletions
+7 -4
View File
@@ -14,16 +14,18 @@ import (
)
type ProgressBarStyle struct {
Color color.RGBA
Color color.RGBA
Progress int
}
func ProgressBar(th *Theme) ProgressBarStyle {
func ProgressBar(th *Theme, progress int) ProgressBarStyle {
return ProgressBarStyle{
Color: th.Color.Primary,
Progress: progress,
Color: th.Color.Primary,
}
}
func (p ProgressBarStyle) Layout(gtx layout.Context, progress int) layout.Dimensions {
func (p ProgressBarStyle) Layout(gtx layout.Context) layout.Dimensions {
shader := func(width float32, color color.RGBA) layout.Dimensions {
maxHeight := unit.Dp(4)
rr := float32(gtx.Px(unit.Dp(2)))
@@ -44,6 +46,7 @@ func (p ProgressBarStyle) Layout(gtx layout.Context, progress int) layout.Dimens
return layout.Dimensions{Size: d}
}
progress := p.Progress
if progress > 100 {
progress = 100
} else if progress < 0 {