Skip to content

Commit

Permalink
create new Driver API DoubleTapDelay
Browse files Browse the repository at this point in the history
  • Loading branch information
dweymouth committed Dec 10, 2023
1 parent 5b8e84a commit f710676
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 22 deletions.
8 changes: 8 additions & 0 deletions driver.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package fyne

import "time"

// Driver defines an abstract concept of a Fyne render driver.
// Any implementation must provide at least these methods.
type Driver interface {
Expand Down Expand Up @@ -29,4 +31,10 @@ type Driver interface {
StartAnimation(*Animation)
// StopAnimation stops an animation and unregisters from this driver.
StopAnimation(*Animation)

// DoubleTapDelay returns the maximum duration where a second tap after a first one
// will be considered a DoubleTap instead of two distinct Tap events.
//
// Since: 2.5
DoubleTapDelay() time.Duration
}
9 changes: 0 additions & 9 deletions internal/driver/consts/consts.go

This file was deleted.

7 changes: 7 additions & 0 deletions internal/driver/glfw/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"runtime"
"sync"
"sync/atomic"
"time"

"github.com/fyne-io/image/ico"

Expand Down Expand Up @@ -36,6 +37,8 @@ var _ fyne.Driver = (*gLDriver)(nil)
// A workaround on Apple M1/M2, just use 1 thread until fixed upstream.
const drawOnMainThread bool = runtime.GOOS == "darwin" && runtime.GOARCH == "arm64"

const doubleTapDelay = 300 * time.Millisecond

type gLDriver struct {
windowLock sync.RWMutex
windows []fyne.Window
Expand Down Expand Up @@ -168,6 +171,10 @@ func (d *gLDriver) Run() {
d.runGL()
}

func (d *gLDriver) DoubleTapDelay() time.Duration {
return doubleTapDelay
}

// NewGLDriver sets up a new Driver instance implemented using the GLFW Go library and OpenGL bindings.
func NewGLDriver() *gLDriver {
repository.Register("file", intRepo.NewFileRepository())
Expand Down
3 changes: 1 addition & 2 deletions internal/driver/glfw/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"fyne.io/fyne/v2/internal/cache"
"fyne.io/fyne/v2/internal/driver"
"fyne.io/fyne/v2/internal/driver/common"
"fyne.io/fyne/v2/internal/driver/consts"
"fyne.io/fyne/v2/internal/scale"
)

Expand Down Expand Up @@ -652,7 +651,7 @@ func (w *window) mouseClickedHandleTapDoubleTap(co fyne.CanvasObject, ev *fyne.P
func (w *window) waitForDoubleTap(co fyne.CanvasObject, ev *fyne.PointEvent) {
var ctx context.Context
w.mouseLock.Lock()
ctx, w.mouseCancelFunc = context.WithDeadline(context.TODO(), time.Now().Add(time.Millisecond*consts.DesktopDoubleClickDelay))
ctx, w.mouseCancelFunc = context.WithDeadline(context.TODO(), time.Now().Add(doubleTapDelay))
defer w.mouseCancelFunc()
w.mouseLock.Unlock()

Expand Down
3 changes: 1 addition & 2 deletions internal/driver/mobile/canvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"fyne.io/fyne/v2/internal/app"
"fyne.io/fyne/v2/internal/driver"
"fyne.io/fyne/v2/internal/driver/common"
"fyne.io/fyne/v2/internal/driver/consts"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
Expand Down Expand Up @@ -375,7 +374,7 @@ func (c *mobileCanvas) tapUp(pos fyne.Position, tapID int,

func (c *mobileCanvas) waitForDoubleTap(co fyne.CanvasObject, ev *fyne.PointEvent, tapCallback func(fyne.Tappable, *fyne.PointEvent), doubleTapCallback func(fyne.DoubleTappable, *fyne.PointEvent)) {
var ctx context.Context
ctx, c.touchCancelFunc = context.WithDeadline(context.TODO(), time.Now().Add(time.Millisecond*consts.MobileDoubleClickDelay))
ctx, c.touchCancelFunc = context.WithDeadline(context.TODO(), time.Now().Add(tapDoubleDelay))
defer c.touchCancelFunc()
<-ctx.Done()
if c.touchTapCount == 2 && c.touchLastTapped == co {
Expand Down
5 changes: 2 additions & 3 deletions internal/driver/mobile/canvas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/driver/mobile"
"fyne.io/fyne/v2/internal/driver/consts"
_ "fyne.io/fyne/v2/test"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
Expand Down Expand Up @@ -299,15 +298,15 @@ func TestCanvas_Focusable(t *testing.T) {
c.tapUp(pos, 0, func(wid fyne.Tappable, ev *fyne.PointEvent) {
wid.Tapped(ev)
}, nil, nil, nil)
time.Sleep(time.Millisecond * (consts.MobileDoubleClickDelay + 150))
time.Sleep(tapDoubleDelay + 150*time.Millisecond)
assert.Equal(t, 1, content.focusedTimes)
assert.Equal(t, 0, content.unfocusedTimes)

c.tapDown(pos, 1)
c.tapUp(pos, 1, func(wid fyne.Tappable, ev *fyne.PointEvent) {
wid.Tapped(ev)
}, nil, nil, nil)
time.Sleep(time.Millisecond * (consts.MobileDoubleClickDelay + 150))
time.Sleep(tapDoubleDelay + 150*time.Millisecond)
assert.Equal(t, 1, content.focusedTimes)
assert.Equal(t, 0, content.unfocusedTimes)

Expand Down
5 changes: 5 additions & 0 deletions internal/driver/mobile/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
const (
tapMoveThreshold = 4.0 // how far can we move before it is a drag
tapSecondaryDelay = 300 * time.Millisecond // how long before secondary tap
tapDoubleDelay = 500 * time.Millisecond // max duration between taps for a DoubleTap event
)

// Configuration is the system information about the current device
Expand Down Expand Up @@ -549,6 +550,10 @@ func (d *mobileDriver) SetOnConfigurationChanged(f func(*Configuration)) {
d.onConfigChanged = f
}

func (d *mobileDriver) DoubleTapDelay() time.Duration {
return tapDoubleDelay
}

// NewGoMobileDriver sets up a new Driver instance implemented using the Go
// Mobile extension and OpenGL bindings.
func NewGoMobileDriver() fyne.Driver {
Expand Down
5 changes: 5 additions & 0 deletions test/testdriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package test
import (
"image"
"sync"
"time"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/internal/driver"
Expand Down Expand Up @@ -129,3 +130,7 @@ func (d *testDriver) removeWindow(w *testWindow) {
d.windows = append(d.windows[:i], d.windows[i+1:]...)
d.windowsMutex.Unlock()
}

func (d *testDriver) DoubleTapDelay() time.Duration {
return 300 * time.Millisecond
}
7 changes: 1 addition & 6 deletions widget/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"fyne.io/fyne/v2/driver/desktop"
"fyne.io/fyne/v2/driver/mobile"
"fyne.io/fyne/v2/internal/cache"
driverconsts "fyne.io/fyne/v2/internal/driver/consts"
"fyne.io/fyne/v2/internal/widget"
"fyne.io/fyne/v2/theme"
)
Expand Down Expand Up @@ -256,11 +255,7 @@ func (e *Entry) DoubleTapped(p *fyne.PointEvent) {
}

func (e *Entry) isTripleTap(nowMilli int64) bool {
doubleClickDelay := int64(driverconsts.DesktopDoubleClickDelay)
if fyne.CurrentDevice().IsMobile() {
doubleClickDelay = driverconsts.MobileDoubleClickDelay
}
return nowMilli-e.doubleTappedAtUnixMillis <= doubleClickDelay
return nowMilli-e.doubleTappedAtUnixMillis <= fyne.CurrentApp().Driver().DoubleTapDelay().Milliseconds()
}

// DragEnd is called at end of a drag event.
Expand Down

0 comments on commit f710676

Please sign in to comment.