Do not handle double clicks by default

This commit is contained in:
Trevor Slocum 2020-04-14 09:46:24 -07:00
parent d2f2938ea0
commit ae4c7b398c
3 changed files with 27 additions and 7 deletions

View File

@ -34,6 +34,10 @@ tview [is not thread-safe](https://godoc.org/github.com/rivo/tview#hdr-Concurren
tview [blocks until the queued function returns](https://github.com/rivo/tview/blob/fe3052019536251fd145835dbaa225b33b7d3088/application.go#L510).
## Double clicks are not handled by default
All clicks are handled as single clicks until an interval is set with [Application.SetDoubleClickInterval](https://docs.rocketnine.space/gitlab.com/tslocum/cview/#Application.SetDoubleClickInterval).
## Lists and Forms do not wrap around by default
Call `SetWrapAround(true)` to wrap around when navigating.

View File

@ -9,6 +9,9 @@ import (
)
const (
// StandardDoubleClick is the standard double click interval.
StandardDoubleClick = 500 * time.Millisecond
// The size of the event/update/redraw channels.
queueSize = 100
@ -16,10 +19,6 @@ const (
resizeEventThrottle = 200 * time.Millisecond
)
// DoubleClickInterval specifies the maximum time between clicks to register a
// double click rather than click.
var DoubleClickInterval = 500 * time.Millisecond
// MouseAction indicates one of the actions the mouse is logically doing.
type MouseAction int16
@ -116,6 +115,10 @@ type Application struct {
// be forwarded).
mouseCapture func(event *tcell.EventMouse, action MouseAction) (*tcell.EventMouse, MouseAction)
// doubleClickInterval specifies the maximum time between clicks to register a
// double click rather than a single click.
doubleClickInterval time.Duration
mouseCapturingPrimitive Primitive // A Primitive returned by a MouseHandler which will capture future mouse events.
lastMouseX, lastMouseY int // The last position of the mouse.
mouseDownX, mouseDownY int // The position of the mouse when its button was last pressed.
@ -176,6 +179,13 @@ func (a *Application) GetMouseCapture() func(event *tcell.EventMouse, action Mou
return a.mouseCapture
}
// SetDoubleClickInterval sets the maximum time between clicks to register a
// double click rather than a single click. A standard duration is provided as
// StandardDoubleClick. No interval is set by default, disabling double clicks.
func (a *Application) SetDoubleClickInterval(interval time.Duration) {
a.doubleClickInterval = interval
}
// SetScreen allows you to provide your own tcell.Screen object. For most
// applications, this is not needed and you should be familiar with
// tcell.Screen when using this function.
@ -472,7 +482,7 @@ func (a *Application) fireMouseActions(event *tcell.EventMouse) (consumed, isMou
} else {
fire(buttonEvent.up)
if !clickMoved {
if a.lastMouseClick.Add(DoubleClickInterval).Before(time.Now()) {
if a.doubleClickInterval == 0 || a.lastMouseClick.Add(a.doubleClickInterval).Before(time.Now()) {
fire(buttonEvent.click)
a.lastMouseClick = time.Now()
} else {

View File

@ -60,9 +60,15 @@ func ExampleNewApplication() {
// Example of an application with mouse support.
func ExampleApplication_EnableMouse() {
// Initialize application and enable mouse support.
// Initialize application.
app := NewApplication()
// Enable mouse support.
app.EnableMouse(true)
// Enable double clicks.
app.SetDoubleClickInterval(StandardDoubleClick)
// Create a textview.
tv := NewTextView().SetText("Click somewhere!")
@ -87,7 +93,7 @@ func ExampleApplication_EnableMouse() {
})
// Run the application.
if err := app.EnableMouse(true).SetRoot(tv, true).Run(); err != nil {
if err := app.SetRoot(tv, true).Run(); err != nil {
panic(err)
}
}