Fix cursor rendering lag and implement Enter key newline insertion
This commit is contained in:
@@ -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
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user