@onekeyfe/react-native-device-utils 6.7.0-4 → 6.7.0-41

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/android/src/main/java/com/margelo/nitro/reactnativedeviceutils/ReactNativeDeviceUtils.kt +156 -35
  2. package/ios/DualScreenDetector.swift +170 -0
  3. package/ios/MainMenuController.swift +170 -0
  4. package/ios/ReactNativeDeviceUtils.swift +29 -4
  5. package/ios/WindowControlsInsetsObserver.swift +151 -0
  6. package/lib/typescript/src/ReactNativeDeviceUtils.nitro.d.ts +46 -0
  7. package/nitrogen/generated/android/c++/JFunc_void_MainMenuCommand.hpp +77 -0
  8. package/nitrogen/generated/android/c++/JFunc_void_WindowControlsInsets.hpp +77 -0
  9. package/nitrogen/generated/android/c++/JHybridReactNativeDeviceUtilsSpec.cpp +45 -0
  10. package/nitrogen/generated/android/c++/JHybridReactNativeDeviceUtilsSpec.hpp +6 -0
  11. package/nitrogen/generated/android/c++/JMainMenuCommand.hpp +61 -0
  12. package/nitrogen/generated/android/c++/JMainMenuConfig.hpp +90 -0
  13. package/nitrogen/generated/android/c++/JMainMenuLink.hpp +61 -0
  14. package/nitrogen/generated/android/c++/JWindowControlsInsets.hpp +69 -0
  15. package/nitrogen/generated/android/kotlin/com/margelo/nitro/reactnativedeviceutils/Func_void_MainMenuCommand.kt +78 -0
  16. package/nitrogen/generated/android/kotlin/com/margelo/nitro/reactnativedeviceutils/Func_void_WindowControlsInsets.kt +78 -0
  17. package/nitrogen/generated/android/kotlin/com/margelo/nitro/reactnativedeviceutils/HybridReactNativeDeviceUtilsSpec.kt +34 -0
  18. package/nitrogen/generated/android/kotlin/com/margelo/nitro/reactnativedeviceutils/MainMenuCommand.kt +24 -0
  19. package/nitrogen/generated/android/kotlin/com/margelo/nitro/reactnativedeviceutils/MainMenuConfig.kt +66 -0
  20. package/nitrogen/generated/android/kotlin/com/margelo/nitro/reactnativedeviceutils/MainMenuLink.kt +56 -0
  21. package/nitrogen/generated/android/kotlin/com/margelo/nitro/reactnativedeviceutils/WindowControlsInsets.kt +66 -0
  22. package/nitrogen/generated/android/reactnativedeviceutilsOnLoad.cpp +4 -0
  23. package/nitrogen/generated/ios/ReactNativeDeviceUtils-Swift-Cxx-Bridge.cpp +16 -0
  24. package/nitrogen/generated/ios/ReactNativeDeviceUtils-Swift-Cxx-Bridge.hpp +73 -0
  25. package/nitrogen/generated/ios/ReactNativeDeviceUtils-Swift-Cxx-Umbrella.hpp +12 -0
  26. package/nitrogen/generated/ios/c++/HybridReactNativeDeviceUtilsSpecSwift.hpp +55 -1
  27. package/nitrogen/generated/ios/swift/Func_void_MainMenuCommand.swift +46 -0
  28. package/nitrogen/generated/ios/swift/Func_void_WindowControlsInsets.swift +46 -0
  29. package/nitrogen/generated/ios/swift/HybridReactNativeDeviceUtilsSpec.swift +6 -0
  30. package/nitrogen/generated/ios/swift/HybridReactNativeDeviceUtilsSpec_cxx.swift +79 -0
  31. package/nitrogen/generated/ios/swift/MainMenuCommand.swift +44 -0
  32. package/nitrogen/generated/ios/swift/MainMenuConfig.swift +50 -0
  33. package/nitrogen/generated/ios/swift/MainMenuLink.swift +34 -0
  34. package/nitrogen/generated/ios/swift/WindowControlsInsets.swift +44 -0
  35. package/nitrogen/generated/shared/c++/HybridReactNativeDeviceUtilsSpec.cpp +6 -0
  36. package/nitrogen/generated/shared/c++/HybridReactNativeDeviceUtilsSpec.hpp +15 -0
  37. package/nitrogen/generated/shared/c++/MainMenuCommand.hpp +80 -0
  38. package/nitrogen/generated/shared/c++/MainMenuConfig.hpp +98 -0
  39. package/nitrogen/generated/shared/c++/MainMenuLink.hpp +87 -0
  40. package/nitrogen/generated/shared/c++/WindowControlsInsets.hpp +95 -0
  41. package/package.json +7 -7
  42. package/src/ReactNativeDeviceUtils.nitro.ts +54 -0
