Double click interval

pull/363/head
Chris Miller 4 years ago
parent 8abbf770ec
commit 5ef51540da

@ -2,6 +2,7 @@ package tview
import (
"sync"
"time"
"github.com/gdamore/tcell"
)
@ -77,6 +78,7 @@ type Application struct {
lastMouseX, lastMouseY int // track last mouse pos
mouseDownX, mouseDownY int // track last mouse down pos
lastMouseAct MouseAction
lastClickTime time.Time
lastMouseBtn tcell.ButtonMask
}
@ -254,7 +256,6 @@ EventLoop:
p := a.focus
inputCapture := a.inputCapture
mouseCapture := a.mouseCapture
mouseHandlerCapture := a.mouseHandlerCapture
screen := a.screen
root := a.root
a.RUnlock()
@ -303,7 +304,7 @@ EventLoop:
}
action = action.getMouseButtonAction(a.lastMouseBtn, btn)
if atX == int(a.mouseDownX) && atY == int(a.mouseDownY) {
action = action.getMouseClickAction(a.lastMouseAct)
action = action.getMouseClickAction(a.lastMouseAct, &a.lastClickTime)
}
a.lastMouseAct = action
a.lastMouseBtn = btn
@ -322,8 +323,8 @@ EventLoop:
}
var newHandlerCapture Primitive = nil // Clear it by default.
if mouseHandlerCapture != nil { // Check if already captured.
if handler := mouseHandlerCapture.MouseHandler(); handler != nil {
if a.mouseHandlerCapture != nil { // Check if already captured.
if handler := a.mouseHandlerCapture.MouseHandler(); handler != nil {
_, newHandlerCapture = handler(action, event, func(p Primitive) {
a.SetFocus(p)
})
@ -335,10 +336,7 @@ EventLoop:
})
a.draw()
}
a.Lock()
a.mouseHandlerCapture = newHandlerCapture
a.Unlock()
}
// If we have updates, now is the time to execute them.

@ -1,6 +1,8 @@
package tview
import (
"time"
"github.com/gdamore/tcell"
)
@ -27,6 +29,8 @@ const (
WheelRight
)
var DoubleClickInterval = 500 * time.Millisecond
// Does not set MouseMove or *Click actions.
func (action MouseAction) getMouseButtonAction(lastBtn, btn tcell.ButtonMask) MouseAction {
btnDiff := btn ^ lastBtn
@ -74,27 +78,33 @@ func (action MouseAction) getMouseButtonAction(lastBtn, btn tcell.ButtonMask) Mo
// Do not call if the mouse moved.
// Sets the *Click, including *DoubleClick.
// This should be called last, after setting all the other flags.
func (action MouseAction) getMouseClickAction(lastAct MouseAction) MouseAction {
func (action MouseAction) getMouseClickAction(lastAct MouseAction, lastClickTime *time.Time) MouseAction {
if action&MouseMove == 0 {
if action&MouseLeftUp != 0 {
if lastAct&(MouseLeftClick|MouseLeftDoubleClick) == 0 {
if (*lastClickTime).Add(DoubleClickInterval).Before(time.Now()) {
action |= MouseLeftClick
} else if lastAct&MouseLeftDoubleClick == 0 {
*lastClickTime = time.Now()
} else {
action |= MouseLeftDoubleClick
*lastClickTime = time.Time{} // reset
}
}
if action&MouseMiddleUp != 0 {
if lastAct&(MouseMiddleClick|MouseMiddleDoubleClick) == 0 {
if (*lastClickTime).Add(DoubleClickInterval).Before(time.Now()) {
action |= MouseMiddleClick
} else if lastAct&MouseMiddleDoubleClick == 0 {
*lastClickTime = time.Now()
} else {
action |= MouseMiddleDoubleClick
*lastClickTime = time.Time{} // reset
}
}
if action&MouseRightUp != 0 {
if lastAct&(MouseRightClick|MouseRightDoubleClick) == 0 {
if (*lastClickTime).Add(DoubleClickInterval).Before(time.Now()) {
action |= MouseRightClick
} else if lastAct&MouseRightDoubleClick == 0 {
*lastClickTime = time.Now()
} else {
action |= MouseRightDoubleClick
*lastClickTime = time.Time{} // reset
}
}
}

Loading…
Cancel
Save