Double click interval

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

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

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

Loading…
Cancel
Save