Skip to content

Add DragGesture and onLongPressGesture - #50

Merged
colemancda merged 5 commits into
masterfrom
feature/gestures
Jul 23, 2026
Merged

Add DragGesture and onLongPressGesture#50
colemancda merged 5 commits into
masterfrom
feature/gestures

Conversation

@colemancda

Copy link
Copy Markdown
Member

Adds continuous gestures: DragGesture() with .onChanged/.onEnded applied via .gesture(_:), and .onLongPressGesture(perform:).

Keeping the bridge O(1)

A drag needs two numbers per event, but the JNI surface is a fixed set of single-value entry points and growing it is the fragile part of this architecture. So the interpreter encodes "<phase>;<startX>,<startY>;<x>,<y>" (in points) through the existing string callback and Swift rebuilds a DragGesture.Value from it — no new bridge entry point.

Two real bugs the on-device pass caught

Both were invisible to unit tests and only showed up dragging a real box:

  1. Self-referential drag. The demo box offsets itself by the translation, so it moves under the finger — absolute pointer positions then compensate for that motion and the translation collapses to ~0 (observed: Offset: 1, 1 for a 150px drag). Fixed by accumulating per-event deltas instead of reading absolute positions.
  2. Gesture torn down mid-drag. Every onChanged writes state → re-evaluation → a fresh callback id. pointerInput was keyed on that id, so the detector — a long-running coroutine — was cancelled and restarted on the first event, and onEnded never fired. Fixed by keying on the node's stable id and reading the current callback id from a holder, so it never dispatches to a reclaimed callback. (Same generation-scoped-id hazard as the search field and .task cancellation.)

Tap and long press also share a single combinedClickable detector rather than two competing pointer handlers, which would fight over the gesture.

Verification

  • swift test — 3 new tests (long press callback distinct from tap; drag payload → translation for changed/ended; malformed payload ignored), 74 total passing
  • Emulator (new "Gestures" catalog screen):
    • Drag of 150×100 px → Offset: 45, 30 dp and Last drop: 45, 30. The shortfall from the raw 55×36 dp is Compose's touch slop consumed before recognition; the 1.5 ratio is preserved exactly, and onEnded reports the same final translation.
    • Tap vs long press on the same view: a tap gave Taps: 1, Long presses: 0; a ~900 ms hold then gave Taps: 1, Long presses: 1 — each fires only its own handler.

Scope

.gesture(_:) accepts DragGesture (a Gesture marker protocol is in place for future types). Magnification/rotation and gesture composition (.sequenced, .simultaneously) are not included.

@colemancda
colemancda merged commit 775ecc9 into master Jul 23, 2026
6 checks passed
@colemancda
colemancda deleted the feature/gestures branch July 23, 2026 22:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant