diff --git a/f32/f32.go b/f32/f32.go index 1ec4652c..044cc51b 100644 --- a/f32/f32.go +++ b/f32/f32.go @@ -81,6 +81,14 @@ func (p Point) In(r Rectangle) bool { r.Min.Y <= p.Y && p.Y < r.Max.Y } +// Round returns the integer point closest to p. +func (p Point) Round() image.Point { + return image.Point{ + X: int(math.Round(float64(p.X))), + Y: int(math.Round(float64(p.Y))), + } +} + // Size returns r's width and height. func (r Rectangle) Size() Point { return Point{X: r.Dx(), Y: r.Dy()} diff --git a/gesture/gesture.go b/gesture/gesture.go index 6ece9cb3..af4aecf8 100644 --- a/gesture/gesture.go +++ b/gesture/gesture.go @@ -91,7 +91,7 @@ type Click struct { // TypeClick for a completed click. type ClickEvent struct { Type ClickType - Position f32.Point + Position image.Point Source pointer.Source Modifiers key.Modifiers // NumClicks records successive clicks occurring @@ -198,7 +198,7 @@ func (c *Click) Events(q event.Queue) []ClickEvent { c.clicks = 1 } c.clickedAt = e.Time - events = append(events, ClickEvent{Type: TypeClick, Position: e.Position, Source: e.Source, Modifiers: e.Modifiers, NumClicks: c.clicks}) + events = append(events, ClickEvent{Type: TypeClick, Position: e.Position.Round(), Source: e.Source, Modifiers: e.Modifiers, NumClicks: c.clicks}) } else { events = append(events, ClickEvent{Type: TypeCancel}) } @@ -224,7 +224,7 @@ func (c *Click) Events(q event.Queue) []ClickEvent { break } c.pressed = true - events = append(events, ClickEvent{Type: TypePress, Position: e.Position, Source: e.Source, Modifiers: e.Modifiers}) + events = append(events, ClickEvent{Type: TypePress, Position: e.Position.Round(), Source: e.Source, Modifiers: e.Modifiers}) case pointer.Leave: if !c.pressed { c.pid = e.PointerID diff --git a/widget/button.go b/widget/button.go index db456f53..8c65f74a 100644 --- a/widget/button.go +++ b/widget/button.go @@ -6,7 +6,6 @@ import ( "image" "time" - "gioui.org/f32" "gioui.org/gesture" "gioui.org/io/key" "gioui.org/io/pointer" @@ -39,7 +38,7 @@ type Click struct { // Press represents a past pointer press. type Press struct { // Position of the press. - Position f32.Point + Position image.Point // Start is when the press began. Start time.Time // End is when the press was ended by a release or cancel. diff --git a/widget/material/button.go b/widget/material/button.go index 94276e5f..a90a04b5 100644 --- a/widget/material/button.go +++ b/widget/material/button.go @@ -297,7 +297,7 @@ func drawInk(gtx layout.Context, c widget.Press) { ink := paint.ColorOp{Color: rgba} ink.Add(gtx.Ops) rr := size * .5 - defer op.Offset(c.Position.Add(f32.Point{ + defer op.Offset(layout.FPt(c.Position).Add(f32.Point{ X: -rr, Y: -rr, })).Push(gtx.Ops).Pop()