WIP baseline: Termux open-file bridge + word-wrap-aware viewport/scroll
- JNI: open_file_in_termux via ACTION_SEND intent (text/plain + file:// uri), global context ref kept from registerFragment - impl_android.go: OpenFile(path) attaches current thread if needed - NewLogic takes openfunc; State.open + ui.OpenFile now func(string) - ChunkedBuffer.VisibleByteRange: word-wrap path using GlyphLayout.VisualLineStarts (+byteOffset, lineHeight, visual index param) - GlyphLayout gains LineHeight; drawWrappedText records VisualLineStarts - WordWrap default true; State.ByteOffset tracks first visible line - types: VisualLineIndex - scroll_fix_test.go (new) - debug prints left in place (WIP; cleanup in later phase)
This commit is contained in:
@@ -407,15 +407,49 @@ func (cb *ChunkedBuffer) Delete(pos, n int) {
|
||||
|
||||
// VisibleByteRange returns the byte range [start, end) of content
|
||||
// visible in the viewport, given the current scroll offset and viewport height.
|
||||
func (cb *ChunkedBuffer) VisibleByteRange(scrollOffset ui.Dp, viewportHeight ui.Dp) (start, end int) {
|
||||
// This function relies on LineIndex being available for precise calculations.
|
||||
// Fallback to estimate if LineIndex is nil.
|
||||
if cb.LineIndex == nil {
|
||||
// Fallback: estimate using average line height (used during index build)
|
||||
return cb.visibleByteRangeEstimate(scrollOffset, viewportHeight)
|
||||
func (cb *ChunkedBuffer) VisibleByteRange(scrollOffset ui.Dp, byteOffset int, viewportHeight ui.Dp, lineHeight ui.Dp, wordWrap bool, layout ui.GlyphLayout, visualIndex *types.VisualLineIndex) (start, end, startLine int) {
|
||||
// If wrapping and we have visual line data, use it for precise calculation
|
||||
if wordWrap && len(layout.VisualLineStarts) > 0 {
|
||||
// Calculate which visual line should be at the top based on scroll offset
|
||||
visualLine := int(scrollOffset / lineHeight)
|
||||
if visualLine >= len(layout.VisualLineStarts) {
|
||||
visualLine = len(layout.VisualLineStarts) - 1
|
||||
}
|
||||
|
||||
start = layout.VisualLineStarts[visualLine] + byteOffset
|
||||
fmt.Printf("VisibleByteRange: visualLine = %d start = %d\n", visualLine, start)
|
||||
|
||||
// Calculate end: enough content to fill viewport + buffer
|
||||
linesInViewport := int(viewportHeight / lineHeight) + 2
|
||||
endLine := visualLine + linesInViewport
|
||||
if endLine >= len(layout.VisualLineStarts) {
|
||||
end = int(cb.fileLen)
|
||||
} else {
|
||||
end = layout.VisualLineStarts[endLine]
|
||||
}
|
||||
|
||||
// Clamp to file bounds
|
||||
if end > int(cb.fileLen) {
|
||||
end = int(cb.fileLen)
|
||||
}
|
||||
if start >= end {
|
||||
end = start + 1000 // minimum buffer
|
||||
if end > int(cb.fileLen) {
|
||||
end = int(cb.fileLen)
|
||||
}
|
||||
}
|
||||
return start, end, visualLine
|
||||
}
|
||||
// Precise: use line index to find the exact byte range
|
||||
return cb.visibleByteRangePrecise(scrollOffset, viewportHeight)
|
||||
|
||||
// Fallback to LineIndex (logical lines) or estimate if:
|
||||
// 1. Not word wrapping
|
||||
// 2. No visual index available
|
||||
if cb.LineIndex == nil {
|
||||
start, end = cb.visibleByteRangeEstimate(scrollOffset, viewportHeight)
|
||||
return start, end, 0
|
||||
}
|
||||
start, end = cb.visibleByteRangePrecise(scrollOffset, viewportHeight)
|
||||
return start, end, 0
|
||||
}
|
||||
|
||||
// visibleByteRangeEstimate approximates the visible byte range using
|
||||
|
||||
@@ -65,9 +65,11 @@ type Logic struct {
|
||||
}
|
||||
|
||||
// NewLogic creates a new Logic instance, accepting an optional mockFS.
|
||||
func NewLogic(mfs pool.FileSystem, startPath ...string) *Logic {
|
||||
func NewLogic(mfs pool.FileSystem, path string, openfunc func(string)) *Logic {
|
||||
state := NewState()
|
||||
TheState = state
|
||||
TheState.open = openfunc
|
||||
ui.OpenFile = openfunc
|
||||
|
||||
// Initialize mock filesystem if nil
|
||||
mockFS := mfs
|
||||
@@ -80,11 +82,6 @@ func NewLogic(mfs pool.FileSystem, startPath ...string) *Logic {
|
||||
wp := pool.NewWorkerPool(8) // Increased worker pool size to 8
|
||||
wp.Start()
|
||||
|
||||
// Set browser initial path
|
||||
path := "/"
|
||||
if len(startPath) > 0 {
|
||||
path = startPath[0]
|
||||
}
|
||||
state.Browser.CurrentPath = path
|
||||
bm, _ := browser.NewBrowserManager(&state.Browser, wp, mockFS)
|
||||
|
||||
@@ -166,6 +163,7 @@ func (l *Logic) Run() {
|
||||
// Store the full GlyphLayout on editor state.
|
||||
// Derive LastLineY from it for scroll clamping.
|
||||
l.state.Editor.GlyphLayout = layout
|
||||
log.Printf("LOGIC received GlyphLayout: ByteOffsets=%d, VisualLineStarts=%d", len(layout.ByteOffsets), len(layout.VisualLineStarts))
|
||||
var derivedLastLineY ui.Dp
|
||||
if len(layout.Y) > 0 {
|
||||
derivedLastLineY = layout.Y[len(layout.Y)-1]
|
||||
|
||||
@@ -0,0 +1,229 @@
|
||||
package editor_test
|
||||
|
||||
import (
|
||||
"math"
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"pad/internal/editor"
|
||||
"pad/internal/io/pool/types"
|
||||
"pad/internal/ui"
|
||||
)
|
||||
|
||||
// absFloat returns the absolute value of a float64
|
||||
func absFloat(x ui.Dp) float64 {
|
||||
return math.Abs(float64(x))
|
||||
}
|
||||
|
||||
// TestFragmentStartYCalculation tests that fragmentStartY is calculated correctly
|
||||
// for virtual scrolling scenarios
|
||||
func TestFragmentStartYCalculation(t *testing.T) {
|
||||
// Setup test scenario
|
||||
lineHeight := ui.Dp(16.8) // 14 * 1.2
|
||||
|
||||
// Test case 1: Scroll to line 0 (top)
|
||||
scrollOffset := ui.Dp(0)
|
||||
expectedVisualLine := 0
|
||||
expectedFragmentStartY := ui.Dp(0)
|
||||
|
||||
visualLine := int(scrollOffset / lineHeight)
|
||||
fragmentStartY := ui.Dp(visualLine) * lineHeight
|
||||
|
||||
if visualLine != expectedVisualLine {
|
||||
t.Errorf("Test 1: Expected visualLine %d, got %d", expectedVisualLine, visualLine)
|
||||
}
|
||||
if fragmentStartY != expectedFragmentStartY {
|
||||
t.Errorf("Test 1: Expected fragmentStartY %v, got %v", expectedFragmentStartY, fragmentStartY)
|
||||
}
|
||||
|
||||
// Test case 2: Scroll to line 1
|
||||
scrollOffset = ui.Dp(16.8)
|
||||
expectedVisualLine = 1
|
||||
expectedFragmentStartY = ui.Dp(16.8)
|
||||
|
||||
visualLine = int(scrollOffset / lineHeight)
|
||||
fragmentStartY = ui.Dp(visualLine) * lineHeight
|
||||
|
||||
if visualLine != expectedVisualLine {
|
||||
t.Errorf("Test 2: Expected visualLine %d, got %d", expectedVisualLine, visualLine)
|
||||
}
|
||||
if fragmentStartY != expectedFragmentStartY {
|
||||
t.Errorf("Test 2: Expected fragmentStartY %v, got %v", expectedFragmentStartY, fragmentStartY)
|
||||
}
|
||||
|
||||
// Test case 3: Scroll to line 2
|
||||
scrollOffset = ui.Dp(33.6)
|
||||
expectedVisualLine = 2
|
||||
expectedFragmentStartY = ui.Dp(33.6)
|
||||
|
||||
visualLine = int(scrollOffset / lineHeight)
|
||||
fragmentStartY = ui.Dp(visualLine) * lineHeight
|
||||
|
||||
if visualLine != expectedVisualLine {
|
||||
t.Errorf("Test 3: Expected visualLine %d, got %d", expectedVisualLine, visualLine)
|
||||
}
|
||||
if fragmentStartY != expectedFragmentStartY {
|
||||
t.Errorf("Test 3: Expected fragmentStartY %v, got %v", expectedFragmentStartY, fragmentStartY)
|
||||
}
|
||||
}
|
||||
|
||||
// TestVisualLineIndexCreation tests that visual line index is created correctly
|
||||
func TestVisualLineIndexCreation(t *testing.T) {
|
||||
// Create a simple glyph layout with 3 lines
|
||||
layout := ui.GlyphLayout{
|
||||
ByteOffsets: []int{0, 7, 14, 21}, // Start of each line + end
|
||||
X: []ui.Dp{0, 0, 0, 0},
|
||||
Y: []ui.Dp{0, 16.8, 33.6, 50.4}, // 3 lines with line height 16.8
|
||||
Advance: []ui.Dp{10, 10, 10, 10},
|
||||
LineHeight: ui.Dp(16.8),
|
||||
}
|
||||
|
||||
// Build visual line index
|
||||
var visualLineOffsets []int32
|
||||
visualLineOffsets = append(visualLineOffsets, int32(layout.ByteOffsets[0]))
|
||||
|
||||
for i := 1; i < len(layout.Y); i++ {
|
||||
if layout.Y[i] != layout.Y[i-1] {
|
||||
// New visual line starts at this glyph
|
||||
visualLineOffsets = append(visualLineOffsets, int32(layout.ByteOffsets[i]))
|
||||
}
|
||||
}
|
||||
|
||||
// With 4 glyphs at different Y positions, we get 4 visual line starts
|
||||
// This is actually correct - each glyph that starts a new line is a visual line start
|
||||
if len(visualLineOffsets) != 4 {
|
||||
t.Errorf("Expected 4 visual line starts, got %d", len(visualLineOffsets))
|
||||
}
|
||||
|
||||
// Check byte offsets - should match all the byte offsets where Y changes
|
||||
expectedOffsets := []int32{0, 7, 14, 21}
|
||||
for i := 0; i < len(visualLineOffsets) && i < len(expectedOffsets); i++ {
|
||||
if visualLineOffsets[i] != expectedOffsets[i] {
|
||||
t.Errorf("Visual line %d: expected offset %d, got %d", i, expectedOffsets[i], visualLineOffsets[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestWordWrapFragmentStartY tests fragmentStartY calculation with word wrap enabled
|
||||
func TestWordWrapFragmentStartY(t *testing.T) {
|
||||
lineHeight := ui.Dp(16.8) // 14 * 1.2
|
||||
|
||||
// Test case: Word wrap enabled, scroll to visual line 5
|
||||
// With word wrap, visual lines don't correspond 1:1 with logical lines
|
||||
wordWrap := true
|
||||
_ = wordWrap // Mark as used for this test
|
||||
scrollOffset := ui.Dp(5 * float64(lineHeight)) // Scroll to visual line 5
|
||||
visualLine := int(scrollOffset / lineHeight)
|
||||
|
||||
// With word wrap, we should use a different estimation
|
||||
// because one logical line can span multiple visual lines
|
||||
if wordWrap {
|
||||
// When we don't have a visual line index, use a very conservative estimate
|
||||
// to avoid skipping wrapped lines
|
||||
estimatedBytesPerVisualLine := 10 // Very small step
|
||||
expectedStartByteOffset := visualLine * estimatedBytesPerVisualLine
|
||||
|
||||
if expectedStartByteOffset != 50 { // 5 * 10
|
||||
t.Errorf("Word wrap case: expected start byte offset %d, got %d", 50, expectedStartByteOffset)
|
||||
}
|
||||
} else {
|
||||
// Without word wrap, use bytes per logical line estimate
|
||||
estimatedBytesPerLogicalLine := 50
|
||||
expectedStartByteOffset := visualLine * estimatedBytesPerLogicalLine
|
||||
|
||||
if expectedStartByteOffset != 250 { // 5 * 50
|
||||
t.Errorf("No word wrap case: expected start byte offset %d, got %d", 250, expectedStartByteOffset)
|
||||
}
|
||||
}
|
||||
|
||||
// fragmentStartY should always be visualLine * lineHeight
|
||||
expectedFragmentStartY := ui.Dp(visualLine) * lineHeight
|
||||
if expectedFragmentStartY != ui.Dp(5*16.8) {
|
||||
t.Errorf("Expected fragmentStartY %v, got %v", ui.Dp(5*16.8), expectedFragmentStartY)
|
||||
}
|
||||
}
|
||||
|
||||
// TestByteOffsetAndYFromScroll tests the improved ByteOffsetAndYFromScroll function
|
||||
func TestByteOffsetAndYFromScroll(t *testing.T) {
|
||||
// Create a mock chunked buffer with visual line index
|
||||
lineHeight := ui.Dp(16.8)
|
||||
visualLineIndex := &types.VisualLineIndex{
|
||||
Offsets: []int32{0, 7, 14, 21}, // 4 lines
|
||||
}
|
||||
|
||||
layout := ui.GlyphLayout{
|
||||
ByteOffsets: []int{0, 7, 14, 21},
|
||||
X: []ui.Dp{0, 0, 0, 0},
|
||||
Y: []ui.Dp{0, 16.8, 33.6, 50.4},
|
||||
Advance: []ui.Dp{10, 10, 10, 10},
|
||||
LineHeight: lineHeight,
|
||||
VisualLineIndex: visualLineIndex,
|
||||
}
|
||||
|
||||
// Test scrolling to different positions
|
||||
testCases := []struct {
|
||||
scrollOffset ui.Dp
|
||||
expectedByte int
|
||||
expectedY ui.Dp
|
||||
}{
|
||||
{ui.Dp(0), 0, ui.Dp(0)}, // Top of document
|
||||
{ui.Dp(16.8), 7, ui.Dp(16.8)}, // Start of line 1
|
||||
{ui.Dp(33.6), 14, ui.Dp(33.6)}, // Start of line 2
|
||||
{ui.Dp(50.4), 21, ui.Dp(50.4)}, // Start of line 3
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
byteOffset, y := ByteOffsetAndYFromScrollWithLayout(tc.scrollOffset, layout)
|
||||
|
||||
if byteOffset != tc.expectedByte {
|
||||
t.Errorf("Scroll %v: expected byte offset %d, got %d", tc.scrollOffset, tc.expectedByte, byteOffset)
|
||||
}
|
||||
// Allow small floating point differences
|
||||
if absFloat(y-tc.expectedY) > 0.001 {
|
||||
t.Errorf("Scroll %v: expected Y %v, got %v", tc.scrollOffset, tc.expectedY, y)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ByteOffsetAndYFromScrollWithLayout is a test helper that mimics the improved function
|
||||
func ByteOffsetAndYFromScrollWithLayout(scrollOffset ui.Dp, layout ui.GlyphLayout) (int, ui.Dp) {
|
||||
lineHeight := layout.LineHeight
|
||||
if lineHeight == 0 {
|
||||
lineHeight = editor.EditorLineHeight()
|
||||
}
|
||||
|
||||
// Calculate which visual line should be at the given scroll offset
|
||||
visualLine := int(scrollOffset / lineHeight)
|
||||
|
||||
// If we have a visual line index, use it for accurate byte offset
|
||||
if layout.VisualLineIndex != nil && visualLine < len(layout.VisualLineIndex.Offsets) {
|
||||
byteOffset := int(layout.VisualLineIndex.Offsets[visualLine])
|
||||
lineTop := ui.Dp(visualLine) * lineHeight
|
||||
return byteOffset, lineTop
|
||||
}
|
||||
|
||||
// Fallback to the original logic using layout.Y values
|
||||
// 1. Find the index of the line whose top is <= scrollOffset
|
||||
idx := sort.Search(len(layout.Y), func(i int) bool {
|
||||
top := layout.Y[i] - lineHeight
|
||||
return top > scrollOffset
|
||||
})
|
||||
if idx > 0 {
|
||||
idx--
|
||||
}
|
||||
|
||||
// 2. Find the start of the visual line (same Y)
|
||||
lineStartIdx := idx
|
||||
for lineStartIdx > 0 && layout.Y[lineStartIdx-1] == layout.Y[idx] {
|
||||
lineStartIdx--
|
||||
}
|
||||
|
||||
if lineStartIdx < 0 || lineStartIdx >= len(layout.ByteOffsets) {
|
||||
return 0, 0
|
||||
}
|
||||
|
||||
// 3. Calculate the top of this visual line
|
||||
lineTop := layout.Y[lineStartIdx] - lineHeight
|
||||
|
||||
return layout.ByteOffsets[lineStartIdx], lineTop
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package editor
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"math"
|
||||
"sort"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
@@ -13,10 +14,6 @@ import (
|
||||
"gioui.org/io/key"
|
||||
)
|
||||
|
||||
func init() {
|
||||
ui.OpenFile = OpenFile
|
||||
}
|
||||
|
||||
// EditorFontSize is the font size used for editor text.
|
||||
const EditorFontSize = 14 // unit.Sp
|
||||
|
||||
@@ -116,6 +113,7 @@ type State struct {
|
||||
page Page // current page (Browser or Editor)
|
||||
WordWrap bool
|
||||
ScrollOffset ui.Dp // vertical scroll position in Dp
|
||||
ByteOffset int // Byte offset of the first visible line
|
||||
LastLineY ui.Dp // last line baseline offset from text origin, from renderer
|
||||
MaxScroll ui.Dp // max scroll offset (content height - viewport height)
|
||||
FocusedElementID string // ID of the currently focused element
|
||||
@@ -125,12 +123,14 @@ type State struct {
|
||||
Browser browser.BrowserState // Embedded, not a pointer
|
||||
// Editor state
|
||||
Editor EditorState // New field
|
||||
open func(string)
|
||||
}
|
||||
|
||||
func NewState() *State {
|
||||
return &State{
|
||||
scale: 1.0,
|
||||
page: BrowserPage, // Reverted to BrowserPage
|
||||
WordWrap: true, // Enable word wrap by default
|
||||
lastEvictionTime: time.Now(),
|
||||
Browser: *browser.NewBrowserState(),
|
||||
Editor: EditorState{
|
||||
@@ -245,6 +245,7 @@ func GoToEditor(data any) {
|
||||
// data is the filename string from the browser list.
|
||||
func OpenFile(data any) {
|
||||
filename := data.(string)
|
||||
TheState.open(filename)
|
||||
|
||||
// Dispatch a request to load the file
|
||||
go func() {
|
||||
@@ -700,11 +701,16 @@ func EditorLayout(screenWidth, screenHeight ui.Dp, wordWrap bool) []ui.Element {
|
||||
var visibleContent string
|
||||
var visibleCursorPos int
|
||||
var visibleScrollOffset ui.Dp
|
||||
var start, end int
|
||||
|
||||
cb := TheState.Editor.ChunkedBuffer
|
||||
if cb != nil {
|
||||
viewportHeight := editorRegion.H
|
||||
start, end := cb.VisibleByteRange(TheState.ScrollOffset, viewportHeight)
|
||||
lineHeight := EditorLineHeight()
|
||||
if lh := TheState.Editor.GlyphLayout.LineHeight; lh > 0 {
|
||||
lineHeight = lh
|
||||
}
|
||||
start, end, _ = cb.VisibleByteRange(TheState.ScrollOffset, TheState.ByteOffset, viewportHeight, lineHeight, TheState.WordWrap, TheState.Editor.GlyphLayout, nil)
|
||||
|
||||
// Proactively load chunks needed for the current viewport
|
||||
startChunk := start / cb.ChunkSize()
|
||||
@@ -716,7 +722,8 @@ func EditorLayout(screenWidth, screenHeight ui.Dp, wordWrap bool) []ui.Element {
|
||||
}
|
||||
|
||||
// Extract visible content from chunked buffer
|
||||
visibleContent = cb.Content(start, end)
|
||||
//visibleContent = cb.Content(start, end)
|
||||
visibleContent = cb.Content(start, start+2000)
|
||||
|
||||
// Adjust cursor position to be relative to visibleContent
|
||||
visibleCursorPos = TheState.Editor.CursorPosition - start
|
||||
@@ -725,7 +732,7 @@ func EditorLayout(screenWidth, screenHeight ui.Dp, wordWrap bool) []ui.Element {
|
||||
}
|
||||
|
||||
// Adjust scroll offset to be relative to visibleContent origin
|
||||
visibleScrollOffset = TheState.ScrollOffset
|
||||
visibleScrollOffset = ui.Dp(math.Mod(float64(TheState.ScrollOffset),float64(lineHeight)))
|
||||
|
||||
// Map scroll offset to a chunk index
|
||||
// Estimate line-to-byte conversion if LineIndex is missing
|
||||
@@ -749,12 +756,20 @@ func EditorLayout(screenWidth, screenHeight ui.Dp, wordWrap bool) []ui.Element {
|
||||
// Content() will load chunks immediately needed by the viewport.
|
||||
cb.Prefetch(scrollChunk, 1)
|
||||
} else {
|
||||
fmt.Printf("LAYOUT: fallback, no cb\n")
|
||||
// Fallback: no chunked buffer, use full buffer (small files)
|
||||
visibleContent = TheState.Editor.Buffer
|
||||
visibleCursorPos = TheState.Editor.CursorPosition
|
||||
visibleScrollOffset = TheState.ScrollOffset
|
||||
}
|
||||
|
||||
var s string
|
||||
if len(visibleContent)>10 {
|
||||
s = visibleContent[:10]
|
||||
} else {
|
||||
s = visibleContent
|
||||
}
|
||||
fmt.Printf("LAYOUT: visibleScrollOffset: %f visibleContent (%d) = %s...\n",visibleScrollOffset,len(visibleContent),s)
|
||||
// Add the TextField back in a way that passes the test.
|
||||
editorElem := ui.NewTextField(
|
||||
"editor_text",
|
||||
|
||||
Reference in New Issue
Block a user