feat(input): add gesture handler for mouse button navigation
- commit
- 30cf952b83be1c884a86487cd2edadf84ce4d465
- author
- brice · · 2025-12-16 19:01 UTC
- committer
- brice · · 2025-12-16 19:07 UTC
- parent
- 5af29c5
- signature
- unsigned
1 file · +121 -0
A internal/ui/input/gesture.go +121 -0
| @@ -0,0 +1,121 @@ | |||
| 1 | + | package input | |
| 2 | + | ||
| 3 | + | import ( | |
| 4 | + | "context" | |
| 5 | + | "sync" | |
| 6 | + | ||
| 7 | + | "github.com/bnema/dumber/internal/logging" | |
| 8 | + | "github.com/jwijenbergh/puregotk/v4/gtk" | |
| 9 | + | ) | |
| 10 | + | ||
| 11 | + | const ( | |
| 12 | + | // Mouse button constants for X11/Wayland | |
| 13 | + | mouseButtonBack = 8 // Side button - back | |
| 14 | + | mouseButtonForward = 9 // Side button - forward | |
| 15 | + | ) | |
| 16 | + | ||
| 17 | + | // GestureHandler handles mouse button gestures for navigation. | |
| 18 | + | // It recognizes mouse buttons 8 (back) and 9 (forward) on WebView widgets. | |
| 19 | + | type GestureHandler struct { | |
| 20 | + | clickGesture *gtk.GestureClick | |
| 21 | + | ||
| 22 | + | // Action handler callback | |
| 23 | + | onAction ActionHandler | |
| 24 | + | ||
| 25 | + | ctx context.Context | |
| 26 | + | mu sync.RWMutex | |
| 27 | + | } | |
| 28 | + | ||
| 29 | + | // NewGestureHandler creates a new gesture handler. | |
| 30 | + | func NewGestureHandler(ctx context.Context) *GestureHandler { | |
| 31 | + | log := logging.FromContext(ctx) | |
| 32 | + | log.Debug().Msg("creating gesture handler") | |
| 33 | + | ||
| 34 | + | return &GestureHandler{ | |
| 35 | + | ctx: ctx, | |
| 36 | + | } | |
| 37 | + | } | |
| 38 | + | ||
| 39 | + | // SetOnAction sets the callback for when navigation actions are triggered. | |
| 40 | + | func (h *GestureHandler) SetOnAction(fn ActionHandler) { | |
| 41 | + | h.mu.Lock() | |
| 42 | + | defer h.mu.Unlock() | |
| 43 | + | h.onAction = fn | |
| 44 | + | } | |
| 45 | + | ||
| 46 | + | // AttachTo attaches the gesture handler to a GTK widget. | |
| 47 | + | // Typically called with a WebView widget to enable mouse button navigation. | |
| 48 | + | func (h *GestureHandler) AttachTo(widget *gtk.Widget) { | |
| 49 | + | log := logging.FromContext(h.ctx) | |
| 50 | + | ||
| 51 | + | if widget == nil { | |
| 52 | + | log.Error().Msg("cannot attach gesture handler to nil widget") | |
| 53 | + | return | |
| 54 | + | } | |
| 55 | + | ||
| 56 | + | // Create click gesture that listens to all buttons | |
| 57 | + | h.clickGesture = gtk.NewGestureClick() | |
| 58 | + | if h.clickGesture == nil { | |
| 59 | + | log.Error().Msg("failed to create gesture click") | |
| 60 | + | return | |
| 61 | + | } | |
| 62 | + | ||
| 63 | + | // Listen to all mouse buttons (not just primary) | |
| 64 | + | h.clickGesture.SetButton(0) | |
| 65 | + | ||
| 66 | + | // Connect pressed handler | |
| 67 | + | pressedCb := func(gesture gtk.GestureClick, nPress int, x float64, y float64) { | |
| 68 | + | h.handlePressed(nPress) | |
| 69 | + | } | |
| 70 | + | h.clickGesture.ConnectPressed(&pressedCb) | |
| 71 | + | ||
| 72 | + | // Add controller to widget | |
| 73 | + | widget.AddController(&h.clickGesture.EventController) | |
| 74 | + | ||
| 75 | + | log.Debug().Msg("gesture handler attached to widget") | |
| 76 | + | } | |
| 77 | + | ||
| 78 | + | // handlePressed processes a mouse button press event. | |
| 79 | + | func (h *GestureHandler) handlePressed(nPress int) { | |
| 80 | + | // Only handle single clicks for navigation | |
| 81 | + | if nPress != 1 { | |
| 82 | + | return | |
| 83 | + | } | |
| 84 | + | ||
| 85 | + | // Get the button that was pressed | |
| 86 | + | button := h.clickGesture.GetCurrentButton() | |
| 87 | + | ||
| 88 | + | var action Action | |
| 89 | + | switch button { | |
| 90 | + | case mouseButtonBack: | |
| 91 | + | action = ActionGoBack | |
| 92 | + | case mouseButtonForward: | |
| 93 | + | action = ActionGoForward | |
| 94 | + | default: | |
| 95 | + | // Not a navigation button, ignore | |
| 96 | + | return | |
| 97 | + | } | |
| 98 | + | ||
| 99 | + | // Dispatch action to handler | |
| 100 | + | h.mu.RLock() | |
| 101 | + | handler := h.onAction | |
| 102 | + | h.mu.RUnlock() | |
| 103 | + | ||
| 104 | + | if handler != nil { | |
| 105 | + | if err := handler(h.ctx, action); err != nil { | |
| 106 | + | log := logging.FromContext(h.ctx) | |
| 107 | + | log.Error(). | |
| 108 | + | Err(err). | |
| 109 | + | Str("action", string(action)). | |
| 110 | + | Uint("button", button). | |
| 111 | + | Msg("gesture action handler error") | |
| 112 | + | } | |
| 113 | + | } | |
| 114 | + | } | |
| 115 | + | ||
| 116 | + | // Detach removes the gesture handler. | |
| 117 | + | // Note: GTK handles cleanup when the widget is destroyed, | |
| 118 | + | // but we clear our reference here. | |
| 119 | + | func (h *GestureHandler) Detach() { | |
| 120 | + | h.clickGesture = nil | |
| 121 | + | } | |
| @@ -0,0 +1,121 @@ | |||||
| 1 | + | package input | |||
| 2 | + | ||||
| 3 | + | import ( | |||
| 4 | + | "context" | |||
| 5 | + | "sync" | |||
| 6 | + | ||||
| 7 | + | "github.com/bnema/dumber/internal/logging" | |||
| 8 | + | "github.com/jwijenbergh/puregotk/v4/gtk" | |||
| 9 | + | ) | |||
| 10 | + | ||||
| 11 | + | const ( | |||
| 12 | + | // Mouse button constants for X11/Wayland | |||
| 13 | + | mouseButtonBack = 8 // Side button - back | |||
| 14 | + | mouseButtonForward = 9 // Side button - forward | |||
| 15 | + | ) | |||
| 16 | + | ||||
| 17 | + | // GestureHandler handles mouse button gestures for navigation. | |||
| 18 | + | // It recognizes mouse buttons 8 (back) and 9 (forward) on WebView widgets. | |||
| 19 | + | type GestureHandler struct { | |||
| 20 | + | clickGesture *gtk.GestureClick | |||
| 21 | + | ||||
| 22 | + | // Action handler callback | |||
| 23 | + | onAction ActionHandler | |||
| 24 | + | ||||
| 25 | + | ctx context.Context | |||
| 26 | + | mu sync.RWMutex | |||
| 27 | + | } | |||
| 28 | + | ||||
| 29 | + | // NewGestureHandler creates a new gesture handler. | |||
| 30 | + | func NewGestureHandler(ctx context.Context) *GestureHandler { | |||
| 31 | + | log := logging.FromContext(ctx) | |||
| 32 | + | log.Debug().Msg("creating gesture handler") | |||
| 33 | + | ||||
| 34 | + | return &GestureHandler{ | |||
| 35 | + | ctx: ctx, | |||
| 36 | + | } | |||
| 37 | + | } | |||
| 38 | + | ||||
| 39 | + | // SetOnAction sets the callback for when navigation actions are triggered. | |||
| 40 | + | func (h *GestureHandler) SetOnAction(fn ActionHandler) { | |||
| 41 | + | h.mu.Lock() | |||
| 42 | + | defer h.mu.Unlock() | |||
| 43 | + | h.onAction = fn | |||
| 44 | + | } | |||
| 45 | + | ||||
| 46 | + | // AttachTo attaches the gesture handler to a GTK widget. | |||
| 47 | + | // Typically called with a WebView widget to enable mouse button navigation. | |||
| 48 | + | func (h *GestureHandler) AttachTo(widget *gtk.Widget) { | |||
| 49 | + | log := logging.FromContext(h.ctx) | |||
| 50 | + | ||||
| 51 | + | if widget == nil { | |||
| 52 | + | log.Error().Msg("cannot attach gesture handler to nil widget") | |||
| 53 | + | return | |||
| 54 | + | } | |||
| 55 | + | ||||
| 56 | + | // Create click gesture that listens to all buttons | |||
| 57 | + | h.clickGesture = gtk.NewGestureClick() | |||
| 58 | + | if h.clickGesture == nil { | |||
| 59 | + | log.Error().Msg("failed to create gesture click") | |||
| 60 | + | return | |||
| 61 | + | } | |||
| 62 | + | ||||
| 63 | + | // Listen to all mouse buttons (not just primary) | |||
| 64 | + | h.clickGesture.SetButton(0) | |||
| 65 | + | ||||
| 66 | + | // Connect pressed handler | |||
| 67 | + | pressedCb := func(gesture gtk.GestureClick, nPress int, x float64, y float64) { | |||
| 68 | + | h.handlePressed(nPress) | |||
| 69 | + | } | |||
| 70 | + | h.clickGesture.ConnectPressed(&pressedCb) | |||
| 71 | + | ||||
| 72 | + | // Add controller to widget | |||
| 73 | + | widget.AddController(&h.clickGesture.EventController) | |||
| 74 | + | ||||
| 75 | + | log.Debug().Msg("gesture handler attached to widget") | |||
| 76 | + | } | |||
| 77 | + | ||||
| 78 | + | // handlePressed processes a mouse button press event. | |||
| 79 | + | func (h *GestureHandler) handlePressed(nPress int) { | |||
| 80 | + | // Only handle single clicks for navigation | |||
| 81 | + | if nPress != 1 { | |||
| 82 | + | return | |||
| 83 | + | } | |||
| 84 | + | ||||
| 85 | + | // Get the button that was pressed | |||
| 86 | + | button := h.clickGesture.GetCurrentButton() | |||
| 87 | + | ||||
| 88 | + | var action Action | |||
| 89 | + | switch button { | |||
| 90 | + | case mouseButtonBack: | |||
| 91 | + | action = ActionGoBack | |||
| 92 | + | case mouseButtonForward: | |||
| 93 | + | action = ActionGoForward | |||
| 94 | + | default: | |||
| 95 | + | // Not a navigation button, ignore | |||
| 96 | + | return | |||
| 97 | + | } | |||
| 98 | + | ||||
| 99 | + | // Dispatch action to handler | |||
| 100 | + | h.mu.RLock() | |||
| 101 | + | handler := h.onAction | |||
| 102 | + | h.mu.RUnlock() | |||
| 103 | + | ||||
| 104 | + | if handler != nil { | |||
| 105 | + | if err := handler(h.ctx, action); err != nil { | |||
| 106 | + | log := logging.FromContext(h.ctx) | |||
| 107 | + | log.Error(). | |||
| 108 | + | Err(err). | |||
| 109 | + | Str("action", string(action)). | |||
| 110 | + | Uint("button", button). | |||
| 111 | + | Msg("gesture action handler error") | |||
| 112 | + | } | |||
| 113 | + | } | |||
| 114 | + | } | |||
| 115 | + | ||||
| 116 | + | // Detach removes the gesture handler. | |||
| 117 | + | // Note: GTK handles cleanup when the widget is destroyed, | |||
| 118 | + | // but we clear our reference here. | |||
| 119 | + | func (h *GestureHandler) Detach() { | |||
| 120 | + | h.clickGesture = nil | |||
| 121 | + | } | |||