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).