Fix cursor movement bug in scrolled editor and add regression test
This commit is contained in:
+50
-14
@@ -514,8 +514,11 @@ func SetCursorFromPoint(x, y float64) {
|
||||
|
||||
// 1. Identify the intended line index based on y
|
||||
// layout.Y values are relative to the text region origin.
|
||||
// We need to account for scroll offset.
|
||||
visualLine := int((y + float64(TheState.ScrollOffset)) / lineHeight)
|
||||
// We need to account for scroll offset: y is passed as relative to the text region top + scroll offset.
|
||||
// So y is the position in the *content*.
|
||||
visualLine := int(y / lineHeight)
|
||||
|
||||
log.Printf("SetCursorFromPoint: y=%f, scroll=%f, visualLine=%d, lineHeight=%f", y, float64(TheState.ScrollOffset), visualLine, lineHeight)
|
||||
|
||||
// Group glyphs by their Y-baseline
|
||||
type lineGroup struct {
|
||||
@@ -523,27 +526,56 @@ func SetCursorFromPoint(x, y float64) {
|
||||
indices []int
|
||||
}
|
||||
groups := []lineGroup{}
|
||||
seenY := make(map[float64]int) // maps Y to group index
|
||||
|
||||
|
||||
// Find all unique baseline Ys
|
||||
// The tap Y is based on line height (top of line).
|
||||
// We need to associate Y-baseline with visual line index.
|
||||
|
||||
// Create map from visual line (0, 1, 2...) to baseline Y.
|
||||
// Since line height is fixed:
|
||||
// Line 0 baseline is at some Y0.
|
||||
// Line 1 baseline is at Y0 + lineHeight.
|
||||
|
||||
// Let's find Y0 first.
|
||||
minY := 1e9
|
||||
for _, yVal := range layout.Y {
|
||||
if float64(yVal) < minY {
|
||||
minY = float64(yVal)
|
||||
}
|
||||
}
|
||||
|
||||
// Now group by baseline
|
||||
groups = []lineGroup{} // Reset groups
|
||||
for i, yVal := range layout.Y {
|
||||
yFloat := float64(yVal)
|
||||
idx, ok := seenY[yFloat]
|
||||
if !ok {
|
||||
idx = len(groups)
|
||||
groups = append(groups, lineGroup{y: yFloat, indices: []int{}})
|
||||
seenY[yFloat] = idx
|
||||
lineIdx := int((yFloat - minY) / lineHeight + 0.5) // round to nearest line
|
||||
if lineIdx < 0 { lineIdx = 0 }
|
||||
|
||||
// Ensure enough groups
|
||||
for len(groups) <= lineIdx {
|
||||
groups = append(groups, lineGroup{
|
||||
y: minY + float64(len(groups))*lineHeight,
|
||||
indices: []int{},
|
||||
})
|
||||
}
|
||||
groups[idx].indices = append(groups[idx].indices, i)
|
||||
groups[lineIdx].indices = append(groups[lineIdx].indices, i)
|
||||
}
|
||||
// Sort groups by Y
|
||||
sort.Slice(groups, func(i, j int) bool { return groups[i].y < groups[j].y })
|
||||
|
||||
// If visualLine is out of bounds, clamp
|
||||
if visualLine < 0 {
|
||||
visualLine = 0
|
||||
}
|
||||
if visualLine >= len(groups) {
|
||||
visualLine = len(groups) - 1
|
||||
if visualLine >= len(groups) || len(groups[visualLine].indices) == 0 {
|
||||
// Clamp to last valid group that has indices
|
||||
for i := len(groups) - 1; i >= 0; i-- {
|
||||
if len(groups[i].indices) > 0 {
|
||||
visualLine = i
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(groups[visualLine].indices) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
targetGroup := groups[visualLine]
|
||||
@@ -558,6 +590,7 @@ func SetCursorFromPoint(x, y float64) {
|
||||
rightmostIdx = i
|
||||
}
|
||||
}
|
||||
log.Printf("TargetGroup: Y=%f, Indices=%v, rightmostIdx=%d, x=%f, rightmostX=%f", targetGroup.y, targetGroup.indices, rightmostIdx, x, rightmostX)
|
||||
|
||||
// 4. Check if tap is to the right of the last character
|
||||
if rightmostIdx != -1 && x > rightmostX {
|
||||
@@ -585,12 +618,15 @@ func SetCursorFromPoint(x, y float64) {
|
||||
if dist < 0 {
|
||||
dist = -dist
|
||||
}
|
||||
log.Printf("Checking glyph %d: x=%f, centerX=%f, dist=%f", i, x, glyphCenterX, dist)
|
||||
if dist < minDist {
|
||||
minDist = dist
|
||||
bestIdx = i
|
||||
}
|
||||
}
|
||||
log.Printf("BestIdx=%d, ByteOffset=%d", bestIdx, layout.ByteOffsets[bestIdx])
|
||||
if bestIdx != -1 {
|
||||
TheState.Editor.CursorPosition = layout.ByteOffsets[bestIdx]
|
||||
log.Printf("After SetCursor: CursorPosition=%d", TheState.Editor.CursorPosition)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user