Implement click-to-move cursor in the editor

This commit is contained in:
gmp
2026-06-03 22:04:16 -04:00
parent 62d1f827e0
commit bab239a38a
5 changed files with 113 additions and 23 deletions
+15 -1
View File
@@ -319,6 +319,7 @@ func (lv ListView) Draw(gtx layout.Context, r *Renderer) {
X: lv.region.X, Y: y,
W: lv.region.W, H: rowHeight,
}, func(data any) {
// ListView clicks are simple taps, not coordinate-based.
if lv.RowTapHandler != nil {
lv.RowTapHandler(rowGlobalIndex)
} else {
@@ -684,7 +685,15 @@ func (ge GioEditor) Draw(gtx layout.Context, r *Renderer) {
gtx.Constraints = layout.Exact(rect.Size())
// Use our RegisterClick to ensure the full region is clickable
r.RegisterClick(gtx, ge.id, ge.region, nil)
// NOTE: We pass the tap handler to RegisterClick which uses coordinates.
var tapHandler func(any)
for _, interaction := range ge.interactions {
if interaction.Gesture == Tap {
tapHandler = interaction.Handler
break
}
}
r.RegisterClick(gtx, ge.id, ge.region, tapHandler)
ge.Editor.Layout(gtx, r.shp, font.Font{}, r.theme.FontSize, textColor, selectionColor)
}
@@ -753,6 +762,11 @@ type Interactive interface {
Interactions() []Interaction
}
// Point defines a 2D coordinate in device-independent pixels (Dp).
type Point struct {
X, Y Dp
}
// InputEvent represents a user input event with its handler.
type InputEvent struct {
Handler func(any)
+4 -4
View File
@@ -193,13 +193,14 @@ func (r *Renderer) RegisterScroll(gtx layout.Context, id string, region Region,
// CheckGestures checks all registered gestures and returns any events.
func (r *Renderer) CheckGestures(q input.Source, m unit.Metric) []InputEvent {
var events []InputEvent
for _, reg := range r.clicks {
for id, reg := range r.clicks {
evt, ok := reg.click.Update(q)
if ok {
if evt.Kind == gesture.KindClick {
log.Printf("Renderer: Click detected on %s, pos=%v", id, evt.Position)
events = append(events, InputEvent{
Handler: reg.handler,
Data: evt,
Data: Point{X: r.toDp(Px(evt.Position.X)), Y: r.toDp(Px(evt.Position.Y))},
})
}
}
@@ -297,6 +298,7 @@ func (r *Renderer) drawElement(gtx layout.Context, e Element) {
}
reg.handler = interaction.Handler
r.clicks[interactive.ID()] = reg
reg.click.Add(gtx.Ops)
}
}
}
@@ -403,8 +405,6 @@ func (r *Renderer) drawText(gtx layout.Context, str string, size unit.Sp, reg Re
textClip.Pop()
}
// drawLineText iterates laid-out glyphs and draws each line using op.Record for clipping safety.
func (r *Renderer) drawLineText(gtx layout.Context, x, y Dp, col Color) {
m := op.Record(gtx.Ops)
var glyphs [32]text.Glyph