react-native-unistyles 2.4.0 → 2.4.1

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 (266) hide show
  1. package/README.md +72 -26
  2. package/android/CMakeLists.txt +18 -2
  3. package/android/build.gradle +5 -1
  4. package/android/src/main/cxx/cpp-adapter.cpp +16 -80
  5. package/android/src/main/cxx/helpers.cpp +61 -0
  6. package/android/src/main/cxx/helpers.h +7 -0
  7. package/android/src/main/cxx/platform.cpp +170 -0
  8. package/android/src/main/cxx/platform.h +20 -0
  9. package/android/src/main/java/com/unistyles/Models.kt +12 -70
  10. package/android/src/main/java/com/unistyles/Platform.kt +311 -10
  11. package/android/src/main/java/com/unistyles/UnistylesModule.kt +145 -153
  12. package/cxx/Macros.h +11 -0
  13. package/cxx/UnistylesImpl.cpp +310 -0
  14. package/cxx/UnistylesModel.cpp +234 -0
  15. package/cxx/UnistylesModel.h +141 -0
  16. package/cxx/UnistylesRuntime.cpp +17 -356
  17. package/cxx/UnistylesRuntime.h +71 -87
  18. package/ios/UnistylesModule.h +13 -0
  19. package/ios/UnistylesModule.mm +20 -90
  20. package/ios/platform/Platform_Shared.h +8 -0
  21. package/ios/platform/Platform_Shared.mm +160 -0
  22. package/ios/platform/Platform_iOS.h +6 -10
  23. package/ios/platform/Platform_iOS.mm +143 -93
  24. package/ios/platform/Platform_macOS.h +3 -7
  25. package/ios/platform/Platform_macOS.mm +52 -34
  26. package/ios/platform/Platform_tvOS.h +17 -0
  27. package/ios/platform/Platform_tvOS.mm +96 -0
  28. package/ios/platform/Platform_visionOS.h +20 -0
  29. package/ios/platform/Platform_visionOS.mm +120 -0
  30. package/lib/commonjs/UnistylesProvider.js +32 -0
  31. package/lib/commonjs/UnistylesProvider.js.map +1 -0
  32. package/lib/commonjs/common.js +3 -2
  33. package/lib/commonjs/common.js.map +1 -1
  34. package/lib/commonjs/core/UnistyleRegistry.js +14 -4
  35. package/lib/commonjs/core/UnistyleRegistry.js.map +1 -1
  36. package/lib/commonjs/core/Unistyles.js +7 -0
  37. package/lib/commonjs/core/Unistyles.js.map +1 -1
  38. package/lib/commonjs/core/UnistylesModule.js +52 -8
  39. package/lib/commonjs/core/UnistylesModule.js.map +1 -1
  40. package/lib/commonjs/core/UnistylesModule.native.js.map +1 -1
  41. package/lib/commonjs/core/UnistylesModule.windows.js.map +1 -1
  42. package/lib/commonjs/core/UnistylesRuntime.js +114 -4
  43. package/lib/commonjs/core/UnistylesRuntime.js.map +1 -1
  44. package/lib/commonjs/core/index.js.map +1 -1
  45. package/lib/commonjs/core/mocks/UnistylesMockedBridge.js +36 -0
  46. package/lib/commonjs/core/mocks/UnistylesMockedBridge.js.map +1 -0
  47. package/lib/commonjs/core/mocks/UnistylesMockedRegistry.js +46 -0
  48. package/lib/commonjs/core/mocks/UnistylesMockedRegistry.js.map +1 -0
  49. package/lib/commonjs/core/mocks/UnistylesMockedRuntime.js +122 -0
  50. package/lib/commonjs/core/mocks/UnistylesMockedRuntime.js.map +1 -0
  51. package/lib/commonjs/core/mocks/index.js +27 -0
  52. package/lib/commonjs/core/mocks/index.js.map +1 -0
  53. package/lib/commonjs/createStyleSheet.js.map +1 -1
  54. package/lib/commonjs/global.js.map +1 -1
  55. package/lib/commonjs/hooks/index.js.map +1 -1
  56. package/lib/commonjs/hooks/useCSS.js.map +1 -1
  57. package/lib/commonjs/hooks/useCSS.native.js.map +1 -1
  58. package/lib/commonjs/hooks/useInitialTheme.js.map +1 -1
  59. package/lib/commonjs/hooks/useSharedContext.js +77 -0
  60. package/lib/commonjs/hooks/useSharedContext.js.map +1 -0
  61. package/lib/commonjs/hooks/useUnistyles.js +18 -65
  62. package/lib/commonjs/hooks/useUnistyles.js.map +1 -1
  63. package/lib/commonjs/hooks/useVariants.js.map +1 -1
  64. package/lib/commonjs/index.js +7 -0
  65. package/lib/commonjs/index.js.map +1 -1
  66. package/lib/commonjs/normalizer/index.js.map +1 -1
  67. package/lib/commonjs/normalizer/module.d.js.map +1 -1
  68. package/lib/commonjs/normalizer/normalizeStyle.js.map +1 -1
  69. package/lib/commonjs/normalizer/normalizer.js +1 -1
  70. package/lib/commonjs/normalizer/normalizer.js.map +1 -1
  71. package/lib/commonjs/normalizer/normalizer.macos.js.map +1 -1
  72. package/lib/commonjs/plugins/cssMediaQueriesPlugin.js.map +1 -1
  73. package/lib/commonjs/plugins/index.js.map +1 -1
  74. package/lib/commonjs/plugins/normalizeWebStylesPlugin.js.map +1 -1
  75. package/lib/commonjs/types/breakpoints.js.map +1 -1
  76. package/lib/commonjs/types/color.js +2 -0
  77. package/lib/commonjs/types/color.js.map +1 -0
  78. package/lib/commonjs/types/common.js.map +1 -1
  79. package/lib/commonjs/types/core.js.map +1 -1
  80. package/lib/commonjs/types/index.js +11 -0
  81. package/lib/commonjs/types/index.js.map +1 -1
  82. package/lib/commonjs/types/normalizer.js.map +1 -1
  83. package/lib/commonjs/types/plugin.js.map +1 -1
  84. package/lib/commonjs/types/stylesheet.js.map +1 -1
  85. package/lib/commonjs/types/unistyles.js.map +1 -1
  86. package/lib/commonjs/types/variants.js.map +1 -1
  87. package/lib/commonjs/useStyles.js +1 -1
  88. package/lib/commonjs/useStyles.js.map +1 -1
  89. package/lib/commonjs/utils/breakpoints.js.map +1 -1
  90. package/lib/commonjs/utils/cssMediaQuery.js.map +1 -1
  91. package/lib/commonjs/utils/generateId.js.map +1 -1
  92. package/lib/commonjs/utils/hash32.js.map +1 -1
  93. package/lib/commonjs/utils/index.js +7 -0
  94. package/lib/commonjs/utils/index.js.map +1 -1
  95. package/lib/commonjs/utils/mq.js.map +1 -1
  96. package/lib/commonjs/utils/mqParser.js.map +1 -1
  97. package/lib/commonjs/utils/parseColor.js +35 -0
  98. package/lib/commonjs/utils/parseColor.js.map +1 -0
  99. package/lib/commonjs/utils/styles.js +15 -1
  100. package/lib/commonjs/utils/styles.js.map +1 -1
  101. package/lib/commonjs/utils/withPlugins.js.map +1 -1
  102. package/lib/module/UnistylesProvider.js +24 -0
  103. package/lib/module/UnistylesProvider.js.map +1 -0
  104. package/lib/module/common.js +2 -1
  105. package/lib/module/common.js.map +1 -1
  106. package/lib/module/core/UnistyleRegistry.js +15 -5
  107. package/lib/module/core/UnistyleRegistry.js.map +1 -1
  108. package/lib/module/core/Unistyles.js +8 -1
  109. package/lib/module/core/Unistyles.js.map +1 -1
  110. package/lib/module/core/UnistylesModule.js +52 -8
  111. package/lib/module/core/UnistylesModule.js.map +1 -1
  112. package/lib/module/core/UnistylesModule.native.js.map +1 -1
  113. package/lib/module/core/UnistylesModule.windows.js.map +1 -1
  114. package/lib/module/core/UnistylesRuntime.js +115 -4
  115. package/lib/module/core/UnistylesRuntime.js.map +1 -1
  116. package/lib/module/core/index.js +2 -1
  117. package/lib/module/core/index.js.map +1 -1
  118. package/lib/module/core/mocks/UnistylesMockedBridge.js +29 -0
  119. package/lib/module/core/mocks/UnistylesMockedBridge.js.map +1 -0
  120. package/lib/module/core/mocks/UnistylesMockedRegistry.js +39 -0
  121. package/lib/module/core/mocks/UnistylesMockedRegistry.js.map +1 -0
  122. package/lib/module/core/mocks/UnistylesMockedRuntime.js +114 -0
  123. package/lib/module/core/mocks/UnistylesMockedRuntime.js.map +1 -0
  124. package/lib/module/core/mocks/index.js +4 -0
  125. package/lib/module/core/mocks/index.js.map +1 -0
  126. package/lib/module/createStyleSheet.js.map +1 -1
  127. package/lib/module/global.js.map +1 -1
  128. package/lib/module/hooks/index.js.map +1 -1
  129. package/lib/module/hooks/useCSS.js.map +1 -1
  130. package/lib/module/hooks/useCSS.native.js.map +1 -1
  131. package/lib/module/hooks/useInitialTheme.js.map +1 -1
  132. package/lib/module/hooks/useSharedContext.js +70 -0
  133. package/lib/module/hooks/useSharedContext.js.map +1 -0
  134. package/lib/module/hooks/useUnistyles.js +19 -66
  135. package/lib/module/hooks/useUnistyles.js.map +1 -1
  136. package/lib/module/hooks/useVariants.js.map +1 -1
  137. package/lib/module/index.js +2 -2
  138. package/lib/module/index.js.map +1 -1
  139. package/lib/module/normalizer/index.js.map +1 -1
  140. package/lib/module/normalizer/module.d.js.map +1 -1
  141. package/lib/module/normalizer/normalizeStyle.js.map +1 -1
  142. package/lib/module/normalizer/normalizer.js.map +1 -1
  143. package/lib/module/normalizer/normalizer.macos.js.map +1 -1
  144. package/lib/module/plugins/cssMediaQueriesPlugin.js.map +1 -1
  145. package/lib/module/plugins/index.js.map +1 -1
  146. package/lib/module/plugins/normalizeWebStylesPlugin.js.map +1 -1
  147. package/lib/module/types/breakpoints.js.map +1 -1
  148. package/lib/module/types/color.js +2 -0
  149. package/lib/module/types/color.js.map +1 -0
  150. package/lib/module/types/common.js.map +1 -1
  151. package/lib/module/types/core.js.map +1 -1
  152. package/lib/module/types/index.js +1 -0
  153. package/lib/module/types/index.js.map +1 -1
  154. package/lib/module/types/normalizer.js.map +1 -1
  155. package/lib/module/types/plugin.js.map +1 -1
  156. package/lib/module/types/stylesheet.js.map +1 -1
  157. package/lib/module/types/unistyles.js.map +1 -1
  158. package/lib/module/types/variants.js.map +1 -1
  159. package/lib/module/useStyles.js +1 -1
  160. package/lib/module/useStyles.js.map +1 -1
  161. package/lib/module/utils/breakpoints.js.map +1 -1
  162. package/lib/module/utils/cssMediaQuery.js.map +1 -1
  163. package/lib/module/utils/generateId.js.map +1 -1
  164. package/lib/module/utils/hash32.js.map +1 -1
  165. package/lib/module/utils/index.js +1 -0
  166. package/lib/module/utils/index.js.map +1 -1
  167. package/lib/module/utils/mq.js.map +1 -1
  168. package/lib/module/utils/mqParser.js.map +1 -1
  169. package/lib/module/utils/parseColor.js +28 -0
  170. package/lib/module/utils/parseColor.js.map +1 -0
  171. package/lib/module/utils/styles.js +15 -1
  172. package/lib/module/utils/styles.js.map +1 -1
  173. package/lib/module/utils/withPlugins.js.map +1 -1
  174. package/lib/typescript/src/UnistylesProvider.d.ts +20 -0
  175. package/lib/typescript/src/UnistylesProvider.d.ts.map +1 -0
  176. package/lib/typescript/src/common.d.ts +3 -2
  177. package/lib/typescript/src/common.d.ts.map +1 -1
  178. package/lib/typescript/src/core/UnistyleRegistry.d.ts.map +1 -1
  179. package/lib/typescript/src/core/Unistyles.d.ts.map +1 -1
  180. package/lib/typescript/src/core/UnistylesModule.d.ts +1 -0
  181. package/lib/typescript/src/core/UnistylesModule.d.ts.map +1 -1
  182. package/lib/typescript/src/core/UnistylesModule.native.d.ts.map +1 -1
  183. package/lib/typescript/src/core/UnistylesModule.windows.d.ts.map +1 -1
  184. package/lib/typescript/src/core/UnistylesRuntime.d.ts +84 -8
  185. package/lib/typescript/src/core/UnistylesRuntime.d.ts.map +1 -1
  186. package/lib/typescript/src/core/index.d.ts +3 -1
  187. package/lib/typescript/src/core/index.d.ts.map +1 -1
  188. package/lib/typescript/src/core/mocks/UnistylesMockedBridge.d.ts +28 -0
  189. package/lib/typescript/src/core/mocks/UnistylesMockedBridge.d.ts.map +1 -0
  190. package/lib/typescript/src/core/mocks/UnistylesMockedRegistry.d.ts +21 -0
  191. package/lib/typescript/src/core/mocks/UnistylesMockedRegistry.d.ts.map +1 -0
  192. package/lib/typescript/src/core/mocks/UnistylesMockedRuntime.d.ts +79 -0
  193. package/lib/typescript/src/core/mocks/UnistylesMockedRuntime.d.ts.map +1 -0
  194. package/lib/typescript/src/core/mocks/index.d.ts +4 -0
  195. package/lib/typescript/src/core/mocks/index.d.ts.map +1 -0
  196. package/lib/typescript/src/createStyleSheet.d.ts.map +1 -1
  197. package/lib/typescript/src/hooks/useCSS.d.ts.map +1 -1
  198. package/lib/typescript/src/hooks/useCSS.native.d.ts.map +1 -1
  199. package/lib/typescript/src/hooks/useSharedContext.d.ts +33 -0
  200. package/lib/typescript/src/hooks/useSharedContext.d.ts.map +1 -0
  201. package/lib/typescript/src/hooks/useUnistyles.d.ts +4 -19
  202. package/lib/typescript/src/hooks/useUnistyles.d.ts.map +1 -1
  203. package/lib/typescript/src/hooks/useVariants.d.ts.map +1 -1
  204. package/lib/typescript/src/index.d.ts +7 -15
  205. package/lib/typescript/src/index.d.ts.map +1 -1
  206. package/lib/typescript/src/normalizer/normalizeStyle.d.ts.map +1 -1
  207. package/lib/typescript/src/types/breakpoints.d.ts +9 -8
  208. package/lib/typescript/src/types/breakpoints.d.ts.map +1 -1
  209. package/lib/typescript/src/types/color.d.ts +4 -0
  210. package/lib/typescript/src/types/color.d.ts.map +1 -0
  211. package/lib/typescript/src/types/core.d.ts +2 -2
  212. package/lib/typescript/src/types/core.d.ts.map +1 -1
  213. package/lib/typescript/src/types/index.d.ts +2 -1
  214. package/lib/typescript/src/types/index.d.ts.map +1 -1
  215. package/lib/typescript/src/types/normalizer.d.ts +1 -1
  216. package/lib/typescript/src/types/normalizer.d.ts.map +1 -1
  217. package/lib/typescript/src/types/stylesheet.d.ts +9 -6
  218. package/lib/typescript/src/types/stylesheet.d.ts.map +1 -1
  219. package/lib/typescript/src/types/unistyles.d.ts +20 -9
  220. package/lib/typescript/src/types/unistyles.d.ts.map +1 -1
  221. package/lib/typescript/src/useStyles.d.ts +1 -1
  222. package/lib/typescript/src/useStyles.d.ts.map +1 -1
  223. package/lib/typescript/src/utils/breakpoints.d.ts.map +1 -1
  224. package/lib/typescript/src/utils/index.d.ts +1 -0
  225. package/lib/typescript/src/utils/index.d.ts.map +1 -1
  226. package/lib/typescript/src/utils/mqParser.d.ts.map +1 -1
  227. package/lib/typescript/src/utils/parseColor.d.ts +3 -0
  228. package/lib/typescript/src/utils/parseColor.d.ts.map +1 -0
  229. package/lib/typescript/src/utils/styles.d.ts.map +1 -1
  230. package/package.json +38 -25
  231. package/react-native-unistyles.podspec +4 -1
  232. package/src/UnistylesProvider.tsx +33 -0
  233. package/src/common.ts +3 -2
  234. package/src/core/UnistyleRegistry.ts +19 -5
  235. package/src/core/Unistyles.ts +10 -1
  236. package/src/core/UnistylesModule.ts +61 -14
  237. package/src/core/UnistylesRuntime.ts +120 -5
  238. package/src/core/index.ts +7 -1
  239. package/src/core/mocks/UnistylesMockedBridge.ts +30 -0
  240. package/src/core/mocks/UnistylesMockedRegistry.ts +47 -0
  241. package/src/core/mocks/UnistylesMockedRuntime.ts +144 -0
  242. package/src/core/mocks/index.ts +3 -0
  243. package/src/hooks/useSharedContext.ts +83 -0
  244. package/src/hooks/useUnistyles.ts +15 -71
  245. package/src/index.ts +10 -4
  246. package/src/types/breakpoints.ts +19 -9
  247. package/src/types/color.ts +26 -0
  248. package/src/types/core.ts +2 -2
  249. package/src/types/index.ts +5 -1
  250. package/src/types/normalizer.ts +1 -1
  251. package/src/types/stylesheet.ts +10 -7
  252. package/src/types/unistyles.ts +28 -12
  253. package/src/useStyles.ts +1 -1
  254. package/src/utils/index.ts +1 -0
  255. package/src/utils/parseColor.ts +33 -0
  256. package/src/utils/styles.ts +24 -1
  257. package/windows/ExperimentalFeatures.props +4 -4
  258. package/windows/NuGet.Config +0 -1
  259. package/windows/ReactNativeUnistyles/ReactNativeUnistyles.h +42 -118
  260. package/windows/ReactNativeUnistyles/ReactNativeUnistyles.vcxproj +7 -2
  261. package/windows/ReactNativeUnistyles/ReactNativeUnistyles.vcxproj.filters +6 -1
  262. package/windows/ReactNativeUnistyles/packages.lock.json +31 -31
  263. package/android/src/main/java/com/unistyles/Config.kt +0 -116
  264. package/android/src/main/java/com/unistyles/Insets.kt +0 -138
  265. package/ios/UnistylesHelpers.h +0 -3
  266. package/ios/UnistylesHelpers.mm +0 -5
