ref: ca677a50fd03b97e8421d5ddcc9c793338c81058
parent: a8bb4e36f51179ad6b544d2236600b88aab371d2
author: Philip Silva <philip.silva@protonmail.com>
date: Sun Jun 29 09:12:42 EDT 2025
improve click event positioning with margins/paddings
--- a/box/box.go
+++ b/box/box.go
@@ -68,11 +68,16 @@
b.mouseXY = mev.Point
}
for i, el := range b.Elements {
+ elR, elMargin := el.Geom()
+
switch tevOrig := evOrig.(type) {
case mouse.Event:
var ev mouse.Event
- ev.Point = tevOrig.Point
+ ev.Point = tevOrig.Point.
+ Sub(b.Padding.TopLeft()).
+ Sub(elR.Min).
+ Sub(elMargin.TopLeft())
ev.Buttons = tevOrig.Buttons
ev.Msec = tevOrig.Msec
--- a/field/field.go
+++ b/field/field.go
@@ -80,6 +80,7 @@
f.hover = false
case mouse.Click:
f.Pos, _ = slices.BinarySearch(f.Offsets, tev.Point.X)
+ f.Pos = slices.Max([]int{0, f.Pos-1})
f.updateTextImgs()
}
--- a/field/field_test.go
+++ b/field/field_test.go
@@ -45,7 +45,7 @@
Type: mouse.Click,
Point: image.Pt(82,423),
})
- if f.Pos != 5 {
+ if f.Pos != 4 {
t.Fatalf("%v", f.Pos)
}
}
--
⑨