@@ -4,9 +4,11 @@ import android.app.Activity
4
4
  import android.content.Context
5
5
  import android.content.pm.PackageManager
6
6
  import android.graphics.Color
7
+ import android.graphics.drawable.GradientDrawable
7
8
  import android.graphics.Rect
8
9
  import android.os.Build
9
10
  import android.view.Gravity
11
+ import android.view.WindowInsetsController
10
12
  import android.view.View
11
13
  import android.view.ViewGroup
12
14
  import android.widget.FrameLayout
@@ -23,6 +25,7 @@ import androidx.window.layout.WindowLayoutInfo
23
25
  import androidx.window.java.layout.WindowInfoTrackerCallbackAdapter
24
26
  import androidx.appcompat.app.AppCompatDelegate
25
27
  import com.facebook.proguard.annotations.DoNotStrip
28
+ import com.facebook.react.ReactRootView
26
29
  import com.facebook.react.bridge.LifecycleEventListener
27
30
  import com.facebook.react.bridge.ReactApplicationContext
28
31
  import com.margelo.nitro.NitroModules
@@ -763,6 +766,35 @@ class ReactNativeDeviceUtils : HybridReactNativeDeviceUtilsSpec(), LifecycleEven
763
766
  spanningChangedListeners.removeIf { it.id == id }
764
767
  }
765
768
 
769
+ // Android has no window controls inside the app window: the value is always
770
+ // zero and never changes, so listeners are accepted but never called.
771
+ override fun getWindowControlsInsets(): WindowControlsInsets =
772
+ WindowControlsInsets(0.0, 0.0, 0.0, 0.0)
773
+
774
+ override fun addWindowControlsInsetsChangedListener(
775
+ callback: (insets: WindowControlsInsets) -> Unit
776
+ ): Double = synchronized(this) {
777
+ val id = nextListenerId
778
+ nextListenerId++
779
+ return@synchronized id
780
+ }
781
+
782
+ override fun removeWindowControlsInsetsChangedListener(id: Double) {}
783
+
784
+ // Android has no app-owned main menu bar: the config is ignored, and
785
+ // listeners are accepted but never called.
786
+ override fun setMainMenuConfig(config: MainMenuConfig) {}
787
+
788
+ override fun addMainMenuCommandListener(
789
+ callback: (command: MainMenuCommand) -> Unit
790
+ ): Double = synchronized(this) {
791
+ val id = nextListenerId
792
+ nextListenerId++
793
+ return@synchronized id
794
+ }
795
+
796
+ override fun removeMainMenuCommandListener(id: Double) {}
797
+
766
798
  private fun startObservingLayoutChanges() {
767
799
  if (isObservingLayoutChanges) {
768
800
  return
@@ -909,20 +941,34 @@ class ReactNativeDeviceUtils : HybridReactNativeDeviceUtilsSpec(), LifecycleEven
909
941
  val window = activity.window
910
942
  val decorView = window.decorView
911
943
 
912
- WindowCompat.getInsetsController(window, decorView)
913
- .isAppearanceLightNavigationBars = appearance.useDarkIcons
944
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
945
+ // WindowInsetsControllerCompat leaves the previous value in place on
946
+ // Android 11-14 once the window is appearance-controlled, so runtime
947
+ // theme switches kept stale button colors. Use the platform API.
948
+ window.insetsController?.setSystemBarsAppearance(
949
+ if (appearance.useDarkIcons) WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS else 0,
950
+ WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS,
951
+ )
952
+ } else {
953
+ WindowCompat.getInsetsController(window, decorView)
954
+ .isAppearanceLightNavigationBars = appearance.useDarkIcons
955
+ }
914
956
 
915
957
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
916
958
  window.isNavigationBarContrastEnforced = false
917
959
  }
918
960
 
