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
@@ -1,7 +1,5 @@
1
1
  #import "UnistylesModule.h"
2
- #import "UnistylesHelpers.h"
3
2
  #import "UnistylesRuntime.h"
4
-
5
3
  #import <React/RCTBridge+Private.h>
6
4
  #import <jsi/jsi.h>
7
5
 
@@ -17,10 +15,14 @@ RCT_EXPORT_MODULE(Unistyles)
17
15
  if ((self = [super init])) {
18
16
  self.platform = [[Platform alloc] init];
19
17
  }
20
-
18
+
21
19
  return self;
22
20
  }
23
21
 
22
+ - (void)dealloc {
23
+ [self.platform clean];
24
+ }
25
+
24
26
  + (BOOL)requiresMainQueueSetup {
25
27
  return YES;
26
28
  }
@@ -47,106 +49,34 @@ RCT_EXPORT_MODULE(Unistyles)
47
49
  #pragma mark - Core
48
50
 
49
51
  RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(install) {
50
- RCTBridge* bridge = [RCTBridge currentBridge];
51
- RCTCxxBridge* cxxBridge = (RCTCxxBridge*)bridge;
52
-
53
- if (cxxBridge == nil) {
54
- return @false;
55
- }
56
-
57
- auto jsiRuntime = (jsi::Runtime*)cxxBridge.runtime;
58
-
59
- if (jsiRuntime == nil) {
52
+ UnistylesModule *__weak weakSelf = self;
53
+ RCTBridge *bridge = self.bridge;
54
+
55
+ if (bridge == nullptr) {
60
56
  return @false;
61
57
  }
62
58
 
63
- auto& runtime = *jsiRuntime;
64
- UnistylesModule *__weak weakSelf = self;
65
-
66
- registerUnistylesHostObject(runtime, weakSelf);
59
+ registerUnistylesHostObject(bridge, weakSelf);
67
60
 
68
61
  NSLog(@"Installed Unistyles 🦄!");
69
62
 
70
63
  return @true;
71
64
  }
72
65
 
73
- void registerUnistylesHostObject(jsi::Runtime &runtime, UnistylesModule* weakSelf) {
74
- auto unistylesRuntime = std::make_shared<UnistylesRuntime>(
75
- weakSelf.platform.initialScreen,
76
- weakSelf.platform.initialColorScheme,
77
- weakSelf.platform.initialContentSizeCategory,
78
- weakSelf.platform.initialInsets,
79
- weakSelf.platform.initialStatusBar,
80
- weakSelf.platform.initialNavigationBar
81
- );
82
-
83
- unistylesRuntime.get()->onThemeChange([=](std::string theme) {
84
- NSDictionary *body = @{
85
- @"type": @"theme",
86
- @"payload": @{
87
- @"themeName": cxxStringToNSString(theme)
88
- }
89
- };
90
-
91
- [weakSelf emitEvent:@"__unistylesOnChange" withBody:body];
92
- });
93
-
94
- unistylesRuntime.get()->onLayoutChange([=](std::string breakpoint, std::string orientation, Dimensions& screen, Dimensions& statusBar, Insets& insets, Dimensions& navigationBar) {
95
- NSDictionary *body = @{
96
- @"type": @"layout",
97
- @"payload": @{
98
- @"breakpoint": cxxStringToNSString(breakpoint),
99
- @"orientation": cxxStringToNSString(orientation),
100
- @"screen": @{
101
- @"width": @(screen.width),
102
- @"height": @(screen.height)
103
- },
104
- @"statusBar": @{
105
- @"width": @(statusBar.width),
106
- @"height": @(statusBar.height)
107
- },
108
- @"navigationBar": @{
109
- @"width": @(navigationBar.width),
110
- @"height": @(navigationBar.height)
111
- },
112
- @"insets": @{
113
- @"top": @(insets.top),
114
- @"bottom": @(insets.bottom),
115
- @"left": @(insets.left),
116
- @"right": @(insets.right)
117
- }
118
- }
119
- };
120
-
121
- [weakSelf emitEvent:@"__unistylesOnChange" withBody:body];
122
- });
66
+ void registerUnistylesHostObject(RCTBridge* bridge, UnistylesModule* weakSelf) {
67
+ std::shared_ptr<react::CallInvoker> callInvoker = bridge.jsCallInvoker;
68
+ auto runOnJSThread = [callInvoker](std::function<void(jsi::Runtime& rt)> &&callback){
69
+ callInvoker->invokeAsync(std::move(callback));
70
+ };
123
71
 
124
- unistylesRuntime.get()->onPluginChange([=]() {
125
- NSDictionary *body = @{
126
- @"type": @"plugin"
127
- };
128
-
129
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
130
- [weakSelf emitEvent:@"__unistylesOnChange" withBody:body];
131
- });
132
- });
133
-
134
- unistylesRuntime.get()->onContentSizeCategoryChange([=](std::string contentSizeCategory) {
135
- NSDictionary *body = @{
136
- @"type": @"dynamicTypeSize",
137
- @"payload": @{
138
- @"contentSizeCategory": cxxStringToNSString(contentSizeCategory)
139
- }
140
- };
141
-
142
- [weakSelf emitEvent:@"__unistylesOnChange" withBody:body];
143
- });
72
+ jsi::Runtime* runtime = reinterpret_cast<jsi::Runtime*>(bridge.runtime);
73
+ auto unistylesRuntime = std::make_shared<UnistylesRuntime>(runOnJSThread);
144
74
 
145
- weakSelf.platform.unistylesRuntime = unistylesRuntime.get();
75
+ [weakSelf.platform makeShared:unistylesRuntime.get()];
146
76
 
147
- auto hostObject = jsi::Object::createFromHostObject(runtime, unistylesRuntime);
77
+ auto hostObject = jsi::Object::createFromHostObject(*runtime, unistylesRuntime);
148
78
 
149
- runtime.global().setProperty(runtime, "__UNISTYLES__", std::move(hostObject));
79
+ runtime->global().setProperty(*runtime, "__UNISTYLES__", std::move(hostObject));
150
80
  }
