react-native-unistyles 3.0.0-alpha.37 → 3.0.0-alpha.39

Sign up to get free protection for your applications and to get access to all the features.
Files changed (180) hide show
  1. package/android/build.gradle +4 -4
  2. package/android/src/main/java/com/unistyles/NativePlatform+android.kt +13 -30
  3. package/android/src/main/java/com/unistyles/NativePlatform+insets.kt +22 -18
  4. package/android/src/main/java/com/unistyles/NativePlatform+listener.kt +10 -5
  5. package/cxx/core/UnistyleWrapper.h +27 -10
  6. package/cxx/core/UnistylesRegistry.cpp +34 -10
  7. package/cxx/core/UnistylesRegistry.h +2 -2
  8. package/cxx/hybridObjects/HybridNavigationBar.cpp +0 -4
  9. package/cxx/hybridObjects/HybridNavigationBar.h +0 -1
  10. package/cxx/hybridObjects/HybridShadowRegistry.cpp +7 -3
  11. package/cxx/hybridObjects/HybridStatusBar.cpp +0 -4
  12. package/cxx/hybridObjects/HybridStatusBar.h +0 -1
  13. package/cxx/hybridObjects/HybridStyleSheet.cpp +59 -28
  14. package/cxx/hybridObjects/HybridStyleSheet.h +7 -3
  15. package/cxx/hybridObjects/HybridUnistylesRuntime.cpp +37 -6
  16. package/cxx/hybridObjects/HybridUnistylesRuntime.h +7 -3
  17. package/cxx/parser/Parser.cpp +7 -7
  18. package/cxx/parser/Parser.h +2 -2
  19. package/ios/NativePlatform+ios.swift +2 -4
  20. package/ios/NativePlatform+keyboard.swift +1 -1
  21. package/ios/NativePlatformListener+ios.swift +6 -6
  22. package/ios/UnistylesModuleOnLoad.h +1 -6
  23. package/lib/commonjs/core/createUnistylesComponent.js +27 -20
  24. package/lib/commonjs/core/createUnistylesComponent.js.map +1 -1
  25. package/lib/commonjs/core/createUnistylesComponent.native.js +15 -18
  26. package/lib/commonjs/core/createUnistylesComponent.native.js.map +1 -1
  27. package/lib/commonjs/specs/NavigtionBar/index.js +0 -15
  28. package/lib/commonjs/specs/NavigtionBar/index.js.map +1 -1
  29. package/lib/commonjs/specs/ShadowRegistry/index.js +5 -7
  30. package/lib/commonjs/specs/ShadowRegistry/index.js.map +1 -1
  31. package/lib/commonjs/specs/StatusBar/index.js +0 -5
  32. package/lib/commonjs/specs/StatusBar/index.js.map +1 -1
  33. package/lib/commonjs/specs/UnistylesRuntime/index.js +0 -2
  34. package/lib/commonjs/specs/UnistylesRuntime/index.js.map +1 -1
  35. package/lib/commonjs/utils.js +26 -0
  36. package/lib/commonjs/utils.js.map +1 -0
  37. package/lib/commonjs/web/convert/index.js +1 -1
  38. package/lib/commonjs/web/convert/object/boxShadow.js +2 -1
  39. package/lib/commonjs/web/convert/object/boxShadow.js.map +1 -1
  40. package/lib/commonjs/web/convert/object/objectStyle.js +3 -2
  41. package/lib/commonjs/web/convert/object/objectStyle.js.map +1 -1
  42. package/lib/commonjs/web/convert/shadow/boxShadow.js +1 -1
  43. package/lib/commonjs/web/convert/shadow/textShadow.js +1 -1
  44. package/lib/commonjs/web/create.js +2 -1
  45. package/lib/commonjs/web/create.js.map +1 -1
  46. package/lib/commonjs/web/index.js +1 -1
  47. package/lib/commonjs/web/listener.js +18 -2
  48. package/lib/commonjs/web/listener.js.map +1 -1
  49. package/lib/commonjs/web/mock.js +1 -2
  50. package/lib/commonjs/web/mock.js.map +1 -1
  51. package/lib/commonjs/web/runtime.js +5 -2
  52. package/lib/commonjs/web/runtime.js.map +1 -1
  53. package/lib/commonjs/web/shadowRegistry.js +53 -73
  54. package/lib/commonjs/web/shadowRegistry.js.map +1 -1
  55. package/lib/commonjs/web/utils/common.js +1 -24
  56. package/lib/commonjs/web/utils/common.js.map +1 -1
  57. package/lib/commonjs/web/utils/unistyle.js.map +1 -1
  58. package/lib/commonjs/web/variants/getVariants.js +1 -1
  59. package/lib/module/core/createUnistylesComponent.js +28 -21
  60. package/lib/module/core/createUnistylesComponent.js.map +1 -1
  61. package/lib/module/core/createUnistylesComponent.native.js +16 -19
  62. package/lib/module/core/createUnistylesComponent.native.js.map +1 -1
  63. package/lib/module/specs/NavigtionBar/index.js +0 -10
  64. package/lib/module/specs/NavigtionBar/index.js.map +1 -1
  65. package/lib/module/specs/ShadowRegistry/index.js +5 -7
  66. package/lib/module/specs/ShadowRegistry/index.js.map +1 -1
  67. package/lib/module/specs/StatusBar/index.js +1 -6
  68. package/lib/module/specs/StatusBar/index.js.map +1 -1
  69. package/lib/module/specs/UnistylesRuntime/index.js +0 -2
  70. package/lib/module/specs/UnistylesRuntime/index.js.map +1 -1
  71. package/lib/module/utils.js +21 -0
  72. package/lib/module/utils.js.map +1 -0
  73. package/lib/module/web/convert/index.js +1 -1
  74. package/lib/module/web/convert/index.js.map +1 -1
  75. package/lib/module/web/convert/object/boxShadow.js +2 -1
  76. package/lib/module/web/convert/object/boxShadow.js.map +1 -1
  77. package/lib/module/web/convert/object/objectStyle.js +2 -1
  78. package/lib/module/web/convert/object/objectStyle.js.map +1 -1
  79. package/lib/module/web/convert/shadow/boxShadow.js +1 -1
  80. package/lib/module/web/convert/shadow/boxShadow.js.map +1 -1
  81. package/lib/module/web/convert/shadow/textShadow.js +1 -1
  82. package/lib/module/web/convert/shadow/textShadow.js.map +1 -1
  83. package/lib/module/web/create.js +2 -1
  84. package/lib/module/web/create.js.map +1 -1
  85. package/lib/module/web/index.js +1 -1
  86. package/lib/module/web/index.js.map +1 -1
  87. package/lib/module/web/listener.js +18 -2
  88. package/lib/module/web/listener.js.map +1 -1
  89. package/lib/module/web/mock.js +1 -2
  90. package/lib/module/web/mock.js.map +1 -1
  91. package/lib/module/web/runtime.js +6 -3
  92. package/lib/module/web/runtime.js.map +1 -1
  93. package/lib/module/web/shadowRegistry.js +51 -71
  94. package/lib/module/web/shadowRegistry.js.map +1 -1
  95. package/lib/module/web/utils/common.js +0 -22
  96. package/lib/module/web/utils/common.js.map +1 -1
  97. package/lib/module/web/utils/unistyle.js.map +1 -1
  98. package/lib/module/web/variants/getVariants.js +1 -1
  99. package/lib/module/web/variants/getVariants.js.map +1 -1
  100. package/lib/typescript/src/core/createUnistylesComponent.d.ts +1 -1
  101. package/lib/typescript/src/core/createUnistylesComponent.d.ts.map +1 -1
  102. package/lib/typescript/src/core/createUnistylesComponent.native.d.ts +1 -1
  103. package/lib/typescript/src/core/createUnistylesComponent.native.d.ts.map +1 -1
  104. package/lib/typescript/src/specs/NativePlatform/NativePlatform.nitro.d.ts +2 -4
  105. package/lib/typescript/src/specs/NativePlatform/NativePlatform.nitro.d.ts.map +1 -1
  106. package/lib/typescript/src/specs/NavigtionBar/UnistylesNavigationBar.nitro.d.ts +0 -1
  107. package/lib/typescript/src/specs/NavigtionBar/UnistylesNavigationBar.nitro.d.ts.map +1 -1
  108. package/lib/typescript/src/specs/NavigtionBar/index.d.ts +1 -15
  109. package/lib/typescript/src/specs/NavigtionBar/index.d.ts.map +1 -1
  110. package/lib/typescript/src/specs/ShadowRegistry/index.d.ts +3 -3
  111. package/lib/typescript/src/specs/ShadowRegistry/index.d.ts.map +1 -1
  112. package/lib/typescript/src/specs/StatusBar/UnistylesStatusBar.nitro.d.ts +0 -1
  113. package/lib/typescript/src/specs/StatusBar/UnistylesStatusBar.nitro.d.ts.map +1 -1
  114. package/lib/typescript/src/specs/StatusBar/index.d.ts +2 -9
  115. package/lib/typescript/src/specs/StatusBar/index.d.ts.map +1 -1
  116. package/lib/typescript/src/specs/UnistylesRuntime/index.d.ts.map +1 -1
  117. package/lib/typescript/src/utils.d.ts +2 -0
  118. package/lib/typescript/src/utils.d.ts.map +1 -0
  119. package/lib/typescript/src/web/convert/object/boxShadow.d.ts.map +1 -1
  120. package/lib/typescript/src/web/convert/object/objectStyle.d.ts.map +1 -1
  121. package/lib/typescript/src/web/create.d.ts.map +1 -1
  122. package/lib/typescript/src/web/listener.d.ts.map +1 -1
  123. package/lib/typescript/src/web/mock.d.ts.map +1 -1
  124. package/lib/typescript/src/web/runtime.d.ts +1 -1
  125. package/lib/typescript/src/web/runtime.d.ts.map +1 -1
  126. package/lib/typescript/src/web/shadowRegistry.d.ts +1 -3
  127. package/lib/typescript/src/web/shadowRegistry.d.ts.map +1 -1
  128. package/lib/typescript/src/web/utils/common.d.ts +0 -4
  129. package/lib/typescript/src/web/utils/common.d.ts.map +1 -1
  130. package/lib/typescript/src/web/utils/unistyle.d.ts +1 -1
  131. package/lib/typescript/src/web/utils/unistyle.d.ts.map +1 -1
  132. package/nitrogen/generated/android/c++/JFunc_void_UnistylesNativeMiniRuntime.hpp +57 -0
  133. package/nitrogen/generated/android/c++/JFunc_void_std__vector_UnistyleDependency__UnistylesNativeMiniRuntime.hpp +69 -0
  134. package/nitrogen/generated/android/c++/JHybridNativePlatformSpec.cpp +8 -16
  135. package/nitrogen/generated/android/c++/JHybridNativePlatformSpec.hpp +2 -4
  136. package/nitrogen/generated/android/kotlin/com/margelo/nitro/unistyles/{Func_void_std__vector_UnistyleDependency_.kt → Func_void_UnistylesNativeMiniRuntime.kt} +5 -5
  137. package/nitrogen/generated/android/kotlin/com/margelo/nitro/unistyles/{Func_void.kt → Func_void_std__vector_UnistyleDependency__UnistylesNativeMiniRuntime.kt} +5 -5
  138. package/nitrogen/generated/android/kotlin/com/margelo/nitro/unistyles/HybridNativePlatformSpec.kt +4 -12
  139. package/nitrogen/generated/android/unistylesOnLoad.cpp +4 -4
  140. package/nitrogen/generated/ios/Unistyles-Swift-Cxx-Bridge.hpp +43 -30
  141. package/nitrogen/generated/ios/c++/HybridNativePlatformSpecSwift.hpp +2 -8
  142. package/nitrogen/generated/ios/swift/HybridNativePlatformSpec.swift +2 -4
  143. package/nitrogen/generated/ios/swift/HybridNativePlatformSpecCxx.swift +10 -32
  144. package/nitrogen/generated/shared/c++/HybridNativePlatformSpec.cpp +0 -2
  145. package/nitrogen/generated/shared/c++/HybridNativePlatformSpec.hpp +2 -4
  146. package/nitrogen/generated/shared/c++/HybridUnistylesNavigationBarSpec.cpp +0 -1
  147. package/nitrogen/generated/shared/c++/HybridUnistylesNavigationBarSpec.hpp +0 -1
  148. package/nitrogen/generated/shared/c++/HybridUnistylesStatusBarSpec.cpp +0 -1
  149. package/nitrogen/generated/shared/c++/HybridUnistylesStatusBarSpec.hpp +0 -1
  150. package/package.json +4 -2
  151. package/plugin/common.js +19 -2
  152. package/plugin/index.js +12 -10
  153. package/plugin/ref.js +80 -15
  154. package/plugin/style.js +113 -18
  155. package/src/core/createUnistylesComponent.native.tsx +16 -23
  156. package/src/core/createUnistylesComponent.tsx +29 -23
  157. package/src/specs/NativePlatform/NativePlatform.nitro.ts +2 -4
  158. package/src/specs/NavigtionBar/UnistylesNavigationBar.nitro.ts +0 -1
  159. package/src/specs/NavigtionBar/index.ts +1 -30
  160. package/src/specs/ShadowRegistry/index.ts +8 -11
  161. package/src/specs/StatusBar/UnistylesStatusBar.nitro.ts +0 -1
  162. package/src/specs/StatusBar/index.ts +2 -17
  163. package/src/specs/UnistylesRuntime/index.ts +1 -2
  164. package/src/utils.ts +22 -0
  165. package/src/web/convert/index.ts +1 -1
  166. package/src/web/convert/object/boxShadow.ts +2 -1
  167. package/src/web/convert/object/objectStyle.ts +2 -1
  168. package/src/web/convert/shadow/boxShadow.ts +1 -1
  169. package/src/web/convert/shadow/textShadow.ts +1 -1
  170. package/src/web/create.ts +2 -1
  171. package/src/web/index.ts +1 -1
  172. package/src/web/listener.ts +22 -2
  173. package/src/web/mock.ts +1 -2
  174. package/src/web/runtime.ts +7 -3
  175. package/src/web/shadowRegistry.ts +55 -70
  176. package/src/web/utils/common.ts +0 -26
  177. package/src/web/utils/unistyle.ts +1 -1
  178. package/src/web/variants/getVariants.ts +1 -1
  179. package/nitrogen/generated/android/c++/JFunc_void.hpp +0 -48
  180. package/nitrogen/generated/android/c++/JFunc_void_std__vector_UnistyleDependency_.hpp +0 -60
