~eliasnaur/gio-patches

material: add text support to icon button v2 PROPOSED

metaclips: 1
 material: add text support to icon button

 1 files changed, 56 insertions(+), 62 deletions(-)
Yes it is, any recommendations? I implemented a new patch as
ButtonLayout was implemented instead

On Thu, Mar 19, 2020 at 4:56 PM Elias Naur <mail@eliasnaur.com> wrote:
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/10027/mbox | git am -3
Learn more about email & git

[PATCH v2] material: add text support to icon button Export this patch

 This implementation makes support for texts in iconbutton If no text is provided,
 a conventional iconbutton UI is shown
 Fixes #43

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

diff --git a/widget/material/button.go b/widget/material/button.go
index 7cec3f3..524a8d4 100644
--- a/widget/material/button.go
+++ b/widget/material/button.go
@@ -33,13 +33,15 @@ type IconButton struct {
	Background color.RGBA
	Color      color.RGBA
	Icon       *Icon
	IconSize   unit.Value
	Text       string
	TextSize   unit.Value
	Font       text.Font
	shaper     text.Shaper
	Size       unit.Value
	Padding    unit.Value
	Inset      layout.Inset
}

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

func (t *Theme) Button(txt string) Button {
@@ -56,15 +58,20 @@ func (t *Theme) Button(txt string) Button {
	}
}

func (t *Theme) IconButton(icon *Icon, txt string) IconButton {
func (t *Theme) IconButton(icon *Icon) IconButton {
	return IconButton{
		Background: t.Color.Primary,
		Color:      t.Color.InvText,
		Icon:       icon,
		IconSize:   unit.Dp(56),
		Text:       txt,
		TextSize:   t.TextSize.Scale(14.0 / 16.0),
		shaper:     t.Shaper,
		Size:       unit.Dp(56),
		Padding:    unit.Dp(16),
	}
}

func (t *Theme) ButtonLayout() ButtonLayout {
	return ButtonLayout{
		Background: t.Color.Primary,
		Color:      t.Color.InvText,
		Padding:    unit.Dp(16),
	}
}
@@ -107,66 +114,53 @@ func (b Button) Layout(gtx *layout.Context, button *widget.Button) {
func (b IconButton) Layout(gtx *layout.Context, button *widget.Button) {
	layout.Stack{Alignment: layout.Center}.Layout(gtx,
		layout.Expanded(func() {
			if b.Text != "" {
				rr := float32(gtx.Px(unit.Dp(8)))
				clip.Rect{
					Rect: f32.Rectangle{Max: f32.Point{
						X: float32(gtx.Constraints.Width.Min),
						Y: float32(gtx.Constraints.Height.Min),
					}},
					NE: rr, NW: rr, SE: rr, SW: rr,
				}.Op(gtx.Ops).Add(gtx.Ops)
			} else {
				size := gtx.Constraints.Width.Min
				sizef := float32(size)
				rr := sizef * .5
				clip.Rect{
					Rect: f32.Rectangle{Max: f32.Point{X: sizef, Y: sizef}},
					NE:   rr, NW: rr, SE: rr, SW: rr,
				}.Op(gtx.Ops).Add(gtx.Ops)
			}

			size := gtx.Constraints.Width.Min
			sizef := float32(size)
			rr := sizef * .5
			clip.Rect{
				Rect: f32.Rectangle{Max: f32.Point{X: sizef, Y: sizef}},
				NE:   rr, NW: rr, SE: rr, SW: rr,
			}.Op(gtx.Ops).Add(gtx.Ops)
			fill(gtx, b.Background)
			for _, c := range button.History() {
				drawInk(gtx, c)
			}
		}),

		layout.Stacked(func() {
			iconAndLabel := layout.Flex{Alignment: layout.Middle}

			icon := layout.Rigid(func() {
				layout.UniformInset(b.Padding).Layout(gtx, func() {
					size := gtx.Px(b.IconSize) - 2*gtx.Px(b.Padding)
					if b.Icon != nil {
						b.Icon.Color = b.Color
						b.Icon.Layout(gtx, unit.Px(float32(size)))
						gtx.Dimensions = layout.Dimensions{
							Size: image.Point{X: size, Y: size},
						}
					}
				})
			})

			label := layout.Rigid(func() {
				// reduce icon-button width
				layout.Inset{Top: b.Padding, Right: b.Padding, Bottom: b.Padding, Left: unit.Dp(-10)}.Layout(gtx, func() {
					paint.ColorOp{Color: b.Color}.Add(gtx.Ops)
					widget.Label{}.Layout(gtx, b.shaper, b.Font, b.TextSize, b.Text)
				})
			})

			if b.Icon != nil {
				if b.Text != "" {
					iconAndLabel.Layout(gtx, icon, label)
				} else {
					iconAndLabel.Layout(gtx, icon)
			layout.UniformInset(b.Padding).Layout(gtx, func() {
				size := gtx.Px(b.Size) - 2*gtx.Px(b.Padding)
				if b.Icon != nil {
					b.Icon.Color = b.Color
					b.Icon.Layout(gtx, unit.Px(float32(size)))
				}
				gtx.Dimensions = layout.Dimensions{
					Size: image.Point{X: size, Y: size},
				}
			})
			pointer.Ellipse(image.Rectangle{Max: gtx.Dimensions.Size}).Add(gtx.Ops)
			button.Layout(gtx)
		}),
	)
}

			} else {
				iconAndLabel.Layout(gtx, label)
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)))
			clip.Rect{
				Rect: f32.Rectangle{Max: f32.Point{
					X: float32(gtx.Constraints.Width.Min),
					Y: float32(gtx.Constraints.Height.Min),
				}},
				NE: rr, NW: rr, SE: rr, SW: rr,
			}.Op(gtx.Ops).Add(gtx.Ops)
			fill(gtx, b.Background)
			for _, c := range button.History() {
				drawInk(gtx, c)
			}

		}),
		layout.Stacked(func() {
			w()
			pointer.Ellipse(image.Rectangle{Max: gtx.Dimensions.Size}).Add(gtx.Ops)
			button.Layout(gtx)
		}),
-- 
2.21.1 (Apple Git-122.3)
Sorry, still getting the hang of it.

On Wed, Mar 18, 2020 at 10:38 PM metaclips <utimichael9@gmail.com> wrote:
Sorry, still getting the hang of it.

On Wed, Mar 18, 2020 at 10:38 PM metaclips <utimichael9@gmail.com> wrote:
View this thread in the archives