From fd2cb4a7a1d62b2e16d8380e902819ae8dcec2ef Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Mon, 11 May 2020 12:26:45 +0200 Subject: [PATCH] widget,widget/material: use constraints for setting up hit area Before this change, the widget.Button.Layout method assumed the caller had set up the pointer hit area before. Further, the very common rectangular hit areas needed both an AreaOp and a widget.Button.Layout call. Make widget.Button less subtle and more useful by setting up a pointer hit area given by the incoming minimum constraints. Drop a pointer.AreaOp made redundant by the change. Signed-off-by: Elias Naur --- widget/button.go | 6 ++++++ widget/material/button.go | 1 - 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/widget/button.go b/widget/button.go index 20fc773a..30583b8f 100644 --- a/widget/button.go +++ b/widget/button.go @@ -3,10 +3,12 @@ package widget import ( + "image" "time" "gioui.org/f32" "gioui.org/gesture" + "gioui.org/io/pointer" "gioui.org/layout" "gioui.org/op" ) @@ -44,7 +46,11 @@ func (b *Button) History() []Click { func (b *Button) Layout(gtx *layout.Context) { // Flush clicks from before the previous frame. b.processEvents(gtx) + var st op.StackOp + st.Push(gtx.Ops) + pointer.Rect(image.Rectangle{Max: gtx.Constraints.Min()}).Add(gtx.Ops) b.click.Add(gtx.Ops) + st.Pop() for len(b.history) > 0 { c := b.history[0] if gtx.Now().Sub(c.Time) < 1*time.Second { diff --git a/widget/material/button.go b/widget/material/button.go index 25c2c594..020c2810 100644 --- a/widget/material/button.go +++ b/widget/material/button.go @@ -116,7 +116,6 @@ func (b ButtonLayoutStyle) Layout(gtx *layout.Context, button *widget.Button, w }) }), layout.Expanded(func() { - pointer.Rect(image.Rectangle{Max: gtx.Constraints.Min()}).Add(gtx.Ops) button.Layout(gtx) }), )