151
81
 
152
82
  @end
@@ -0,0 +1,8 @@
1
+ #import <UIKit/UIKit.h>
2
+ #import "UnistylesRuntime.h"
3
+ #include <string>
4
+
5
+ std::string getContentSizeCategory();
6
+ float getFontScale();
7
+ std::string getColorScheme();
8
+ UIColor* colorFromHexString(NSString* hexString, float alpha);
@@ -0,0 +1,160 @@
1
+ #if TARGET_OS_TV || TARGET_OS_VISION || TARGET_OS_IOS
2
+
3
+ #include "Platform_Shared.h"
4
+
5
+ std::string getContentSizeCategory() {
6
+ UIContentSizeCategory contentSizeCategory = [[UIApplication sharedApplication] preferredContentSizeCategory];
7
+
8
+ if ([contentSizeCategory isEqualToString:UIContentSizeCategoryExtraExtraExtraLarge]) {
9
+ return std::string([@"xxxLarge" UTF8String]);
10
+ }
11
+
12
+ if ([contentSizeCategory isEqualToString:UIContentSizeCategoryExtraExtraLarge]) {
13
+ return std::string([@"xxLarge" UTF8String]);
14
+ }
15
+
16
+ if ([contentSizeCategory isEqualToString:UIContentSizeCategoryExtraLarge]) {
17
+ return std::string([@"xLarge" UTF8String]);
18
+ }
19
+
20
+ if ([contentSizeCategory isEqualToString:UIContentSizeCategoryLarge]) {
21
+ return std::string([@"Large" UTF8String]);
22
+ }
23
+
24
+ if ([contentSizeCategory isEqualToString:UIContentSizeCategoryMedium]) {
25
+ return std::string([@"Medium" UTF8String]);
26
+ }
27
+
28
+ if ([contentSizeCategory isEqualToString:UIContentSizeCategorySmall]) {
29
+ return std::string([@"Small" UTF8String]);
30
+ }
31
+
32
+ if ([contentSizeCategory isEqualToString:UIContentSizeCategoryExtraSmall]) {
33
+ return std::string([@"xSmall" UTF8String]);
34
+ }
35
+
36
+ if ([contentSizeCategory isEqualToString:UIContentSizeCategoryAccessibilityMedium]) {
37
+ return std::string([@"accessibilityMedium" UTF8String]);
38
+ }
39
+
40
+ if ([contentSizeCategory isEqualToString:UIContentSizeCategoryAccessibilityLarge]) {
41
+ return std::string([@"accessibilityLarge" UTF8String]);
42
+ }
43
+
44
+ if ([contentSizeCategory isEqualToString:UIContentSizeCategoryAccessibilityExtraLarge]) {
45
+ return std::string([@"accessibilityExtraLarge" UTF8String]);
46
+ }
47
+
48
+ if ([contentSizeCategory isEqualToString:UIContentSizeCategoryAccessibilityExtraExtraLarge]) {
49
+ return std::string([@"accessibilityExtraExtraLarge" UTF8String]);
50
+ }
51
+
52
+ if ([contentSizeCategory isEqualToString:UIContentSizeCategoryAccessibilityExtraExtraExtraLarge]) {
53
+ return std::string([@"accessibilityExtraExtraExtraLarge" UTF8String]);
54
+ }
55
+
56
+ return std::string([@"unspecified" UTF8String]);
57
+ }
58
+
59
+ // based on Apple Human Interface Guidelines
60
+ // https://developer.apple.com/design/human-interface-guidelines/typography#Specifications
61
+ float getFontScale() {
62
+ UIContentSizeCategory contentSizeCategory = [[UIApplication sharedApplication] preferredContentSizeCategory];
63
+ float defaultMultiplier = 17.0;
64
+
65
+ if ([contentSizeCategory isEqualToString:UIContentSizeCategoryExtraExtraExtraLarge]) {
66
+ return 23.0 / defaultMultiplier;
67
+ }
68
+
69
+ if ([contentSizeCategory isEqualToString:UIContentSizeCategoryExtraExtraLarge]) {
70
+ return 21.0 / defaultMultiplier;
71
+ }
72
+
73
+ if ([contentSizeCategory isEqualToString:UIContentSizeCategoryExtraLarge]) {
74
+ return 19.0 / defaultMultiplier;
75
+ }
76
+
77
+ if ([contentSizeCategory isEqualToString:UIContentSizeCategoryLarge]) {
78
+ return 17.0 / defaultMultiplier;
79
+ }
80
+
81
+ if ([contentSizeCategory isEqualToString:UIContentSizeCategoryMedium]) {
82
+ return 16.0 / defaultMultiplier;
83
+ }
84
+
85
+ if ([contentSizeCategory isEqualToString:UIContentSizeCategorySmall]) {
86
+ return 15.0 / defaultMultiplier;
87
+ }
88
+
89
+ if ([contentSizeCategory isEqualToString:UIContentSizeCategoryExtraSmall]) {
90
+ return 14.0 / defaultMultiplier;
91
+ }
92
+
93
+ if ([contentSizeCategory isEqualToString:UIContentSizeCategoryAccessibilityMedium]) {
94
+ return 29.0 / defaultMultiplier;
95
+ }
96
+
97
+ if ([contentSizeCategory isEqualToString:UIContentSizeCategoryAccessibilityLarge]) {
98
+ return 33.0 / defaultMultiplier;
99
+ }
100
+
101
+ if ([contentSizeCategory isEqualToString:UIContentSizeCategoryAccessibilityExtraLarge]) {
102
+ return 40.0 / defaultMultiplier;
103
+ }
104
+
105
+ if ([contentSizeCategory isEqualToString:UIContentSizeCategoryAccessibilityExtraExtraLarge]) {
106
+ return 47.0 / defaultMultiplier;
107
+ }
108
+
109
+ if ([contentSizeCategory isEqualToString:UIContentSizeCategoryAccessibilityExtraExtraExtraLarge]) {
110
+ return 53.0 / defaultMultiplier;
111
+ }
112
+
113
+ return 1.0;
114
+ }
115
+
116
+ std::string getColorScheme() {
117
+ #if !TARGET_OS_VISION
118
+ UIUserInterfaceStyle colorScheme = [UIScreen mainScreen].traitCollection.userInterfaceStyle;
119
+
120
+ switch (colorScheme) {
121
+ case UIUserInterfaceStyleLight:
122
+ return UnistylesLightScheme;
123
+ case UIUserInterfaceStyleDark:
124
+ return UnistylesDarkScheme;
125
+ case UIUserInterfaceStyleUnspecified:
126
+ default:
127
+ return UnistylesUnspecifiedScheme;
128
+ }
129
+ #endif
130
+
131
+ return UnistylesUnspecifiedScheme;
132
+ }
133
+
134
+ UIColor* colorFromHexString(NSString* hexString, float alpha) {
135
+ unsigned rgbValue = 0;
136
+ unsigned alphaValue = 0xFF;
137
+
138
+ NSScanner *scanner = [NSScanner scannerWithString:hexString];
139
+
140
+ if (![hexString hasPrefix:@"#"]) {
141
+ return nil;
142
+ }
143
+
144
+ [scanner setScanLocation:1];
145
+
146
+ if ((hexString.length == 9 && ![scanner scanHexInt:&alphaValue]) || ![scanner scanHexInt:&rgbValue]) {
147
+ return nil;
148
+ }
149
+
150
+ if (hexString.length == 9) {
151
+ alpha = alphaValue / 255.0;
152
+ }
153
+
154
+ return [UIColor colorWithRed:((rgbValue & 0xFF0000) >> 16) / 255.0
155
+ green:((rgbValue & 0x00FF00) >> 8) / 255.0
156
+ blue:(rgbValue & 0x0000FF) / 255.0
157
+ alpha:alpha];
158
+ }
159
+
160
+ #endif
@@ -1,25 +1,21 @@
1
1
  #include <string>
