|
| 1 | +// |
| 2 | +// Copyright 2025 Readium Foundation. All rights reserved. |
| 3 | +// Use of this source code is governed by the BSD-style license |
| 4 | +// available in the top-level LICENSE file of the project. |
| 5 | +// |
| 6 | + |
| 7 | +import Foundation |
| 8 | + |
| 9 | +public extension InputObserving where Self == DragPointerObserver { |
| 10 | + static func drag( |
| 11 | + onStart: @MainActor @escaping (PointerEvent) -> Bool = { _ in false }, |
| 12 | + onMove: @MainActor @escaping (PointerEvent) -> Bool = { _ in false }, |
| 13 | + onEnd: @MainActor @escaping (PointerEvent) -> Bool = { _ in false }, |
| 14 | + onCancel: @MainActor @escaping (PointerEvent) -> Bool = { _ in false } |
| 15 | + ) -> DragPointerObserver { |
| 16 | + DragPointerObserver( |
| 17 | + onStart: onStart, |
| 18 | + onMove: onMove, |
| 19 | + onEnd: onEnd, |
| 20 | + onCancel: onCancel |
| 21 | + ) |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +/// Pointer observer recognizing drag gestures. |
| 26 | +@MainActor public final class DragPointerObserver: InputObserving { |
| 27 | + private let onStart: @MainActor (PointerEvent) -> Bool |
| 28 | + private let onMove: @MainActor (PointerEvent) -> Bool |
| 29 | + private let onEnd: @MainActor (PointerEvent) -> Bool |
| 30 | + private let onCancel: @MainActor (PointerEvent) -> Bool |
| 31 | + |
| 32 | + public init( |
| 33 | + onStart: @MainActor @escaping (PointerEvent) -> Bool, |
| 34 | + onMove: @MainActor @escaping (PointerEvent) -> Bool, |
| 35 | + onEnd: @MainActor @escaping (PointerEvent) -> Bool, |
| 36 | + onCancel: @MainActor @escaping (PointerEvent) -> Bool |
| 37 | + ) { |
| 38 | + self.onStart = onStart |
| 39 | + self.onMove = onMove |
| 40 | + self.onEnd = onEnd |
| 41 | + self.onCancel = onCancel |
| 42 | + } |
| 43 | + |
| 44 | + private var state: State = .idle |
| 45 | + |
| 46 | + private enum State { |
| 47 | + case idle |
| 48 | + case pending(id: AnyHashable, startLocation: CGPoint) |
| 49 | + case dragging(id: AnyHashable, lastEvent: PointerEvent) |
| 50 | + case failed(activePointers: Set<AnyHashable>) |
| 51 | + } |
| 52 | + |
| 53 | + private enum Action { |
| 54 | + case start(PointerEvent) |
| 55 | + case move(PointerEvent) |
| 56 | + case end(PointerEvent) |
| 57 | + case cancel(PointerEvent) |
| 58 | + case none |
| 59 | + } |
| 60 | + |
| 61 | + public func didReceive(_ event: KeyEvent) async -> Bool { |
| 62 | + false |
| 63 | + } |
| 64 | + |
| 65 | + public func didReceive(_ event: PointerEvent) async -> Bool { |
| 66 | + let (newState, action) = transition(state: state, event: event) |
| 67 | + state = newState |
| 68 | + |
| 69 | + switch action { |
| 70 | + case let .start(event): |
| 71 | + return onStart(event) |
| 72 | + case let .move(event): |
| 73 | + return onMove(event) |
| 74 | + case let .end(event): |
| 75 | + return onEnd(event) |
| 76 | + case let .cancel(event): |
| 77 | + return onCancel(event) |
| 78 | + case .none: |
| 79 | + return false |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private func transition(state: State, event: PointerEvent) -> (State, Action) { |
| 84 | + let id = event.pointer.id |
| 85 | + |
| 86 | + switch (state, event.phase) { |
| 87 | + case (.idle, .down): |
| 88 | + return (.pending(id: id, startLocation: event.location), .none) |
| 89 | + |
| 90 | + case let (.pending(pendingID, _), .down) where pendingID != id: |
| 91 | + return (.failed(activePointers: [pendingID, id]), .none) |
| 92 | + |
| 93 | + case let (.pending(pendingID, _), .cancel) where pendingID == id: |
| 94 | + return (.idle, .none) |
| 95 | + |
| 96 | + case let (.pending(pendingID, startLocation), .move) where pendingID == id: |
| 97 | + // Check if pointer has moved enough to start dragging. |
| 98 | + if abs(startLocation.x - event.location.x) > 1 || abs(startLocation.y - event.location.y) > 1 { |
| 99 | + return (.dragging(id: pendingID, lastEvent: event), .start(event)) |
| 100 | + } else { |
| 101 | + return (.pending(id: pendingID, startLocation: startLocation), .none) |
| 102 | + } |
| 103 | + |
| 104 | + case let (.pending(pendingID, _), .up) where pendingID == id: |
| 105 | + // Pointer went up without moving - this is a tap, not a drag. |
| 106 | + return (.idle, .none) |
| 107 | + |
| 108 | + case let (.dragging(draggingID, lastEvent), .down) where draggingID != id: |
| 109 | + // Second pointer detected during drag - cancel the drag |
| 110 | + return (.failed(activePointers: [draggingID, id]), .cancel(lastEvent)) |
| 111 | + |
| 112 | + case let (.dragging(draggingID, lastEvent), .cancel) where draggingID == id: |
| 113 | + return (.idle, .cancel(lastEvent)) |
| 114 | + |
| 115 | + case let (.dragging(draggingID, _), .move) where draggingID == id: |
| 116 | + return (.dragging(id: draggingID, lastEvent: event), .move(event)) |
| 117 | + |
| 118 | + case let (.dragging(draggingID, _), .up) where draggingID == id: |
| 119 | + return (.idle, .end(event)) |
| 120 | + |
| 121 | + case var (.failed(activePointers), .down): |
| 122 | + activePointers.insert(id) |
| 123 | + return (.failed(activePointers: activePointers), .none) |
| 124 | + |
| 125 | + case var (.failed(activePointers), .up), |
| 126 | + var (.failed(activePointers), .cancel): |
| 127 | + activePointers.remove(id) |
| 128 | + if activePointers.isEmpty { |
| 129 | + return (.idle, .none) |
| 130 | + } else { |
| 131 | + return (.failed(activePointers: activePointers), .none) |
| 132 | + } |
| 133 | + |
| 134 | + default: |
| 135 | + return (state, .none) |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments