RxGestureRecognizerDelegate.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright (c) RxSwiftCommunity
  2. // Permission is hereby granted, free of charge, to any person obtaining a copy
  3. // of this software and associated documentation files (the "Software"), to deal
  4. // in the Software without restriction, including without limitation the rights
  5. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  6. // copies of the Software, and to permit persons to whom the Software is
  7. // furnished to do so, subject to the following conditions:
  8. // The above copyright notice and this permission notice shall be included in
  9. // all copies or substantial portions of the Software.
  10. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  11. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  12. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  13. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  14. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  15. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  16. // THE SOFTWARE.
  17. #if os(iOS)
  18. import UIKit
  19. #elseif os(OSX)
  20. import AppKit
  21. #endif
  22. import RxSwift
  23. import RxCocoa
  24. public struct GestureRecognizerDelegatePolicy<PolicyInput> {
  25. public typealias PolicyBody = (PolicyInput) -> Bool
  26. private let policy: PolicyBody
  27. private init(policy: @escaping PolicyBody) {
  28. self.policy = policy
  29. }
  30. public static func custom(_ policy: @escaping PolicyBody)
  31. -> GestureRecognizerDelegatePolicy<PolicyInput> {
  32. return .init(policy: policy)
  33. }
  34. public static var always: GestureRecognizerDelegatePolicy<PolicyInput> {
  35. return .init { _ in true }
  36. }
  37. public static var never: GestureRecognizerDelegatePolicy<PolicyInput> {
  38. return .init { _ in false }
  39. }
  40. fileprivate func isPolicyPassing(with args: PolicyInput) -> Bool {
  41. return policy(args)
  42. }
  43. }
  44. public final class RxGestureRecognizerDelegate: NSObject, GestureRecognizerDelegate {
  45. /// Corresponding delegate method: gestureRecognizerShouldBegin(:_)
  46. public var beginPolicy: GestureRecognizerDelegatePolicy<GestureRecognizer> = .always
  47. /// Corresponding delegate method: gestureRecognizer(_:shouldReceive:)
  48. public var touchReceptionPolicy: GestureRecognizerDelegatePolicy<(GestureRecognizer, Touch)> = .always
  49. /// Corresponding delegate method: gestureRecognizer(_:shouldBeRequiredToFailBy:)
  50. public var selfFailureRequirementPolicy: GestureRecognizerDelegatePolicy<(GestureRecognizer, GestureRecognizer)> = .never
  51. /// Corresponding delegate method: gestureRecognizer(_:shouldRequireFailureOf:)
  52. public var otherFailureRequirementPolicy: GestureRecognizerDelegatePolicy<(GestureRecognizer, GestureRecognizer)> = .never
  53. /// Corresponding delegate method: gestureRecognizer(_:shouldRecognizeSimultaneouslyWith:)
  54. public var simultaneousRecognitionPolicy: GestureRecognizerDelegatePolicy<(GestureRecognizer, GestureRecognizer)> = .always
  55. #if os(iOS)
  56. // Workaround because we can't have stored properties with @available annotation
  57. private var _pressReceptionPolicy: Any?
  58. @available(iOS 9.0, *)
  59. /// Corresponding delegate method: gestureRecognizer(_:shouldReceive:)
  60. public var pressReceptionPolicy: GestureRecognizerDelegatePolicy<(GestureRecognizer, UIPress)> {
  61. get {
  62. if let policy = _pressReceptionPolicy as? GestureRecognizerDelegatePolicy<(GestureRecognizer, UIPress)> {
  63. return policy
  64. }
  65. return GestureRecognizerDelegatePolicy<(GestureRecognizer, UIPress)>.always
  66. }
  67. set {
  68. _pressReceptionPolicy = newValue
  69. }
  70. }
  71. #endif
  72. #if os(OSX)
  73. /// Corresponding delegate method: gestureRecognizer(_:shouldAttemptToRecognizeWith:)
  74. public var eventRecognitionAttemptPolicy: GestureRecognizerDelegatePolicy<(GestureRecognizer, NSEvent)> = .always
  75. #endif
  76. public func gestureRecognizerShouldBegin(
  77. _ gestureRecognizer: GestureRecognizer
  78. ) -> Bool {
  79. return beginPolicy.isPolicyPassing(with: gestureRecognizer)
  80. }
  81. public func gestureRecognizer(
  82. _ gestureRecognizer: GestureRecognizer,
  83. shouldReceive touch: Touch
  84. ) -> Bool {
  85. return touchReceptionPolicy.isPolicyPassing(
  86. with: (gestureRecognizer, touch)
  87. )
  88. }
  89. public func gestureRecognizer(
  90. _ gestureRecognizer: GestureRecognizer,
  91. shouldRequireFailureOf otherGestureRecognizer: GestureRecognizer
  92. ) -> Bool {
  93. return otherFailureRequirementPolicy.isPolicyPassing(
  94. with: (gestureRecognizer, otherGestureRecognizer)
  95. )
  96. }
  97. public func gestureRecognizer(
  98. _ gestureRecognizer: GestureRecognizer,
  99. shouldBeRequiredToFailBy otherGestureRecognizer: GestureRecognizer
  100. ) -> Bool {
  101. return selfFailureRequirementPolicy.isPolicyPassing(
  102. with: (gestureRecognizer, otherGestureRecognizer)
  103. )
  104. }
  105. public func gestureRecognizer(
  106. _ gestureRecognizer: GestureRecognizer,
  107. shouldRecognizeSimultaneouslyWith otherGestureRecognizer: GestureRecognizer
  108. ) -> Bool {
  109. return simultaneousRecognitionPolicy.isPolicyPassing(
  110. with: (gestureRecognizer, otherGestureRecognizer)
  111. )
  112. }
  113. #if os(iOS)
  114. @available(iOS 9.0, *)
  115. public func gestureRecognizer(
  116. _ gestureRecognizer: UIGestureRecognizer,
  117. shouldReceive press: UIPress
  118. ) -> Bool {
  119. return pressReceptionPolicy.isPolicyPassing(
  120. with: (gestureRecognizer, press)
  121. )
  122. }
  123. #endif
  124. #if os(OSX)
  125. public func gestureRecognizer(
  126. _ gestureRecognizer: NSGestureRecognizer,
  127. shouldAttemptToRecognizeWith event: NSEvent
  128. ) -> Bool {
  129. return eventRecognitionAttemptPolicy.isPolicyPassing(
  130. with: (gestureRecognizer, event)
  131. )
  132. }
  133. #endif
  134. }