diff --git a/driver.go b/driver.go index 8737f69148..2cc7cecd2f 100644 --- a/driver.go +++ b/driver.go @@ -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 { @@ -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 } diff --git a/internal/driver/consts/consts.go b/internal/driver/consts/consts.go deleted file mode 100644 index 15d750c6b0..0000000000 --- a/internal/driver/consts/consts.go +++ /dev/null @@ -1,9 +0,0 @@ -package consts - -const ( - // moved here from the platform driver packages to allow - // referencing in widget package without an import cycle - - DesktopDoubleClickDelay = 300 // ms - MobileDoubleClickDelay = 500 // ms -) diff --git a/internal/driver/glfw/driver.go b/internal/driver/glfw/driver.go index 241b5e09e1..6a44a1c45d 100644 --- a/internal/driver/glfw/driver.go +++ b/internal/driver/glfw/driver.go @@ -9,6 +9,7 @@ import ( "runtime" "sync" "sync/atomic" + "time" "github.com/fyne-io/image/ico" @@ -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 @@ -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()) diff --git a/internal/driver/glfw/window.go b/internal/driver/glfw/window.go index 1bd456842a..b58ef13650 100644 --- a/internal/driver/glfw/window.go +++ b/internal/driver/glfw/window.go @@ -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" ) @@ -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() diff --git a/internal/driver/mobile/canvas.go b/internal/driver/mobile/canvas.go index 8756f47a5c..806ffea6f5 100644 --- a/internal/driver/mobile/canvas.go +++ b/internal/driver/mobile/canvas.go @@ -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" ) @@ -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 { diff --git a/internal/driver/mobile/canvas_test.go b/internal/driver/mobile/canvas_test.go index 92cfcb9e61..68468ea6ae 100644 --- a/internal/driver/mobile/canvas_test.go +++ b/internal/driver/mobile/canvas_test.go @@ -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" @@ -299,7 +298,7 @@ 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) @@ -307,7 +306,7 @@ func TestCanvas_Focusable(t *testing.T) { 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) diff --git a/internal/driver/mobile/driver.go b/internal/driver/mobile/driver.go index 5e06cffbdc..53000d9d79 100644 --- a/internal/driver/mobile/driver.go +++ b/internal/driver/mobile/driver.go @@ -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 @@ -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 { diff --git a/test/testdriver.go b/test/testdriver.go index 146e44e1ff..6bc2a2269a 100644 --- a/test/testdriver.go +++ b/test/testdriver.go @@ -3,6 +3,7 @@ package test import ( "image" "sync" + "time" "fyne.io/fyne/v2" "fyne.io/fyne/v2/internal/driver" @@ -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 +} diff --git a/widget/entry.go b/widget/entry.go index e7c760c5ec..2ab8f8871d 100644 --- a/widget/entry.go +++ b/widget/entry.go @@ -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" ) @@ -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.