2
2
  #include <map>
3
3
  #include <UnistylesRuntime.h>
4
+ #include "Platform_Shared.h"
4
5
 
5
6
  @interface Platform : NSObject
6
7
 
7
- @property (nonatomic, assign) Dimensions initialScreen;
8
- @property (nonatomic, assign) std::string initialColorScheme;
9
- @property (nonatomic, assign) std::string initialContentSizeCategory;
10
- @property (nonatomic, assign) Insets initialInsets;
11
- @property (nonatomic, assign) Dimensions initialStatusBar;
12
- @property (nonatomic, assign) Dimensions initialNavigationBar;
13
8
  @property (nonatomic, assign) void* unistylesRuntime;
14
9
 
15
10
  - (instancetype)init;
16
11
 
12
+ - (void)clean;
17
13
  - (void)setupListeners;
18
- - (void)onOrientationChange:(NSNotification *)notification;
14
+ - (void)makeShared:(void*)runtime;
15
+ - (void)onWindowChange:(NSNotification *)notification;
19
16
  - (void)onAppearanceChange:(NSNotification *)notification;
20
17
  - (void)onContentSizeCategoryChange:(NSNotification *)notification;
21
-
22
- - (std::string)getColorScheme;
23
- - (std::string)getContentSizeCategory:(UIContentSizeCategory)contentSizeCategory;
18
+ - (void)setStatusBarHidden:(bool)isHidden;
19
+ - (void)setRootViewBackgroundColor:(std::string)color alpha:(float)alpha;
24
20
 