@@ -7,8 +7,8 @@ buildscript {
7
7
  gradlePluginPortal()
8
8
  }
9
9
  dependencies {
10
- classpath("com.android.tools.build:gradle:7.3.1")
11
- classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.22")
10
+ classpath("com.android.tools.build:gradle:8.2.1")
11
+ classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.10")
12
12
  }
13
13
  }
14
14
 
@@ -32,12 +32,12 @@ if (isNewArchitectureEnabled()) {
32
32
  }
33
33
 
34
34
  android {
35
- compileSdkVersion safeExtGet('compileSdkVersion', 34)
35
+ compileSdkVersion safeExtGet('compileSdkVersion', 35)
36
36
  namespace "com.unistyles"
37
37
 
38
38
  defaultConfig {
39
39
  buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
40
- minSdkVersion safeExtGet('minSdkVersion', 23)
40
+ minSdkVersion safeExtGet('minSdkVersion', 24)
41
41
  externalNativeBuild {
42
42
  cmake {
43
43
  arguments "-DANDROID_STL=c++_shared"
@@ -8,7 +8,6 @@ import android.view.View
8
8
  import android.view.WindowManager
9
9
  import androidx.core.text.TextUtilsCompat
10
10
  import androidx.core.view.ViewCompat
11
- import androidx.core.view.WindowCompat
12
11
  import androidx.core.view.WindowInsetsCompat
13
12
  import androidx.core.view.WindowInsetsControllerCompat
14
13
  import com.facebook.react.bridge.LifecycleEventListener
@@ -23,11 +22,12 @@ import com.margelo.nitro.unistyles.UnistylesNativeMiniRuntime
23
22
  import java.util.Locale
24
23
 
25
24
  class NativePlatformAndroid(private val reactContext: ReactApplicationContext): HybridNativePlatformSpec(), LifecycleEventListener {
26
- private val _insets = NativePlatformInsets(reactContext) { this.diffMiniRuntime() }
25
+ private val _insets = NativePlatformInsets(reactContext, this::getMiniRuntime) { this.diffMiniRuntime() }
27
26
  private var _miniRuntime: UnistylesNativeMiniRuntime = buildMiniRuntime()
28
- private val _listener = NativePlatformListener(reactContext) { this.diffMiniRuntime() }
27
+ private val _listener = NativePlatformListener(reactContext, this::getMiniRuntime) { this.diffMiniRuntime() }
29
28
 
30
29
  init {
30
+ checkEdgeToEdge()
31
31
  reactContext.addLifecycleEventListener(this)
32
32
  }
33
33
 
@@ -37,7 +37,6 @@ class NativePlatformAndroid(private val reactContext: ReactApplicationContext):
37
37
  }
38
38
 
39
39
  override fun onHostResume() {
40
- enableEdgeToEdge()
41
40
  _insets.startInsetsListener()
42
41
  }
43
42
 
@@ -168,15 +167,6 @@ class NativePlatformAndroid(private val reactContext: ReactApplicationContext):
168
167
  }
169
168
  }
170
169
 
171
- @Suppress("DEPRECATION")
172
- override fun setNavigationBarBackgroundColor(color: Double) {
173
- reactContext.currentActivity?.let { activity ->
174
- activity.runOnUiThread {
175
- activity.window.navigationBarColor = color.toInt()
176
- }
177
- }
178
- }
179
-
180
170
  override fun setNavigationBarHidden(isHidden: Boolean) {
181
171
  reactContext.currentActivity?.let { activity ->
182
172
  WindowInsetsControllerCompat(activity.window, activity.window.decorView).apply {
@@ -231,16 +221,6 @@ class NativePlatformAndroid(private val reactContext: ReactApplicationContext):
231
221
  }
232
222
  }
233
223
 
234
- @Suppress("DEPRECATION")
235
- override fun setStatusBarBackgroundColor(color: Double) {
236
- reactContext.currentActivity?.let { activity ->
237
- activity.runOnUiThread {
238
- activity.window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
239
- activity.window.statusBarColor = color.toInt()
240
- }
241
- }
242
- }
243
-
244
224
  override fun setImmersiveMode(isEnabled: Boolean) {
245
225
  this.setStatusBarHidden(isEnabled)
246
226
  this.setNavigationBarHidden(isEnabled)
@@ -279,11 +259,11 @@ class NativePlatformAndroid(private val reactContext: ReactApplicationContext):
279
259
  return changedDependencies
280
260
  }
281
261
 
282
- override fun registerPlatformListener(callback: (dependencies: Array<UnistyleDependency>) -> Unit) {
262
+ override fun registerPlatformListener(callback: (dependencies: Array<UnistyleDependency>, miniRuntime: UnistylesNativeMiniRuntime) -> Unit) {
283
263
  this._listener.addPlatformListener(callback)
284
264
  }
285
265
 
286
- override fun registerImeListener(callback: () -> Unit) {
266
+ override fun registerImeListener(callback: (miniRuntime: UnistylesNativeMiniRuntime) -> Unit) {
287
267
  this._insets.addImeListener(callback)
288
268
  }
289
269
 
@@ -292,11 +272,14 @@ class NativePlatformAndroid(private val reactContext: ReactApplicationContext):
292
272
  this._insets.removeImeListeners()
293
273
  }
294
274
 
295
- private fun enableEdgeToEdge() {
296
- reactContext.currentActivity?.let { activity ->
297
- activity.runOnUiThread {
298
- WindowCompat.setDecorFitsSystemWindows(activity.window, false)
299
- }
275
+ private fun checkEdgeToEdge() {
276
+ // react-native-edge-to-edge will set setDecorFitsSystemWindows automatically
277
+ // if it's present we assume that edge-to-edge is enabled
278
+
279
+ try {
280
+ Class.forName("com.zoontek.rnedgetoedge.EdgeToEdgePackage")
281
+ } catch (exception: ClassNotFoundException) {
282
+ throw IllegalStateException("Unistyles: react-native-edge-to-edge is not installed.")
300
283
  }
301
284
  }
302
285
  }
@@ -11,10 +11,15 @@ import androidx.core.view.WindowInsetsCompat
11
11
  import com.facebook.react.bridge.ReactApplicationContext
12
12
  import com.margelo.nitro.unistyles.Insets
13
13
  import com.margelo.nitro.unistyles.UnistyleDependency
14
+ import com.margelo.nitro.unistyles.UnistylesNativeMiniRuntime
14
15
 
15
- typealias CxxImeListener = () -> Unit
16
+ typealias CxxImeListener = (miniRuntime: UnistylesNativeMiniRuntime) -> Unit
16
17
 
17
- class NativePlatformInsets(private val reactContext: ReactApplicationContext, private val diffMiniRuntime: () -> Array<UnistyleDependency>) {
18
+ class NativePlatformInsets(
19
+ private val reactContext: ReactApplicationContext,
20
+ private val getMiniRuntime: () -> UnistylesNativeMiniRuntime,
21
+ private val diffMiniRuntime: () -> Array<UnistyleDependency>
22
+ ) {
18
23
  private val _imeListeners: MutableList<CxxImeListener> = mutableListOf()
19
24
  private var _insets: Insets = Insets(0.0, 0.0, 0.0, 0.0, 0.0)
20
25
 
@@ -58,30 +63,30 @@ class NativePlatformInsets(private val reactContext: ReactApplicationContext, pr
58
63
 
59
64
  // Android 10 and below - set bottom insets to 0 while keyboard is visible and use default bottom insets otherwise
60
65
  // Android 11 and above - animate bottom insets while keyboard is appearing and disappearing
61
- val imeInsets = if (Build.VERSION.SDK_INT >= 30) {
62
- animatedBottomInsets ?: this._insets.ime
63
- } else {
64
- val nextBottomInset = insetsCompat.getInsets(WindowInsetsCompat.Type.ime()).bottom - insets.bottom
65
-
66
- // call new IME event here, as for SDK >= 30 it's called in AnimationCallback
67
- this@NativePlatformInsets.emitImeEvent()
68
-
69
- if (nextBottomInset < 0) {
70
- 0
71
- } else {
72
- nextBottomInset
66
+ val imeInsets = when {
67
+ animatedBottomInsets != null && Build.VERSION.SDK_INT >= 30 -> animatedBottomInsets
68
+ Build.VERSION.SDK_INT < 30 -> {
69
+ val nextBottomInset = insetsCompat.getInsets(WindowInsetsCompat.Type.ime()).bottom - insets.bottom
70
+ maxOf(nextBottomInset, 0).toDouble()
73
71
  }
72
+ else -> 0.0
74
73
  }
75
74
 
75
+ val shouldEmitImeEvent = Build.VERSION.SDK_INT < 30 && imeInsets != this._insets.ime || animatedBottomInsets != null && Build.VERSION.SDK_INT >= 30
76
+
76
77
  this._insets = Insets(
77
78
  statusBarTopInset.toDouble(),
78
79
  insets.bottom.toDouble(),
79
80
  insets.left.toDouble(),
80
81
  insets.right.toDouble(),
81
- imeInsets.toDouble()
82
+ imeInsets
82
83
  )
83
84
 
84
85
  diffMiniRuntime()
86
+
87
+ if (shouldEmitImeEvent) {
88
+ this@NativePlatformInsets.emitImeEvent(this.getMiniRuntime())
89
+ }
85
90
  }
86
91
 
87
92
  fun startInsetsListener() {
@@ -111,7 +116,6 @@ class NativePlatformInsets(private val reactContext: ReactApplicationContext, pr
111
116
  }
112
117
 
113
118
  this@NativePlatformInsets.setInsets(insets, activity.window, nextBottomInset)
114
- this@NativePlatformInsets.emitImeEvent()
115
119
  }
116
120
 
117
121
  return insets
@@ -123,9 +127,9 @@ class NativePlatformInsets(private val reactContext: ReactApplicationContext, pr
123
127
  }
124
128
  }
125
129
 
126
- fun emitImeEvent() {
130
+ fun emitImeEvent(miniRuntime: UnistylesNativeMiniRuntime) {
127
131
  _imeListeners.forEach { listener ->
128
- listener()
132
+ listener(miniRuntime)
129
133
  }
130
134
  }
131
135
 
@@ -8,10 +8,15 @@ import android.os.Handler
8
8
  import android.os.Looper
9
9
  import com.facebook.react.bridge.ReactApplicationContext
10
10
  import com.margelo.nitro.unistyles.UnistyleDependency
11
+ import com.margelo.nitro.unistyles.UnistylesNativeMiniRuntime
11
12
 
12
- typealias CxxDependencyListener = (dependencies: Array<UnistyleDependency>) -> Unit
13
+ typealias CxxDependencyListener = (dependencies: Array<UnistyleDependency>, miniRuntime: UnistylesNativeMiniRuntime) -> Unit
13
14
 
14
- class NativePlatformListener(private val reactContext: ReactApplicationContext, private val diffMiniRuntime: () -> Array<UnistyleDependency>) {
15
+ class NativePlatformListener(
16
+ private val reactContext: ReactApplicationContext,
17
+ private val getMiniRuntime: () -> UnistylesNativeMiniRuntime,
18
+ private val diffMiniRuntime: () -> Array<UnistyleDependency>
19
+ ) {
15
20
  private val _dependencyListeners: MutableList<CxxDependencyListener> = mutableListOf()
16
21
 
17
22
  private val configurationChangeReceiver = object : BroadcastReceiver() {
@@ -38,9 +43,9 @@ class NativePlatformListener(private val reactContext: ReactApplicationContext,
38
43
  this._dependencyListeners.clear()
39
44
  }
40
45
 
41
- private fun emitCxxEvent(dependencies: Array<UnistyleDependency>) {
46
+ private fun emitCxxEvent(dependencies: Array<UnistyleDependency>, miniRuntime: UnistylesNativeMiniRuntime) {
42
47
  this._dependencyListeners.forEach { listener ->
43
- listener(dependencies)
48
+ listener(dependencies, miniRuntime)
44
49
  }
45
50
  }
46
51
 
@@ -48,7 +53,7 @@ class NativePlatformListener(private val reactContext: ReactApplicationContext,
48
53
  val changedDependencies = diffMiniRuntime()
49
54
 
50
55
  if (changedDependencies.isNotEmpty()) {
51
- emitCxxEvent(changedDependencies)
56
+ emitCxxEvent(changedDependencies, getMiniRuntime())
52
57
  }
53
58
  }
54
59
  }
@@ -85,18 +85,35 @@ inline static jsi::Object generateUnistylesPrototype(
85
85
  }
86
86
 
87
87
  inline static std::vector<Unistyle::Shared> unistyleFromValue(jsi::Runtime& rt, const jsi::Value& value) {
88
- if (value.isNull()) {
88
+ if (value.isNull() || !value.isObject()) {
89
89
  return {};
90
90
  }
91
-
92
- auto obj = value.getObject(rt);
93
-
94
- // possible if user used React Native styles or inline styles or did spread styles
95
- if (!obj.hasNativeState(rt)) {
96
- return unistylesFromNonExistentNativeState(rt, obj);
97
- }
98
-
99
- return {value.getObject(rt).getNativeState<UnistyleWrapper>(rt)->unistyle};
91
+
92
+ auto maybeArray = value.asObject(rt);
93
+
94
+ helpers::assertThat(rt, maybeArray.isArray(rt), "Unistyles: can't retrieve Unistyle state from node as it's not an array.");
95
+
96
+ std::vector<Unistyle::Shared> unistyles;
97
+ jsi::Array unistylesArray = maybeArray.asArray(rt);
98
+
99
+ helpers::iterateJSIArray(rt, unistylesArray, [&rt, &unistyles](size_t index, jsi::Value& value){
100
+ auto obj = value.getObject(rt);
101
+
102
+ // possible if user used React Native styles or inline styles or did spread styles
103
+ if (!obj.hasNativeState(rt)) {
104
+ auto exoticUnistyles = unistylesFromNonExistentNativeState(rt, obj);
105
+
106
+ for (auto& exoticUnistyle: exoticUnistyles) {
107
+ unistyles.emplace_back(exoticUnistyle);
108
+ }
109
+
110
+ return;
111
+ }
112
+
113
+ unistyles.emplace_back(value.getObject(rt).getNativeState<UnistyleWrapper>(rt)->unistyle);
114
+ });
115
+
116
+ return unistyles;
100
117
  }
101
118
 
102
119
  inline static jsi::Value valueFromUnistyle(jsi::Runtime& rt, std::shared_ptr<HybridUnistylesRuntime> unistylesRuntime, Unistyle::Shared unistyle, int tag) {
@@ -73,13 +73,15 @@ void core::UnistylesRegistry::linkShadowNodeWithUnistyle(
73
73
  const ShadowNodeFamily* shadowNodeFamily,
74
74
  std::vector<core::Unistyle::Shared>& unistyles,
75
75
  Variants& variants,
76
- std::vector<folly::dynamic>& arguments
76
+ std::vector<std::vector<folly::dynamic>>& arguments
77
77
  ) {
78
78
  auto parser = parser::Parser(nullptr);
79
79
  shadow::ShadowLeafUpdates updates;
80
+
81
+ for (size_t index = 0; index < unistyles.size(); index++) {
82
+ Unistyle::Shared unistyle = unistyles[index];
80
83
 
81
- std::for_each(unistyles.begin(), unistyles.end(), [&, this](Unistyle::Shared unistyle){
82
- this->_shadowRegistry[&rt][shadowNodeFamily].emplace_back(std::make_shared<UnistyleData>(unistyle, variants, arguments));
84
+ this->_shadowRegistry[&rt][shadowNodeFamily].emplace_back(std::make_shared<UnistyleData>(unistyle, variants, arguments[index]));
83
85
 
84
86
  // add or update node for shadow leaf updates
85
87
  // dynamic functions are parsed later
@@ -92,7 +94,7 @@ void core::UnistylesRegistry::linkShadowNodeWithUnistyle(
92
94
  }
93
95
  }
94
96
  }
95
- });
97
+ }
96
98
 
97
99
  this->trafficController.setUpdates(updates);
98
100
  this->trafficController.resumeUnistylesTraffic();
@@ -153,6 +155,8 @@ void core::UnistylesRegistry::shadowLeafUpdateFromUnistyle(jsi::Runtime& rt, Uni
153
155
  for (const auto& [family, unistyles] : this->_shadowRegistry[&rt]) {
154
156
  for (const auto& unistyleData : unistyles) {
155
157
  if (unistyleData->unistyle == unistyle) {
158
+ // update state eg. for pressable
159
+ unistyleData->parsedStyle = jsi::Value(rt, unistyle->parsedStyle.value()).asObject(rt);
156
160
  updates[family] = parser.parseStylesToShadowTreeStyles(rt, { unistyleData });
157
161
  }
158
162
  }
@@ -161,9 +165,15 @@ void core::UnistylesRegistry::shadowLeafUpdateFromUnistyle(jsi::Runtime& rt, Uni
161
165
  this->trafficController.setUpdates(updates);
162
166
  }
163
167
 
164
- std::vector<std::shared_ptr<core::StyleSheet>> core::UnistylesRegistry::getStyleSheetsToRefresh(jsi::Runtime& rt, bool themeDidChange, bool runtimeDidChange) {
168
+ std::vector<std::shared_ptr<core::StyleSheet>> core::UnistylesRegistry::getStyleSheetsToRefresh(jsi::Runtime& rt, std::vector<UnistyleDependency>& unistylesDependencies) {
165
169
  std::vector<std::shared_ptr<core::StyleSheet>> stylesheetsToRefresh{};
166
-
170
+ auto themeDidChangeIt = std::find(unistylesDependencies.begin(),
171
+ unistylesDependencies.end(),
172
+ UnistyleDependency::THEME);
173
+ auto themeDidChange = themeDidChangeIt != unistylesDependencies.end();
174
+ auto runtimeDidChange = (themeDidChange && unistylesDependencies.size() > 1) || unistylesDependencies.size() > 0;
175
+
176
+ // if nothing changed, skip further lookup
167
177
  if (!themeDidChange && !runtimeDidChange) {
168
178
  return stylesheetsToRefresh;
169
179
  }
@@ -173,10 +183,24 @@ std::vector<std::shared_ptr<core::StyleSheet>> core::UnistylesRegistry::getStyle
173
183
  std::for_each(styleSheets.begin(), styleSheets.end(), [&](std::pair<int, std::shared_ptr<core::StyleSheet>> pair){
174
184
  auto& [_, styleSheet] = pair;
175
185
 
176
- if (styleSheet->type == StyleSheetType::ThemableWithMiniRuntime && (themeDidChange || runtimeDidChange)) {
177
- stylesheetsToRefresh.emplace_back(styleSheet);
178
-
179
- return;
186
+ if (styleSheet->type == StyleSheetType::ThemableWithMiniRuntime) {
187
+ for (const auto& unistylePair: styleSheet->unistyles) {
188
+ auto& [_, unistyle] = unistylePair;
189
+
190
+ bool hasAnyOfDependencies = std::any_of(
191
+ unistyle->dependencies.begin(),
192
+ unistyle->dependencies.end(),
193
+ [&unistylesDependencies](UnistyleDependency dep) {
194
+ return std::find(unistylesDependencies.begin(), unistylesDependencies.end(), dep) != unistylesDependencies.end();
195
+ }
196
+ );
197
+
198
+ if (hasAnyOfDependencies) {
199
+ stylesheetsToRefresh.emplace_back(styleSheet);
200
+
201
+ return;
202
+ }
203
+ }
180
204
  }
181
205
 
182
206
  if (styleSheet->type == StyleSheetType::Themable && themeDidChange) {
@@ -36,8 +36,8 @@ struct UnistylesRegistry: public StyleSheetRegistry {
36
36
 
37
37
  UnistylesState& getState(jsi::Runtime& rt);
38
38
  void createState(jsi::Runtime& rt);
39
- std::vector<std::shared_ptr<core::StyleSheet>> getStyleSheetsToRefresh(jsi::Runtime& rt, bool themeDidChange, bool runtimeDidChange);
40
- void linkShadowNodeWithUnistyle(jsi::Runtime& rt, const ShadowNodeFamily*, std::vector<core::Unistyle::Shared>& unistyles, Variants& variants, std::vector<folly::dynamic>&);
39
+ std::vector<std::shared_ptr<core::StyleSheet>> getStyleSheetsToRefresh(jsi::Runtime& rt, std::vector<UnistyleDependency>& unistylesDependencies);
40
+ void linkShadowNodeWithUnistyle(jsi::Runtime& rt, const ShadowNodeFamily*, std::vector<core::Unistyle::Shared>& unistyles, Variants& variants, std::vector<std::vector<folly::dynamic>>&);
41
41
  void unlinkShadowNodeWithUnistyles(jsi::Runtime& rt, const ShadowNodeFamily*);
42
42
  std::shared_ptr<core::StyleSheet> addStyleSheet(jsi::Runtime& rt, int tag, core::StyleSheetType type, jsi::Object&& rawValue);
43
43
  DependencyMap buildDependencyMap(jsi::Runtime& rt, std::vector<UnistyleDependency>& deps);
@@ -1,9 +1,5 @@
1
1
  #include "HybridNavigationBar.h"
2
2
 
3
- void HybridNavigationBar::setBackgroundColor(double color) {
4
- this->_nativePlatform.setNavigationBarBackgroundColor(color);
5
- };
6
-
7
3
  void HybridNavigationBar::setHidden(bool isHidden) {
8
4
  this->_nativePlatform.setNavigationBarHidden(isHidden);
9
5
  };
@@ -9,7 +9,6 @@ using namespace margelo::nitro::unistyles;
9
9
  struct HybridNavigationBar: public HybridUnistylesNavigationBarSpec {
10
10
  HybridNavigationBar(Unistyles::HybridNativePlatformSpecCxx nativePlatform): HybridObject(TAG), _nativePlatform{nativePlatform} {}
11
11
 
12
- void setBackgroundColor(double color) override;
13
12
  void setHidden(bool isHidden) override;
14
13
  double getWidth() override;
15
14
  double getHeight() override;
@@ -9,10 +9,14 @@ jsi::Value HybridShadowRegistry::link(jsi::Runtime &rt, const jsi::Value &thisVa
9
9
  ShadowNode::Shared shadowNodeWrapper = shadowNodeFromValue(rt, args[0]);
10
10
  std::vector<core::Unistyle::Shared> unistyleWrappers = core::unistyleFromValue(rt, args[1]);
11
11
  core::Variants variants = helpers::variantsToPairs(rt, args[2].asObject(rt));
12
- std::vector<folly::dynamic> arguments = helpers::parseDynamicFunctionArguments(rt, args[3].asObject(rt).asArray(rt));
13
-
12
+ jsi::Array rawArguments = args[3].asObject(rt).asArray(rt);
13
+ std::vector<std::vector<folly::dynamic>> arguments;
14
14
  auto& registry = core::UnistylesRegistry::get();
15
-
15
+
16
+ helpers::iterateJSIArray(rt, rawArguments, [&rt, &arguments](size_t index, jsi::Value& value){
17
+ arguments.push_back(helpers::parseDynamicFunctionArguments(rt, value.asObject(rt).asArray(rt)));
18
+ });
19
+
16
20
  registry.linkShadowNodeWithUnistyle(rt, &shadowNodeWrapper->getFamily(), unistyleWrappers, variants, arguments);
17
21
 
18
22
  return jsi::Value::undefined();
@@ -1,9 +1,5 @@
1
1
  #include "HybridStatusBar.h"
2
2
 
3
- void HybridStatusBar::setBackgroundColor(double color) {
4
- this->_nativePlatform.setStatusBarBackgroundColor(color);
5
- }
6
-
7
3
  double HybridStatusBar::getWidth() {
8
4
  return this->_nativePlatform.getStatusBarDimensions().width;
9
5
  }
@@ -9,7 +9,6 @@ using namespace margelo::nitro::unistyles;
9
9
  struct HybridStatusBar: public HybridUnistylesStatusBarSpec {
10
10
  HybridStatusBar(Unistyles::HybridNativePlatformSpecCxx nativePlatform): HybridObject(TAG), _nativePlatform{nativePlatform} {}
11
11
 
12
- void setBackgroundColor(double color) override;
13
12
  double getWidth() override;
14
13
  double getHeight() override;
15
14
  void setHidden(bool isHidden) override;
@@ -237,76 +237,107 @@ void HybridStyleSheet::registerHooks(jsi::Runtime& rt) {
237
237
  this->_unistylesMountHook = std::make_shared<core::UnistylesMountHook>(this->_uiManager, this->_unistylesRuntime);
238
238
  }
239
239
 
240
- void HybridStyleSheet::onPlatformDependenciesChange(std::vector<UnistyleDependency> unistylesDependencies) {
241
- if (this->_unistylesRuntime == nullptr) {
240
+ void HybridStyleSheet::onPlatformDependenciesChange(std::vector<UnistyleDependency> dependencies) {
241
+ // this event listener is triggered from C++ module, and it's only about theme / adaptive theme changes
242
+ if (dependencies.size() == 0) {
243
+ return;
244
+ }
245
+
246
+ auto& registry = core::UnistylesRegistry::get();
247
+ auto& rt = this->_unistylesRuntime->getRuntime();
248
+ auto parser = parser::Parser(this->_unistylesRuntime);
249
+ auto dependencyMap = registry.buildDependencyMap(rt, dependencies);
250
+
251
+ if (dependencyMap.empty()) {
252
+ this->notifyJSListeners(dependencies);
253
+
242
254
  return;
243
255
  }
244
256
 
245
- this->_unistylesRuntime->runOnJSThread([this, unistylesDependencies](jsi::Runtime& rt){
257
+ // in a later step, we will rebuild only Unistyles with mounted StyleSheets
258
+ // however, user may have StyleSheets with components that haven't mounted yet
259
+ // we need to rebuild all dependent StyleSheets as well
260
+ auto dependentStyleSheets = registry.getStyleSheetsToRefresh(rt, dependencies);
261
+
262
+ parser.rebuildUnistylesInDependencyMap(rt, dependencyMap, dependentStyleSheets, std::nullopt);
263
+ parser.rebuildShadowLeafUpdates(rt, dependencyMap);
264
+
265
+ this->notifyJSListeners(dependencies);
266
+ shadow::ShadowTreeManager::updateShadowTree(UIManagerBinding::getBinding(rt)->getUIManager().getShadowTreeRegistry());
267
+ }
268
+
269
+ void HybridStyleSheet::onPlatformNativeDependenciesChange(std::vector<UnistyleDependency> dependencies, UnistylesNativeMiniRuntime miniRuntime) {
270
+ // this event listener is triggered from Native platform
271
+ if (dependencies.size() == 0 || this->_unistylesRuntime == nullptr) {
272
+ return;
273
+ }
274
+
275
+ this->_unistylesRuntime->runOnJSThread([this, dependencies, miniRuntime](jsi::Runtime& rt){
246
276
  auto& registry = core::UnistylesRegistry::get();
247
277
  auto parser = parser::Parser(this->_unistylesRuntime);
248
- auto dependencies = std::move(unistylesDependencies);
278
+ auto unistyleDependencies = std::move(dependencies);
249
279
 
250
280
  // re-compute new breakpoint
251
- auto dimensionsIt = std::find(unistylesDependencies.begin(), unistylesDependencies.end(), UnistyleDependency::DIMENSIONS);
281
+ auto dimensionsIt = std::find(dependencies.begin(), dependencies.end(), UnistyleDependency::DIMENSIONS);
252
282
 
253
- if (dimensionsIt != unistylesDependencies.end()) {
283
+ if (dimensionsIt != dependencies.end()) {
254
284
  registry.getState(rt).computeCurrentBreakpoint(this->_unistylesRuntime->getScreen().width);
255
285
  }
256
286
 
257
287
  // check if color scheme changed and then if Unistyles state depend on it (adaptive themes)
258
- auto colorSchemeIt = std::find(unistylesDependencies.begin(), unistylesDependencies.end(), UnistyleDependency::COLORSCHEME);
259
- auto hasNewColorScheme = colorSchemeIt != unistylesDependencies.end();
260
-
261
- // in a later step, we will rebuild only Unistyles with mounted StyleSheets
262
- // however, user may have StyleSheets with components that haven't mounted yet
263
- // we need to rebuild all dependent StyleSheets as well
264
- auto dependentStyleSheets = registry.getStyleSheetsToRefresh(rt, hasNewColorScheme, unistylesDependencies.size() > 1);
288
+ auto colorSchemeIt = std::find(dependencies.begin(), dependencies.end(), UnistyleDependency::COLORSCHEME);
289
+ auto hasNewColorScheme = colorSchemeIt != dependencies.end();
265
290
 
266
291
  if (hasNewColorScheme) {
267
- this->_unistylesRuntime->includeDependenciesForColorSchemeChange(dependencies);
292
+ this->_unistylesRuntime->includeDependenciesForColorSchemeChange(unistyleDependencies);
268
293
  }
269
294
 
270
- auto dependencyMap = registry.buildDependencyMap(rt, dependencies);
295
+ auto dependencyMap = registry.buildDependencyMap(rt, unistyleDependencies);
271
296
 
272
297
  if (dependencyMap.empty()) {
273
- this->notifyJSListeners(dependencies);
298
+ this->notifyJSListeners(unistyleDependencies);
274
299
 
275
300
  return;
276
301
  }
277
302
 
278
- parser.rebuildUnistylesInDependencyMap(rt, dependencyMap, dependentStyleSheets);
303
+ // in a later step, we will rebuild only Unistyles with mounted StyleSheets
304
+ // however, user may have StyleSheets with components that haven't mounted yet
305
+ // we need to rebuild all dependent StyleSheets as well
306
+ auto dependentStyleSheets = registry.getStyleSheetsToRefresh(rt, unistyleDependencies);
307
+
308
+ parser.rebuildUnistylesInDependencyMap(rt, dependencyMap, dependentStyleSheets, miniRuntime);
279
309
  parser.rebuildShadowLeafUpdates(rt, dependencyMap);
280
310
 
281
- this->notifyJSListeners(dependencies);
311
+ this->notifyJSListeners(unistyleDependencies);
282
312
  shadow::ShadowTreeManager::updateShadowTree(UIManagerBinding::getBinding(rt)->getUIManager().getShadowTreeRegistry());
283
313
  });
284
314
  }
285
315
 
286
- void HybridStyleSheet::onImeChange() {
316
+ void HybridStyleSheet::onImeChange(UnistylesNativeMiniRuntime miniRuntime) {
287
317
  if (this->_unistylesRuntime == nullptr) {
288
318
  return;
289
319
  }
290
320
 
291
- this->_unistylesRuntime->runOnJSThread([this](jsi::Runtime& rt){
321
+ this->_unistylesRuntime->runOnJSThread([this, miniRuntime](jsi::Runtime& rt){
322
+ std::vector<UnistyleDependency> dependencies{UnistyleDependency::IME};
292
323
  auto& registry = core::UnistylesRegistry::get();
293
324
  auto parser = parser::Parser(this->_unistylesRuntime);
294
-
295
- std::vector<UnistyleDependency> dependencies{UnistyleDependency::IME};
296
-
297
- this->notifyJSListeners(dependencies);
298
-
299
325
  auto dependencyMap = registry.buildDependencyMap(rt, dependencies);
300
326
 
301
327
  if (dependencyMap.empty()) {
328
+ this->notifyJSListeners(dependencies);
329
+
302
330
  return;
303
331
  }
304
-
305
- std::vector<std::shared_ptr<core::StyleSheet>> styleSheet;
306
332
 
307
- parser.rebuildUnistylesInDependencyMap(rt, dependencyMap, styleSheet);
333
+ // we don't care about other unmounted stylesheets as their not visible
334
+ // so user won't see any changes
335
+ std::vector<std::shared_ptr<core::StyleSheet>> dependentStyleSheets;
336
+
337
+ parser.rebuildUnistylesInDependencyMap(rt, dependencyMap, dependentStyleSheets, miniRuntime);
308
338
  parser.rebuildShadowLeafUpdates(rt, dependencyMap);
309
339
 
340
+ this->notifyJSListeners(dependencies);
310
341
  shadow::ShadowTreeManager::updateShadowTree(UIManagerBinding::getBinding(rt)->getUIManager().getShadowTreeRegistry());
311
342
  });
312
343
  }
@@ -21,13 +21,16 @@ struct HybridStyleSheet: public HybridUnistylesStyleSheetSpec {
21
21
  this->_unistylesRuntime->registerPlatformListener(
22
22
  std::bind(&HybridStyleSheet::onPlatformDependenciesChange, this, std::placeholders::_1)
23
23
  );
24
+ this->_unistylesRuntime->registerNativePlatformListener(
25
+ std::bind(&HybridStyleSheet::onPlatformNativeDependenciesChange, this, std::placeholders::_1, std::placeholders::_2)
26
+ );
24
27
  this->_unistylesRuntime->registerImeListener(
25
- std::bind(&HybridStyleSheet::onImeChange, this)
28
+ std::bind(&HybridStyleSheet::onImeChange, this, std::placeholders::_1)
26
29
  );
27
30
  }
28
31
 
29
32
  ~HybridStyleSheet() {
30
- this->_unistylesRuntime->unregisterPlatformListeners();
33
+ this->_unistylesRuntime->unregisterNativePlatformListeners();
31
34
  }
32
35
 
33
36
  jsi::Value create(jsi::Runtime& rt,
@@ -66,7 +69,8 @@ private:
66
69
  void loadExternalMethods(const jsi::Value& thisValue, jsi::Runtime& rt);
67
70
  void registerHooks(jsi::Runtime& rt);
68
71
  void onPlatformDependenciesChange(std::vector<UnistyleDependency> dependencies);
69
- void onImeChange();
72
+ void onPlatformNativeDependenciesChange(std::vector<UnistyleDependency> dependencies, UnistylesNativeMiniRuntime miniRuntime);
73
+ void onImeChange(UnistylesNativeMiniRuntime miniRuntime);
70
74
  void notifyJSListeners(std::vector<UnistyleDependency>& dependencies);
71
75
 
72
76
  double __unid = -1;