Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add double tapping to mobile and don't allow both single and double tap events to fire #1381

Merged
merged 19 commits into from Oct 11, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
89 changes: 56 additions & 33 deletions internal/driver/glfw/window.go
Expand Up @@ -3,6 +3,7 @@ package glfw
import "C"
import (
"bytes"
"context"
"image"
_ "image/png" // for the icon
"runtime"
Expand All @@ -21,7 +22,7 @@ import (

const (
scrollSpeed = 10
doubleClickDelay = 500 // ms (maximum interval between clicks for double click detection)
doubleClickDelay = 300 // ms (maximum interval between clicks for double click detection)
)

var (
Expand Down Expand Up @@ -73,6 +74,8 @@ type window struct {
mouseClickTime time.Time
mouseLastClick fyne.CanvasObject
mousePressed fyne.CanvasObject
mouseClickCount int
mouseCancelFunc context.CancelFunc
onClosed func()
onCloseIntercepted func()

Expand Down Expand Up @@ -656,7 +659,6 @@ func (w *window) mouseClicked(_ *glfw.Window, btn glfw.MouseButton, action glfw.
co, _ = w.mouseDragged.(fyne.CanvasObject)
ev.Position = w.mousePos.Subtract(w.mouseDraggedOffset).Subtract(co.Position())
}

button, modifiers := convertMouseButton(btn, mods)
if wid, ok := co.(desktop.Mouseable); ok {
mev := new(desktop.MouseEvent)
Expand Down Expand Up @@ -685,54 +687,75 @@ func (w *window) mouseClicked(_ *glfw.Window, btn glfw.MouseButton, action glfw.
w.mouseButton = 0
}

// Check for double click/tap
doubleTapped := false
if action == glfw.Release && button == desktop.LeftMouseButton {
now := time.Now()
// we can safely subtract the first "zero" time as it'll be much larger than doubleClickDelay
if now.Sub(w.mouseClickTime).Nanoseconds()/1e6 <= doubleClickDelay && w.mouseLastClick == co {
if wid, ok := co.(fyne.DoubleTappable); ok {
doubleTapped = true
w.queueEvent(func() { wid.DoubleTapped(ev) })
}
if wid, ok := co.(fyne.Draggable); ok {
if action == glfw.Press {
w.mouseDragPos = w.mousePos
w.mouseDragged = wid
w.mouseDraggedOffset = w.mousePos.Subtract(co.Position()).Subtract(ev.Position)
}
w.mouseClickTime = now
w.mouseLastClick = co
}

if action == glfw.Release && w.mouseDragged != nil {
if w.mouseDragStarted {
w.queueEvent(w.mouseDragged.DragEnd)
w.mouseDragStarted = false
}
if w.objIsDragged(w.mouseOver) && !w.objIsDragged(coMouse) {
w.mouseOut()
}
w.mouseDragged = nil
}
_, tap := co.(fyne.Tappable)
_, altTap := co.(fyne.SecondaryTappable)
// Prevent Tapped from triggering if DoubleTapped has been sent
if (tap || altTap) && !doubleTapped {
if tap || altTap {
if action == glfw.Press {
w.mousePressed = co
} else if action == glfw.Release {
if co == w.mousePressed {
if button == desktop.RightMouseButton && altTap {
w.queueEvent(func() { co.(fyne.SecondaryTappable).TappedSecondary(ev) })
} else if button == desktop.LeftMouseButton && tap {
w.queueEvent(func() { co.(fyne.Tappable).Tapped(ev) })
}
}
w.mousePressed = nil
}
}
if wid, ok := co.(fyne.Draggable); ok {
if action == glfw.Press {
w.mouseDragPos = w.mousePos
w.mouseDragged = wid
w.mouseDraggedOffset = w.mousePos.Subtract(co.Position()).Subtract(ev.Position)

// Check for double click/tap on left mouse button
if action == glfw.Release && button == desktop.LeftMouseButton {
_, doubleTap := co.(fyne.DoubleTappable)
if doubleTap {
w.mouseClickCount++
w.mouseLastClick = co
if w.mouseCancelFunc != nil {
w.mouseCancelFunc()
}
go w.waitForDoubleTap(co, ev, action, button)
} else {
if wid, ok := co.(fyne.Tappable); ok {
w.queueEvent(func() { wid.Tapped(ev) })
}
}
w.mousePressed = nil
}
if action == glfw.Release && w.mouseDragged != nil {
if w.mouseDragStarted {
w.queueEvent(w.mouseDragged.DragEnd)
w.mouseDragStarted = false
}
if w.objIsDragged(w.mouseOver) && !w.objIsDragged(coMouse) {
w.mouseOut()
}

func (w *window) waitForDoubleTap(co fyne.CanvasObject, ev *fyne.PointEvent, action glfw.Action, button desktop.MouseButton) {
var ctx context.Context
ctx, w.mouseCancelFunc = context.WithDeadline(context.TODO(), time.Now().Add(time.Millisecond*doubleClickDelay))
defer w.mouseCancelFunc()
select {
case <-ctx.Done():
stuartmscott marked this conversation as resolved.
Show resolved Hide resolved
if w.mouseClickCount == 2 && w.mouseLastClick == co {
if wid, ok := co.(fyne.DoubleTappable); ok {
w.queueEvent(func() { wid.DoubleTapped(ev) })
}
} else if co == w.mousePressed {
if wid, ok := co.(fyne.Tappable); ok {
w.queueEvent(func() { wid.Tapped(ev) })
}
}
w.mouseDragged = nil
w.mouseClickCount = 0
w.mouseCancelFunc = nil
w.mouseLastClick = nil
return
}
}

Expand Down
7 changes: 7 additions & 0 deletions internal/driver/glfw/window_test.go
Expand Up @@ -465,11 +465,13 @@ func TestWindow_TappedSecondary_OnPrimaryOnlyTarget(t *testing.T) {
w.mouseClicked(w.viewport, glfw.MouseButton2, glfw.Press, 0)
w.mouseClicked(w.viewport, glfw.MouseButton2, glfw.Release, 0)
w.waitForEvents()

assert.False(t, tapped)

w.mouseClicked(w.viewport, glfw.MouseButton1, glfw.Press, 0)
w.mouseClicked(w.viewport, glfw.MouseButton1, glfw.Release, 0)
w.waitForEvents()

assert.True(t, tapped)
}

Expand Down Expand Up @@ -497,13 +499,15 @@ func TestWindow_TappedIgnoresScrollerClip(t *testing.T) {
w.mouseClicked(w.viewport, glfw.MouseButton1, glfw.Release, 0)

w.waitForEvents()

assert.False(t, tapped, "Tapped button that was clipped")

w.mousePos = fyne.NewPos(10, 120)
w.mouseClicked(w.viewport, glfw.MouseButton1, glfw.Press, 0)
w.mouseClicked(w.viewport, glfw.MouseButton1, glfw.Release, 0)

w.waitForEvents()

assert.True(t, tapped, "Tapped button that was clipped")
}

Expand All @@ -519,6 +523,7 @@ func TestWindow_TappedIgnoredWhenMovedOffOfTappable(t *testing.T) {
w.mouseClicked(w.viewport, glfw.MouseButton1, glfw.Release, 0)

w.waitForEvents()

assert.Equal(t, 1, tapped, "Button 1 should be tapped")
tapped = 0

Expand All @@ -527,12 +532,14 @@ func TestWindow_TappedIgnoredWhenMovedOffOfTappable(t *testing.T) {
w.mouseClicked(w.viewport, glfw.MouseButton1, glfw.Release, 0)

w.waitForEvents()

assert.Equal(t, 0, tapped, "button was tapped without mouse press & release on it %d", tapped)

w.mouseClicked(w.viewport, glfw.MouseButton1, glfw.Press, 0)
w.mouseClicked(w.viewport, glfw.MouseButton1, glfw.Release, 0)

w.waitForEvents()

assert.Equal(t, 2, tapped, "Button 2 should be tapped")
}

Expand Down