From a893fc0ca2d5c205c394e8f149b48e8f2007d181 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Tue, 3 Jan 2023 01:21:40 +0900 Subject: [PATCH] Clicks with different x coordinates shouldn't be seen as a double-click --- src/tui/light.go | 10 ++++---- src/tui/tcell.go | 60 +++++++++++++++++------------------------------- src/tui/tui.go | 4 ++-- 3 files changed, 28 insertions(+), 46 deletions(-) diff --git a/src/tui/light.go b/src/tui/light.go index 7745efd..ec5cf33 100644 --- a/src/tui/light.go +++ b/src/tui/light.go @@ -72,7 +72,7 @@ type LightRenderer struct { forceBlack bool clearOnExit bool prevDownTime time.Time - clickY []int + clicks [][2]int ttyin *os.File buffer []byte origState *term.State @@ -580,15 +580,15 @@ func (r *LightRenderer) mouseSequence(sz *int) Event { if down && !drag { now := time.Now() if !left { // Right double click is not allowed - r.clickY = []int{} + r.clicks = [][2]int{} } else if now.Sub(r.prevDownTime) < doubleClickDuration { - r.clickY = append(r.clickY, y) + r.clicks = append(r.clicks, [2]int{x, y}) } else { - r.clickY = []int{y} + r.clicks = [][2]int{{x, y}} } r.prevDownTime = now } else { - if len(r.clickY) > 1 && r.clickY[0] == r.clickY[1] && + if len(r.clicks) > 1 && r.clicks[0][0] == r.clicks[1][0] && r.clicks[0][1] == r.clicks[1][1] && time.Since(r.prevDownTime) < doubleClickDuration { double = true } diff --git a/src/tui/tcell.go b/src/tui/tcell.go index a82bf94..befdb58 100644 --- a/src/tui/tcell.go +++ b/src/tui/tcell.go @@ -6,8 +6,6 @@ import ( "os" "time" - "runtime" - "github.com/gdamore/tcell/v2" "github.com/gdamore/tcell/v2/encoding" @@ -218,54 +216,38 @@ func (r *FullscreenRenderer) GetChar() Event { return Event{Mouse, 0, &MouseEvent{y, x, -1, false, false, false, mod}} case button&tcell.WheelUp != 0: return Event{Mouse, 0, &MouseEvent{y, x, +1, false, false, false, mod}} - case button&tcell.Button1 != 0 && !drag: - // all potential double click events put their 'line' coordinate in the clickY array - // double click event has two conditions, temporal and spatial, the first is checked here - now := time.Now() - if now.Sub(r.prevDownTime) < doubleClickDuration { - r.clickY = append(r.clickY, y) - } else { - r.clickY = []int{y} - } - r.prevDownTime = now + case button&tcell.Button1 != 0: + double := false + if !drag { + // all potential double click events put their coordinates in the clicks array + // double click event has two conditions, temporal and spatial, the first is checked here + now := time.Now() + if now.Sub(r.prevDownTime) < doubleClickDuration { + r.clicks = append(r.clicks, [2]int{x, y}) + } else { + r.clicks = [][2]int{{x, y}} + } + r.prevDownTime = now - // detect double clicks (also check for spatial condition) - n := len(r.clickY) - double := n > 1 && r.clickY[n-2] == r.clickY[n-1] - if double { - // make sure two consecutive double clicks require four clicks - r.clickY = []int{} + // detect double clicks (also check for spatial condition) + n := len(r.clicks) + double = n > 1 && r.clicks[n-2][0] == r.clicks[n-1][0] && r.clicks[n-2][1] == r.clicks[n-1][1] + if double { + // make sure two consecutive double clicks require four clicks + r.clicks = [][2]int{} + } } - // fire single or double click event return Event{Mouse, 0, &MouseEvent{y, x, 0, true, !double, double, mod}} - case button&tcell.Button2 != 0 && !drag: + case button&tcell.Button2 != 0: return Event{Mouse, 0, &MouseEvent{y, x, 0, false, true, false, mod}} - case runtime.GOOS != "windows": - + default: // double and single taps on Windows don't quite work due to // the console acting on the events and not allowing us // to consume them. - left := button&tcell.Button1 != 0 down := left || button&tcell.Button3 != 0 double := false - if down { - now := time.Now() - if !left { - r.clickY = []int{} - } else if now.Sub(r.prevDownTime) < doubleClickDuration { - r.clickY = append(r.clickY, x) - } else { - r.clickY = []int{x} - r.prevDownTime = now - } - } else { - if len(r.clickY) > 1 && r.clickY[0] == r.clickY[1] && - time.Now().Sub(r.prevDownTime) < doubleClickDuration { - double = true - } - } return Event{Mouse, 0, &MouseEvent{y, x, 0, left, down, double, mod}} } diff --git a/src/tui/tui.go b/src/tui/tui.go index a9368c7..063c4e6 100644 --- a/src/tui/tui.go +++ b/src/tui/tui.go @@ -431,7 +431,7 @@ type FullscreenRenderer struct { mouse bool forceBlack bool prevDownTime time.Time - clickY []int + clicks [][2]int } func NewFullscreenRenderer(theme *ColorTheme, forceBlack bool, mouse bool) Renderer { @@ -440,7 +440,7 @@ func NewFullscreenRenderer(theme *ColorTheme, forceBlack bool, mouse bool) Rende mouse: mouse, forceBlack: forceBlack, prevDownTime: time.Unix(0, 0), - clickY: []int{}} + clicks: [][2]int{}} return r }