1 // Copyright 2015-present 650 Industries. All rights reserved.
2 
3 import Foundation
4 import UIKit
5 
6 class DevMenuMotionInterceptor {
7   /**
8    Returns bool value whether the dev menu shake gestures are being intercepted.
9    */
10   static var isInstalled: Bool = false {
11     willSet {
12       if isInstalled != newValue {
13         // Capture shake gesture from any window by swizzling default implementation from UIWindow.
14         swizzle()
15       }
16     }
17   }
18 
19   static var isEnabled: Bool = true
20 
swizzlenull21   static private func swizzle() {
22     DevMenuUtils.swizzle(
23       selector: #selector(UIWindow.motionEnded(_:with:)),
24       withSelector: #selector(UIWindow.EXDevMenu_motionEnded(_:with:)),
25       forClass: UIWindow.self
26     )
27   }
28 }
29 
30 extension UIWindow {
31   @objc
EXDevMenu_motionEndednull32   func EXDevMenu_motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
33     if event?.subtype == .motionShake && DevMenuMotionInterceptor.isEnabled {
34       DevMenuManager.shared.toggleMenu()
35     }
36   }
37 }
38