diff options
author | Junegunn Choi <junegunn.c@gmail.com> | 2023-01-03 01:21:40 +0900 |
---|---|---|
committer | Junegunn Choi <junegunn.c@gmail.com> | 2023-01-03 01:21:40 +0900 |
commit | a893fc0ca2d5c205c394e8f149b48e8f2007d181 (patch) | |
tree | 5d8511b25b420397f25eb678c002c962ab450e99 /src | |
parent | 3761dc0433edc32ea890ac57c383ea2dfda32b1b (diff) |
Clicks with different x coordinates shouldn't be seen as a double-click
Diffstat (limited to 'src')
-rw-r--r-- | src/tui/light.go | 10 | ||||
-rw-r--r-- | src/tui/tcell.go | 62 | ||||
-rw-r--r-- | src/tui/tui.go | 4 |
3 files changed, 29 insertions, 47 deletions
diff --git a/src/tui/light.go b/src/tui/light.go index 7745efd9..ec5cf334 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 a82bf944..befdb58f 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 - - // 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{} + 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.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 a9368c73..063c4e6f 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 } |