@@ -4,42 +4,45 @@ import android.content.BroadcastReceiver
4
4
  import android.content.Context
5
5
  import android.content.Intent
6
6
  import android.content.IntentFilter
7
+ import android.os.Build
7
8
  import android.os.Handler
8
9
  import android.os.Looper
9
10
  import android.util.Log
10
- import android.view.ViewTreeObserver
11
- import com.facebook.react.bridge.Arguments
11
+ import android.view.View
12
+ import androidx.core.view.ViewCompat
13
+ import androidx.core.view.WindowCompat
14
+ import androidx.core.view.WindowInsetsAnimationCompat
15
+ import androidx.core.view.WindowInsetsCompat
12
16
  import com.facebook.react.bridge.LifecycleEventListener
13
17
  import com.facebook.react.bridge.ReactApplicationContext
14
18
  import com.facebook.react.bridge.ReactContextBaseJavaModule
15
19
  import com.facebook.react.bridge.ReactMethod
16
- import com.facebook.react.modules.core.DeviceEventManagerModule
20
+ import com.facebook.react.turbomodule.core.interfaces.CallInvokerHolder
21
+ import kotlin.math.roundToInt
17
22
 
18
23
  class UnistylesModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext), LifecycleEventListener {
19
- private val drawHandler = Handler(Looper.getMainLooper())
20
- private val debounceDuration = 250L
21
- private var runnable: Runnable? = null
22
-
23
24
  private var isCxxReady: Boolean = false
24
- private val platform: Platform = Platform(reactContext)
25
- private val layoutListener = ViewTreeObserver.OnGlobalLayoutListener {
26
- if (this.isCxxReady) {
27
- runnable?.let { drawHandler.removeCallbacks(it) }
28
-
29
- runnable = Runnable {
30
- this@UnistylesModule.onLayoutConfigChange()
31
- }.also {
32
- drawHandler.postDelayed(it, debounceDuration)
33
- }
34
- }
35
- }
25
+ private var platform: Platform = Platform(reactContext)
26
+ private var _shouldListenToImeEvents = false
36
27
 
37
28
  private val configurationChangeReceiver = object : BroadcastReceiver() {
38
29
  override fun onReceive(context: Context, intent: Intent) {
39
- if (intent.action == Intent.ACTION_CONFIGURATION_CHANGED && this@UnistylesModule.isCxxReady) {
30
+ if (!this@UnistylesModule.isCxxReady) {
31
+ return
32
+ }
33
+
34
+ if (intent.action == Intent.ACTION_CONFIGURATION_CHANGED) {
40
35
  Handler(Looper.getMainLooper()).postDelayed({
41
36
  this@UnistylesModule.onConfigChange()
42
- }, 10)
37
+ }, 25)
38
+ }
39
+
40
+ val newConfig = context.resources.configuration
41
+
42
+ if (newConfig.orientation != platform.orientation) {
43
+ platform.orientation = newConfig.orientation
44
+
45
+ this@UnistylesModule.onLayoutConfigChange()
43
46
  }
44
47
  }
45
48
  }
