touchpad_gestures.go RawHistoryFolder
1package webkit
2
3import (
4 "fmt"
5 "math"
6
7 "github.com/bnema/dumber/internal/logging"
8 "github.com/diamondburned/gotk4/pkg/gtk/v4"
9)
10
11// Touchpad swipe velocity threshold (pixels per second)
12// Lower values make gestures more sensitive, higher values require faster swipes
13const (
14 swipeVelocityThreshold = 500.0 // Reasonable threshold for intentional swipes
15)
16
17// AttachTouchpadGestures attaches touchpad swipe gesture handling to the WebView
18// This enables two-finger swipe navigation (left/right for back/forward)
19func (w *WebView) AttachTouchpadGestures() {
20 if w.view == nil {
21 logging.Error(fmt.Sprintf("[touchpad-gestures] Cannot attach: WebView is nil"))
22 return
23 }
24
25 // Create gesture controller for swipe detection
26 gestureSwipe := gtk.NewGestureSwipe()
27
28 // Set to capture phase to intercept before WebView processes
29 gestureSwipe.SetPropagationPhase(gtk.PhaseCapture)
30
31 // Set to require touchpad (2 fingers minimum for touchpad gestures)
32 // This prevents single-finger drags from triggering navigation
33 gestureSwipe.SetTouchOnly(false) // Allow both touch and touchpad
34
35 // Connect to swipe signal
36 gestureSwipe.ConnectSwipe(func(velocityX, velocityY float64) {
37 // Only process horizontal swipes (ignore mostly vertical swipes)
38 // Use absolute values to compare magnitudes
39 absVelocityX := math.Abs(velocityX)
40 absVelocityY := math.Abs(velocityY)
41
42 // Require horizontal velocity to be dominant
43 if absVelocityY > absVelocityX {
44 logging.Debug(fmt.Sprintf("[touchpad-gestures] Swipe is more vertical (X:%.1f Y:%.1f), ignoring", velocityX, velocityY))
45 return
46 }
47
48 // Check if swipe velocity meets threshold
49 if absVelocityX < swipeVelocityThreshold {
50 logging.Debug(fmt.Sprintf("[touchpad-gestures] Swipe velocity too low (%.1f < %.1f), ignoring", absVelocityX, swipeVelocityThreshold))
51 return
52 }
53
54 // Determine swipe direction and navigate
55 if velocityX > 0 {
56 // Swipe right (positive velocity) - go back
57 if w.view.CanGoBack() {
58 logging.Debug(fmt.Sprintf("[touchpad-gestures] Swipe right detected (velocity: %.1f) - navigating backward", velocityX))
59 w.view.GoBack()
60 } else {
61 logging.Debug(fmt.Sprintf("[touchpad-gestures] Swipe right detected but cannot go back"))
62 }
63 } else {
64 // Swipe left (negative velocity) - go forward
65 if w.view.CanGoForward() {
66 logging.Debug(fmt.Sprintf("[touchpad-gestures] Swipe left detected (velocity: %.1f) - navigating forward", velocityX))
67 w.view.GoForward()
68 } else {
69 logging.Debug(fmt.Sprintf("[touchpad-gestures] Swipe left detected but cannot go forward"))
70 }
71 }
72 })
73
74 // Attach controller to WebView
75 w.view.AddController(gestureSwipe)
76 logging.Debug(fmt.Sprintf("[touchpad-gestures] Touchpad gesture controller attached to WebView ID %d", w.id))
77}