Let clients see mouse events before the views do

A later commit lets the user drag the boundary between the side panels
and the main view. This boundary consists of the frames of several
views, so it can't be handled by the mouse bindings of a single view.
OnMouseMove lets the client highlight the boundary when the mouse
hovers over it, and InterceptMouse lets it take over a gesture that
starts on it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Stefan HallerandClaude Opus 5.5 committed 2026-11-15 19:40:26 +01:00
1 parent 6b95a3b227
commit 7883453114
2 files changed
+165

No files matched your search

+51
View File
@@ -110,6 +110,16 @@ type ViewMouseBindingOpts struct {
IsDoubleClick bool // true if this is a double click
}
// MouseEvent is a mouse event as Gui.InterceptMouse gets to see it.
type MouseEvent struct {
// The position on the screen
X, Y int
Key KeyName
Mod Modifier
// When the terminal reported the event
When time.Time
}
type GuiMutexes struct {
ViewsMutex sync.Mutex
}
@@ -232,6 +242,16 @@ type Gui struct {
mouseCapture *View
mouseGestureCanceled bool
// OnMouseMove, if set, is called whenever the mouse moves while no button
// is held, wherever the pointer is.
OnMouseMove func(x, y int)
// InterceptMouse, if set, sees mouse button events before the views do.
// If it returns true for a press, it owns the gesture: all events up to
// and including the release go to it, and the views never see them.
InterceptMouse func(ev MouseEvent) bool
mouseIntercepted bool
// uiThreadID is the goroutine id of the main event loop, recorded when
// MainLoop starts. IsUIThread compares against it. Written once, read from
// worker goroutines, so it's atomic.
@@ -1832,6 +1852,9 @@ func (g *Gui) onKey(ev *GocuiEvent) error {
}
return nil
}
if g.interceptMouseEvent(ev) {
return nil
}
// While the mouse is captured, all mouse events go to the view that
// was under the pointer when the button was pressed, even if the
// pointer has since left it; this is what lets drag gestures keep
@@ -1974,6 +1997,9 @@ func (g *Gui) onKey(ev *GocuiEvent) error {
case eventMouseMove:
mx, my := ev.MouseX, ev.MouseY
if g.OnMouseMove != nil {
g.OnMouseMove(mx, my)
}
v, err := g.VisibleViewByPosition(mx, my)
if err != nil {
break
@@ -1991,6 +2017,31 @@ func (g *Gui) onKey(ev *GocuiEvent) error {
return nil
}
// interceptMouseEvent gives InterceptMouse a chance to handle a mouse event,
// and returns true if the views must not see it.
func (g *Gui) interceptMouseEvent(ev *GocuiEvent) bool {
intercepted := g.mouseIntercepted
if !intercepted && (g.InterceptMouse == nil || g.mouseCapture != nil) {
return false
}
handled := g.InterceptMouse(MouseEvent{
X: ev.MouseX,
Y: ev.MouseY,
Key: ev.Key.KeyName(),
Mod: ev.Key.Mod(),
When: ev.When,
})
isPress := ev.Key.Mod()&ModMotion == 0 &&
(ev.Key.KeyName() == MouseLeft || ev.Key.KeyName() == MouseRight || ev.Key.KeyName() == MouseMiddle)
if ev.Key.KeyName() == MouseRelease {
g.mouseIntercepted = false
} else if handled && isPress {
g.mouseIntercepted = true
}
return handled || intercepted
}
// isDoubleClick reports whether this click follows one just like it, closely
// enough in time to count as a double click. It goes by when the clicks were
// read. The second click may wait in the event queue while the first one is
+114
View File
@@ -0,0 +1,114 @@
package gocui
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
// setUpViewWithClickBindings creates a view that records the mouse keys of the
// bindings it receives.
func setUpViewWithClickBindings(t *testing.T, g *Gui) *[]KeyName {
t.Helper()
_, err := g.SetView("view", 10, 5, 30, 15, 0)
if err != nil && !errors.Is(err, ErrUnknownView) {
assert.NoError(t, err)
}
received := []KeyName{}
for _, binding := range []*ViewMouseBinding{
{ViewName: "view", Key: MouseLeft},
{ViewName: "view", Key: MouseLeft, Modifier: ModMotion},
{ViewName: "view", Key: MouseRelease},
{ViewName: "view", Key: MouseWheelDown},
} {
binding.Handler = func(opts ViewMouseBindingOpts) error {
received = append(received, opts.Key)
return nil
}
g.SetViewClickBinding(binding)
}
return &received
}
// mouseEvent returns a mouse event in row 7, the third row of the view
func mouseEvent(x int, key KeyName, mod Modifier) *GocuiEvent {
return &GocuiEvent{Type: eventMouse, MouseX: x, MouseY: 7, Key: NewKey(key, "", mod)}
}
func TestInterceptMouseOwnsTheGestureOfAPressItHandles(t *testing.T) {
g := newTestGui(t)
viewReceived := setUpViewWithClickBindings(t, g)
intercepted := []MouseEvent{}
g.InterceptMouse = func(ev MouseEvent) bool {
intercepted = append(intercepted, ev)
return ev.X == 10 && ev.Key == MouseLeft && ev.Mod == ModNone
}
assert.NoError(t, g.onKey(mouseEvent(10, MouseLeft, ModNone)))
assert.NoError(t, g.onKey(mouseEvent(15, MouseLeft, ModMotion)))
assert.NoError(t, g.onKey(mouseEvent(16, MouseRelease, ModNone)))
assert.Equal(t, []MouseEvent{
{X: 10, Y: 7, Key: MouseLeft, Mod: ModNone},
{X: 15, Y: 7, Key: MouseLeft, Mod: ModMotion},
{X: 16, Y: 7, Key: MouseRelease, Mod: ModNone},
}, intercepted)
assert.Empty(t, *viewReceived)
// The next gesture reaches the view if InterceptMouse doesn't handle it
assert.NoError(t, g.onKey(mouseEvent(15, MouseLeft, ModNone)))
assert.NoError(t, g.onKey(mouseEvent(15, MouseRelease, ModNone)))
assert.Equal(t, []KeyName{MouseLeft, MouseRelease}, *viewReceived)
}
func TestInterceptMouseDoesNotOwnTheGestureOfAWheelEventItHandles(t *testing.T) {
g := newTestGui(t)
viewReceived := setUpViewWithClickBindings(t, g)
g.InterceptMouse = func(ev MouseEvent) bool {
return ev.Key == MouseWheelDown
}
assert.NoError(t, g.onKey(mouseEvent(15, MouseWheelDown, ModNone)))
assert.NoError(t, g.onKey(mouseEvent(15, MouseLeft, ModNone)))
assert.Equal(t, []KeyName{MouseLeft}, *viewReceived)
}
func TestInterceptMouseIsNotAskedWhileAViewHasCapturedTheMouse(t *testing.T) {
g := newTestGui(t)
viewReceived := setUpViewWithClickBindings(t, g)
asked := false
g.InterceptMouse = func(ev MouseEvent) bool {
asked = true
return false
}
assert.NoError(t, g.onKey(mouseEvent(15, MouseLeft, ModNone)))
asked = false
g.InterceptMouse = func(ev MouseEvent) bool {
asked = true
return true
}
assert.NoError(t, g.onKey(mouseEvent(16, MouseLeft, ModMotion)))
assert.NoError(t, g.onKey(mouseEvent(16, MouseRelease, ModNone)))
assert.False(t, asked)
assert.Equal(t, []KeyName{MouseLeft, MouseLeft, MouseRelease}, *viewReceived)
}
func TestOnMouseMoveSeesMovesOutsideViews(t *testing.T) {
g := newTestGui(t)
setUpViewWithClickBindings(t, g)
type position struct{ x, y int }
moves := []position{}
g.OnMouseMove = func(x, y int) {
moves = append(moves, position{x, y})
}
assert.NoError(t, g.onKey(&GocuiEvent{Type: eventMouseMove, MouseX: 15, MouseY: 7}))
assert.NoError(t, g.onKey(&GocuiEvent{Type: eventMouseMove, MouseX: 50, MouseY: 20}))
assert.Equal(t, []position{{15, 7}, {50, 20}}, moves)
}