25
21
  @end
@@ -1,7 +1,6 @@
1
1
  #if TARGET_OS_IOS
2
2
 
3
3
  #import "Platform_iOS.h"
4
- #import "UnistylesRuntime.h"
5
4
  #import <React/RCTAppearance.h>
6
5
 
7
6
  @implementation Platform
@@ -9,21 +8,16 @@
9
8
  - (instancetype)init {
10
9
  self = [super init];
11
10
  if (self) {
12
- UIScreen *screen = [UIScreen mainScreen];
13
- UIContentSizeCategory contentSizeCategory = [[UIApplication sharedApplication] preferredContentSizeCategory];
14
-
15
- self.initialScreen = {(int)screen.bounds.size.width, (int)screen.bounds.size.height};
16
- self.initialColorScheme = [self getColorScheme];
17
- self.initialContentSizeCategory = [self getContentSizeCategory:contentSizeCategory];
18
- self.initialStatusBar = [self getStatusBarDimensions];
19
- self.initialInsets = [self getInsets];
20
-
21
11
  [self setupListeners];
22
12
  }
23
13
  return self;
24
14
  }
25
15
 
26
16
  - (void)dealloc {
17
+ [self clean];
18
+ }
19
+
20
+ - (void)clean {
27
21
  if (self.unistylesRuntime != nullptr) {
28
22
  self.unistylesRuntime = nullptr;
29
23
  }
@@ -32,17 +26,17 @@
32
26
  name: UIContentSizeCategoryDidChangeNotification
33
27
  object: nil];
