Fix cursor rendering lag and implement Enter key newline insertion

This commit is contained in:
gmp
2026-06-03 20:19:35 -04:00
parent 3f53836ade
commit 62d1f827e0
4 changed files with 52 additions and 59 deletions
+5 -42
View File
@@ -209,7 +209,7 @@ func HandleKeyDown(data any) {
log.Printf("HandleKeyDown: EditEvent text=%q", v.Text)
HandleInsert(v.Text)
case key.Name:
log.Printf("HandleKeyDown: %s", v)
log.Printf("HandleKeyDown: name=%q", v)
switch v {
case key.NameLeftArrow:
HandleCursorMove(-1)
@@ -223,6 +223,8 @@ func HandleKeyDown(data any) {
HandleBackspace()
case key.NameDeleteForward:
HandleDelete()
case key.NameReturn:
HandleInsert("\n")
}
}
}
@@ -425,6 +427,7 @@ func EditorLayout(screenWidth, screenHeight ui.Dp, wordWrap bool) []ui.Element {
editorRegion,
editorRegion.W,
TheState.ScrollOffset,
TheState.Editor.CursorPosition,
[]ui.Interaction{
{Gesture: ui.Scroll, Handler: HandleScroll},
{Gesture: ui.KeyDown, Handler: HandleKeyDown},
@@ -434,47 +437,7 @@ func EditorLayout(screenWidth, screenHeight ui.Dp, wordWrap bool) []ui.Element {
// for Gio to deliver key events to this element.
editorElem.Focused = TheState.FocusedElementID == "editor_text"
// Position cursor from GlyphLayout.
layout := TheState.Editor.GlyphLayout
pos := TheState.Editor.CursorPosition
lineHeight := EditorLineHeight()
var cursorX, cursorY ui.Dp
if len(layout.ByteOffsets) == 0 {
// Empty buffer — place cursor at the top-left of the editor region.
cursorX = editorRegion.X
cursorY = editorRegion.Y
} else {
// Binary search for the glyph at or after the cursor byte offset.
idx := sort.Search(len(layout.ByteOffsets), func(i int) bool {
return layout.ByteOffsets[i] >= pos
})
if idx < len(layout.ByteOffsets) {
// Cursor is at a glyph position.
cursorX = editorRegion.X + layout.X[idx]
cursorY = editorRegion.Y - TheState.ScrollOffset + layout.Y[idx] - EditorFontSize
} else {
// Cursor is at end of buffer — place after last glyph.
cursorX = editorRegion.X + layout.X[len(layout.X)-1] + layout.Advance[len(layout.Advance)-1]
cursorY = editorRegion.Y - TheState.ScrollOffset + layout.Y[len(layout.Y)-1] - EditorFontSize
}
}
// Derive line/col for bottom bar display (still using byte-based
// approximation until GlyphLayout-based line tracking is added).
line, col := byteOffsetToLineCol(TheState.Editor.Buffer, TheState.Editor.CursorPosition)
// Create the cursor element at the computed position.
// Ensure the cursor element knows its bounds for clipping.
cursorElem := ui.NewCursor(
"editor_cursor",
ui.Region{X: cursorX, Y: cursorY, W: ui.Dp(2), H: lineHeight},
line, col,
true,
)
cursorElem.ClipRegion = editorRegion
return []ui.Element{statusBar, editorElem, cursorElem, bottomBar}
return []ui.Element{statusBar, editorElem, bottomBar}
}
// byteOffsetToLineCol converts a byte offset in the buffer to (line, column).
+4 -2
View File
@@ -187,6 +187,7 @@ type TextField struct {
Placeholder string
Focused bool
Multiline bool
CursorPosition int
ScrollOffset Dp
VisibleLines []Line
WordWrap bool
@@ -212,11 +213,11 @@ func (tf TextField) Draw(gtx layout.Context, r *Renderer) {
fmt.Printf("Focused: tag = %s\n", tf.id)
gtx.Execute(key.FocusCmd{Tag: tf.id})
}
r.drawWrappedText(gtx, tf.Value, tf.region, tf.WrapWidth, tf.ScrollOffset)
r.drawWrappedText(gtx, tf.Value, tf.region, tf.WrapWidth, tf.ScrollOffset, tf.CursorPosition)
}
// NewTextField creates a visible multiline TextField.
func NewTextField(id string, value string, region Region, wrapWidth Dp, scrollOffset Dp, interactions []Interaction) TextField {
func NewTextField(id string, value string, region Region, wrapWidth Dp, scrollOffset Dp, cursorPos int, interactions []Interaction) TextField {
return TextField{
id: id,
region: region,
@@ -227,6 +228,7 @@ func NewTextField(id string, value string, region Region, wrapWidth Dp, scrollOf
WordWrap: true,
WrapWidth: wrapWidth,
ScrollOffset: scrollOffset,
CursorPosition: cursorPos,
}
}
+29 -1
View File
@@ -7,6 +7,7 @@ import (
"image/color"
_ "image/png"
"log"
"sort"
"time"
"unicode/utf8"
@@ -455,7 +456,7 @@ 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, wrapWidth Dp, scrollOffset Dp) {
func (r *Renderer) drawWrappedText(gtx layout.Context, str string, reg Region, wrapWidth Dp, scrollOffset Dp, cursorPos int) {
if str == "" {
return
}
@@ -525,6 +526,33 @@ func (r *Renderer) drawWrappedText(gtx layout.Context, str string, reg Region, w
call := m.Stop()
call.Add(gtx.Ops)
// Determine cursor position from `layout` and `cursorPos`
var cursorX, cursorY Dp
if len(layout.ByteOffsets) == 0 {
cursorX = reg.X
cursorY = reg.Y
} else {
idx := sort.Search(len(layout.ByteOffsets), func(i int) bool {
return layout.ByteOffsets[i] >= cursorPos
})
if idx < len(layout.ByteOffsets) {
cursorX = reg.X + layout.X[idx]
cursorY = reg.Y - scrollOffset + layout.Y[idx] - Dp(r.theme.FontSize)
} else {
cursorX = reg.X + layout.X[len(layout.X)-1] + layout.Advance[len(layout.Advance)-1]
cursorY = reg.Y - scrollOffset + layout.Y[len(layout.Y)-1] - Dp(r.theme.FontSize)
}
}
// Draw the cursor (thin vertical bar)
cursorRegion := Region{
X: cursorX,
Y: cursorY,
W: Dp(2),
H: Dp(r.theme.FontSize) * 1.2, // Use line height
}
r.drawBg(gtx, cursorRegion, Color{R: 0, G: 0, B: 0, A: 255})
textClip.Pop()
r.displayLineCount = lineCount
// Store captured layout; derive lastLineY from it.