From 562ac8ffe3b3677467054eb971b2230bea2b687f Mon Sep 17 00:00:00 2001 From: Nicolas Peugnet Date: Sun, 18 Feb 2024 20:17:35 +0100 Subject: [PATCH] Add a custom mouse event wrapper around tcell's one This wrapper implements the HasMotion() function which value is set by the main application based on the previous mouse event. --- application.go | 7 ++++++- eventhandler.go | 9 +++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/application.go b/application.go index fc2d865..b7edf10 100644 --- a/application.go +++ b/application.go @@ -38,6 +38,7 @@ type FocusableComponent interface { type Application struct { screenLock sync.RWMutex screen tcell.Screen + prevMouseEvt *tcell.EventMouse root Component updates chan interface{} redrawTicker *time.Ticker @@ -50,6 +51,7 @@ const queueSize = 255 func NewApplication() *Application { return &Application{ + prevMouseEvt: &tcell.EventMouse{}, updates: make(chan interface{}, queueSize), redrawTicker: time.NewTicker(1 * time.Minute), stop: make(chan struct{}, 1), @@ -132,7 +134,10 @@ func (app *Application) Start() error { redraw = app.root.OnPasteEvent(customEvt) } case *tcell.EventMouse: - redraw = app.root.OnMouseEvent(event) + hasMotion := app.prevMouseEvt.Buttons() == event.Buttons() + customEvt := customMouseEvent{event, hasMotion} + app.prevMouseEvt = event + redraw = app.root.OnMouseEvent(customEvt) case *tcell.EventResize: clear = true redraw = true diff --git a/eventhandler.go b/eventhandler.go index 3d45b78..fa04259 100644 --- a/eventhandler.go +++ b/eventhandler.go @@ -38,6 +38,15 @@ type PasteEvent interface { Text() string } +type customMouseEvent struct { + *tcell.EventMouse + motion bool +} + +func (cme customMouseEvent) HasMotion() bool { + return cme.motion +} + // MouseEvent is an interface of the *tcell.EventMouse type. type MouseEvent interface { tcell.Event