34
28
  [[NSNotificationCenter defaultCenter] removeObserver:self
35
- name: RCTUserInterfaceStyleDidChangeNotification
36
- object: nil];
29
+ name:RCTWindowFrameDidChangeNotification
30
+ object:nil];
37
31
  [[NSNotificationCenter defaultCenter] removeObserver:self
38
- name: UIDeviceOrientationDidChangeNotification
32
+ name: RCTUserInterfaceStyleDidChangeNotification
39
33
  object: nil];
40
34
  }
41
35
 
42
36
  - (void)setupListeners {
43
37
  [[NSNotificationCenter defaultCenter] addObserver:self
44
- selector:@selector(onOrientationChange:)
45
- name:UIDeviceOrientationDidChangeNotification
38
+ selector:@selector(onWindowChange:)
39
+ name:RCTWindowFrameDidChangeNotification
46
40
  object:nil];
47
41
  [[NSNotificationCenter defaultCenter] addObserver:self
48
42
  selector:@selector(onAppearanceChange:)
@@ -54,117 +48,173 @@
54
48
  object:nil];
55
49
  }
56
50
 
57
- - (void)onAppearanceChange:(NSNotification *)notification {
58
- std::string colorScheme = [self getColorScheme];
51
+ - (void)makeShared:(void*)runtime {
52
+ self.unistylesRuntime = runtime;
53
+
54
+ auto unistylesRuntime = ((UnistylesRuntime*)self.unistylesRuntime);
55
+
56
+ unistylesRuntime->setScreenDimensionsCallback([self](){
57
+ return [self getScreenDimensions];
58
+ });
59
+
60
+ unistylesRuntime->setContentSizeCategoryCallback([](){
61
+ return getContentSizeCategory();
62
+ });
63
+
64
+ unistylesRuntime->setColorSchemeCallback([self](){
65
+ return getColorScheme();
66
+ });
67
+
68
+ unistylesRuntime->setStatusBarDimensionsCallback([self](){
69
+ return [self getStatusBarDimensions];
70
+ });
71
+
72
+ unistylesRuntime->setInsetsCallback([self](){
73
+ return [self getInsets];
74
+ });
59
75
 
76
+ unistylesRuntime->setStatusBarHiddenCallback([self](bool hidden){
77
+ return [self setStatusBarHidden:hidden];
78
+ });
79
+
80
+ unistylesRuntime->setImmersiveModeCallback([self](bool hidden){
81
+ return [self setStatusBarHidden:hidden];
82
+ });
83
+
84
+ unistylesRuntime->setRootViewBackgroundColorCallback([self](const std::string &color, float alpha){
85
+ return [self setRootViewBackgroundColor:color alpha:alpha];
86
+ });
87
+
88
+ dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
89
+ dispatch_async(dispatch_get_main_queue(), ^{
90
+ Screen screen = [self getScreenDimensions];
91
+
92
+ unistylesRuntime->screen = Dimensions({ screen.width, screen.height });
93
+ unistylesRuntime->contentSizeCategory = getContentSizeCategory();
94
+ unistylesRuntime->colorScheme = getColorScheme();
95
+ unistylesRuntime->statusBar = [self getStatusBarDimensions];
96
+ unistylesRuntime->insets = [self getInsets];
97
+ unistylesRuntime->pixelRatio = screen.pixelRatio;
98
+ unistylesRuntime->fontScale = screen.fontScale;
99
+ unistylesRuntime->rtl = [self isRtl];
100
+
101
+ dispatch_semaphore_signal(semaphore);
102
+ });
103
+
104
+ dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
105
+ }
106
+
107
+ - (void)onAppearanceChange:(NSNotification *)notification {
60
108
  if (self.unistylesRuntime != nullptr) {
61
- ((UnistylesRuntime*)self.unistylesRuntime)->handleAppearanceChange(colorScheme);
109
+ ((UnistylesRuntime*)self.unistylesRuntime)->handleAppearanceChange(getColorScheme());
62
110
  }
63
111
  }
64
112
 
65
113
  - (void)onContentSizeCategoryChange:(NSNotification *)notification {
66
- UIContentSizeCategory contentSizeCategory = [[UIApplication sharedApplication] preferredContentSizeCategory];
67
-
68
114
  if (self.unistylesRuntime != nullptr) {
69
- ((UnistylesRuntime*)self.unistylesRuntime)->handleContentSizeCategoryChange([self getContentSizeCategory:contentSizeCategory]);
115
+ ((UnistylesRuntime*)self.unistylesRuntime)->handleContentSizeCategoryChange(getContentSizeCategory());
70
116
  }
71
117
  }
72
118
 
73
- - (void)onOrientationChange:(NSNotification *)notification {
119
+ - (void)onWindowChange:(NSNotification *)notification {
74
120
  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
75
- UIScreen *mainScreen = [UIScreen mainScreen];
76
- Dimensions screen = {(int)mainScreen.bounds.size.width, (int)mainScreen.bounds.size.height};
121
+ UIApplicationState appState = [UIApplication sharedApplication].applicationState;
122
+
123
+ if (appState != UIApplicationStateActive) {
124
+ return;
125
+ }
126
+
127
+ Screen screen = [self getScreenDimensions];
77
128
  Insets insets = [self getInsets];
78
129
  Dimensions statusBar = [self getStatusBarDimensions];
79
- Dimensions navigationBar = [self getNavigationBarDimensions];
80
130
 
81
131
  if (self.unistylesRuntime != nullptr) {
82
- ((UnistylesRuntime*)self.unistylesRuntime)->handleScreenSizeChange(screen, insets, statusBar, navigationBar);
132
+ ((UnistylesRuntime*)self.unistylesRuntime)->handleScreenSizeChange(screen, insets, statusBar, std::nullopt);
83
133
  }
84
134
  });
85
135
  }
