Handle drags: 1:1 finger tracking with cross-flip; fix caret/taps on empty lines
Selection handles now track the finger 1:1 (anchor grab point + displacement) instead of snapping by whole lines, and crossing the opposite handle flips the selection (native behaviour) instead of clearing it. Caret and tap/handle line resolution use VisualLineStarts instead of the min-Y baseline: the window's first visual line may be an empty line with no recorded glyphs, which used to draw boundary carets one line too low per leading empty line and land taps/dragged handles one line below the finger. New exported ui.CaretPoint centralises byte->insertion-point mapping. The off-screen caret no longer clamps to the window edge: EditorLayout ships the true (possibly negative / past-end) window-relative cursor and the renderer skips the caret when the cursor is outside the shaped window, so scrolling past the caret no longer makes it jump onto the top/bottom line. IME/router replay fixes: key.FocusCmd is issued only on a focus transition (a per-frame no-op still takes the immediate-command path and re-queues all pointer events), and the key.SelectionCmd IME sync is deferred while a handle drag is in progress (each push re-injected the drag into every gesture). Handle drags forward only Grabbed events; a tap inside a handle grab box is a no-op. Also: key.FocusEvent no longer logs as unexpected in main; dead code removed (worker taskWrapper, browser applyXxxResult stubs, scrollIndex, mock_setup sortModeKey/lineSpan helpers); mock FileSystem.ListPaths prefix match uses strings.HasPrefix; build scripts run the new scripts/check.sh static gate (go vet + staticcheck). Tests: caret_point_test, touch_selection updates (flip/empty-line cases), off-window caret e2e, selection drag e2e grab step.
This commit is contained in:
@@ -107,8 +107,7 @@ func TestAutoSaveE2E(t *testing.T) {
|
||||
// After saving, the file is re-opened and the full content is compared
|
||||
// byte-for-byte against the expected result.
|
||||
func TestLargeFileChunkBoundary(t *testing.T) {
|
||||
const chunkSize = DefaultChunkSize // 64 KB
|
||||
const fileSize = 256 * 1024 // 4 chunks
|
||||
const fileSize = 256 * 1024 // 4 chunks
|
||||
|
||||
// --- 1. Setup: create a 256 KB file with a predictable pattern ---
|
||||
mockFS := mock.NewFileSystem()
|
||||
|
||||
@@ -583,20 +583,6 @@ func (l *Logic) handleWorkerResult(res pool.Result) {
|
||||
l.emitFrame()
|
||||
}
|
||||
|
||||
// applyBuildIndexResult applies a completed BuildIndexTask result to browser state.
|
||||
func (l *Logic) applyBuildIndexResult(res pool.Result) {
|
||||
// Delegated to browserManager
|
||||
}
|
||||
|
||||
// applyLoadPagesResult applies a completed LoadPagesTask result to browser state.
|
||||
func (l *Logic) applyLoadPagesResult(res pool.Result) {
|
||||
// Delegated to browserManager
|
||||
}
|
||||
|
||||
// applyReadDirResult applies a completed ReadDirTask result to browser state.
|
||||
func (l *Logic) applyReadDirResult(res pool.Result) {
|
||||
// Delegated to browserManager
|
||||
}
|
||||
|
||||
// State returns the current state.
|
||||
func (l *Logic) State() *State {
|
||||
|
||||
@@ -1,14 +1,10 @@
|
||||
package editor
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"pad/internal/browser"
|
||||
"pad/internal/io/pool"
|
||||
"pad/internal/io/pool/mock"
|
||||
"pad/internal/io/pool/types"
|
||||
)
|
||||
|
||||
// populateMockFileSystem seeds the mock filesystem with sample data for testing.
|
||||
@@ -139,114 +135,3 @@ func populateMockFileSystem(fs pool.FileSystem) {
|
||||
mfs.AddFile("/Downloads/"+f.name, []byte(f.content), baseTime.AddDate(0, 0, f.days))
|
||||
}
|
||||
}
|
||||
|
||||
// sortModeKey converts a browser.SortMode to its JSON key string.
|
||||
func sortModeKey(mode browser.SortMode) string {
|
||||
switch mode {
|
||||
case 0: // SortModeNameAsc
|
||||
return "name_asc"
|
||||
case 1: // SortModeNameDesc
|
||||
return "name_desc"
|
||||
case 2: // SortModeDateAsc
|
||||
return "date_asc"
|
||||
case 3: // SortModeDateDesc
|
||||
return "date_desc"
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
// modeCount returns the total number of sort modes.
|
||||
func modeCount() int {
|
||||
return 4
|
||||
}
|
||||
|
||||
// buildBrowserIndex converts types.DirEntry results into a browser.DirectoryIndex
|
||||
// with pre-computed position maps for all sort modes.
|
||||
func buildBrowserIndex(entries []types.DirEntry) *browser.DirectoryIndex {
|
||||
var browserEntries []browser.Entry
|
||||
for _, e := range entries {
|
||||
info, err := e.Info()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
browserEntries = append(browserEntries, browser.Entry{
|
||||
Path: e.Name(),
|
||||
Name: e.Name(),
|
||||
Size: info.Size(),
|
||||
ModTime: info.ModTime(),
|
||||
IsDir: info.IsDir(),
|
||||
})
|
||||
}
|
||||
|
||||
// Build position maps for all sort modes
|
||||
sortOrders := make(map[string][]int)
|
||||
for mode := browser.SortMode(0); mode < browser.SortMode(modeCount()); mode++ {
|
||||
key := sortModeKey(mode)
|
||||
if key != "" {
|
||||
sortOrders[key] = buildPositionMap(browserEntries, mode)
|
||||
}
|
||||
}
|
||||
|
||||
return &browser.DirectoryIndex{
|
||||
Path: "/",
|
||||
EntryCount: len(browserEntries),
|
||||
Entries: browserEntries,
|
||||
SortOrders: sortOrders,
|
||||
}
|
||||
}
|
||||
|
||||
// buildPositionMap creates a sorted index → raw index mapping.
|
||||
func buildPositionMap(entries []browser.Entry, mode browser.SortMode) []int {
|
||||
n := len(entries)
|
||||
indices := make([]int, n)
|
||||
for i := range indices {
|
||||
indices[i] = i
|
||||
}
|
||||
|
||||
// Sort indices based on entry comparison
|
||||
cmp := comparator(mode)
|
||||
sort.SliceStable(indices, func(i, j int) bool {
|
||||
a, b := entries[indices[i]], entries[indices[j]]
|
||||
return cmp(a, b) < 0
|
||||
})
|
||||
|
||||
return indices
|
||||
}
|
||||
|
||||
// comparator returns a comparison function for the given SortMode.
|
||||
// Returns negative if a < b, zero if equal, positive if a > b.
|
||||
func comparator(mode browser.SortMode) func(a, b browser.Entry) int {
|
||||
switch mode {
|
||||
case 0: // SortModeNameAsc
|
||||
return func(a, b browser.Entry) int {
|
||||
if c := strings.Compare(strings.ToLower(a.Name), strings.ToLower(b.Name)); c != 0 {
|
||||
return c
|
||||
}
|
||||
return strings.Compare(a.Name, b.Name)
|
||||
}
|
||||
case 1: // SortModeNameDesc
|
||||
return func(a, b browser.Entry) int {
|
||||
if c := strings.Compare(strings.ToLower(b.Name), strings.ToLower(a.Name)); c != 0 {
|
||||
return c
|
||||
}
|
||||
return strings.Compare(b.Name, a.Name)
|
||||
}
|
||||
case 2: // SortModeDateAsc
|
||||
return func(a, b browser.Entry) int {
|
||||
if c := a.ModTime.Compare(b.ModTime); c != 0 {
|
||||
return c
|
||||
}
|
||||
return strings.Compare(strings.ToLower(a.Name), strings.ToLower(b.Name))
|
||||
}
|
||||
case 3: // SortModeDateDesc
|
||||
return func(a, b browser.Entry) int {
|
||||
if c := b.ModTime.Compare(a.ModTime); c != 0 {
|
||||
return c
|
||||
}
|
||||
return strings.Compare(strings.ToLower(a.Name), strings.ToLower(b.Name))
|
||||
}
|
||||
default:
|
||||
return func(a, b browser.Entry) int { return 0 }
|
||||
}
|
||||
}
|
||||
|
||||
+218
-106
@@ -3,7 +3,6 @@ package editor
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"math"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -107,16 +106,26 @@ type EditorState struct {
|
||||
SelDragging bool
|
||||
SelDragWhich int
|
||||
SelDragRel int
|
||||
// SelDragPressY is the text-local Y of the first event of an in-progress
|
||||
// handle/body drag (the grab), and SelDragPressLine the visual line of
|
||||
// the dragged anchor AT THE GRAB. Start/end handles move relative to
|
||||
// these (see handleAnchorPos). The base line must be captured once at the
|
||||
// grab: re-resolving it from the anchor's current byte on every event
|
||||
// makes the anchor's own movement feed back into its target, and a
|
||||
// jittering finger near a line boundary races the anchor off the screen.
|
||||
SelDragPressY float64
|
||||
SelDragPressLine int
|
||||
Filename string
|
||||
// SelDragSwapped: during a start/end-handle drag the finger crossed the
|
||||
// opposite handle and the selection was flipped (native behaviour): the
|
||||
// dragged handle now controls the end it crossed. Cleared when the finger
|
||||
// crosses back or the drag ends.
|
||||
SelDragSwapped bool
|
||||
// SelDragPressX/Y is the text-local position of the first event of an
|
||||
// in-progress handle/caret drag (the grab), and SelDragAnchorX/Y the
|
||||
// text-local point of the dragged anchor AT THE GRAB (see
|
||||
// textPointLocal). Each later event moves the anchor by the finger's
|
||||
// displacement from the grab, so the handle tracks the finger 1:1 (the
|
||||
// teardrop stays under the finger) and the selection resizes
|
||||
// continuously — no line snapping, no jump on grab. The reference is
|
||||
// fixed at the grab, so the anchor's own movement cannot feed back into
|
||||
// its target: a jittering finger near a line boundary cannot race the
|
||||
// anchor off the screen.
|
||||
SelDragPressX float64
|
||||
SelDragPressY float64
|
||||
SelDragAnchorX float64
|
||||
SelDragAnchorY float64
|
||||
Filename string
|
||||
// TooLarge is set when an opened file exceeds MaxEditableFileSize. The
|
||||
// editor shows a "too large to edit" notice instead of content (the
|
||||
// browser can still list the file).
|
||||
@@ -1096,6 +1105,7 @@ func HandleSelDragEvt(data any) {
|
||||
selDragMove(ev.Which, ev.X, ev.Y)
|
||||
case ui.SelectionDragEnd:
|
||||
TheState.Editor.SelDragging = false
|
||||
TheState.Editor.SelDragSwapped = false
|
||||
TheState.Editor.SelDragRel = 0
|
||||
// The caret handle (long press on blank space) is a transient affordance;
|
||||
// the selection handles come back from the selection state on the next
|
||||
@@ -1113,17 +1123,9 @@ func selDragMove(which int, x, y ui.Dp) {
|
||||
if !e.SelDragging {
|
||||
e.SelDragging = true
|
||||
e.SelDragWhich = which
|
||||
e.SelDragSwapped = false
|
||||
e.SelDragPressX = localX
|
||||
e.SelDragPressY = localY
|
||||
if which == 0 {
|
||||
if l, ok2 := visualLineOfByte(e.SelectionStart - glyphBase()); ok2 {
|
||||
e.SelDragPressLine = l
|
||||
}
|
||||
}
|
||||
if which == 1 {
|
||||
if l, ok2 := visualLineOfByte(e.SelectionEnd - glyphBase()); ok2 {
|
||||
e.SelDragPressLine = l
|
||||
}
|
||||
}
|
||||
if which == 2 && ok {
|
||||
// Body drag: the grab fixes the finger's offset from the selection
|
||||
// start; the selection itself moves on later events.
|
||||
@@ -1133,30 +1135,73 @@ func selDragMove(which int, x, y ui.Dp) {
|
||||
}
|
||||
return
|
||||
}
|
||||
// Start/end/caret handles: the first event already positions the
|
||||
// handle (no grab offset needed).
|
||||
}
|
||||
if !ok {
|
||||
return // finger outside the laid-out window: keep last position
|
||||
// Start/end/caret handles: the grab fixes the anchor's text position;
|
||||
// later events move it by the finger's displacement from the grab.
|
||||
var anchor int
|
||||
switch which {
|
||||
case 0:
|
||||
anchor = e.SelectionStart
|
||||
case 1:
|
||||
anchor = e.SelectionEnd
|
||||
default: // 3: caret
|
||||
anchor = e.CursorPosition
|
||||
}
|
||||
if ax, ay, ok2 := textPointLocal(anchor); ok2 {
|
||||
e.SelDragAnchorX = ax
|
||||
e.SelDragAnchorY = ay
|
||||
}
|
||||
return
|
||||
}
|
||||
switch e.SelDragWhich {
|
||||
case 0: // start handle
|
||||
if p2, ok2 := handleAnchorPos(e, e.SelectionStart, localX, localY); ok2 {
|
||||
pos = p2
|
||||
case 0, 1: // start / end handle
|
||||
// Move the anchor by the finger's displacement from the grab (1:1
|
||||
// tracking: the teardrop stays under the finger, the selection
|
||||
// resizes continuously, lines are crossed only when the finger's
|
||||
// mapped point crosses them).
|
||||
pos, ok = handleFollowPos(e, localX, localY)
|
||||
if !ok {
|
||||
return // finger outside the laid-out window: keep last position
|
||||
}
|
||||
if pos > e.SelectionEnd {
|
||||
pos = e.SelectionEnd
|
||||
// Crossing the opposite handle flips the selection (native behaviour)
|
||||
// instead of clamping to zero length and clearing it; while flipped the
|
||||
// dragged handle controls the end it crossed.
|
||||
if e.SelDragWhich == 0 {
|
||||
if !e.SelDragSwapped {
|
||||
if pos > e.SelectionEnd {
|
||||
e.SelDragSwapped = true
|
||||
SetSelection(e.SelectionEnd, pos)
|
||||
} else if pos < e.SelectionEnd {
|
||||
SetSelection(pos, e.SelectionEnd)
|
||||
}
|
||||
} else {
|
||||
if pos < e.SelectionStart {
|
||||
e.SelDragSwapped = false
|
||||
SetSelection(pos, e.SelectionStart)
|
||||
} else if pos > e.SelectionStart {
|
||||
SetSelection(e.SelectionStart, pos)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if !e.SelDragSwapped {
|
||||
if pos < e.SelectionStart {
|
||||
e.SelDragSwapped = true
|
||||
SetSelection(pos, e.SelectionStart)
|
||||
} else if pos > e.SelectionStart {
|
||||
SetSelection(e.SelectionStart, pos)
|
||||
}
|
||||
} else {
|
||||
if pos > e.SelectionEnd {
|
||||
e.SelDragSwapped = false
|
||||
SetSelection(e.SelectionEnd, pos)
|
||||
} else if pos < e.SelectionEnd {
|
||||
SetSelection(e.SelectionEnd, pos)
|
||||
}
|
||||
}
|
||||
}
|
||||
SetSelection(pos, e.SelectionEnd)
|
||||
case 1: // end handle
|
||||
if p2, ok2 := handleAnchorPos(e, e.SelectionEnd, localX, localY); ok2 {
|
||||
pos = p2
|
||||
}
|
||||
if pos < e.SelectionStart {
|
||||
pos = e.SelectionStart
|
||||
}
|
||||
SetSelection(e.SelectionStart, pos)
|
||||
case 2: // body: move the whole selection, preserving length
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
selLen := e.SelectionEnd - e.SelectionStart
|
||||
ns := pos - e.SelDragRel
|
||||
fl := fileLenBytes()
|
||||
@@ -1168,51 +1213,87 @@ func selDragMove(which int, x, y ui.Dp) {
|
||||
}
|
||||
SetSelection(ns, ns+selLen)
|
||||
case 3: // caret drag handle
|
||||
e.CursorPosition = pos
|
||||
if p, ok2 := handleFollowPos(e, localX, localY); ok2 {
|
||||
e.CursorPosition = p
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// handleAnchorPos resolves the new anchor byte for a start/end-handle drag
|
||||
// event. The anchor moves RELATIVE to its own visual line: the finger's
|
||||
// vertical displacement from the grab (in whole visual lines) selects the
|
||||
// target line, and the finger's x is projected onto that line.
|
||||
//
|
||||
// This gives the native behaviour — dragging a handle down extends the
|
||||
// selection across lines, dragging it up shrinks from the far side — while
|
||||
// keeping the grab safe. The 48dp grab box is centred BELOW the line (the
|
||||
// teardrop hangs off the line's bottom edge), so the press usually lands on
|
||||
// the neighbouring line; mapping the press to its own line used to clamp the
|
||||
// anchor onto the other handle and clear the selection on the very first
|
||||
// drag event. With the relative mapping a stationary or horizontal drag
|
||||
// (displacement under half a line) never moves the anchor off its line, and
|
||||
// a deliberate vertical drag crosses lines only after the finger has moved
|
||||
// past half a line height from the grab.
|
||||
func handleAnchorPos(e *EditorState, anchorByte int, localX, localY float64) (int, bool) {
|
||||
// Base line is the anchor's line at the GRAB (captured in selDragMove),
|
||||
// never the anchor's current line: the anchor's own movement must not
|
||||
// feed back into its target.
|
||||
line := e.SelDragPressLine
|
||||
lh := float64(EffectiveLineHeight())
|
||||
if lh > 0 {
|
||||
target := line + int(math.Round((localY-e.SelDragPressY)/lh))
|
||||
if target < 0 {
|
||||
target = 0
|
||||
}
|
||||
return textPosOnLineAtX(target, localX)
|
||||
// handleFollowPos maps the dragged handle's target point — the anchor's grab
|
||||
// position plus the finger's displacement from the grab — to a byte offset.
|
||||
func handleFollowPos(e *EditorState, localX, localY float64) (int, bool) {
|
||||
tx := e.SelDragAnchorX + (localX - e.SelDragPressX)
|
||||
ty := e.SelDragAnchorY + (localY - e.SelDragPressY)
|
||||
return textPosFromLocalPoint(tx, ty)
|
||||
}
|
||||
|
||||
// textPointLocal is the inverse of textPosFromLocalPoint: the text-local
|
||||
// (x, y) point that maps back to byteOff. x is the insertion point's
|
||||
// x (the glyph's left edge, or the previous glyph's right edge), and y is
|
||||
// the MIDDLE of the byte's visual line band so that textPosFromLocalPoint's
|
||||
// int(y/lineHeight) line resolution lands on the byte's own line.
|
||||
func textPointLocal(byteOff int) (x, y float64, ok bool) {
|
||||
layout := TheState.Editor.GlyphLayout
|
||||
if len(layout.ByteOffsets) == 0 || len(layout.Y) == 0 {
|
||||
return 0, 0, false
|
||||
}
|
||||
return textPosOnLineAtX(line, localX)
|
||||
lh := float64(EffectiveLineHeight())
|
||||
if lh <= 0 {
|
||||
return 0, 0, false
|
||||
}
|
||||
base := glyphBase()
|
||||
winByte := byteOff - base
|
||||
if winByte < 0 {
|
||||
winByte = 0
|
||||
}
|
||||
idx := sort.Search(len(layout.ByteOffsets), func(i int) bool {
|
||||
return layout.ByteOffsets[i] >= winByte
|
||||
})
|
||||
if idx < len(layout.ByteOffsets) && layout.ByteOffsets[idx] == winByte {
|
||||
x = float64(layout.X[idx])
|
||||
} else if idx > 0 {
|
||||
// The byte sits between glyphs (mid-multibyte) or past the last
|
||||
// glyph (EOF): the previous glyph's right edge.
|
||||
x = float64(layout.X[idx-1] + layout.Advance[idx-1])
|
||||
}
|
||||
line, ok2 := visualLineOfByte(winByte)
|
||||
if !ok2 {
|
||||
return 0, 0, false
|
||||
}
|
||||
y = (float64(line) + 0.5) * lh
|
||||
return x, y, true
|
||||
}
|
||||
|
||||
// visualLineOfByte returns the visual line (0-based within the shaped
|
||||
// window) that holds the insertion point at the given window-relative byte
|
||||
// offset, using the same rule the renderer uses to place the handles
|
||||
// (handleAt): the line of the first glyph whose byte offset is at or past
|
||||
// the offset, or the last line when the offset is past the last glyph.
|
||||
// offset: the last visual line whose start byte is at or before the offset.
|
||||
// A line's terminating newline belongs to that line, and an empty line's
|
||||
// lone insertion point sits on the empty line (the first-glyph-at-or-past
|
||||
// rule would put both on the next line, which is wrong for empty lines).
|
||||
func visualLineOfByte(winByte int) (int, bool) {
|
||||
layout := TheState.Editor.GlyphLayout
|
||||
if len(layout.ByteOffsets) == 0 || len(layout.Y) == 0 {
|
||||
return 0, false
|
||||
}
|
||||
// The insertion point at winByte is drawn on the last visual line whose
|
||||
// first byte is at or before it: a line's terminating newline belongs to
|
||||
// that line, and an empty line's lone insertion point sits on the empty
|
||||
// line. (Looking at the first glyph at/past the byte instead is wrong
|
||||
// around empty lines: that glyph is on the NEXT line.) This must agree
|
||||
// with textPosOnLineAtX's line starts so a dragged handle's grab anchor
|
||||
// round-trips.
|
||||
if starts := layout.VisualLineStarts; len(starts) > 0 {
|
||||
if winByte < 0 {
|
||||
winByte = 0
|
||||
}
|
||||
idx := sort.Search(len(starts), func(i int) bool {
|
||||
return starts[i] > winByte
|
||||
})
|
||||
if idx > 0 {
|
||||
idx--
|
||||
}
|
||||
return idx, true
|
||||
}
|
||||
lineHeight := float64(EffectiveLineHeight())
|
||||
minY := 1e9
|
||||
for _, yVal := range layout.Y {
|
||||
@@ -1946,11 +2027,13 @@ func EditorLayout(screenWidth, screenHeight ui.Dp, wordWrap bool) []ui.Element {
|
||||
// window could leave the lower rows blank).
|
||||
visibleContent = cb.Content(start, end)
|
||||
|
||||
// Adjust cursor position to be relative to visibleContent
|
||||
// Adjust cursor position to be relative to visibleContent. It may be
|
||||
// NEGATIVE (the cursor's line scrolled above the window) or exceed
|
||||
// len(visibleContent) (scrolled below): the renderer treats an
|
||||
// out-of-window cursor as off-screen and draws no caret, like a native
|
||||
// editor. Clamping to the window edge here made the caret appear on
|
||||
// the top/bottom visible line whenever the user scrolled past it.
|
||||
visibleCursorPos = TheState.Editor.CursorPosition - start
|
||||
if visibleCursorPos < 0 {
|
||||
visibleCursorPos = 0
|
||||
}
|
||||
|
||||
// Adjust scroll offset to be relative to visibleContent origin
|
||||
// Sub-line shift for the renderer: the SAME visual-line decomposition
|
||||
@@ -2284,7 +2367,7 @@ func textPosOnLineAtX(visualLine int, x float64) (int, bool) {
|
||||
y float64
|
||||
indices []int
|
||||
}
|
||||
groups := []lineGroup{}
|
||||
var groups []lineGroup // assigned in the grouping loop below
|
||||
|
||||
// Find all unique baseline Ys
|
||||
// The tap Y is based on line height (top of line).
|
||||
@@ -2303,11 +2386,20 @@ func textPosOnLineAtX(visualLine int, x float64) (int, bool) {
|
||||
}
|
||||
}
|
||||
|
||||
// Now group by baseline
|
||||
// Now group by visual line. A glyph's visual line is resolved from the
|
||||
// recorded line starts (VisualLineStarts), NOT from its Y-baseline: the
|
||||
// window's first visual line may be an empty line with no recorded
|
||||
// glyphs, in which case min(layout.Y) is the first NON-empty line's
|
||||
// baseline and (Y-minY)/lineHeight numbers every line one too high —
|
||||
// taps and dragged handles then landed one line below the finger
|
||||
// whenever the viewport top sat on an empty line.
|
||||
groups = []lineGroup{}
|
||||
for i, yVal := range layout.Y {
|
||||
yFloat := float64(yVal)
|
||||
lineIdx := int((yFloat-minY)/lineHeight + 0.5) // round to nearest line
|
||||
lineIdx, ok := visualLineOfByte(layout.ByteOffsets[i])
|
||||
if !ok {
|
||||
lineIdx = int((yFloat-minY)/lineHeight + 0.5) // round to nearest line
|
||||
}
|
||||
if lineIdx < 0 {
|
||||
lineIdx = 0
|
||||
}
|
||||
@@ -2327,7 +2419,17 @@ func textPosOnLineAtX(visualLine int, x float64) (int, bool) {
|
||||
visualLine = 0
|
||||
}
|
||||
if visualLine >= len(groups) || len(groups[visualLine].indices) == 0 {
|
||||
// Clamp to last valid group that has indices
|
||||
// A line with no glyphs: an empty line (a lone "\n"), or the trailing
|
||||
// empty line of a file ending in "\n". Its single insertion point is
|
||||
// the start of the line, before its terminating newline. Anchor there:
|
||||
// a handle dragged across an empty line sweeps up to the line instead
|
||||
// of snapping back to the last text line (or jumping past the empty
|
||||
// line to the text after it).
|
||||
if visualLine < len(layout.VisualLineStarts) {
|
||||
return base + layout.VisualLineStarts[visualLine], true
|
||||
}
|
||||
// No line start recorded for this line (finger beyond the content):
|
||||
// clamp to the last non-empty line.
|
||||
for i := len(groups) - 1; i >= 0; i-- {
|
||||
if len(groups[i].indices) > 0 {
|
||||
visualLine = i
|
||||
@@ -2335,7 +2437,7 @@ func textPosOnLineAtX(visualLine int, x float64) (int, bool) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(groups[visualLine].indices) == 0 {
|
||||
if visualLine >= len(groups) || len(groups[visualLine].indices) == 0 {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
@@ -2352,9 +2454,9 @@ func textPosOnLineAtX(visualLine int, x float64) (int, bool) {
|
||||
}
|
||||
}
|
||||
|
||||
// 4. Check if tap is to the right of the last character
|
||||
if rightmostIdx != -1 && x > rightmostX {
|
||||
// Position at the end of the line content, before any trailing newline.
|
||||
// lineEndByte is the insertion point at the end of the line content,
|
||||
// before any trailing newline.
|
||||
lineEndByte := func() (int, bool) {
|
||||
start := base + layout.ByteOffsets[rightmostIdx]
|
||||
buf := TheState.Editor.ChunkedBuffer
|
||||
var fileContent string
|
||||
@@ -2371,31 +2473,41 @@ func textPosOnLineAtX(visualLine int, x float64) (int, bool) {
|
||||
r, size := utf8.DecodeRuneInString(fileContent[start:])
|
||||
if r == '\n' {
|
||||
return start, true
|
||||
} else {
|
||||
return start + size, true
|
||||
}
|
||||
return start + size, true
|
||||
}
|
||||
|
||||
// 5. Otherwise, find the closest glyph on this line.
|
||||
bestIdx := -1
|
||||
minDist := float64(1e9)
|
||||
for _, i := range targetGroup.indices {
|
||||
if bestIdx == -1 {
|
||||
bestIdx = i
|
||||
}
|
||||
// Calculate distance to the glyph center
|
||||
glyphCenterX := float64(layout.X[i] + layout.Advance[i]/2)
|
||||
dist := glyphCenterX - x
|
||||
if dist < 0 {
|
||||
dist = -dist
|
||||
}
|
||||
if dist < minDist {
|
||||
minDist = dist
|
||||
bestIdx = i
|
||||
}
|
||||
// 4. Check if tap is to the right of the last character
|
||||
if rightmostIdx != -1 && x > rightmostX {
|
||||
return lineEndByte()
|
||||
}
|
||||
if bestIdx != -1 {
|
||||
return base + layout.ByteOffsets[bestIdx], true
|
||||
|
||||
// 5. Otherwise, map x to the nearest insertion point on the line: the
|
||||
// glyph edge closest to x. x exactly on an edge maps to that edge's byte
|
||||
// (this is where handle anchors sit, so a dragged handle must map back
|
||||
// to its own byte), and a tie (x exactly midway between two edges) maps
|
||||
// to the left byte. Tapping the right half of a glyph positions the
|
||||
// caret after it, as on Android.
|
||||
idxs := targetGroup.indices
|
||||
// First glyph whose left edge is right of x.
|
||||
m := sort.Search(len(idxs), func(k int) bool {
|
||||
return float64(layout.X[idxs[k]]) > x
|
||||
})
|
||||
if m == 0 {
|
||||
// x at or left of the first glyph's left edge.
|
||||
return base + layout.ByteOffsets[idxs[0]], true
|
||||
}
|
||||
return 0, false
|
||||
// x sits in the span of glyph idxs[m-1] (or, when m == len(idxs), at or
|
||||
// past its left edge but within its span, since x <= rightmostX here).
|
||||
g := idxs[m-1]
|
||||
left := x - float64(layout.X[g])
|
||||
right := float64(layout.Advance[g]) - left
|
||||
if left <= right {
|
||||
return base + layout.ByteOffsets[g], true
|
||||
}
|
||||
if m < len(idxs) {
|
||||
return base + layout.ByteOffsets[idxs[m]], true
|
||||
}
|
||||
// Right half of the last glyph: the insertion point after it.
|
||||
return lineEndByte()
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ func fuzzStateAPI(t *testing.T, initial string, chunkSize, ops int) {
|
||||
cb.LineIndex = types.NewLineIndex(lineStartOffsets([]byte(initial)), 0, int64(len(initial)))
|
||||
|
||||
model := initial
|
||||
cursor := len(model)
|
||||
var cursor int // every op path sets this before the invariant reads it
|
||||
rng := rand.New(rand.NewSource(4242))
|
||||
modelCap := 32 * 1024
|
||||
|
||||
|
||||
@@ -204,9 +204,12 @@ func TestHandleTapAt_InsideMenuIsIgnored(t *testing.T) {
|
||||
func TestSelDrag_EndHandleExtends(t *testing.T) {
|
||||
touchSelState("hello world")
|
||||
HandleLongPressAt(18, 108) // selects "hello" [0,5)
|
||||
// Drag the end handle (which=1) past the last glyph (local 115 > 110)
|
||||
// so the whole line is selected.
|
||||
HandleSelDragEvt(ui.SelectionDragEvent{Which: 1, X: 16 + 115, Y: 108})
|
||||
// Grab the end handle (which=1) near the anchor (byte 5 is at local
|
||||
// x=50): the grab itself does not move the selection...
|
||||
HandleSelDragEvt(ui.SelectionDragEvent{Which: 1, X: 16 + 52, Y: 108})
|
||||
assertSelection(t, 0, 5)
|
||||
// ...and dragging past the last glyph (x=50+63=113 > 110) selects the
|
||||
// whole line: the anchor follows the finger 1:1.
|
||||
HandleSelDragEvt(ui.SelectionDragEvent{Which: 1, X: 16 + 115, Y: 108})
|
||||
assertSelection(t, 0, 11)
|
||||
}
|
||||
@@ -214,20 +217,49 @@ func TestSelDrag_EndHandleExtends(t *testing.T) {
|
||||
func TestSelDrag_StartHandleExtends(t *testing.T) {
|
||||
touchSelState("hello world")
|
||||
HandleLongPressAt(18, 108) // selects "hello" [0,5)
|
||||
// Drag the start handle (which=0) to the 'o' (glyph 4, x=40..50; local 45 -> pt 61).
|
||||
HandleSelDragEvt(ui.SelectionDragEvent{Which: 0, X: 16 + 45, Y: 108})
|
||||
// Grab the start handle (which=0) near the anchor (byte 0 at local x=0).
|
||||
HandleSelDragEvt(ui.SelectionDragEvent{Which: 0, X: 16 + 2, Y: 108})
|
||||
assertSelection(t, 0, 5)
|
||||
// Drag right to x=0+43=43 (near glyph 4's centre 45): the start moves
|
||||
// to byte 4.
|
||||
HandleSelDragEvt(ui.SelectionDragEvent{Which: 0, X: 16 + 45, Y: 108})
|
||||
assertSelection(t, 4, 5)
|
||||
}
|
||||
|
||||
func TestSelDrag_StartHandleCollapseClears(t *testing.T) {
|
||||
func TestSelDrag_StartHandleCrossesEndFlips(t *testing.T) {
|
||||
touchSelState("hello world")
|
||||
HandleLongPressAt(18, 108) // selects "hello" [0,5)
|
||||
// Drag the start handle to the end of the selection -> zero-length -> cleared.
|
||||
HandleSelDragEvt(ui.SelectionDragEvent{Which: 0, X: 16 + 52, Y: 108})
|
||||
HandleSelDragEvt(ui.SelectionDragEvent{Which: 0, X: 16 + 52, Y: 108})
|
||||
assertSelection(t, -1, -1)
|
||||
assertMenu(t, false)
|
||||
// Grab the start handle near the anchor (byte 0 at local x=0).
|
||||
HandleSelDragEvt(ui.SelectionDragEvent{Which: 0, X: 16 + 2, Y: 108})
|
||||
// Drag past the end handle (x=0+64=64 -> glyph 'w' at byte 6): the
|
||||
// selection flips (native behaviour) instead of clamping to zero length
|
||||
// and clearing. The dragged handle now controls the end it crossed.
|
||||
HandleSelDragEvt(ui.SelectionDragEvent{Which: 0, X: 16 + 66, Y: 108})
|
||||
assertSelection(t, 5, 6)
|
||||
// While flipped, dragging before the (new) start flips back (x=0+14=14
|
||||
// -> byte 1).
|
||||
HandleSelDragEvt(ui.SelectionDragEvent{Which: 0, X: 16 + 16, Y: 108})
|
||||
assertSelection(t, 1, 5)
|
||||
HandleSelDragEvt(ui.SelectionDragEnd{})
|
||||
}
|
||||
|
||||
func TestSelDrag_EndHandleCrossesStartFlips(t *testing.T) {
|
||||
touchSelState("hello world")
|
||||
HandleLongPressAt(16+62, 108) // selects "world" [6,11)
|
||||
// Grab the end handle near the anchor (byte 11 at local x=110).
|
||||
HandleSelDragEvt(ui.SelectionDragEvent{Which: 1, X: 16 + 112, Y: 108})
|
||||
// Drag before the start handle (x=110-96=14 -> glyph 'e' at byte 1):
|
||||
// the selection flips (native behaviour) instead of clamping to zero
|
||||
// length and clearing: it now spans the finger (byte 1) to the other
|
||||
// handle (6).
|
||||
HandleSelDragEvt(ui.SelectionDragEvent{Which: 1, X: 16 + 16, Y: 108})
|
||||
assertSelection(t, 1, 6)
|
||||
// While flipped, the dragged handle controls the start: dragging back
|
||||
// past the other handle (x=110-16=94 -> glyph 'l' at byte 9) un-flips,
|
||||
// and the handle becomes the end again: [6,9).
|
||||
HandleSelDragEvt(ui.SelectionDragEvent{Which: 1, X: 16 + 96, Y: 108})
|
||||
assertSelection(t, 6, 9)
|
||||
HandleSelDragEvt(ui.SelectionDragEnd{})
|
||||
}
|
||||
|
||||
func TestSelDrag_BodyMovesPreservingLength(t *testing.T) {
|
||||
@@ -253,8 +285,13 @@ func TestSelDrag_CaretHandleMovesCursor(t *testing.T) {
|
||||
if !TheState.Editor.CaretDrag {
|
||||
t.Fatal("expected caret drag mode")
|
||||
}
|
||||
// Drag the caret handle (which=3) to glyph 2 (local 25 -> pt 41).
|
||||
HandleSelDragEvt(ui.SelectionDragEvent{Which: 3, X: 16 + 25, Y: 108})
|
||||
// Grab the caret handle near the anchor (byte 12 at local x=120), then
|
||||
// drag left so the mapped point is x=120-97=23 (glyph 2's centre 25):
|
||||
// the caret follows the finger 1:1.
|
||||
HandleSelDragEvt(ui.SelectionDragEvent{Which: 3, X: 16 + 122, Y: 108})
|
||||
if TheState.Editor.CursorPosition != 12 {
|
||||
t.Errorf("cursor = %d after grab, want 12 (unchanged)", TheState.Editor.CursorPosition)
|
||||
}
|
||||
HandleSelDragEvt(ui.SelectionDragEvent{Which: 3, X: 16 + 25, Y: 108})
|
||||
if TheState.Editor.CursorPosition != 2 {
|
||||
t.Errorf("cursor = %d after caret drag, want 2", TheState.Editor.CursorPosition)
|
||||
@@ -418,25 +455,29 @@ func TestSelDrag_StartHandle_PressOnLineBelow_KeepsSelection(t *testing.T) {
|
||||
touchSelState2Lines()
|
||||
HandleLongPressAt(18, 108) // selects "hello" [0,5)
|
||||
checkInitialSelection(t)
|
||||
// Start handle (which=0), anchor at byte 0 (local x=0). Press at local
|
||||
// Start handle (which=0), anchor at byte 0 (local x=0). Grab at local
|
||||
// (22, 25): localY 25 is on line 1 (line height 16.8), on the finger's
|
||||
// own line x=22 is byte 14 (past SelectionEnd=5) — the original bug's
|
||||
// exact geometry. Zero vertical displacement from the grab -> the anchor
|
||||
// stays on line 0, x=22 there is byte 2: the selection shrinks to "llo"
|
||||
// but is never cleared.
|
||||
// exact geometry. The anchor's reference is its own point (x=0, line 0)
|
||||
// fixed at the grab, so the grab and a zero-displacement event do not
|
||||
// move it — the selection is never cleared.
|
||||
HandleSelDragEvt(ui.SelectionDragEvent{Which: 0, X: 16 + 22, Y: 100 + 25})
|
||||
HandleSelDragEvt(ui.SelectionDragEvent{Which: 0, X: 16 + 22, Y: 100 + 25})
|
||||
assertSelection(t, 0, 5)
|
||||
// Drag right by 20dp: the anchor moves to x=0+20=20 on its own line 0 —
|
||||
// exactly the left edge of the glyph for byte 2, which maps to its own
|
||||
// byte. The finger's own line is never used.
|
||||
HandleSelDragEvt(ui.SelectionDragEvent{Which: 0, X: 16 + 42, Y: 100 + 25})
|
||||
assertSelection(t, 2, 5)
|
||||
// Continue dragging left: x=12 on line 0 is byte 1.
|
||||
HandleSelDragEvt(ui.SelectionDragEvent{Which: 0, X: 16 + 12, Y: 100 + 25})
|
||||
assertSelection(t, 1, 5) // "ello"
|
||||
// Continue: x=0+40=40 is the edge of byte 4.
|
||||
HandleSelDragEvt(ui.SelectionDragEvent{Which: 0, X: 16 + 62, Y: 100 + 25})
|
||||
assertSelection(t, 4, 5) // "o"
|
||||
}
|
||||
|
||||
// TestSelDrag_EndHandle_DragDownExtendsAcrossLines is the multi-line
|
||||
// extension: grabbing the end handle and dragging it down more than half a
|
||||
// line height moves the anchor to the next visual line (relative to the
|
||||
// anchor's own line), so the selection grows across the newline — the
|
||||
// native behaviour.
|
||||
// extension: the anchor's mapped point (its grab position plus the finger's
|
||||
// displacement) crosses into the next visual line, so the selection grows
|
||||
// across the newline — the native behaviour.
|
||||
func TestSelDrag_EndHandle_DragDownExtendsAcrossLines(t *testing.T) {
|
||||
touchSelState2Lines()
|
||||
HandleLongPressAt(18, 108) // selects "hello" [0,5)
|
||||
@@ -445,10 +486,11 @@ func TestSelDrag_EndHandle_DragDownExtendsAcrossLines(t *testing.T) {
|
||||
// (52, 25) — the teardrop hangs below line 0, its box centre at y≈26.8.
|
||||
HandleSelDragEvt(ui.SelectionDragEvent{Which: 1, X: 16 + 52, Y: 100 + 25})
|
||||
assertSelection(t, 0, 5) // grab alone does not move the anchor
|
||||
// Drag down 20dp (past half a line height of 16.8): the anchor crosses
|
||||
// to line 1, x=52 there is byte 17 (the 'n' of "second").
|
||||
// Drag down 20dp: the mapped point (50, 8.4+20=28.4) is on line 1, where
|
||||
// x=50 is the left edge of the glyph for byte 17 (exact edges map to
|
||||
// their own byte).
|
||||
HandleSelDragEvt(ui.SelectionDragEvent{Which: 1, X: 16 + 52, Y: 100 + 45})
|
||||
assertSelection(t, 0, 17) // "hello world\nsecond" minus the last d
|
||||
assertSelection(t, 0, 17) // "hello world\nsecon"
|
||||
}
|
||||
|
||||
// TestSelDrag_StartHandle_DragUpExtendsToLineAbove is the upward
|
||||
@@ -462,8 +504,8 @@ func TestSelDrag_StartHandle_DragUpExtendsToLineAbove(t *testing.T) {
|
||||
// local (0, 43) — the box centre for a line-1 handle is at y≈43.6.
|
||||
HandleSelDragEvt(ui.SelectionDragEvent{Which: 0, X: 16 + 0, Y: 100 + 43})
|
||||
checkSelection12_18(t)
|
||||
// A small wobble (3dp) must NOT cross lines: still line 1, x=8 -> byte 12.
|
||||
HandleSelDragEvt(ui.SelectionDragEvent{Which: 0, X: 16 + 8, Y: 100 + 46})
|
||||
// A small wobble (3dp) must NOT cross lines: still line 1, x=2 -> byte 12.
|
||||
HandleSelDragEvt(ui.SelectionDragEvent{Which: 0, X: 16 + 2, Y: 100 + 46})
|
||||
checkSelection12_18(t)
|
||||
// Drag up 20dp from the grab: the anchor crosses to line 0, x=0 there is
|
||||
// byte 0.
|
||||
@@ -513,12 +555,11 @@ func touchSelState3Lines() {
|
||||
}
|
||||
|
||||
// TestSelDrag_EndHandle_JitterNearLineBoundary_Stable is a regression test
|
||||
// for a feedback runaway: the target line must be computed from the
|
||||
// anchor's line AT THE GRAB. Re-resolving the base line from the anchor's
|
||||
// current byte on every event makes the anchor's own movement feed into its
|
||||
// target: once the anchor crosses a line, every further event adds another
|
||||
// line, and a jittering finger near a boundary races the anchor to the
|
||||
// bottom of the file.
|
||||
// for a feedback runaway: the anchor's reference point is fixed AT THE
|
||||
// GRAB. Re-resolving it from the anchor's current byte on every event
|
||||
// makes the anchor's own movement feed into its target: once the anchor
|
||||
// crosses a line, every further event adds another line, and a jittering
|
||||
// finger near a boundary races the anchor to the bottom of the file.
|
||||
func TestSelDrag_EndHandle_JitterNearLineBoundary_Stable(t *testing.T) {
|
||||
touchSelState3Lines()
|
||||
HandleLongPressAt(18, 108) // selects "hello" [0,5)
|
||||
@@ -526,10 +567,10 @@ func TestSelDrag_EndHandle_JitterNearLineBoundary_Stable(t *testing.T) {
|
||||
// End handle (which=1), anchor byte 5 on line 0. Grab at local (52, 25).
|
||||
HandleSelDragEvt(ui.SelectionDragEvent{Which: 1, X: 16 + 52, Y: 100 + 25})
|
||||
checkInitialSelection(t)
|
||||
// The finger hovers just past the half-line boundary (dY = 10, 11, 10
|
||||
// around the 8.4 threshold) and jitters. The anchor must settle one line
|
||||
// down (byte 17, the 'n' of "second") and STAY there — not creep to
|
||||
// line 2 (byte 29+) or further with each event.
|
||||
// The finger hovers just past the line boundary (the mapped point's y
|
||||
// = 18.4/19.4, past line 0's band of 0..16.8) and jitters. The anchor
|
||||
// must settle one line down (byte 17) and STAY there — not creep to line
|
||||
// 2 (byte 24+) or further with each event.
|
||||
for _, dy := range []int{35, 36, 35, 36, 35} {
|
||||
HandleSelDragEvt(ui.SelectionDragEvent{Which: 1, X: 16 + 52, Y: ui.Dp(100 + dy)})
|
||||
assertSelection(t, 0, 17)
|
||||
@@ -543,3 +584,137 @@ func checkInitialSelection(t *testing.T) {
|
||||
t.Fatalf("precondition: selection = [%d,%d), want [0,5)", s.SelectionStart, s.SelectionEnd)
|
||||
}
|
||||
}
|
||||
|
||||
// touchSelStateLines is like touchSelState but builds a genuine multi-line
|
||||
// layout from content: each non-newline byte becomes a 10dp-wide glyph at
|
||||
// x=10*column on its visual line (baseline y = line*lineH), and
|
||||
// VisualLineStarts records each visual line's first byte. Line-break glyphs
|
||||
// are absent from the arrays, as in the real renderer.
|
||||
func touchSelStateLines(content string) {
|
||||
TheState = NewState()
|
||||
TheState.Editor.Buffer = content
|
||||
TheState.Editor.CursorPosition = len(content)
|
||||
TheState.EditorRegion = ui.Region{X: 16, Y: 100, W: 379, H: 700}
|
||||
TheState.ScrollOffset = 0
|
||||
TheState.Editor.IMEWindowStartByte = 0
|
||||
TheState.Editor.IMEWindowText = content
|
||||
lh := ui.Dp(16.8)
|
||||
layout := ui.GlyphLayout{
|
||||
LineHeight: lh,
|
||||
VisualLineStarts: []int{0},
|
||||
}
|
||||
line, col := 0, 0
|
||||
for i := 0; i < len(content); i++ {
|
||||
if content[i] == '\n' {
|
||||
layout.VisualLineStarts = append(layout.VisualLineStarts, i+1)
|
||||
line, col = line+1, 0
|
||||
continue
|
||||
}
|
||||
layout.ByteOffsets = append(layout.ByteOffsets, i)
|
||||
layout.X = append(layout.X, ui.Dp(10*col))
|
||||
layout.Y = append(layout.Y, ui.Dp(float64(lh)*float64(line)))
|
||||
layout.Advance = append(layout.Advance, 10)
|
||||
col++
|
||||
}
|
||||
TheState.Editor.GlyphLayout = layout
|
||||
}
|
||||
|
||||
// TestTextPos_EmptyLine is a regression test for selection anchors snapping
|
||||
// back to a single line when dragged across an empty line: a glyph-less
|
||||
// line has exactly one insertion point, its first byte, and must map there
|
||||
// (the old code clamped to the LAST non-empty line, so the anchor landed on
|
||||
// a neighbouring text line at the finger's x and the selection collapsed).
|
||||
func TestTextPos_EmptyLine(t *testing.T) {
|
||||
touchSelStateLines("abc\n\ndef\n")
|
||||
lh := float64(EffectiveLineHeight()) // 16.8
|
||||
// Empty line 1's band: y in [16.8, 33.6).
|
||||
pos, ok := textPosFromLocalPoint(5, 1.5*lh)
|
||||
if !ok || pos != 4 {
|
||||
t.Errorf("textPosFromLocalPoint(5, 25.2) = %d, %v; want 4 (the empty line's lone insertion point)", pos, ok)
|
||||
}
|
||||
// The trailing empty line (after the last "\n") maps to EOF (byte 9).
|
||||
pos, ok = textPosFromLocalPoint(5, 3.5*lh)
|
||||
if !ok || pos != 9 {
|
||||
t.Errorf("textPosFromLocalPoint(5, 58.8) = %d, %v; want 9 (EOF)", pos, ok)
|
||||
}
|
||||
// Past the end of line 0's text: the line's terminating newline, byte 3.
|
||||
pos, ok = textPosFromLocalPoint(29, 0.5*lh)
|
||||
if !ok || pos != 3 {
|
||||
t.Errorf("textPosFromLocalPoint(29, 8.4) = %d, %v; want 3 (end of line 0)", pos, ok)
|
||||
}
|
||||
// Mid line 2: unchanged line behaviour (the 'e' of "def").
|
||||
pos, ok = textPosFromLocalPoint(15, 2.5*lh)
|
||||
if !ok || pos != 6 {
|
||||
t.Errorf("textPosFromLocalPoint(15, 42) = %d, %v; want 6", pos, ok)
|
||||
}
|
||||
}
|
||||
|
||||
// TestTextPos_EmptyFirstLine is the user-visible regression for the caret
|
||||
// jumping to an adjacent line: when the shaped WINDOW starts on an empty
|
||||
// line (no glyphs on visual line 0), the old (Y-minY)/lineHeight grouping
|
||||
// numbered every glyph line one too high, so a tap on visual line N landed
|
||||
// on line N+1 — the caret appeared one line below the finger, and it
|
||||
// "jumped back" when the window scrolled past the empty line.
|
||||
func TestTextPos_EmptyFirstLine(t *testing.T) {
|
||||
// Window text starting on an empty line (line 0), as when the viewport
|
||||
// top sits on an empty line: "\nabc\ndef\n" — line 0 empty (byte 0),
|
||||
// line 1 "abc" (bytes 1-3, "\n" at 4), line 2 "def" (bytes 5-7, "\n" at 8).
|
||||
touchSelStateLines("\nabc\ndef\n")
|
||||
lh := float64(EffectiveLineHeight()) // 16.8
|
||||
|
||||
// Tap on window line 1 (the "abc" line): the byte must be on line 1,
|
||||
// not line 2 (the old off-by-one landing).
|
||||
pos, ok := textPosFromLocalPoint(5, 1.5*lh)
|
||||
if !ok || pos != 1 {
|
||||
t.Errorf("tap on line 1 = %d, %v; want 1 (line 1's first glyph; the old code gave 5, line 2)", pos, ok)
|
||||
}
|
||||
// Tap past the end of line 1's text: line 1's terminating "\n" (byte 4),
|
||||
// not line 2's.
|
||||
pos, ok = textPosFromLocalPoint(29, 1.5*lh)
|
||||
if !ok || pos != 4 {
|
||||
t.Errorf("tap right of line 1 = %d, %v; want 4 (line 1 end); the old code gave 8 (line 2 end)", pos, ok)
|
||||
}
|
||||
// Tap on the empty FIRST line: its lone insertion point, byte 0 (the old
|
||||
// code clamped onto line 1's text).
|
||||
pos, ok = textPosFromLocalPoint(5, 0.5*lh)
|
||||
if !ok || pos != 0 {
|
||||
t.Errorf("tap on empty line 0 = %d, %v; want 0 (the empty line's lone insertion point)", pos, ok)
|
||||
}
|
||||
// Tap on window line 2: unaffected beyond the renumbering fix — line 2's
|
||||
// first glyph (byte 5).
|
||||
pos, ok = textPosFromLocalPoint(5, 2.5*lh)
|
||||
if !ok || pos != 5 {
|
||||
t.Errorf("tap on line 2 = %d, %v; want 5", pos, ok)
|
||||
}
|
||||
}
|
||||
|
||||
// TestSelDrag_EndHandleAcrossEmptyLine is the user-visible regression:
|
||||
// dragging the end handle down across an empty line must sweep the selection
|
||||
// up to the empty line (its lone insertion point) and then continue onto
|
||||
// the text after it — not snap back to a single line.
|
||||
func TestSelDrag_EndHandleAcrossEmptyLine(t *testing.T) {
|
||||
touchSelStateLines("abc\n\ndef\n")
|
||||
HandleLongPressAt(16+5, 100+8) // selects "abc" [0,3)
|
||||
assertSelection(t, 0, 3)
|
||||
// Grab the end handle: the anchor is at byte 3 (end of line 0: x=30,
|
||||
// y=8.4, the line's middle).
|
||||
HandleSelDragEvt(ui.SelectionDragEvent{Which: 1, X: 16 + 30, Y: 100 + 8.4})
|
||||
assertSelection(t, 0, 3)
|
||||
// Drag down into the empty line's band: the end sweeps to the empty
|
||||
// line's insertion point (byte 4) instead of snapping back.
|
||||
HandleSelDragEvt(ui.SelectionDragEvent{Which: 1, X: 16 + 30, Y: 100 + 25.2})
|
||||
assertSelection(t, 0, 4)
|
||||
// Continue onto line 2: the end follows the finger to the end of "def"
|
||||
// (byte 8).
|
||||
HandleSelDragEvt(ui.SelectionDragEvent{Which: 1, X: 16 + 30, Y: 100 + 42})
|
||||
assertSelection(t, 0, 8)
|
||||
HandleSelDragEvt(ui.SelectionDragEnd{})
|
||||
// Grab the start handle at byte 0 (line 0: x=0, y=8.4) and drag it down
|
||||
// onto the empty line: it sweeps to the empty line's insertion point
|
||||
// (byte 4) and the selection shrinks from [0,8) to [4,8) — no collapse,
|
||||
// no snap back.
|
||||
HandleSelDragEvt(ui.SelectionDragEvent{Which: 0, X: 16 + 0, Y: 100 + 8.4})
|
||||
HandleSelDragEvt(ui.SelectionDragEvent{Which: 0, X: 16 + 0, Y: 100 + 25.2})
|
||||
assertSelection(t, 4, 8)
|
||||
HandleSelDragEvt(ui.SelectionDragEnd{})
|
||||
}
|
||||
|
||||
@@ -203,16 +203,3 @@ func byteOffsetOfLine(content string, i int) int {
|
||||
return o
|
||||
}
|
||||
|
||||
// lineSpan returns the total byte length of the first n lines (including
|
||||
// their terminators) of content.
|
||||
func lineSpan(content string, n int) int {
|
||||
o := 0
|
||||
for line := 0; line < n; line++ {
|
||||
nx := strings.IndexByte(content[o:], '\n')
|
||||
if nx < 0 {
|
||||
return len(content) - o
|
||||
}
|
||||
o += nx + 1
|
||||
}
|
||||
return o
|
||||
}
|
||||
|
||||
@@ -197,10 +197,8 @@ func newTestBufferForWrap(n int, counts []int32) *ChunkedBuffer {
|
||||
}
|
||||
cb.LineIndex = types.NewLineIndex(offsets, 0, int64(len(buf)))
|
||||
w := NewWrapIndex(len(offsets))
|
||||
if counts != nil {
|
||||
for i, c := range counts {
|
||||
w.Set(i, c)
|
||||
}
|
||||
for i, c := range counts {
|
||||
w.Set(i, c)
|
||||
}
|
||||
cb.WrapIndex = w
|
||||
|
||||
|
||||
Reference in New Issue
Block a user