@@ -49,192 +52,181 @@ class UnistylesModule(reactContext: ReactApplicationContext) : ReactContextBaseJ
49
52
  const val NAME = "Unistyles"
50
53
  }
51
54
 
52
- //region Lifecycle
53
55
  init {
54
56
  reactApplicationContext.registerReceiver(configurationChangeReceiver, IntentFilter(Intent.ACTION_CONFIGURATION_CHANGED))
57
+ reactApplicationContext.addLifecycleEventListener(this)
55
58
  }
56
59
 
57
- private fun setupLayoutListener() {
58
- val activity = currentActivity ?: return
59
- activity.window.decorView.rootView.viewTreeObserver.addOnGlobalLayoutListener(layoutListener)
60
- }
61
-
62
- @Deprecated("Deprecated in Java")
63
- override fun onCatalystInstanceDestroy() {
64
- val activity = currentActivity ?: return
65
-
66
- activity.window.decorView.rootView.viewTreeObserver.removeOnGlobalLayoutListener(layoutListener)
60
+ override fun invalidate() {
67
61
  reactApplicationContext.unregisterReceiver(configurationChangeReceiver)
68
- runnable?.let { drawHandler.removeCallbacks(it) }
69
- this.nativeDestroy()
62
+ reactApplicationContext.removeLifecycleEventListener(this)
63
+
64
+ if (this.isCxxReady) {
65
+ this.nativeDestroy()
66
+ }
70
67
  }
71
68
 
72
- //endregion
73
- //region Event handlers
74
69
  private fun onConfigChange() {
75
- if (!platform.hasNewConfig()) {
70
+ if (!this.isCxxReady) {
76
71
  return
77
72
  }
78
73
 
79
- val config = platform.getConfig()
74
+ val colorScheme = this.platform.getColorScheme()
75
+ val contentSizeCategory = this.platform.getContentSizeCategory()
80
76
 
81
77
  reactApplicationContext.runOnJSQueueThread {
82
- if (config.hasNewColorScheme) {
83
- this.nativeOnAppearanceChange(config.colorScheme)
84
- }
85
-
86
- if (config.hasNewContentSizeCategory) {
87
- this.nativeOnContentSizeCategoryChange(config.contentSizeCategory)
88
- }
78
+ this.nativeOnAppearanceChange(colorScheme)
79
+ this.nativeOnContentSizeCategoryChange(contentSizeCategory)
89
80
  }
90
81
  }
91
82
 
92
83
  private fun onLayoutConfigChange() {
93
- if (!platform.hasNewLayoutConfig()) {
84
+ if (!this.isCxxReady) {
94
85
  return
95
86
  }
96
87
 
97
- val config = platform.getLayoutConfig()
88
+ val screen = this.platform.getScreenDimensions()
89
+ val insets = this.platform.getInsets()
90
+ val statusBar = this.platform.getStatusBarDimensions()
91
+ val navigationBar = this.platform.getNavigationBarDimensions()
98
92
 
99
93
  reactApplicationContext.runOnJSQueueThread {
100
94
  this.nativeOnOrientationChange(
101
- config.screen,
102
- config.insets,
103
- config.statusBar,
104
- config.navigationBar
95
+ screen,
96
+ insets,
97
+ statusBar,
98
+ navigationBar
105
99
  )
106
100
  }
107
101
  }
108
102
 
109
- //endregion
110
- //region Core
111
103
  @ReactMethod(isBlockingSynchronousMethod = true)
112
104
  fun install(): Boolean {
113
105
  return try {
114
106
  System.loadLibrary("unistyles")
115
- val config = platform.getConfig()
116
- val layoutConfig = platform.getLayoutConfig()
117
-
118
- this.setupLayoutListener()
119
- this.reactApplicationContext.javaScriptContextHolder?.let {
120
- this.nativeInstall(
121
- it.get(),
122
- layoutConfig.screen,
123
- config.colorScheme,
124
- config.contentSizeCategory,
125
- layoutConfig.insets,
126
- layoutConfig.statusBar,
127
- layoutConfig.navigationBar
128
- )
129
- this.isCxxReady = true
130
-
131
- Log.i(NAME, "Installed Unistyles \uD83E\uDD84!")
132
-
133
- return true
107
+
108
+ this.reactApplicationContext.javaScriptContextHolder?.let { contextHolder ->
109
+ this.reactApplicationContext.catalystInstance.jsCallInvokerHolder?.let { callInvokerHolder: CallInvokerHolder ->
110
+ this.nativeInstall(contextHolder.get(), callInvokerHolder)
111
+ this.isCxxReady = true
112
+
113
+ Log.i(NAME, "Installed Unistyles \uD83E\uDD84!")
114
+
115
+ return true
116
+ }
134
117
  }
135
118
 
136
119
  false
137
120
  } catch (e: Exception) {
138
- false
121
+ this.isCxxReady = false
122
+
123
+ return false
139
124
  }
140
125
  }
141
126
 
142
- private external fun nativeInstall(
143
- jsi: Long,
144
- screen: Dimensions,
145
- colorScheme: String,
146
- contentSizeCategory: String,
147
- insets: Insets,
148
- statusBar: Dimensions,
149
- navigationBar: Dimensions
150
- )
127
+ private external fun nativeInstall(jsi: Long, callInvoker: CallInvokerHolder)
151
128
  private external fun nativeDestroy()
152
- private external fun nativeOnOrientationChange(screen: Dimensions, insets: Insets, statusBar: Dimensions, navigationBar: Dimensions)
129
+ private external fun nativeOnOrientationChange(screen: Screen, insets: Insets, statusBar: Dimensions, navigationBar: Dimensions)
153
130
  private external fun nativeOnAppearanceChange(colorScheme: String)
154
131
  private external fun nativeOnContentSizeCategoryChange(contentSizeCategory: String)
155
132
 
156
- //endregion
157
- //region Event emitter
158
- private fun onLayoutChange(breakpoint: String, orientation: String, screen: Dimensions, statusBar: Dimensions, insets: Insets, navigationBar: Dimensions) {
159
- val body = Arguments.createMap().apply {
160
- putString("type", "layout")
161
- putMap("payload", Arguments.createMap().apply {
162
- putString("breakpoint", breakpoint)
163
- putString("orientation", orientation)
164
- putMap("screen", Arguments.createMap().apply {
165
- putInt("width", screen.width)
166
- putInt("height", screen.height)
167
- })
168
- putMap("statusBar", Arguments.createMap().apply {
169
- putInt("width", statusBar.width)
170
- putInt("height", statusBar.height)
171
- })
172
- putMap("insets", Arguments.createMap().apply {
173
- putInt("top", insets.top)
174
- putInt("bottom", insets.bottom)
175
- putInt("left", insets.left)
176
- putInt("right", insets.right)
177
- })
178
- putMap("navigationBar", Arguments.createMap().apply {
179
- putInt("width", navigationBar.width)
180
- putInt("height", navigationBar.height)
181
- })
182
- })
133
+ private fun enableEdgeToEdge() {
134
+ this.reactApplicationContext.currentActivity?.let { activity ->
135
+ activity.runOnUiThread {
136
+ WindowCompat.setDecorFitsSystemWindows(activity.window, false)
137
+ }
183
138
  }
184
-
185
- reactApplicationContext
186
- .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
187
- .emit("__unistylesOnChange", body)
188
139
  }
189
140
 
190
- private fun onThemeChange(themeName: String) {
191
- val body = Arguments.createMap().apply {
192
- putString("type", "theme")
193
- putMap("payload", Arguments.createMap().apply {
194
- putString("themeName", themeName)
195
- })
196
- }
197
-
198
- reactApplicationContext
199
- .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
200
- .emit("__unistylesOnChange", body)
201
- }
141
+ @ReactMethod
142
+ fun addListener(eventName: String?) = Unit
202
143
 
203
- private fun onPluginChange() {
204
- val body = Arguments.createMap().apply {
205
- putString("type", "plugin")
206
- }
144
+ @ReactMethod
145
+ fun removeListeners(count: Double) = Unit
207
146
 
208
- reactApplicationContext
209
- .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
210
- .emit("__unistylesOnChange", body)
211
- }
147
+ override fun onHostResume() {
148
+ this.enableEdgeToEdge()
149
+ _shouldListenToImeEvents = true
212
150
 
213
- private fun onContentSizeCategoryChange(contentSizeCategory: String) {
214
- val body = Arguments.createMap().apply {
215
- putString("type", "dynamicTypeSize")
216
- putMap("payload", Arguments.createMap().apply {
217
- putString("contentSizeCategory", contentSizeCategory)
218
- })
151
+ if (isCxxReady) {
152
+ this.onConfigChange()
219
153
  }
220
154
 
221
- reactApplicationContext
222
- .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
223
- .emit("__unistylesOnChange", body)
155
+ this.reactApplicationContext.currentActivity?.let { activity ->
156
+ activity.findViewById<View>(android.R.id.content)?.let { mainView ->
157
+ ViewCompat.setOnApplyWindowInsetsListener(mainView) { _, insets ->
158
+ this.platform.setInsetsCompat(insets, activity.window, null)
159
+
160
+ if (this.isCxxReady) {
161
+ this.onLayoutConfigChange()
162
+ }
163
+
164
+ insets
165
+ }
166
+
167
+ if (platform.hasAnimatedInsets && Build.VERSION.SDK_INT >= 30) {
168
+ ViewCompat.setWindowInsetsAnimationCallback(
169
+ mainView,
170
+ object : WindowInsetsAnimationCompat.Callback(DISPATCH_MODE_STOP) {
171
+ var initialBottomInsets = 0
172
+ var isGoingUp = false
173
+
174
+ override fun onPrepare(animation: WindowInsetsAnimationCompat) {
175
+ val insets = ViewCompat.getRootWindowInsets(mainView)
176
+ val isKeyboardVisible = insets?.isVisible(WindowInsetsCompat.Type.ime()) ?: false
177
+
178
+ if (!isKeyboardVisible) {
179
+ val density = reactApplicationContext.resources.displayMetrics.density
180
+
181
+ initialBottomInsets = (this@UnistylesModule.platform.getInsets().bottom * density).roundToInt()
182
+ }
183
+
184
+ isGoingUp = !isKeyboardVisible
185
+ }
186
+
187
+ override fun onProgress(
188
+ insets: WindowInsetsCompat,
189
+ runningAnimations: List<WindowInsetsAnimationCompat>
190
+ ): WindowInsetsCompat {
191
+ if (!_shouldListenToImeEvents) {
192
+ return insets
193
+ }
194
+
195
+ runningAnimations.firstOrNull()?.let { animation ->
196
+ val progress = animation.fraction
197
+ val nextBottomInset = if (isGoingUp) {
198
+ (initialBottomInsets - (progress * initialBottomInsets).roundToInt())
199
+ } else {
200
+ // enable this in Unistyles 3.0 to get real time bottom insets
201
+ // initialBottomInsets - (initialBottomInsets - (progress * initialBottomInsets).roundToInt())
202
+ initialBottomInsets
203
+ }
204
+
205
+ this@UnistylesModule.platform.setInsetsCompat(insets, activity.window, nextBottomInset)
206
+
207
+ if (!isGoingUp) {
208
+ this@UnistylesModule.onLayoutConfigChange()
209
+ }
210
+ }
211
+
212
+ return insets
213
+ }
214
+ }
215
+ )
216
+ }
217
+ }
218
+ }
224
219
  }
225
220
 
226
- @ReactMethod
227
- fun addListener(eventName: String?) = Unit
221
+ override fun onHostPause() {
222
+ this.reactApplicationContext.currentActivity?.let { activity ->
223
+ activity.window?.decorView?.let { view ->
224
+ ViewCompat.setOnApplyWindowInsetsListener(view, null)
225
+ }
226
+ }
228
227
 
229
- @ReactMethod
230
- fun removeListeners(count: Double) = Unit
231
- override fun onHostResume() {
232
- this.onConfigChange()
233
- this.onLayoutConfigChange()
228
+ _shouldListenToImeEvents = false
234
229
  }
235
230
 
236
- override fun onHostPause() {}
237
-
238
231
  override fun onHostDestroy() {}
239
- //endregion
240
232
  }
package/cxx/Macros.h ADDED
@@ -0,0 +1,11 @@
1
+ #pragma once
2
+
3
+ #define HOST_FN(name, args, fn_body) \
4
+ jsi::Function::createFromHostFunction(rt, \
5
+ jsi::PropNameID::forAscii(rt, name), \
6
+ args, \
7
+ [this, &fnName](jsi::Runtime &rt, const jsi::Value &thisVal, const jsi::Value *arguments, size_t count) -> jsi::Value \
8
+ fn_body \
9
+ ); \
10
+
11
+ #define BIND_FN(fn) std::bind(&UnistylesRuntime::fn, this, std::placeholders::_1, std::placeholders::_2)