86
136
 
87
- - (std::string)getColorScheme {
88
- UIUserInterfaceStyle colorScheme = [UIScreen mainScreen].traitCollection.userInterfaceStyle;
89
-
90
- switch (colorScheme) {
91
- case UIUserInterfaceStyleLight:
92
- return UnistylesLightScheme;
93
- case UIUserInterfaceStyleDark:
94
- return UnistylesDarkScheme;
95
- case UIUserInterfaceStyleUnspecified:
96
- default:
97
- return UnistylesUnspecifiedScheme;
137
+ - (UIWindow *)getMainWindow {
138
+ for (UIScene *scene in [UIApplication sharedApplication].connectedScenes) {
139
+ if ([scene isKindOfClass:[UIWindowScene class]]) {
140
+ UIWindowScene *windowScene = (UIWindowScene *)scene;
141
+ for (UIWindow *window in windowScene.windows) {
142
+ if (window.isKeyWindow) {
143
+ return window;
144
+ }
145
+ }
146
+ }
98
147
  }
148
+
149
+ return nil;
99
150
  }
100
151
 
101
152
  - (Insets)getInsets {
102
- UIWindow *window = UIApplication.sharedApplication.windows.firstObject;
153
+ UIWindow *window = [self getMainWindow];
154
+
155
+ if (window == nil) {
156
+ return {0, 0, 0, 0};
157
+ }
158
+
103
159
  UIEdgeInsets safeArea = window.safeAreaInsets;
104
-
160
+
105
161
  return {(int)safeArea.top, (int)safeArea.bottom, (int)safeArea.left, (int)safeArea.right};
106
162
  }
107
163
 
108
164
  - (Dimensions)getStatusBarDimensions {
109
- CGRect statusBarFrame = UIApplication.sharedApplication.statusBarFrame;
110
-
165
+ UIWindow *window = [self getMainWindow];
166
+
167
+ if (window == nil) {
168
+ return {0, 0};
169
+ }
170
+
171
+ CGRect statusBarFrame = window.windowScene.statusBarManager.statusBarFrame;
172
+
111
173
  return {(int)statusBarFrame.size.width, (int)statusBarFrame.size.height};
112
174
  }
113
175
 
114
- - (Dimensions)getNavigationBarDimensions {
115
- return {0, 0};
176
+ - (Screen)getScreenDimensions {
177
+ UIViewController *presentedViewController = RCTPresentedViewController();
178
+ CGRect windowFrame = presentedViewController.view.window.frame;
179
+ int width = (int)windowFrame.size.width;
180
+ int height = (int)windowFrame.size.height;
181
+ float pixelRatio = presentedViewController.view.window.screen.scale;
182
+ float fontScale = getFontScale();
183
+
184
+ return Screen({width, height, pixelRatio, fontScale});
116
185
  }
117
186
 
118
- - (std::string)getContentSizeCategory:(UIContentSizeCategory)contentSizeCategory {
119
- if ([contentSizeCategory isEqualToString:UIContentSizeCategoryExtraExtraExtraLarge]) {
120
- return std::string([@"xxxLarge" UTF8String]);
121
- }
122
-
123
- if ([contentSizeCategory isEqualToString:UIContentSizeCategoryExtraExtraLarge]) {
124
- return std::string([@"xxLarge" UTF8String]);
125
- }
126
-
127
- if ([contentSizeCategory isEqualToString:UIContentSizeCategoryExtraLarge]) {
128
- return std::string([@"xLarge" UTF8String]);
129
- }
130
-
131
- if ([contentSizeCategory isEqualToString:UIContentSizeCategoryLarge]) {
132
- return std::string([@"Large" UTF8String]);
133
- }
134
-
135
- if ([contentSizeCategory isEqualToString:UIContentSizeCategoryMedium]) {
136
- return std::string([@"Medium" UTF8String]);
137
- }
138
-
139
- if ([contentSizeCategory isEqualToString:UIContentSizeCategorySmall]) {
140
- return std::string([@"Small" UTF8String]);
141
- }
142
-
143
- if ([contentSizeCategory isEqualToString:UIContentSizeCategoryExtraSmall]) {
144
- return std::string([@"xSmall" UTF8String]);
145
- }
146
-
147
- if ([contentSizeCategory isEqualToString:UIContentSizeCategoryAccessibilityMedium]) {
148
- return std::string([@"accessibilityMedium" UTF8String]);
149
- }
150
-
151
- if ([contentSizeCategory isEqualToString:UIContentSizeCategoryAccessibilityLarge]) {
152
- return std::string([@"accessibilityLarge" UTF8String]);
153
- }
154
-
155
- if ([contentSizeCategory isEqualToString:UIContentSizeCategoryAccessibilityExtraLarge]) {
156
- return std::string([@"accessibilityExtraLarge" UTF8String]);
157
- }
158
-
159
- if ([contentSizeCategory isEqualToString:UIContentSizeCategoryAccessibilityExtraExtraLarge]) {
160
- return std::string([@"accessibilityExtraExtraLarge" UTF8String]);
161
- }
162
-
163
- if ([contentSizeCategory isEqualToString:UIContentSizeCategoryAccessibilityExtraExtraExtraLarge]) {
164
- return std::string([@"accessibilityExtraExtraExtraLarge" UTF8String]);
165
- }
166
-
167
- return std::string([@"unspecified" UTF8String]);
187
+ - (bool)isRtl {
188
+ // forced by React Native
189
+ BOOL hasForcedRtl = [[NSUserDefaults standardUserDefaults] boolForKey:@"RCTI18nUtil_forceRTL"];
190
+ // user preferences
191
+ BOOL isRtl = [UIApplication sharedApplication].userInterfaceLayoutDirection == UIUserInterfaceLayoutDirectionRightToLeft;
192
+
193
+ return hasForcedRtl || isRtl;
194
+ }
195
+
196
+ - (void)setStatusBarHidden:(bool)isHidden {
197
+ // forward it to React Native ViewController
198
+ dispatch_async(dispatch_get_main_queue(), ^{
199
+ #pragma clang diagnostic ignored "-Wdeprecated-declarations"
200
+ [RCTSharedApplication() setStatusBarHidden:isHidden animated:true];
201
+ });
202
+ }
203
+
204
+ - (void)setRootViewBackgroundColor:(std::string)color alpha:(float)alpha {
205
+ dispatch_async(dispatch_get_main_queue(), ^{
206
+ UIViewController *presentedViewController = RCTPresentedViewController();
207
+ NSString *colorString = [NSString stringWithUTF8String:color.c_str()];
208
+ UIColor *backgroundColor = colorFromHexString(colorString, alpha);
209
+
210
+ if (backgroundColor == nil) {
211
+ NSLog(@"🦄 Unistyles: Couldn't set rootView to %@ color", colorString);
212
+
213
+ return;
214
+ }
215
+
216
+ presentedViewController.view.backgroundColor = backgroundColor;
217
+ });
168
218
  }
169
219
 
170
220
  @end
@@ -4,18 +4,14 @@
4
4
 
5
5
  @interface Platform : NSObject
6
6
 
7
- @property (nonatomic, assign) Dimensions initialScreen;
8
- @property (nonatomic, assign) std::string initialColorScheme;
9
- @property (nonatomic, assign) std::string initialContentSizeCategory;
10
- @property (nonatomic, assign) Insets initialInsets;
11
- @property (nonatomic, assign) Dimensions initialStatusBar;
12
- @property (nonatomic, assign) Dimensions initialNavigationBar;
13
7
  @property (nonatomic, assign) void* unistylesRuntime;
14
8
 
15
9
  - (instancetype)init;
16
10
 
11
+ - (void)clean;
17
12
  - (void)setupListeners;
18
- - (void)onWindowResize;
13
+ - (void)makeShared:(void*)runtime;
14
+ - (void)onWindowChange;
19
15
  - (void)onAppearanceChange;
20
16
 
21
17
  - (std::string)getColorScheme;