~eliasnaur/gio-patches

widget/material: add ButtonLayout v4 PROPOSED

metaclips: 1
 widget/material: add ButtonLayout

 1 files changed, 41 insertions(+), 26 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~eliasnaur/gio-patches/patches/10039/mbox | git am -3
Learn more about email & git

[PATCH v4] widget/material: add ButtonLayout Export this patch

Add ButtonLayout for adding button behaviour and style to arbitrary content such
as a combined icon-and-text button.
Fixes #43

Signed-off-by: metaclips <utimichael9@gmail.com>
---
 widget/material/button.go | 67 ++++++++++++++++++++++++---------------
 1 file changed, 41 insertions(+), 26 deletions(-)

diff --git a/widget/material/button.go b/widget/material/button.go
index 8cea790..986ce54 100644
--- a/widget/material/button.go
+++ b/widget/material/button.go
@@ -18,15 +18,19 @@ import (
)

type Button struct {
	Text string
	// Color is the text color.
	Color        color.RGBA
	Font         text.Font
	TextSize     unit.Value
	Background   color.RGBA
	CornerRadius unit.Value
	Inset        layout.Inset
	shaper       text.Shaper
	ButtonLayout

	Text     string
	Font     text.Font
	TextSize unit.Value
	Inset    layout.Inset
	shaper   text.Shaper
}

type ButtonLayout struct {
	Background color.RGBA
	Color      color.RGBA
	Padding    unit.Value
}

type IconButton struct {
@@ -40,10 +44,13 @@ type IconButton struct {

func (t *Theme) Button(txt string) Button {
	return Button{
		Text:       txt,
		Color:      rgb(0xffffff),
		Background: t.Color.Primary,
		TextSize:   t.TextSize.Scale(14.0 / 16.0),
		ButtonLayout: ButtonLayout{
			Color:      rgb(0xffffff),
			Background: t.Color.Primary,
		},

		Text:     txt,
		TextSize: t.TextSize.Scale(14.0 / 16.0),
		Inset: layout.Inset{
			Top: unit.Dp(10), Bottom: unit.Dp(10),
			Left: unit.Dp(12), Right: unit.Dp(12),
@@ -52,6 +59,14 @@ func (t *Theme) Button(txt string) Button {
	}
}

func (t *Theme) ButtonLayout() ButtonLayout {
	return ButtonLayout{
		Background: t.Color.Primary,
		Color:      t.Color.InvText,
		Padding:    unit.Dp(16),
	}
}

func (t *Theme) IconButton(icon *Icon) IconButton {
	return IconButton{
		Background: t.Color.Primary,
@@ -63,10 +78,17 @@ func (t *Theme) IconButton(icon *Icon) IconButton {
}

func (b Button) Layout(gtx *layout.Context, button *widget.Button) {
	col := b.Color
	bgcol := b.Background
	hmin := gtx.Constraints.Width.Min
	vmin := gtx.Constraints.Height.Min
	b.ButtonLayout.Layout(gtx, button, func() {
		layout.Center.Layout(gtx, func() {
			b.Inset.Layout(gtx, func() {
				paint.ColorOp{Color: b.Color}.Add(gtx.Ops)
				widget.Label{}.Layout(gtx, b.shaper, b.Font, b.TextSize, b.Text)
			})
		})
	})
}

func (b ButtonLayout) Layout(gtx *layout.Context, button *widget.Button, w layout.Widget) {
	layout.Stack{Alignment: layout.Center}.Layout(gtx,
		layout.Expanded(func() {
			rr := float32(gtx.Px(unit.Dp(4)))
@@ -77,20 +99,13 @@ func (b Button) Layout(gtx *layout.Context, button *widget.Button) {
				}},
				NE: rr, NW: rr, SE: rr, SW: rr,
			}.Op(gtx.Ops).Add(gtx.Ops)
			fill(gtx, bgcol)
			fill(gtx, b.Background)
			for _, c := range button.History() {
				drawInk(gtx, c)
			}
		}),
		layout.Stacked(func() {
			gtx.Constraints.Width.Min = hmin
			gtx.Constraints.Height.Min = vmin
			layout.Center.Layout(gtx, func() {
				b.Inset.Layout(gtx, func() {
					paint.ColorOp{Color: col}.Add(gtx.Ops)
					widget.Label{}.Layout(gtx, b.shaper, b.Font, b.TextSize, b.Text)
				})
			})
			w()
			pointer.Rect(image.Rectangle{Max: gtx.Dimensions.Size}).Add(gtx.Ops)
			button.Layout(gtx)
		}),
-- 
2.21.1 (Apple Git-122.3)
View this thread in the archives