fix(editor): wire key events to cursor movement

- Replace gtx.Event(nil) with key.Filter{Focus: focusedID} and
  key.FocusFilter{Target: focusedID} so Gio correctly routes key
  and edit events to the focused editor text field.

- Convert EditorState.CursorPosition (byte offset) to line/column
  coordinates for accurate cursor rendering.

- Add debug logging in HandleKeyDown and HandleCursorMove.
This commit is contained in:
gmp
2026-06-03 07:41:18 -04:00
parent a17c3760f2
commit 0dac354f2a
13 changed files with 467 additions and 77 deletions
+42 -18
View File
@@ -13,6 +13,7 @@ import (
"gioui.org/op/paint"
"gioui.org/unit"
"gioui.org/widget"
"gioui.org/io/key"
)
// Region defines a screen area in device-independent pixels (Dp).
@@ -34,6 +35,7 @@ type Element interface {
Visible() bool
Draw(gtx layout.Context, r *Renderer)
String() string
Type() string
}
// Container holds child elements and draws them within its bounds.
@@ -46,6 +48,7 @@ type Container struct {
children []Element
}
func (c Container) Type() string { return "container" }
func (c Container) Region() Region { return c.region }
func (c Container) Visible() bool { return c.visible }
func (c Container) Draw(gtx layout.Context, r *Renderer) {
@@ -94,6 +97,7 @@ type Label struct {
Bold bool
}
func (l Label) Type() string { return "label" }
func (l Label) Region() Region { return l.region }
func (l Label) Visible() bool { return l.visible }
func (l Label) Interactions() []Interaction { return l.interactions }
@@ -136,6 +140,7 @@ type Icon struct {
Size Dp // 0 = default icon size
}
func (i Icon) Type() string { return "icon" }
func (i Icon) Region() Region { return i.region }
func (i Icon) Visible() bool { return i.visible }
func (i Icon) Interactions() []Interaction { return i.interactions }
@@ -188,10 +193,12 @@ type TextField struct {
WrapWidth Dp
}
func (tf TextField) Type() string { return "textfield" }
func (tf TextField) Region() Region { return tf.region }
func (tf TextField) Visible() bool { return tf.visible }
func (tf TextField) ID() string { return tf.id }
func (tf TextField) Interactions() []Interaction { return tf.interactions }
func (tf TextField) NeedsClip() bool { return true }
// String returns a string representation of the TextField.
func (tf TextField) String() string {
@@ -201,6 +208,10 @@ func (tf TextField) String() string {
// Draw renders the TextField. For multiline text, it shapes with word wrap
// and draws display lines inline — one LayoutString call, no double-shaping.
func (tf TextField) Draw(gtx layout.Context, r *Renderer) {
if tf.Focused {
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)
}
@@ -237,6 +248,7 @@ type ListView struct {
RowTapHandler func(any) // handler for row taps, receives index as any
}
func (lv ListView) Type() string { return "listview" }
func (lv ListView) Region() Region { return lv.region }
func (lv ListView) Visible() bool { return lv.visible }
func (lv ListView) ID() string { return lv.id }
@@ -372,6 +384,7 @@ type AlphaIndex struct {
ActiveLetter string
}
func (ai AlphaIndex) Type() string { return "alphaindex" }
func (ai AlphaIndex) Region() Region { return ai.region }
func (ai AlphaIndex) Visible() bool { return ai.visible }
func (ai AlphaIndex) ID() string { return ai.id }
@@ -382,24 +395,6 @@ func (ai AlphaIndex) String() string {
return fmt.Sprintf("AlphaIndex[%s] region=%+v letters=%v active=%q", ai.id, ai.region, ai.Letters, ai.ActiveLetter)
}
// Draw renders letters vertically along the right edge.
func (ai AlphaIndex) Draw(gtx layout.Context, r *Renderer) {
letterHeight := ai.region.H / Dp(len(ai.Letters))
for i, letter := range ai.Letters {
region := Region{
X: ai.region.X,
Y: ai.region.Y + Dp(i)*letterHeight,
W: ai.region.W,
H: letterHeight,
}
col := Color{R: 100, G: 100, B: 100, A: 255}
if letter == ai.ActiveLetter {
col = Color{R: 0, G: 100, B: 200, A: 255}
}
r.drawText(gtx, letter, 10, region, AlignCenter, col, "")
}
}
// NewAlphaIndex creates a visible AlphaIndex element.
func NewAlphaIndex(region Region, letters []string) AlphaIndex {
return AlphaIndex{
@@ -420,6 +415,7 @@ type Button struct {
Primary bool
}
func (b Button) Type() string { return "button" }
func (b Button) Region() Region { return b.region }
func (b Button) Visible() bool { return b.visible }
func (b Button) Interactions() []Interaction { return b.interactions }
@@ -457,6 +453,7 @@ type SearchBar struct {
Forward bool
}
func (sb SearchBar) Type() string { return "searchbar" }
func (sb SearchBar) Region() Region { return sb.region }
func (sb SearchBar) Visible() bool { return sb.visible }
func (sb SearchBar) ID() string { return sb.id }
@@ -491,6 +488,7 @@ type Cursor struct {
Selection *Selection
}
func (c Cursor) Type() string { return "cursor" }
func (c Cursor) Region() Region { return c.region }
func (c Cursor) Visible() bool { return c.visible }
func (c Cursor) ID() string { return c.id }
@@ -500,6 +498,28 @@ func (c Cursor) Interactions() []Interaction { return c.interactions }
func (c Cursor) String() string {
return fmt.Sprintf("Cursor[%s] region=%+v line=%d col=%d", c.id, c.region, c.Line, c.Column)
}
func (c Cursor) Draw(gtx layout.Context, r *Renderer) {
// Draw a thin vertical bar (e.g., width 2dp, height 18dp) at the cursor's top-left position
// instead of filling the entire region, which covers the text editor.
cursorRegion := Region{
X: c.region.X,
Y: c.region.Y,
W: Dp(2),
H: Dp(18),
}
r.drawBg(gtx, cursorRegion, Color{R: 0, G: 0, B: 0, A: 255})
}
func NewCursor(id string, region Region, line, col int, blinking bool) Cursor {
return Cursor{
id: id,
region: region,
visible: true,
Line: line,
Column: col,
Blinking: blinking,
}
}
// Selection represents a text selection range.
type Selection struct {
@@ -522,6 +542,7 @@ type MergeHunk struct {
Resolution HunkResolution
}
func (mh MergeHunk) Type() string { return "mergehunk" }
func (mh MergeHunk) Region() Region { return mh.region }
func (mh MergeHunk) Visible() bool { return mh.visible }
func (mh MergeHunk) ID() string { return mh.id }
@@ -552,6 +573,7 @@ type Toast struct {
Timeout int // milliseconds
}
func (t Toast) Type() string { return "toast" }
func (t Toast) Region() Region { return t.region }
func (t Toast) Visible() bool { return t.visible }
func (t Toast) ID() string { return t.id }
@@ -580,6 +602,7 @@ type Spacer struct {
interactions []Interaction
}
func (s Spacer) Type() string { return "spacer" }
func (s Spacer) Region() Region { return s.region }
func (s Spacer) Visible() bool { return s.visible }
func (s Spacer) ID() string { return s.id }
@@ -607,6 +630,7 @@ type GioEditor struct {
Editor *widget.Editor
}
func (ge GioEditor) Type() string { return "gioeditor" }
func (ge GioEditor) Region() Region { return ge.region }
func (ge GioEditor) Visible() bool { return ge.visible }
func (ge GioEditor) ID() string { return ge.id }
+47 -1
View File
@@ -6,10 +6,12 @@ import (
"image"
"image/color"
_ "image/png"
"log"
"time"
"gioui.org/f32"
"gioui.org/gesture"
"gioui.org/io/event" // Import event package
"gioui.org/io/input"
"gioui.org/io/pointer"
"gioui.org/layout"
@@ -38,6 +40,11 @@ type clickReg struct {
handler func(any)
}
// keyReg pairs a handler for key events.
type keyReg struct {
Handler func(any)
}
// scrollReg pairs a gesture.Scroll with its handler.
type scrollReg struct {
scroll *gesture.Scroll
@@ -51,6 +58,7 @@ type Renderer struct {
scale ScaleProvider
icons map[string]image.Image
clicks map[string]clickReg
Keys map[string]keyReg // Exported Keys map
scrolls map[string]scrollReg
displayLineCount int // number of display lines from last drawWrappedText
lastLineY Dp // last line baseline offset from text origin, in Dp
@@ -64,6 +72,7 @@ func New(th Theme, shp *text.Shaper, scale ScaleProvider) *Renderer {
scale: scale,
icons: make(map[string]image.Image),
clicks: make(map[string]clickReg),
Keys: make(map[string]keyReg),
scrolls: make(map[string]scrollReg),
}
r.loadIcons()
@@ -243,17 +252,54 @@ func (r *Renderer) drawElement(gtx layout.Context, e Element) {
offset.Pop()
clipRect.Pop()
} else if clippable, ok := e.(clippableElement); ok && clippable.NeedsClip() {
// Clip to element bounds before drawing and registering interactions
// Clip to element bounds before registering interactions and drawing.
// event.Op for key events must be within the clip so Gio routes events
// to this element's tag.
clipRect := clip.Rect{
Min: image.Point{X: int(r.toPx(reg.X)), Y: int(r.toPx(reg.Y))},
Max: image.Point{X: int(r.toPx(reg.X + reg.W)), Y: int(r.toPx(reg.Y + reg.H))},
}.Push(gtx.Ops)
// Register interactions inside the clip so event.Op is scoped to this region.
if interactive, ok := e.(Interactive); ok {
for _, interaction := range interactive.Interactions() {
if interaction.Gesture == KeyDown || interaction.Gesture == KeyUp {
log.Printf("register gesture for %s", interactive.ID())
event.Op(gtx.Ops, interactive.ID())
reg := keyReg{Handler: interaction.Handler}
r.Keys[interactive.ID()] = reg
break
}
}
for _, interaction := range interactive.Interactions() {
if interaction.Gesture == Tap {
reg, ok := r.clicks[interactive.ID()]
if !ok {
reg = clickReg{click: &gesture.Click{}}
}
reg.handler = interaction.Handler
r.clicks[interactive.ID()] = reg
}
}
}
e.Draw(gtx, r)
clipRect.Pop()
} else {
// Leaf element: register click handlers, then draw.
// For text elements, click registration happens inside Draw after shaping.
if interactive, ok := e.(Interactive); ok {
// Register input tag for key events
for _, interaction := range interactive.Interactions() {
if interaction.Gesture == KeyDown || interaction.Gesture == KeyUp {
log.Printf("register gesture for %s", interactive.ID())
event.Op(gtx.Ops, interactive.ID())
// Register handler
reg := keyReg{Handler: interaction.Handler}
r.Keys[interactive.ID()] = reg
break
}
}
for _, interaction := range interactive.Interactions() {
// Set up click handler (but don't call Add for text elements)
if interaction.Gesture == Tap {