Highlight search matches in the text view
- ui: TextField gains MatchRanges (window-relative byte ranges) + CurrentMatch; drawWrappedText draws a per-glyph highlight pass (same tiling as the selection, so wrapped lines are covered): all matches in translucent yellow, the selection in blue as before, and the current match in stronger orange on top so it stands out. - editor: EditorLayout windows the absolute FindState matches into the visible content (binary search on the sorted ranges, clamp to the window) and stamps them on the editor TextField. - e2e: poll the captured frames until the highlight data lands; assert all matches are windowed, each highlights a "needle", and CurrentMatch follows FindNext.
This commit is contained in:
@@ -192,6 +192,13 @@ type TextField struct {
|
||||
// in-app highlight.
|
||||
SelectionStart int
|
||||
SelectionEnd int
|
||||
// MatchRanges are byte ranges [start,end) into Value (the visible
|
||||
// window) of in-file search matches; the renderer draws them as yellow
|
||||
// highlights. CurrentMatch is the index into MatchRanges of the match the
|
||||
// user has navigated to (it is also the active selection); it gets the
|
||||
// stronger highlight so it stands out among the others. -1 = none.
|
||||
MatchRanges [][2]int
|
||||
CurrentMatch int
|
||||
// CaretDrag is set after a long press on blank space: a single caret
|
||||
// handle is shown and can be dragged to move the caret.
|
||||
CaretDrag bool
|
||||
@@ -330,7 +337,7 @@ func (tf TextField) Draw(gtx layout.Context, r *Renderer) {
|
||||
r.lastSelCaret = -1
|
||||
r.lastIMEShowSeq = 0
|
||||
}
|
||||
r.drawWrappedText(gtx, tf.Value, tf.region, tf.WordWrap, tf.WrapWidth, tf.ScrollOffset, tf.CursorPosition, tf.SelectionStart, tf.SelectionEnd, tf.CaretDrag)
|
||||
r.drawWrappedText(gtx, tf.Value, tf.region, tf.WordWrap, tf.WrapWidth, tf.ScrollOffset, tf.CursorPosition, tf.SelectionStart, tf.SelectionEnd, tf.CaretDrag, tf.MatchRanges, tf.CurrentMatch)
|
||||
}
|
||||
|
||||
// runeCount returns the number of UTF-8 runes in s[:bytePos] (bytePos is a
|
||||
|
||||
+46
-24
@@ -750,7 +750,34 @@ func (r *Renderer) drawLine(gtx layout.Context, line []text.Glyph, x, y Dp, col
|
||||
// detection via WrapHeuristically. Long words overflow the wrap width.
|
||||
// Line spacing is fixed: LineHeight = fontSize × LineHeightScale, independent
|
||||
// of glyph metrics. The shaper's first.Y accounts for line spacing.
|
||||
func (r *Renderer) drawWrappedText(gtx layout.Context, str string, reg Region, wordWrap bool, wrapWidth Dp, scrollOffset Dp, cursorPos, selStart, selEnd int, caretDrag bool) {
|
||||
// drawRangeHighlight paints one translucent rect per glyph covered by the
|
||||
// window-relative byte range [start,end), the same per-glyph tiling the
|
||||
// selection highlight uses, so wrapped lines are covered too.
|
||||
func (r *Renderer) drawRangeHighlight(gtx layout.Context, layout *GlyphLayout, str string, reg Region, scrollOffset Dp, start, end int, ascent, lineH Dp, c color.NRGBA) {
|
||||
for i := range layout.ByteOffsets {
|
||||
b0 := layout.ByteOffsets[i]
|
||||
b1 := len(str)
|
||||
if i+1 < len(layout.ByteOffsets) {
|
||||
b1 = layout.ByteOffsets[i+1]
|
||||
}
|
||||
if b0 >= end || b1 <= start {
|
||||
continue
|
||||
}
|
||||
hx := reg.X + layout.X[i]
|
||||
hy := reg.Y - scrollOffset + layout.Y[i] - ascent
|
||||
hw := layout.Advance[i]
|
||||
hh := lineH
|
||||
rect := clip.Rect{
|
||||
Min: image.Point{X: int(r.toPx(hx)), Y: int(r.toPx(hy))},
|
||||
Max: image.Point{X: int(r.toPx(hx + hw)), Y: int(r.toPx(hy + hh))},
|
||||
}.Op().Push(gtx.Ops)
|
||||
paint.ColorOp{Color: c}.Add(gtx.Ops)
|
||||
paint.PaintOp{}.Add(gtx.Ops)
|
||||
rect.Pop()
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Renderer) drawWrappedText(gtx layout.Context, str string, reg Region, wordWrap bool, wrapWidth Dp, scrollOffset Dp, cursorPos, selStart, selEnd int, caretDrag bool, matchRanges [][2]int, currentMatch int) {
|
||||
if str == "" {
|
||||
return
|
||||
}
|
||||
@@ -857,31 +884,26 @@ func (r *Renderer) drawWrappedText(gtx layout.Context, str string, reg Region, w
|
||||
lineCount++
|
||||
}
|
||||
|
||||
// Pass 2: selection highlight (translucent blue), one rect per covered
|
||||
// glyph, emitted before the text so glyphs draw on top of it.
|
||||
if selStart >= 0 && selEnd > selStart {
|
||||
for i := range layout.ByteOffsets {
|
||||
b0 := layout.ByteOffsets[i]
|
||||
b1 := len(str)
|
||||
if i+1 < len(layout.ByteOffsets) {
|
||||
b1 = layout.ByteOffsets[i+1]
|
||||
}
|
||||
if b0 >= selEnd || b1 <= selStart {
|
||||
continue
|
||||
}
|
||||
hx := reg.X + layout.X[i]
|
||||
hy := reg.Y - scrollOffset + layout.Y[i] - ascent
|
||||
hw := layout.Advance[i]
|
||||
hh := lineH
|
||||
rect := clip.Rect{
|
||||
Min: image.Point{X: int(r.toPx(hx)), Y: int(r.toPx(hy))},
|
||||
Max: image.Point{X: int(r.toPx(hx + hw)), Y: int(r.toPx(hy + hh))},
|
||||
}.Op().Push(gtx.Ops)
|
||||
paint.ColorOp{Color: color.NRGBA{R: 0x33, G: 0x99, B: 0xFF, A: 0x59}}.Add(gtx.Ops)
|
||||
paint.PaintOp{}.Add(gtx.Ops)
|
||||
rect.Pop()
|
||||
// Pass 2: highlights, emitted before the text so glyphs draw on top of
|
||||
// them. In-file search matches first (translucent yellow), then the
|
||||
// selection (translucent blue), then the CURRENT search match again in a
|
||||
// stronger orange so it stands out even though the selection also covers
|
||||
// it.
|
||||
for i, m := range matchRanges {
|
||||
if i != currentMatch {
|
||||
r.drawRangeHighlight(gtx, &layout, str, reg, scrollOffset, m[0], m[1], ascent, lineH,
|
||||
color.NRGBA{R: 0xFF, G: 0xE2, B: 0x4D, A: 0x66})
|
||||
}
|
||||
}
|
||||
if selStart >= 0 && selEnd > selStart {
|
||||
r.drawRangeHighlight(gtx, &layout, str, reg, scrollOffset, selStart, selEnd, ascent, lineH,
|
||||
color.NRGBA{R: 0x33, G: 0x99, B: 0xFF, A: 0x59})
|
||||
}
|
||||
if currentMatch >= 0 && currentMatch < len(matchRanges) {
|
||||
m := matchRanges[currentMatch]
|
||||
r.drawRangeHighlight(gtx, &layout, str, reg, scrollOffset, m[0], m[1], ascent, lineH,
|
||||
color.NRGBA{R: 0xFF, G: 0x98, B: 0x00, A: 0x80})
|
||||
}
|
||||
|
||||
// Pass 3: the text itself.
|
||||
m := op.Record(gtx.Ops)
|
||||
|
||||
Reference in New Issue
Block a user