Skip to content

Commit

Permalink
Add a custom mouse event wrapper around tcell's one
Browse files Browse the repository at this point in the history
This wrapper implements the HasMotion() function which value is set by
the main application based on the previous mouse event.
  • Loading branch information
n-peugnet committed Feb 18, 2024
1 parent d3eaa9a commit a00973e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
11 changes: 10 additions & 1 deletion application.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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),
Expand Down Expand Up @@ -132,7 +134,14 @@ func (app *Application) Start() error {
redraw = app.root.OnPasteEvent(customEvt)
}
case *tcell.EventMouse:
redraw = app.root.OnMouseEvent(event)
var customEvt customMouseEvent
if app.prevMouseEvt.Buttons() == event.Buttons() {
customEvt = customMouseEvent{event, true}
} else {
customEvt = customMouseEvent{event, false}
}
app.prevMouseEvt = event
redraw = app.root.OnMouseEvent(customEvt)
case *tcell.EventResize:
clear = true
redraw = true
Expand Down
9 changes: 9 additions & 0 deletions eventhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ type MouseEvent interface {
HasMotion() bool
}

type customMouseEvent struct {
*tcell.EventMouse
motion bool
}

func (cme customMouseEvent) HasMotion() bool {
return cme.motion
}

// SimpleEventHandler is a simple implementation of the event handling methods required for components.
type SimpleEventHandler struct {
OnKey func(event KeyEvent) bool
Expand Down

0 comments on commit a00973e

Please sign in to comment.