Compute glyphLayout after text rendering. Implement correct cursor
movement.
This commit is contained in:
@@ -49,7 +49,7 @@ type Logic struct {
|
||||
configChan chan ConfigUpdate
|
||||
frameChan chan []ui.Element
|
||||
inputChan chan []ui.InputEvent
|
||||
lastLineYChan chan int // last line Y (Dp, sent as int) feedback from renderer
|
||||
layoutChan chan ui.GlyphLayout // per-glyph layout feedback from renderer (replaces lastLineYChan)
|
||||
resultChan chan ResultEvent
|
||||
searchQueryChan chan string // search text updates from main goroutine
|
||||
openFileChan chan string // Added for asynchronous file loading
|
||||
@@ -82,7 +82,7 @@ func NewLogic() *Logic {
|
||||
configChan: make(chan ConfigUpdate),
|
||||
frameChan: make(chan []ui.Element),
|
||||
inputChan: make(chan []ui.InputEvent),
|
||||
lastLineYChan: make(chan int),
|
||||
layoutChan: make(chan ui.GlyphLayout),
|
||||
resultChan: make(chan ResultEvent),
|
||||
searchQueryChan: make(chan string),
|
||||
openFileChan: make(chan string), // Initialized
|
||||
@@ -110,9 +110,9 @@ func (l *Logic) InputChan() chan<- []ui.InputEvent {
|
||||
return l.inputChan
|
||||
}
|
||||
|
||||
// DisplayLineChan returns the last line Y feedback channel.
|
||||
func (l *Logic) DisplayLineChan() chan<- int {
|
||||
return l.lastLineYChan
|
||||
// LayoutChan returns the glyph layout feedback channel.
|
||||
func (l *Logic) LayoutChan() chan<- ui.GlyphLayout {
|
||||
return l.layoutChan
|
||||
}
|
||||
|
||||
// ResultChan returns the result channel for the logic goroutine.
|
||||
@@ -148,9 +148,16 @@ func (l *Logic) Run() {
|
||||
log.Printf("Logic: ConfigEvent")
|
||||
update.apply(l.state)
|
||||
l.frameChan <- l.state.layout(l.browserManager)
|
||||
case y := <-l.lastLineYChan:
|
||||
if ui.Dp(y) != l.state.LastLineY {
|
||||
l.state.LastLineY = ui.Dp(y)
|
||||
case layout := <-l.layoutChan:
|
||||
// Store the full GlyphLayout on editor state.
|
||||
// Derive LastLineY from it for scroll clamping.
|
||||
l.state.Editor.GlyphLayout = layout
|
||||
var derivedLastLineY ui.Dp
|
||||
if len(layout.Y) > 0 {
|
||||
derivedLastLineY = layout.Y[len(layout.Y)-1]
|
||||
}
|
||||
if derivedLastLineY != l.state.LastLineY {
|
||||
l.state.LastLineY = derivedLastLineY
|
||||
l.frameChan <- l.state.layout(l.browserManager)
|
||||
}
|
||||
case events := <-l.inputChan:
|
||||
|
||||
@@ -2,6 +2,7 @@ package editor
|
||||
|
||||
import (
|
||||
"log"
|
||||
"sort"
|
||||
|
||||
"pad/internal/browser"
|
||||
"pad/internal/ui"
|
||||
@@ -45,6 +46,7 @@ const (
|
||||
type EditorState struct {
|
||||
Buffer string // Document content
|
||||
CursorPosition int // Byte offset
|
||||
GlyphLayout ui.GlyphLayout // Per-glyph layout from renderer
|
||||
SelectionStart int // -1 if no selection
|
||||
SelectionEnd int // -1 if no selection
|
||||
Dirty bool // Needs save
|
||||
@@ -342,15 +344,35 @@ 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"
|
||||
|
||||
// Convert byte offset (CursorPosition) to line/column for cursor rendering.
|
||||
line, col := byteOffsetToLineCol(TheState.Editor.Buffer, TheState.Editor.CursorPosition)
|
||||
|
||||
// Compute cursor X/Y in screen coordinates.
|
||||
// We approximate character width as EditorFontSize * 0.6 for monospace.
|
||||
charWidth := ui.Dp(float32(EditorFontSize) * 0.6)
|
||||
// Position cursor from GlyphLayout.
|
||||
layout := TheState.Editor.GlyphLayout
|
||||
pos := TheState.Editor.CursorPosition
|
||||
lineHeight := EditorLineHeight()
|
||||
cursorX := editorRegion.X + ui.Dp(col)*charWidth
|
||||
cursorY := editorRegion.Y + ui.Dp(line)*lineHeight - TheState.ScrollOffset
|
||||
|
||||
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
|
||||
cursorElem := ui.NewCursor(
|
||||
|
||||
Reference in New Issue
Block a user