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:
+41
-2
@@ -10,6 +10,7 @@ import (
|
||||
"gioui.org/text"
|
||||
"gioui.org/unit"
|
||||
"gioui.org/font/gofont"
|
||||
"gioui.org/io/key"
|
||||
|
||||
"pad/internal/editor"
|
||||
"pad/internal/ui"
|
||||
@@ -71,11 +72,49 @@ func run(w *app.Window) error {
|
||||
if newQuery != logic.State().Browser.Query {
|
||||
sendQuery = true
|
||||
}
|
||||
if events := renderer.CheckGestures(e.Source, gtx.Metric); len(events) > 0 {
|
||||
logic.InputChan() <- events
|
||||
events := renderer.CheckGestures(e.Source, gtx.Metric)
|
||||
|
||||
// Gather key events
|
||||
focusedID := logic.State().FocusedElementID
|
||||
log.Printf("focusedID = %s", focusedID)
|
||||
if focusedID != "" {
|
||||
if reg, ok := renderer.Keys[focusedID]; ok {
|
||||
log.Printf("looking for events")
|
||||
// Use key.Filter to only receive events destined for the focused element.
|
||||
// We need both Key events (for arrow keys) and Edit events (for text input).
|
||||
// In Gio, key.Filter covers key presses, while key.FocusFilter covers
|
||||
// focus and text edit events.
|
||||
for {
|
||||
// Filter for key events and focus/edit events targeted at focusedID
|
||||
evt, ok := gtx.Event(key.Filter{Focus: focusedID}, key.FocusFilter{Target: focusedID})
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
log.Printf("found an event: %T", evt)
|
||||
switch k := evt.(type) {
|
||||
case key.Event:
|
||||
log.Printf("key event: %v state=%v", k.Name, k.State)
|
||||
if k.State == key.Press {
|
||||
events = append(events, ui.InputEvent{
|
||||
Handler: reg.Handler,
|
||||
Data: k.Name,
|
||||
})
|
||||
}
|
||||
case key.EditEvent:
|
||||
log.Printf("edit event: %v", k.Text)
|
||||
events = append(events, ui.InputEvent{
|
||||
Handler: reg.Handler,
|
||||
Data: k,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
e.Frame(&ops)
|
||||
mu.Unlock()
|
||||
if len(events) > 0 {
|
||||
logic.InputChan() <- events
|
||||
}
|
||||
if sendQuery {
|
||||
logic.SearchQueryChan() <- newQuery
|
||||
}
|
||||
|
||||
BIN
Binary file not shown.
Reference in New Issue
Block a user