919
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) {
920
- // Android 15 enforces edge-to-edge. Keep the platform bar transparent
921
- // and draw the requested color only behind three-button navigation.
961
+ if (
962
+ Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM ||
963
+ com.facebook.react.views.view.isEdgeToEdgeFeatureFlagOn
964
+ ) {
965
+ // Edge-to-edge windows (enforced on Android 15, opted in below it) keep
966
+ // the platform bar transparent and draw the requested color only
967
+ // behind three-button navigation.
922
968
  window.navigationBarColor = Color.TRANSPARENT
923
- installNavigationBarProtection(decorView, appearance.color)
969
+ installNavigationBarProtection(activity, appearance.color)
924
970
  } else {
925
- removeNavigationBarProtection(decorView)
971
+ removeNavigationBarProtection(activity)
926
972
  window.navigationBarColor = appearance.color
927
973
  }
928
974
  } catch (e: Exception) {
@@ -934,28 +980,40 @@ class ReactNativeDeviceUtils : HybridReactNativeDeviceUtilsSpec(), LifecycleEven
934
980
  }
935
981
  }
936
982
 
937
- private fun installNavigationBarProtection(decorView: View, color: Int) {
938
- val decorContainer = decorView as? ViewGroup ?: return
939
- val protectionView = findNavigationBarProtection(decorContainer) ?: View(
940
- decorContainer.context
941
- ).also { view ->
942
- view.tag = NAVIGATION_BAR_PROTECTION_VIEW_TAG
943
- view.isClickable = false
944
- view.isFocusable = false
945
- view.importantForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_NO
946
- decorContainer.addView(
947
- view,
948
- FrameLayout.LayoutParams(
949
- ViewGroup.LayoutParams.MATCH_PARENT,
950
- 0,
951
- Gravity.BOTTOM,
952
- ),
953
- )
954
- }
983
+ private fun installNavigationBarProtection(activity: Activity, color: Int) {
984
+ val decorView = activity.window?.decorView as? ViewGroup ?: return
985
+ val contentView = activity.findViewById<ViewGroup>(android.R.id.content)
986
+ val reactRootIndex = contentView?.let(::findReactRootIndex)
987
+ // Overlay hosts are content-view children added after the React root, and a
988
+ // dimmed overlay must cover this bar together with the rest of the screen,
989
+ // so the protection belongs directly above the React root and below them.
990
+ // While the React root does not exist yet, fall back to the decor view; the
991
+ // next call moves it into place.
992
+ val container =
993
+ if (contentView != null && reactRootIndex != null) contentView else decorView
994
+ val targetIndex =
995
+ if (container === contentView && reactRootIndex != null) {
996
+ reactRootIndex + 1
997
+ } else {
998
+ container.childCount
999
+ }
1000
+ // A detached React root falls back to the decor view while an earlier
1001
+ // protection may still live in the content view. Reuse it (moving it to the
1002
+ // fallback container) and drop any duplicate, so one activity never ends up
1003
+ // with a second protection stacked above later overlays.
1004
+ val protectionView =
1005
+ findNavigationBarProtection(contentView ?: decorView)
1006
+ ?: findNavigationBarProtection(decorView)
1007
+ ?: View(container.context).also { view ->
1008
+ view.tag = NAVIGATION_BAR_PROTECTION_VIEW_TAG
1009
+ view.isClickable = false
1010
+ view.isFocusable = false
1011
+ view.importantForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_NO
1012
+ }
1013
+ removeDuplicateNavigationBarProtections(contentView, decorView, protectionView)
1014
+ placeNavigationBarProtection(container, protectionView, targetIndex)
955
1015
 
956
- protectionView.setBackgroundColor(color)
957
1016
  protectionView.visibility = View.VISIBLE
958
- protectionView.bringToFront()
959
1017
  ViewCompat.setOnApplyWindowInsetsListener(protectionView) { view, windowInsets ->
960
1018
  val navigationBarInsets = windowInsets.getInsets(
961
1019
  WindowInsetsCompat.Type.navigationBars()
@@ -985,6 +1043,17 @@ class ReactNativeDeviceUtils : HybridReactNativeDeviceUtilsSpec(), LifecycleEven
985
1043
  )
986
1044
  else -> Triple(0, 0, Gravity.BOTTOM)
987
1045
  }
1046
+ // Fade in over the first third from the content edge so the protection never
1047
+ // draws a hard line against the page; the buttons sit on the solid rest.
1048
+ val fadeOrientation = when (gravity) {
1049
+ Gravity.LEFT -> GradientDrawable.Orientation.RIGHT_LEFT
1050
+ Gravity.RIGHT -> GradientDrawable.Orientation.LEFT_RIGHT
1051
+ else -> GradientDrawable.Orientation.TOP_BOTTOM
1052
+ }
1053
+ view.background = GradientDrawable(
1054
+ fadeOrientation,
1055
+ intArrayOf(color and 0x00FFFFFF, color, color, color),
1056
+ )
988
1057
  if (
989
1058
  layoutParams.width != width ||
990
1059
  layoutParams.height != height ||
@@ -1005,9 +1074,57 @@ class ReactNativeDeviceUtils : HybridReactNativeDeviceUtilsSpec(), LifecycleEven
1005
1074
  ViewCompat.requestApplyInsets(protectionView)
1006
1075
  }
1007
1076
 
1008
- private fun findNavigationBarProtection(decorContainer: ViewGroup): View? {
1009
- for (index in 0 until decorContainer.childCount) {
1010
- val child = decorContainer.getChildAt(index)
1077
+ /** Removes extra protection views left by an earlier fallback. */
1078
+ private fun removeDuplicateNavigationBarProtections(
1079
+ contentView: ViewGroup?,
1080
+ decorView: ViewGroup,
1081
+ keep: View,
1082
+ ) {
1083
+ for (container in listOfNotNull(contentView, decorView)) {
1084
+ for (index in container.childCount - 1 downTo 0) {
1085
+ val child = container.getChildAt(index)
1086
+ if (child !== keep && child.tag == NAVIGATION_BAR_PROTECTION_VIEW_TAG) {
1087
+ ViewCompat.setOnApplyWindowInsetsListener(child, null)
1088
+ container.removeView(child)
1089
+ }
1090
+ }
1091
+ }
1092
+ }
1093
+
1094
+ /** Index of the React root child, or null before the root view exists. */
1095
+ private fun findReactRootIndex(container: ViewGroup): Int? {
1096
+ for (index in 0 until container.childCount) {
1097
+ if (container.getChildAt(index) is ReactRootView) {
1098
+ return index
1099
+ }
1100
+ }
1101
+ return null
1102
+ }
1103
+
1104
+ private fun placeNavigationBarProtection(
1105
+ container: ViewGroup,
1106
+ protectionView: View,
1107
+ index: Int,
1108
+ ) {
1109
+ val parent = protectionView.parent as? ViewGroup
1110
+ if (parent === container && container.indexOfChild(protectionView) == index) {
1111
+ return
1112
+ }
1113
+ parent?.removeView(protectionView)
1114
+ container.addView(
1115
+ protectionView,
1116
+ index.coerceIn(0, container.childCount),
1117
+ FrameLayout.LayoutParams(
1118
+ ViewGroup.LayoutParams.MATCH_PARENT,
1119
+ 0,
1120
+ Gravity.BOTTOM,
1121
+ ),
1122
+ )
1123
+ }
1124
+
1125
+ private fun findNavigationBarProtection(container: ViewGroup): View? {
1126
+ for (index in 0 until container.childCount) {
1127
+ val child = container.getChildAt(index)
1011
1128
  if (child.tag == NAVIGATION_BAR_PROTECTION_VIEW_TAG) {
1012
1129
  return child
1013
1130
  }
@@ -1015,11 +1132,15 @@ class ReactNativeDeviceUtils : HybridReactNativeDeviceUtilsSpec(), LifecycleEven
1015
1132
  return null
1016
1133
  }
1017
1134
 
1018
- private fun removeNavigationBarProtection(decorView: View) {
1019
- val decorContainer = decorView as? ViewGroup ?: return
1020
- val protectionView = findNavigationBarProtection(decorContainer) ?: return
1021
- ViewCompat.setOnApplyWindowInsetsListener(protectionView, null)
1022
- decorContainer.removeView(protectionView)
1135
+ private fun removeNavigationBarProtection(activity: Activity?) {
1136
+ if (activity == null) return
1137
+ val decorView = activity.window?.decorView as? ViewGroup
1138
+ val contentView = activity.findViewById<ViewGroup>(android.R.id.content)
1139
+ for (container in listOfNotNull(contentView, decorView)) {
1140
+ val protectionView = findNavigationBarProtection(container) ?: continue
1141
+ ViewCompat.setOnApplyWindowInsetsListener(protectionView, null)
1142
+ container.removeView(protectionView)
1143
+ }
1023
1144
  }
1024
1145
 
1025
1146
  override fun onHostResume() {
@@ -0,0 +1,170 @@
1
+ import Foundation
2
+ import UIKit
3
+ import ReactNativeNativeLogger
4
+
5
+ /// Detects foldable iPhones such as iPhone Duo.
6
+ ///
7
+ /// Detection runs in two steps so the answer is available synchronously at
8
+ /// launch and still covers models that are not listed yet:
9
+ /// 1. The model identifier is matched against known foldable iPhones.
10
+ /// 2. A `UIHingeInteraction` (iOS 27.1+) confirms the hinge at runtime.
11
+ /// A positive result is persisted, so later launches answer synchronously
12
+ /// even for models that only the hinge check recognized.
13
+ final class DualScreenDetector {
14
+ static let shared = DualScreenDetector()
15
+
16
+ private static let cacheKey = "1k_is_dual_screen_device"
17
+ private static let foldableModelIdentifiers: Set<String> = [
18
+ "iPhone19,4", // iPhone Duo
19
+ ]
20
+
21
+ private let lock = NSLock()
22
+ private var isDualScreen: Bool
23
+ private var spanning = false
24
+ private var listeners: [Double: (Bool) -> Void] = [:]
25
+ private var nextListenerId: Double = 1
26
+ private var hingeInteraction: UIInteraction?
27
+ private var keyWindowObserver: NSObjectProtocol?
28
+ private var resignKeyWindowObserver: NSObjectProtocol?
29
+
30
+ private init() {
31
+ let modelIdentifier = DualScreenDetector.modelIdentifier()
32
+ let isKnownModel = DualScreenDetector.foldableModelIdentifiers.contains(modelIdentifier)
33
+ let isCached = UserDefaults.standard.string(forKey: DualScreenDetector.cacheKey) == modelIdentifier
34
+ isDualScreen = isKnownModel || isCached
35
+ if isKnownModel && !isCached {
36
+ UserDefaults.standard.set(modelIdentifier, forKey: DualScreenDetector.cacheKey)
37
+ }
38
+ OneKeyLog.info(
39
+ "DeviceUtils",
40
+ "Dual screen detection: model=\(modelIdentifier), knownModel=\(isKnownModel), cached=\(isCached)"
41
+ )
42
+ installHingeObserver()
43
+ }
44
+
45
+ static func modelIdentifier() -> String {
46
+ #if targetEnvironment(simulator)
47
+ if let identifier = ProcessInfo.processInfo.environment["SIMULATOR_MODEL_IDENTIFIER"],
48
+ !identifier.isEmpty {
49
+ return identifier
50
+ }
51
+ #endif
52
+ var systemInfo = utsname()
53
+ uname(&systemInfo)
54
+ return withUnsafePointer(to: &systemInfo.machine) { pointer in
55
+ pointer.withMemoryRebound(to: CChar.self, capacity: MemoryLayout.size(ofValue: pointer.pointee)) {
56
+ String(cString: $0)
57
+ }
58
+ }
59
+ }
60
+
61
+ var isDualScreenDevice: Bool {
62
+ lock.lock()
63
+ defer { lock.unlock() }
64
+ return isDualScreen
65
+ }
66
+
67
+ var isSpanning: Bool {
68
+ lock.lock()
69
+ defer { lock.unlock() }
70
+ return spanning
71
+ }
72
+
73
+ func addSpanningChangedListener(_ callback: @escaping (Bool) -> Void) -> Double {
74
+ lock.lock()
75
+ defer { lock.unlock() }
76
+ let id = nextListenerId
77
+ nextListenerId += 1
78
+ listeners[id] = callback
79
+ return id
80
+ }
81
+
82
+ func removeSpanningChangedListener(_ id: Double) {
83
+ lock.lock()
84
+ defer { lock.unlock() }
85
+ listeners.removeValue(forKey: id)
86
+ }
87
+
88
+ private func installHingeObserver() {
89
+ // UIHinge ships in the iOS 27.1 SDK (Swift 6.4). Older toolchains keep
90
+ // the model identifier and cache only.
91
+ #if compiler(>=6.4)
92
+ guard #available(iOS 27.1, *) else { return }
93
+ DispatchQueue.main.async { [weak self] in
94
+ guard let self else { return }
95
+ self.keyWindowObserver = NotificationCenter.default.addObserver(
96
+ forName: UIWindow.didBecomeKeyNotification,
97
+ object: nil,
98
+ queue: .main
99
+ ) { [weak self] notification in
100
+ guard let window = notification.object as? UIWindow else { return }
101
+ self?.attachHingeInteraction(in: window)
102
+ }
103
+ self.resignKeyWindowObserver = NotificationCenter.default.addObserver(
104
+ forName: UIWindow.didResignKeyNotification,
105
+ object: nil,
106
+ queue: .main
107
+ ) { [weak self] notification in
108
+ guard let self, let window = notification.object as? UIWindow,
109
+ self.hingeInteraction?.view === window else { return }
110
+ self.attachHingeInteraction(in: nil)
111
+ }
112
+ self.attachHingeInteraction(in: DualScreenDetector.keyWindow())
113
+ }
114
+ #endif
115
+ }
116
+
117
+ #if compiler(>=6.4)
118
+ @available(iOS 27.1, *)
119
+ private func attachHingeInteraction(in window: UIWindow?) {
120
+ if let interaction = hingeInteraction, interaction.view === window { return }
121
+ let previousInteraction = hingeInteraction
122
+ hingeInteraction = nil
123
+ if let interaction = previousInteraction {
124
+ interaction.view?.removeInteraction(interaction)
125
+ }
126
+ handleHingeUpdate(nil)
127
+ // A listener can change the key window while spanning is reset.
128
+ guard let window, window.isKeyWindow, hingeInteraction == nil else { return }
129
+ let interaction = UIHingeInteraction { [weak self] interaction, update in
130
+ guard let self, self.hingeInteraction === interaction else { return }
131
+ self.handleHingeUpdate(update.hinge)
132
+ }
133
+ hingeInteraction = interaction
134
+ window.addInteraction(interaction)
135
+ }
136
+
137
+ @available(iOS 27.1, *)
138
+ private func handleHingeUpdate(_ hinge: UIHinge?) {
139
+ let nextSpanning = hinge?.status == .fullyOpen || hinge?.status == .partiallyOpen
140
+
141
+ lock.lock()
142
+ let isNewlyDetected = hinge != nil && !isDualScreen
143
+ if hinge != nil { isDualScreen = true }
144
+ let didChangeSpanning = spanning != nextSpanning
145
+ spanning = nextSpanning
146
+ let callbacks = Array(listeners.values)
147
+ lock.unlock()
148
+
149
+ if isNewlyDetected {
150
+ UserDefaults.standard.set(DualScreenDetector.modelIdentifier(), forKey: DualScreenDetector.cacheKey)
151
+ OneKeyLog.info(
152
+ "DeviceUtils",
153
+ "Dual screen detected by hinge: model=\(DualScreenDetector.modelIdentifier())"
154
+ )
155
+ }
156
+ // A newly detected device also notifies, so JS can react without
157
+ // waiting for the next fold.
158
+ if didChangeSpanning || isNewlyDetected {
159
+ callbacks.forEach { $0(nextSpanning) }
160
+ }
161
+ }
162
+ #endif
163
+
164
+ private static func keyWindow() -> UIWindow? {
165
+ UIApplication.shared.connectedScenes
166
+ .compactMap { $0 as? UIWindowScene }
167
+ .flatMap { $0.windows }
168
+ .first { $0.isKeyWindow }
169
+ }
170
+ }
@@ -0,0 +1,170 @@
1
+ import Foundation
2
+ import UIKit
3
+ import ReactNativeNativeLogger
4
+
5
+ /// Owns the app's custom main menu (the iPadOS menu bar).
6
+ ///
7
+ /// JS supplies localized titles and Help links through `setMainMenuConfig`.
8
+ /// The app delegate forwards `buildMenu(with:)` to `buildMenu(withBuilder:)`
9
+ /// (it reaches this class through `NSClassFromString`, because the app target
10
+ /// cannot import Nitro modules). App commands are `UIKeyCommand`s whose action
11
+ /// lives on `UIApplication`, which is always in the responder chain, so the
12
+ /// app delegate needs no command handler.
13
+ public final class MainMenuController: NSObject {
14
+ static let shared = MainMenuController()
15
+
16
+ private struct Link {
17
+ let title: String
18
+ let url: URL
19
+ }
20
+
21
+ private struct Config {
22
+ let aboutTitle: String
23
+ let preferencesTitle: String
24
+ let lockNowTitle: String
25
+ let helpLinks: [Link]
26
+ }
27
+
28
+ private static let aboutMenuIdentifier = UIMenu.Identifier("so.onekey.mainMenu.about")
29
+ private static let settingsMenuIdentifier = UIMenu.Identifier("so.onekey.mainMenu.settings")
30
+
31
+ private let lock = NSLock()
32
+ private var config: Config?
33
+ private var listeners: [Double: (MainMenuCommand) -> Void] = [:]
34
+ private var nextListenerId: Double = 1
35
+
36
+ func setConfig(_ config: MainMenuConfig) {
37
+ let links = config.helpLinks.compactMap { link -> Link? in
38
+ guard let url = URL(string: link.url) else {
39
+ OneKeyLog.warn("DeviceUtils", "Main menu: invalid help link URL")
40
+ return nil
41
+ }
42
+ return Link(title: link.title, url: url)
43
+ }
44
+ let next = Config(
45
+ aboutTitle: config.aboutTitle,
46
+ preferencesTitle: config.preferencesTitle,
47
+ lockNowTitle: config.lockNowTitle,
48
+ helpLinks: links
49
+ )
50
+ lock.lock()
51
+ self.config = next
52
+ lock.unlock()
53
+ DispatchQueue.main.async {
54
+ UIMenuSystem.main.setNeedsRebuild()
55
+ }
56
+ }
57
+
58
+ func addListener(_ callback: @escaping (MainMenuCommand) -> Void) -> Double {
59
+ lock.lock()
60
+ defer { lock.unlock() }
61
+ let id = nextListenerId
62
+ nextListenerId += 1
63
+ listeners[id] = callback
64
+ return id
65
+ }
66
+
67
+ func removeListener(_ id: Double) {
68
+ lock.lock()
69
+ defer { lock.unlock() }
70
+ listeners.removeValue(forKey: id)
71
+ }
72
+
73
+ /// Called by the app delegate's `buildMenu(with:)` on the main thread.
74
+ @objc(buildMenuWithBuilder:)
75
+ public static func buildMenu(withBuilder builder: UIMenuBuilder) {
76
+ shared.build(builder)
77
+ }
78
+
79
+ fileprivate func perform(commandName: String) {
80
+ guard let command = MainMenuCommand(fromString: commandName) else { return }
81
+ lock.lock()
82
+ let callbacks = Array(listeners.values)
83
+ lock.unlock()
84
+ OneKeyLog.info("DeviceUtils", "Main menu command: \(commandName), listeners=\(callbacks.count)")
85
+ callbacks.forEach { $0(command) }
86
+ }
87
+
88
+ private func build(_ builder: UIMenuBuilder) {
89
+ guard builder.system == .main else { return }
90
+
91
+ // A wallet has no documents to open or text to format.
92
+ builder.remove(menu: .file)
93
+ builder.remove(menu: .format)
94
+
95
+ lock.lock()
96
+ let config = self.config
97
+ lock.unlock()
98
+ // Keep the system app and Help menus until JS provides localized titles.
99
+ guard let config else { return }
100
+
101
+ let action = #selector(UIApplication.onekeyPerformMainMenuCommand(_:))
102
+ let about = UICommand(
103
+ title: config.aboutTitle,
104
+ action: action,
105
+ propertyList: MainMenuCommand.about.stringValue
106
+ )
107
+ let preferences = UIKeyCommand(
108
+ title: config.preferencesTitle,
109
+ action: action,
110
+ input: ",",
111
+ modifierFlags: .command,
112
+ propertyList: MainMenuCommand.preferences.stringValue
113
+ )
114
+ let lockNow = UIKeyCommand(
115
+ title: config.lockNowTitle,
116
+ action: action,
117
+ input: "l",
118
+ modifierFlags: [.command, .shift],
119
+ propertyList: MainMenuCommand.locknow.stringValue
120
+ )
121
+ let aboutMenu = UIMenu(
122
+ title: "",
123
+ identifier: MainMenuController.aboutMenuIdentifier,
124
+ options: .displayInline,
125
+ children: [about]
126
+ )
127
+ let settingsMenu = UIMenu(
128
+ title: "",
129
+ identifier: MainMenuController.settingsMenuIdentifier,
130
+ options: .displayInline,
131
+ children: [preferences, lockNow]
132
+ )
133
+
134
+ // The system Preferences item opens the app's page in the Settings app;
135
+ // replace it with the in-app preferences, which own the same shortcut.
136
+ if builder.menu(for: .preferences) != nil {
137
+ builder.replace(menu: .preferences, with: settingsMenu)
138
+ } else {
139
+ builder.insertChild(settingsMenu, atStartOfMenu: .application)
140
+ }
141
+ // An empty About title removes the item (Travel Mode hides About).
142
+ if config.aboutTitle.isEmpty {
143
+ builder.remove(menu: .about)
144
+ } else if builder.menu(for: .about) != nil {
145
+ builder.replace(menu: .about, with: aboutMenu)
146
+ } else {
147
+ builder.insertSibling(aboutMenu, beforeMenu: MainMenuController.settingsMenuIdentifier)
148
+ }
149
+
150
+ if config.helpLinks.isEmpty {
151
+ builder.remove(menu: .help)
152
+ } else if builder.menu(for: .help) != nil {
153
+ let links = config.helpLinks.map { link in
154
+ UIAction(title: link.title) { _ in
155
+ UIApplication.shared.open(link.url)
156
+ }
157
+ }
158
+ builder.replaceChildren(ofMenu: .help) { _ in links }
159
+ }
160
+ }
161
+ }
162
+
163
+ extension UIApplication {
164
+ /// Action of the main menu app commands; the command name is the
165
+ /// command's property list.
166
+ @objc func onekeyPerformMainMenuCommand(_ sender: UICommand) {
167
+ guard let name = sender.propertyList as? String else { return }
168
+ MainMenuController.shared.perform(commandName: name)
169
+ }
170
+ }
@@ -112,14 +112,14 @@ class ReactNativeDeviceUtils: HybridReactNativeDeviceUtilsSpec {
112
112
  }
113
113
 
114
114
  public func isDualScreenDevice() throws -> Bool {
115
- return false
115
+ return DualScreenDetector.shared.isDualScreenDevice
116
116
  }
117
117
 
118
118
  public func initEventListeners() throws -> Void {
119
119
  }
120
-
120
+
121
121
  public func isSpanning() throws -> Bool {
122
- return false
122
+ return DualScreenDetector.shared.isSpanning
123
123
  }
124
124
 
125
125
  public func getWindowRects() throws -> Promise<[DualScreenInfoRect]> {
@@ -132,10 +132,35 @@ class ReactNativeDeviceUtils: HybridReactNativeDeviceUtilsSpec {
132
132
 
133
133
 
134
134
  func addSpanningChangedListener(callback: @escaping (Bool) -> Void) throws -> Double {
135
- return 0
135
+ return DualScreenDetector.shared.addSpanningChangedListener(callback)
136
136
  }
137
137
 
138
138
  func removeSpanningChangedListener(id: Double) throws -> Void {
139
+ DualScreenDetector.shared.removeSpanningChangedListener(id)
140
+ }
141
+
142
+ func getWindowControlsInsets() throws -> WindowControlsInsets {
143
+ return WindowControlsInsetsObserver.shared.currentInsets
144
+ }
145
+
146
+ func addWindowControlsInsetsChangedListener(callback: @escaping (WindowControlsInsets) -> Void) throws -> Double {
147
+ return WindowControlsInsetsObserver.shared.addListener(callback)
148
+ }
149
+
150
+ func removeWindowControlsInsetsChangedListener(id: Double) throws -> Void {
151
+ WindowControlsInsetsObserver.shared.removeListener(id)
152
+ }
153
+
154
+ func setMainMenuConfig(config: MainMenuConfig) throws -> Void {
155
+ MainMenuController.shared.setConfig(config)
156
+ }
157
+
158
+ func addMainMenuCommandListener(callback: @escaping (MainMenuCommand) -> Void) throws -> Double {
159
+ return MainMenuController.shared.addListener(callback)
160
+ }
161
+
162
+ func removeMainMenuCommandListener(id: Double) throws -> Void {
163
+ MainMenuController.shared.removeListener(id)
139
164
  }
140
165
 
141
166