Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
210 changes: 139 additions & 71 deletions cmd/viva/templates/markdown_index.go.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ type mdBlockSpan struct {
markerPiece int32 // Item bullet/number piece, or -1
}

// AllocatedBytes reports a conservative estimate of retained heap memory held
// by v, including backing slice capacities so callers can enforce strict
// per-document and global memory budgets.
// AllocatedBytes reports a conservative estimate of the heap bytes retained by
// v's backing slices, including spare slice capacity.
func (v *MarkdownView) AllocatedBytes() int {
total := cap(v.pieces)*int(unsafe.Sizeof(mdPiece{})) +
cap(v.runs)*int(unsafe.Sizeof(mdCodeRun{})) +
Expand Down Expand Up @@ -191,38 +190,43 @@ func (v *MarkdownView) visitVisibleRows(firstBlk, lastBlk int, clipped bool, cli
}

// offsetAtIndexed locates the rendered byte offset nearest (x, y) in local
// coordinates using the hierarchical spatial index.
// coordinates using the hierarchical spatial index. Because every block, table
// row, and visual row strictly encloses its pieces vertically, widening outward
// by increasing vertical distance and pruning once minDy*1_000_000 > bestScore
// produces the exact same result as a full linear scan over v.pieces.
func (v *MarkdownView) offsetAtIndexed(x, y float32) (int, bool) {
if len(v.blockSpans) == 0 {
return 0, false
}
// Find the block containing y, or the closest vertical neighbor(s) when y
// sits in a gap between blocks.
n := len(v.blockSpans)
bi := sort.Search(n, func(i int) bool { return v.blockSpans[i].bottom >= y })
loBlk, hiBlk := bi, bi+1
if bi >= n {
loBlk, hiBlk = n-1, n
} else if bi > 0 && y < v.blockSpans[bi].y {
// y is in the vertical gap between block bi-1 and block bi; check both.
loBlk = bi - 1
best, bestScore := -1, float32(0)

prevPlainPiece := func(pi int32) *mdPiece {
for j := pi - 1; j >= 0; j-- {
if v.pieces[j].plainLo >= 0 {
return &v.pieces[j]
}
}
return nil
}

best, bestScore := -1, float32(0)
checkRow := func(row *mdRowSpan) (int, bool) {
var previous *mdPiece
for pi := row.pieceLo; pi < row.pieceHi; pi++ {
piece := &v.pieces[pi]
if piece.plainLo < 0 {
continue
}
if previous != nil && previous.y == piece.y && previous.plainHi < piece.plainLo && y >= piece.y && y <= piece.y+piece.h {
start := previous.x + v.caretAdvance(previous, int(previous.plainHi-previous.plainLo))
prev := previous
if prev == nil && y >= piece.y && y <= piece.y+piece.h {
prev = prevPlainPiece(pi)
}
if prev != nil && prev.y == piece.y && prev.plainHi < piece.plainLo && y >= piece.y && y <= piece.y+piece.h {
start := prev.x + v.caretAdvance(prev, int(prev.plainHi-prev.plainLo))
end := piece.x + v.caretAdvance(piece, 0)
if end > start && x >= start && x <= end {
gap := int(piece.plainLo - previous.plainHi)
gap := int(piece.plainLo - prev.plainHi)
position := int(float32(gap)*(x-start)/(end-start) + 0.5)
return gfx.GraphemeStart(unsafe.String(unsafe.SliceData(v.plain), len(v.plain)), int(previous.plainHi)+min(max(position, 0), gap)), true
return gfx.GraphemeStart(unsafe.String(unsafe.SliceData(v.plain), len(v.plain)), int(prev.plainHi)+min(max(position, 0), gap)), true
}
}
previous = piece
Expand All @@ -238,90 +242,154 @@ func (v *MarkdownView) offsetAtIndexed(x, y float32) (int, bool) {
dy = y - piece.y - piece.h
}
score := dy*1_000_000 + dx
if best < 0 || score < bestScore {
if best < 0 || score < bestScore || (score == bestScore && int(pi) < best) {
best, bestScore = int(pi), score
}
}
return 0, false
}

spanDist := func(top, bottom float32) float32 {
if y < top {
return top - y
}
if y > bottom {
return y - bottom
}
return 0
}

spanCouldBeat := func(top, bottom float32) bool {
return best < 0 || spanDist(top, bottom)*1_000_000 <= bestScore
}

// checkRowsNearY searches rows sorted by y around the pointer, widening
// within the slice before returning so empty rows (such as blank lines
// inside a code block) resolve against other rows of the same cell/block.
checkRowsNearY := func(rows []mdRowSpan) (int, bool) {
rn := len(rows)
if rn == 0 {
return 0, false
}
ri := sort.Search(rn, func(i int) bool { return rows[i].bottom >= y })
rLo, rHi := ri, ri+1
if ri >= rn {
rLo, rHi = rn-1, rn
} else if ri > 0 && y < rows[ri].y {
rLo = ri - 1
}
for k := rLo; k < rHi; k++ {
if off, hit := checkRow(&rows[k]); hit {
up := ri - 1
down := ri
if down < rn && y >= rows[down].y && y <= rows[down].bottom {
if off, hit := checkRow(&rows[down]); hit {
return off, true
}
down++
}
for up >= 0 || down < rn {
upOK := up >= 0 && spanCouldBeat(rows[up].y, rows[up].bottom)
downOK := down < rn && spanCouldBeat(rows[down].y, rows[down].bottom)
if !upOK && !downOK {
break
}
if upOK && (!downOK || spanDist(rows[up].y, rows[up].bottom) <= spanDist(rows[down].y, rows[down].bottom)) {
if off, hit := checkRow(&rows[up]); hit {
return off, true
}
up--
} else {
if off, hit := checkRow(&rows[down]); hit {
return off, true
}
down++
}
}
return 0, false
}

// Expand outward if the nearest block(s) had no selectable text (e.g. a Rule).
for expand := 0; expand < n; expand++ {
if expand > 0 {
foundAny := false
if loBlk > 0 {
loBlk--
foundAny = true
}
if hiBlk < n {
hiBlk++
foundAny = true
}
if !foundAny {
break
checkTableRow := func(tr *mdTableRowSpan) (int, bool) {
for ci := tr.cellLo; ci < tr.cellHi; ci++ {
cell := &v.cellSpans[ci]
if off, hit := checkRowsNearY(v.rowSpans[cell.rowLo:cell.rowHi]); hit {
return off, true
}
}
for idx := loBlk; idx < hiBlk; idx++ {
blk := &v.blockSpans[idx]
switch blk.kind {
case markdown.Table:
tRows := v.tableRows[blk.tableRowLo:blk.tableRowHi]
trn := len(tRows)
if trn == 0 {
continue
return 0, false
}

checkBlock := func(blk *mdBlockSpan) (int, bool) {
switch blk.kind {
case markdown.Table:
tRows := v.tableRows[blk.tableRowLo:blk.tableRowHi]
trn := len(tRows)
if trn == 0 {
return 0, false
}
tri := sort.Search(trn, func(i int) bool { return tRows[i].bottom >= y })
up := tri - 1
down := tri
if down < trn && y >= tRows[down].y && y <= tRows[down].bottom {
if off, hit := checkTableRow(&tRows[down]); hit {
return off, true
}
tri := sort.Search(trn, func(i int) bool { return tRows[i].bottom >= y })
tLo, tHi := tri, tri+1
if tri >= trn {
tLo, tHi = trn-1, trn
} else if tri > 0 && y < tRows[tri].y {
tLo = tri - 1
down++
}
for up >= 0 || down < trn {
upOK := up >= 0 && spanCouldBeat(tRows[up].y, tRows[up].bottom)
downOK := down < trn && spanCouldBeat(tRows[down].y, tRows[down].bottom)
if !upOK && !downOK {
break
}
for k := tLo; k < tHi; k++ {
tr := &tRows[k]
for ci := tr.cellLo; ci < tr.cellHi; ci++ {
cell := &v.cellSpans[ci]
if off, hit := checkRowsNearY(v.rowSpans[cell.rowLo:cell.rowHi]); hit {
return off, true
}
if upOK && (!downOK || spanDist(tRows[up].y, tRows[up].bottom) <= spanDist(tRows[down].y, tRows[down].bottom)) {
if off, hit := checkTableRow(&tRows[up]); hit {
return off, true
}
}
case markdown.Labeled:
for ci := blk.cellLo; ci < blk.cellHi; ci++ {
cell := &v.cellSpans[ci]
if off, hit := checkRowsNearY(v.rowSpans[cell.rowLo:cell.rowHi]); hit {
up--
} else {
if off, hit := checkTableRow(&tRows[down]); hit {
return off, true
}
down++
}
default:
if off, hit := checkRowsNearY(v.rowSpans[blk.rowLo:blk.rowHi]); hit {
}
case markdown.Labeled:
for ci := blk.cellLo; ci < blk.cellHi; ci++ {
cell := &v.cellSpans[ci]
if off, hit := checkRowsNearY(v.rowSpans[cell.rowLo:cell.rowHi]); hit {
return off, true
}
}
default:
if off, hit := checkRowsNearY(v.rowSpans[blk.rowLo:blk.rowHi]); hit {
return off, true
}
}
return 0, false
}

// Search the block covering y first, then widen outward by increasing
// vertical distance while any neighbour block could still beat bestScore.
n := len(v.blockSpans)
bi := sort.Search(n, func(i int) bool { return v.blockSpans[i].bottom >= y })
up := bi - 1
down := bi
if down < n && y >= v.blockSpans[down].y && y <= v.blockSpans[down].bottom {
if off, hit := checkBlock(&v.blockSpans[down]); hit {
return off, true
}
if best >= 0 {
down++
}
for up >= 0 || down < n {
upOK := up >= 0 && spanCouldBeat(v.blockSpans[up].y, v.blockSpans[up].bottom)
downOK := down < n && spanCouldBeat(v.blockSpans[down].y, v.blockSpans[down].bottom)
if !upOK && !downOK {
break
}
if upOK && (!downOK || spanDist(v.blockSpans[up].y, v.blockSpans[up].bottom) <= spanDist(v.blockSpans[down].y, v.blockSpans[down].bottom)) {
if off, hit := checkBlock(&v.blockSpans[up]); hit {
return off, true
}
up--
} else {
if off, hit := checkBlock(&v.blockSpans[down]); hit {
return off, true
}
down++
}
}

if best < 0 {
Expand Down
3 changes: 3 additions & 0 deletions cmd/viva/templates/tree.go.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ func (tree *Tree) Item(item TreeItem) TreeRow {
hovered := enabled && tree.canvas.Hovered() == item.ID
focused := enabled && !item.Dragging && tree.canvas.FocusVisible() == item.ID
radius := tree.theme.Metrics.RadiusControl
if item.Background.Alpha > 0 {
tree.canvas.Fill(row, item.Background)
}
if item.Selected || hovered {
background := tree.theme.Muted
if item.Selected {
Expand Down
Loading
Loading