2020-01-24 20:40:34 +00:00
|
|
|
package tview
|
|
|
|
|
|
|
|
import (
|
2020-02-14 02:45:13 +00:00
|
|
|
"time"
|
|
|
|
|
2020-01-24 20:40:34 +00:00
|
|
|
"github.com/gdamore/tcell"
|
|
|
|
)
|
|
|
|
|
|
|
|
// MouseAction are bit flags indicating what the mouse is logically doing.
|
|
|
|
type MouseAction int32
|
|
|
|
|
|
|
|
const (
|
|
|
|
MouseMove MouseAction = 1 << iota
|
|
|
|
MouseLeftDown
|
|
|
|
MouseLeftUp
|
|
|
|
MouseLeftClick
|
|
|
|
MouseLeftDoubleClick
|
|
|
|
MouseMiddleDown
|
|
|
|
MouseMiddleUp
|
|
|
|
MouseMiddleClick
|
|
|
|
MouseMiddleDoubleClick
|
|
|
|
MouseRightDown
|
|
|
|
MouseRightUp
|
|
|
|
MouseRightClick
|
|
|
|
MouseRightDoubleClick
|
|
|
|
WheelUp
|
|
|
|
WheelDown
|
|
|
|
WheelLeft
|
|
|
|
WheelRight
|
|
|
|
)
|
|
|
|
|
2020-02-14 02:45:13 +00:00
|
|
|
var DoubleClickInterval = 500 * time.Millisecond
|
|
|
|
|
2020-01-24 20:40:34 +00:00
|
|
|
// Does not set MouseMove or *Click actions.
|
2020-02-14 02:09:09 +00:00
|
|
|
func (action MouseAction) getMouseButtonAction(lastBtn, btn tcell.ButtonMask) MouseAction {
|
2020-01-24 20:40:34 +00:00
|
|
|
btnDiff := btn ^ lastBtn
|
|
|
|
|
|
|
|
if btnDiff&tcell.Button1 != 0 {
|
|
|
|
if btn&tcell.Button1 != 0 {
|
|
|
|
action |= MouseLeftDown
|
|
|
|
} else {
|
|
|
|
action |= MouseLeftUp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if btnDiff&tcell.Button2 != 0 {
|
|
|
|
if btn&tcell.Button2 != 0 {
|
|
|
|
action |= MouseMiddleDown
|
|
|
|
} else {
|
|
|
|
action |= MouseMiddleUp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if btnDiff&tcell.Button3 != 0 {
|
|
|
|
if btn&tcell.Button3 != 0 {
|
|
|
|
action |= MouseRightDown
|
|
|
|
} else {
|
|
|
|
action |= MouseRightUp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if btn&tcell.WheelUp != 0 {
|
|
|
|
action |= WheelUp
|
|
|
|
}
|
|
|
|
if btn&tcell.WheelDown != 0 {
|
|
|
|
action |= WheelDown
|
|
|
|
}
|
|
|
|
if btn&tcell.WheelLeft != 0 {
|
|
|
|
action |= WheelLeft
|
|
|
|
}
|
|
|
|
if btn&tcell.WheelRight != 0 {
|
|
|
|
action |= WheelRight
|
|
|
|
}
|
|
|
|
|
|
|
|
return action
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do not call if the mouse moved.
|
|
|
|
// Sets the *Click, including *DoubleClick.
|
|
|
|
// This should be called last, after setting all the other flags.
|
2020-02-14 02:45:13 +00:00
|
|
|
func (action MouseAction) getMouseClickAction(lastAct MouseAction, lastClickTime *time.Time) MouseAction {
|
2020-01-24 20:40:34 +00:00
|
|
|
if action&MouseMove == 0 {
|
|
|
|
if action&MouseLeftUp != 0 {
|
2020-02-14 02:45:13 +00:00
|
|
|
if (*lastClickTime).Add(DoubleClickInterval).Before(time.Now()) {
|
2020-01-24 20:40:34 +00:00
|
|
|
action |= MouseLeftClick
|
2020-02-14 02:45:13 +00:00
|
|
|
*lastClickTime = time.Now()
|
|
|
|
} else {
|
2020-01-24 20:40:34 +00:00
|
|
|
action |= MouseLeftDoubleClick
|
2020-02-14 02:45:13 +00:00
|
|
|
*lastClickTime = time.Time{} // reset
|
2020-01-24 20:40:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if action&MouseMiddleUp != 0 {
|
2020-02-14 02:45:13 +00:00
|
|
|
if (*lastClickTime).Add(DoubleClickInterval).Before(time.Now()) {
|
2020-01-24 20:40:34 +00:00
|
|
|
action |= MouseMiddleClick
|
2020-02-14 02:45:13 +00:00
|
|
|
*lastClickTime = time.Now()
|
|
|
|
} else {
|
2020-01-24 20:40:34 +00:00
|
|
|
action |= MouseMiddleDoubleClick
|
2020-02-14 02:45:13 +00:00
|
|
|
*lastClickTime = time.Time{} // reset
|
2020-01-24 20:40:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if action&MouseRightUp != 0 {
|
2020-02-14 02:45:13 +00:00
|
|
|
if (*lastClickTime).Add(DoubleClickInterval).Before(time.Now()) {
|
2020-01-24 20:40:34 +00:00
|
|
|
action |= MouseRightClick
|
2020-02-14 02:45:13 +00:00
|
|
|
*lastClickTime = time.Now()
|
|
|
|
} else {
|
2020-01-24 20:40:34 +00:00
|
|
|
action |= MouseRightDoubleClick
|
2020-02-14 02:45:13 +00:00
|
|
|
*lastClickTime = time.Time{} // reset
|
2020-01-24 20:40:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return action
|
|
|
|
}
|