react-native-unistyles 2.4.0-rc.0 → 2.4.0-rc.2
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.
- package/README.md +5 -2
- package/android/src/main/cxx/cpp-adapter.cpp +28 -13
- package/android/src/main/cxx/helpers.cpp +21 -56
- package/android/src/main/cxx/helpers.h +3 -3
- package/android/src/main/java/com/unistyles/Config.kt +116 -0
- package/android/src/main/java/com/unistyles/Insets.kt +138 -0
- package/android/src/main/java/com/unistyles/Models.kt +85 -0
- package/android/src/main/java/com/unistyles/Platform.kt +10 -79
- package/android/src/main/java/com/unistyles/UnistylesModule.kt +85 -25
- package/cxx/UnistylesRuntime.cpp +57 -42
- package/cxx/UnistylesRuntime.h +30 -19
- package/ios/UnistylesModule.mm +20 -6
- package/ios/platform/Platform_iOS.h +5 -4
- package/ios/platform/Platform_iOS.mm +35 -22
- package/ios/platform/Platform_macOS.h +5 -4
- package/ios/platform/Platform_macOS.mm +18 -25
- package/lib/commonjs/common.js +7 -0
- package/lib/commonjs/common.js.map +1 -1
- package/lib/commonjs/core/UnistylesModule.js +6 -0
- package/lib/commonjs/core/UnistylesModule.js.map +1 -1
- package/lib/commonjs/core/UnistylesRuntime.js +8 -0
- package/lib/commonjs/core/UnistylesRuntime.js.map +1 -1
- package/lib/commonjs/hooks/useUnistyles.js +18 -1
- package/lib/commonjs/hooks/useUnistyles.js.map +1 -1
- package/lib/module/common.js +7 -0
- package/lib/module/common.js.map +1 -1
- package/lib/module/core/UnistylesModule.js +6 -0
- package/lib/module/core/UnistylesModule.js.map +1 -1
- package/lib/module/core/UnistylesRuntime.js +8 -0
- package/lib/module/core/UnistylesRuntime.js.map +1 -1
- package/lib/module/hooks/useUnistyles.js +18 -1
- package/lib/module/hooks/useUnistyles.js.map +1 -1
- package/lib/typescript/src/common.d.ts +8 -1
- package/lib/typescript/src/common.d.ts.map +1 -1
- package/lib/typescript/src/core/UnistylesModule.d.ts.map +1 -1
- package/lib/typescript/src/core/UnistylesRuntime.d.ts +6 -1
- package/lib/typescript/src/core/UnistylesRuntime.d.ts.map +1 -1
- package/lib/typescript/src/hooks/useUnistyles.d.ts +14 -0
- package/lib/typescript/src/hooks/useUnistyles.d.ts.map +1 -1
- package/lib/typescript/src/types/unistyles.d.ts +6 -2
- package/lib/typescript/src/types/unistyles.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/common.ts +8 -1
- package/src/core/UnistylesModule.ts +8 -2
- package/src/core/UnistylesRuntime.ts +8 -0
- package/src/hooks/useUnistyles.ts +18 -1
- package/src/types/unistyles.ts +6 -2
- package/windows/ReactNativeUnistyles/ReactNativeUnistyles.h +1 -1
- package/windows/ReactNativeUnistyles/ReactNativeUnistyles.vcxproj +8 -1
- package/windows/ReactNativeUnistyles/ReactNativeUnistyles.vcxproj.filters +2 -1
@@ -7,6 +7,7 @@ import android.content.IntentFilter
|
|
7
7
|
import android.os.Handler
|
8
8
|
import android.os.Looper
|
9
9
|
import android.util.Log
|
10
|
+
import android.view.ViewTreeObserver
|
10
11
|
import com.facebook.react.bridge.Arguments
|
11
12
|
import com.facebook.react.bridge.LifecycleEventListener
|
12
13
|
import com.facebook.react.bridge.ReactApplicationContext
|
@@ -15,11 +16,27 @@ import com.facebook.react.bridge.ReactMethod
|
|
15
16
|
import com.facebook.react.modules.core.DeviceEventManagerModule
|
16
17
|
|
17
18
|
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
|
+
private var isCxxReady: Boolean = false
|
18
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
|
+
}
|
19
36
|
|
20
37
|
private val configurationChangeReceiver = object : BroadcastReceiver() {
|
21
38
|
override fun onReceive(context: Context, intent: Intent) {
|
22
|
-
if (intent.action == Intent.ACTION_CONFIGURATION_CHANGED) {
|
39
|
+
if (intent.action == Intent.ACTION_CONFIGURATION_CHANGED && this@UnistylesModule.isCxxReady) {
|
23
40
|
Handler(Looper.getMainLooper()).postDelayed({
|
24
41
|
this@UnistylesModule.onConfigChange()
|
25
42
|
}, 10)
|
@@ -37,51 +54,79 @@ class UnistylesModule(reactContext: ReactApplicationContext) : ReactContextBaseJ
|
|
37
54
|
reactApplicationContext.registerReceiver(configurationChangeReceiver, IntentFilter(Intent.ACTION_CONFIGURATION_CHANGED))
|
38
55
|
}
|
39
56
|
|
57
|
+
private fun setupLayoutListener() {
|
58
|
+
val activity = currentActivity ?: return
|
59
|
+
activity.window.decorView.rootView.viewTreeObserver.addOnGlobalLayoutListener(layoutListener)
|
60
|
+
}
|
61
|
+
|
40
62
|
@Deprecated("Deprecated in Java")
|
41
63
|
override fun onCatalystInstanceDestroy() {
|
64
|
+
val activity = currentActivity ?: return
|
65
|
+
|
66
|
+
activity.window.decorView.rootView.viewTreeObserver.removeOnGlobalLayoutListener(layoutListener)
|
42
67
|
reactApplicationContext.unregisterReceiver(configurationChangeReceiver)
|
68
|
+
runnable?.let { drawHandler.removeCallbacks(it) }
|
43
69
|
this.nativeDestroy()
|
44
70
|
}
|
45
71
|
|
46
72
|
//endregion
|
47
73
|
//region Event handlers
|
48
|
-
@Suppress("UNCHECKED_CAST")
|
49
74
|
private fun onConfigChange() {
|
75
|
+
if (!platform.hasNewConfig()) {
|
76
|
+
return
|
77
|
+
}
|
78
|
+
|
50
79
|
val config = platform.getConfig()
|
51
80
|
|
81
|
+
reactApplicationContext.runOnJSQueueThread {
|
82
|
+
if (config.hasNewColorScheme) {
|
83
|
+
this.nativeOnAppearanceChange(config.colorScheme)
|
84
|
+
}
|
85
|
+
|
86
|
+
if (config.hasNewContentSizeCategory) {
|
87
|
+
this.nativeOnContentSizeCategoryChange(config.contentSizeCategory)
|
88
|
+
}
|
89
|
+
}
|
90
|
+
}
|
91
|
+
|
92
|
+
private fun onLayoutConfigChange() {
|
93
|
+
if (!platform.hasNewLayoutConfig()) {
|
94
|
+
return
|
95
|
+
}
|
96
|
+
|
97
|
+
val config = platform.getLayoutConfig()
|
98
|
+
|
52
99
|
reactApplicationContext.runOnJSQueueThread {
|
53
100
|
this.nativeOnOrientationChange(
|
54
|
-
config
|
55
|
-
config
|
56
|
-
config
|
57
|
-
config
|
58
|
-
)
|
59
|
-
this.nativeOnAppearanceChange(
|
60
|
-
config["colorScheme"] as String
|
101
|
+
config.screen,
|
102
|
+
config.insets,
|
103
|
+
config.statusBar,
|
104
|
+
config.navigationBar
|
61
105
|
)
|
62
|
-
this.nativeOnContentSizeCategoryChange(config["contentSizeCategory"] as String)
|
63
106
|
}
|
64
107
|
}
|
65
108
|
|
66
109
|
//endregion
|
67
110
|
//region Core
|
68
|
-
@Suppress("UNCHECKED_CAST")
|
69
111
|
@ReactMethod(isBlockingSynchronousMethod = true)
|
70
112
|
fun install(): Boolean {
|
71
113
|
return try {
|
72
114
|
System.loadLibrary("unistyles")
|
73
115
|
val config = platform.getConfig()
|
116
|
+
val layoutConfig = platform.getLayoutConfig()
|
74
117
|
|
118
|
+
this.setupLayoutListener()
|
75
119
|
this.reactApplicationContext.javaScriptContextHolder?.let {
|
76
120
|
this.nativeInstall(
|
77
121
|
it.get(),
|
78
|
-
|
79
|
-
config
|
80
|
-
config
|
81
|
-
|
82
|
-
|
83
|
-
|
122
|
+
layoutConfig.screen,
|
123
|
+
config.colorScheme,
|
124
|
+
config.contentSizeCategory,
|
125
|
+
layoutConfig.insets,
|
126
|
+
layoutConfig.statusBar,
|
127
|
+
layoutConfig.navigationBar
|
84
128
|
)
|
129
|
+
this.isCxxReady = true
|
85
130
|
|
86
131
|
Log.i(NAME, "Installed Unistyles \uD83E\uDD84!")
|
87
132
|
|
@@ -96,29 +141,43 @@ class UnistylesModule(reactContext: ReactApplicationContext) : ReactContextBaseJ
|
|
96
141
|
|
97
142
|
private external fun nativeInstall(
|
98
143
|
jsi: Long,
|
99
|
-
|
100
|
-
height: Int,
|
144
|
+
screen: Dimensions,
|
101
145
|
colorScheme: String,
|
102
146
|
contentSizeCategory: String,
|
103
|
-
insets:
|
104
|
-
statusBar:
|
147
|
+
insets: Insets,
|
148
|
+
statusBar: Dimensions,
|
149
|
+
navigationBar: Dimensions
|
105
150
|
)
|
106
151
|
private external fun nativeDestroy()
|
107
|
-
private external fun nativeOnOrientationChange(
|
152
|
+
private external fun nativeOnOrientationChange(screen: Dimensions, insets: Insets, statusBar: Dimensions, navigationBar: Dimensions)
|
108
153
|
private external fun nativeOnAppearanceChange(colorScheme: String)
|
109
154
|
private external fun nativeOnContentSizeCategoryChange(contentSizeCategory: String)
|
110
155
|
|
111
156
|
//endregion
|
112
157
|
//region Event emitter
|
113
|
-
private fun onLayoutChange(breakpoint: String, orientation: String,
|
158
|
+
private fun onLayoutChange(breakpoint: String, orientation: String, screen: Dimensions, statusBar: Dimensions, insets: Insets, navigationBar: Dimensions) {
|
114
159
|
val body = Arguments.createMap().apply {
|
115
160
|
putString("type", "layout")
|
116
161
|
putMap("payload", Arguments.createMap().apply {
|
117
162
|
putString("breakpoint", breakpoint)
|
118
163
|
putString("orientation", orientation)
|
119
164
|
putMap("screen", Arguments.createMap().apply {
|
120
|
-
putInt("width", width)
|
121
|
-
putInt("height", height)
|
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)
|
122
181
|
})
|
123
182
|
})
|
124
183
|
}
|
@@ -171,6 +230,7 @@ class UnistylesModule(reactContext: ReactApplicationContext) : ReactContextBaseJ
|
|
171
230
|
fun removeListeners(count: Double) = Unit
|
172
231
|
override fun onHostResume() {
|
173
232
|
this.onConfigChange()
|
233
|
+
this.onLayoutConfigChange()
|
174
234
|
}
|
175
235
|
|
176
236
|
override fun onHostPause() {}
|
package/cxx/UnistylesRuntime.cpp
CHANGED
@@ -27,6 +27,7 @@ std::vector<jsi::PropNameID> UnistylesRuntime::getPropertyNames(jsi::Runtime& ru
|
|
27
27
|
properties.push_back(jsi::PropNameID::forUtf8(runtime, std::string("enabledPlugins")));
|
28
28
|
properties.push_back(jsi::PropNameID::forUtf8(runtime, std::string("insets")));
|
29
29
|
properties.push_back(jsi::PropNameID::forUtf8(runtime, std::string("statusBar")));
|
30
|
+
properties.push_back(jsi::PropNameID::forUtf8(runtime, std::string("navigationBar")));
|
30
31
|
|
31
32
|
// setters
|
32
33
|
properties.push_back(jsi::PropNameID::forUtf8(runtime, std::string("themes")));
|
@@ -38,17 +39,17 @@ jsi::Value UnistylesRuntime::get(jsi::Runtime& runtime, const jsi::PropNameID& p
|
|
38
39
|
std::string propName = propNameId.utf8(runtime);
|
39
40
|
|
40
41
|
if (propName == "screenWidth") {
|
41
|
-
return jsi::Value(this->
|
42
|
+
return jsi::Value(this->screen.width);
|
42
43
|
}
|
43
44
|
|
44
45
|
if (propName == "screenHeight") {
|
45
|
-
return jsi::Value(this->
|
46
|
+
return jsi::Value(this->screen.height);
|
46
47
|
}
|
47
48
|
|
48
49
|
if (propName == "hasAdaptiveThemes") {
|
49
50
|
return jsi::Value(this->hasAdaptiveThemes);
|
50
51
|
}
|
51
|
-
|
52
|
+
|
52
53
|
if (propName == "contentSizeCategory") {
|
53
54
|
return jsi::Value(jsi::String::createFromUtf8(runtime, this->contentSizeCategory));
|
54
55
|
}
|
@@ -68,14 +69,14 @@ jsi::Value UnistylesRuntime::get(jsi::Runtime& runtime, const jsi::PropNameID& p
|
|
68
69
|
if (propName == "colorScheme") {
|
69
70
|
return jsi::Value(jsi::String::createFromUtf8(runtime, this->colorScheme));
|
70
71
|
}
|
71
|
-
|
72
|
+
|
72
73
|
if (propName == "enabledPlugins") {
|
73
74
|
auto jsiArray = facebook::jsi::Array(runtime, this->pluginNames.size());
|
74
|
-
|
75
|
+
|
75
76
|
for (size_t i = 0; i < this->pluginNames.size(); i++) {
|
76
77
|
jsiArray.setValueAtIndex(runtime, i, facebook::jsi::String::createFromUtf8(runtime, this->pluginNames[i]));
|
77
78
|
}
|
78
|
-
|
79
|
+
|
79
80
|
return jsiArray;
|
80
81
|
}
|
81
82
|
|
@@ -93,7 +94,7 @@ jsi::Value UnistylesRuntime::get(jsi::Runtime& runtime, const jsi::PropNameID& p
|
|
93
94
|
|
94
95
|
return jsi::Value(runtime, *sortedBreakpointEntriesArray);
|
95
96
|
}
|
96
|
-
|
97
|
+
|
97
98
|
if (propName == "addPlugin") {
|
98
99
|
return jsi::Function::createFromHostFunction(
|
99
100
|
runtime,
|
@@ -102,19 +103,19 @@ jsi::Value UnistylesRuntime::get(jsi::Runtime& runtime, const jsi::PropNameID& p
|
|
102
103
|
[this](jsi::Runtime &runtime, const jsi::Value &thisVal, const jsi::Value *arguments, size_t count) -> jsi::Value {
|
103
104
|
std::string pluginName = arguments[0].asString(runtime).utf8(runtime);
|
104
105
|
bool notify = arguments[1].asBool();
|
105
|
-
|
106
|
+
|
106
107
|
this->pluginNames.push_back(pluginName);
|
107
|
-
|
108
|
+
|
108
109
|
// registry enabled plugins won't notify listeners
|
109
110
|
if (notify) {
|
110
111
|
this->onPluginChangeCallback();
|
111
112
|
}
|
112
|
-
|
113
|
+
|
113
114
|
return jsi::Value::undefined();
|
114
115
|
}
|
115
116
|
);
|
116
117
|
}
|
117
|
-
|
118
|
+
|
118
119
|
if (propName == "removePlugin") {
|
119
120
|
return jsi::Function::createFromHostFunction(
|
120
121
|
runtime,
|
@@ -122,14 +123,14 @@ jsi::Value UnistylesRuntime::get(jsi::Runtime& runtime, const jsi::PropNameID& p
|
|
122
123
|
1,
|
123
124
|
[this](jsi::Runtime &runtime, const jsi::Value &thisVal, const jsi::Value *arguments, size_t count) -> jsi::Value {
|
124
125
|
std::string pluginName = arguments[0].asString(runtime).utf8(runtime);
|
125
|
-
|
126
|
+
|
126
127
|
auto it = std::find(this->pluginNames.begin(), this->pluginNames.end(), pluginName);
|
127
|
-
|
128
|
+
|
128
129
|
if (it != this->pluginNames.end()) {
|
129
130
|
this->pluginNames.erase(it);
|
130
131
|
this->onPluginChangeCallback();
|
131
132
|
}
|
132
|
-
|
133
|
+
|
133
134
|
return jsi::Value::undefined();
|
134
135
|
}
|
135
136
|
);
|
@@ -171,7 +172,7 @@ jsi::Value UnistylesRuntime::get(jsi::Runtime& runtime, const jsi::PropNameID& p
|
|
171
172
|
|
172
173
|
this->sortedBreakpointPairs = sortedBreakpointEntriesVec;
|
173
174
|
|
174
|
-
std::string breakpoint = this->getBreakpointFromScreenWidth(this->
|
175
|
+
std::string breakpoint = this->getBreakpointFromScreenWidth(this->screen.width, sortedBreakpointEntriesVec);
|
175
176
|
|
176
177
|
this->breakpoint = breakpoint;
|
177
178
|
|
@@ -191,12 +192,12 @@ jsi::Value UnistylesRuntime::get(jsi::Runtime& runtime, const jsi::PropNameID& p
|
|
191
192
|
this->themeName = themeName;
|
192
193
|
this->onThemeChangeCallback(themeName);
|
193
194
|
}
|
194
|
-
|
195
|
+
|
195
196
|
return jsi::Value::undefined();
|
196
197
|
}
|
197
198
|
);
|
198
199
|
}
|
199
|
-
|
200
|
+
|
200
201
|
if (propName == "updateTheme") {
|
201
202
|
return jsi::Function::createFromHostFunction(runtime,
|
202
203
|
jsi::PropNameID::forAscii(runtime, "updateTheme"),
|
@@ -207,7 +208,7 @@ jsi::Value UnistylesRuntime::get(jsi::Runtime& runtime, const jsi::PropNameID& p
|
|
207
208
|
if (this->themeName == themeName) {
|
208
209
|
this->onThemeChangeCallback(themeName);
|
209
210
|
}
|
210
|
-
|
211
|
+
|
211
212
|
return jsi::Value::undefined();
|
212
213
|
}
|
213
214
|
);
|
@@ -225,7 +226,7 @@ jsi::Value UnistylesRuntime::get(jsi::Runtime& runtime, const jsi::PropNameID& p
|
|
225
226
|
if (!enableAdaptiveThemes || !this->supportsAutomaticColorScheme) {
|
226
227
|
return jsi::Value::undefined();
|
227
228
|
}
|
228
|
-
|
229
|
+
|
229
230
|
if (this->themeName != this->colorScheme) {
|
230
231
|
this->themeName = this->colorScheme;
|
231
232
|
this->onThemeChangeCallback(this->themeName);
|
@@ -235,27 +236,36 @@ jsi::Value UnistylesRuntime::get(jsi::Runtime& runtime, const jsi::PropNameID& p
|
|
235
236
|
}
|
236
237
|
);
|
237
238
|
}
|
238
|
-
|
239
|
+
|
239
240
|
if (propName == "insets") {
|
240
241
|
auto insets = jsi::Object(runtime);
|
241
|
-
|
242
|
-
insets.setProperty(runtime, "top", this->insets.
|
243
|
-
insets.setProperty(runtime, "bottom", this->insets.
|
244
|
-
insets.setProperty(runtime, "left", this->insets.
|
245
|
-
insets.setProperty(runtime, "right", this->insets.
|
246
|
-
|
242
|
+
|
243
|
+
insets.setProperty(runtime, "top", this->insets.top);
|
244
|
+
insets.setProperty(runtime, "bottom", this->insets.bottom);
|
245
|
+
insets.setProperty(runtime, "left", this->insets.left);
|
246
|
+
insets.setProperty(runtime, "right", this->insets.right);
|
247
|
+
|
247
248
|
return insets;
|
248
249
|
}
|
249
|
-
|
250
|
+
|
250
251
|
if (propName == "statusBar") {
|
251
252
|
auto statusBar = jsi::Object(runtime);
|
252
|
-
|
253
|
-
statusBar.setProperty(runtime, "
|
254
|
-
statusBar.setProperty(runtime, "
|
255
|
-
|
253
|
+
|
254
|
+
statusBar.setProperty(runtime, "width", this->statusBar.width);
|
255
|
+
statusBar.setProperty(runtime, "height", this->statusBar.height);
|
256
|
+
|
256
257
|
return statusBar;
|
257
258
|
}
|
258
259
|
|
260
|
+
if (propName == "navigationBar") {
|
261
|
+
auto navigationBarValue = jsi::Object(runtime);
|
262
|
+
|
263
|
+
navigationBarValue.setProperty(runtime, "width", this->navigationBar.width);
|
264
|
+
navigationBarValue.setProperty(runtime, "height", this->navigationBar.height);
|
265
|
+
|
266
|
+
return navigationBarValue;
|
267
|
+
}
|
268
|
+
|
259
269
|
return jsi::Value::undefined();
|
260
270
|
}
|
261
271
|
|
@@ -308,22 +318,27 @@ std::string UnistylesRuntime::getBreakpointFromScreenWidth(int width, const std:
|
|
308
318
|
return sortedBreakpointPairs.empty() ? "" : sortedBreakpointPairs.back().first;
|
309
319
|
}
|
310
320
|
|
311
|
-
void UnistylesRuntime::handleScreenSizeChange(
|
312
|
-
std::string breakpoint = this->getBreakpointFromScreenWidth(width, this->sortedBreakpointPairs);
|
313
|
-
bool
|
314
|
-
|
321
|
+
void UnistylesRuntime::handleScreenSizeChange(Dimensions& screen, Insets& insets, Dimensions& statusBar, Dimensions& navigationBar) {
|
322
|
+
std::string breakpoint = this->getBreakpointFromScreenWidth(screen.width, this->sortedBreakpointPairs);
|
323
|
+
bool hasDifferentBreakpoint = this->breakpoint != breakpoint;
|
324
|
+
bool hasDifferentScreenDimensions = this->screen.width != screen.width || this->screen.height != screen.height;
|
325
|
+
bool hasDifferentInsets = this->insets.top != insets.top || this->insets.bottom != insets.bottom || this->insets.left != insets.left || this->insets.right != insets.right;
|
326
|
+
|
327
|
+
// we don't need to check statusBar/navigationBar as they will only change on orientation change witch is equal to hasDifferentScreenDimensions
|
328
|
+
bool shouldNotify = hasDifferentBreakpoint || hasDifferentScreenDimensions || hasDifferentInsets;
|
329
|
+
|
315
330
|
this->breakpoint = breakpoint;
|
316
|
-
this->
|
317
|
-
this->
|
318
|
-
this->
|
319
|
-
this->
|
331
|
+
this->screen = {screen.width, screen.height};
|
332
|
+
this->insets = {insets.top, insets.bottom, insets.left, insets.right};
|
333
|
+
this->statusBar = {statusBar.width, statusBar.height};
|
334
|
+
this->navigationBar = {navigationBar.width, navigationBar.height};
|
320
335
|
|
321
|
-
std::string orientation = width > height
|
336
|
+
std::string orientation = screen.width > screen.height
|
322
337
|
? UnistylesOrientationLandscape
|
323
338
|
: UnistylesOrientationPortrait;
|
324
339
|
|
325
340
|
if (shouldNotify) {
|
326
|
-
this->onLayoutChangeCallback(breakpoint, orientation,
|
341
|
+
this->onLayoutChangeCallback(breakpoint, orientation, screen, statusBar, insets, navigationBar);
|
327
342
|
}
|
328
343
|
}
|
329
344
|
|
@@ -333,7 +348,7 @@ void UnistylesRuntime::handleAppearanceChange(std::string colorScheme) {
|
|
333
348
|
if (!this->supportsAutomaticColorScheme || !this->hasAdaptiveThemes) {
|
334
349
|
return;
|
335
350
|
}
|
336
|
-
|
351
|
+
|
337
352
|
if (this->themeName != this->colorScheme) {
|
338
353
|
this->onThemeChangeCallback(this->colorScheme);
|
339
354
|
this->themeName = this->colorScheme;
|
package/cxx/UnistylesRuntime.h
CHANGED
@@ -17,32 +17,46 @@ const std::string UnistylesErrorBreakpointsCannotBeEmpty = "You are trying to re
|
|
17
17
|
const std::string UnistylesErrorBreakpointsMustStartFromZero = "You are trying to register breakpoints that don't start from 0";
|
18
18
|
const std::string UnistylesErrorThemesCannotBeEmpty = "You are trying to register empty themes object";
|
19
19
|
|
20
|
+
struct Dimensions {
|
21
|
+
int width;
|
22
|
+
int height;
|
23
|
+
};
|
24
|
+
|
25
|
+
struct Insets {
|
26
|
+
int top;
|
27
|
+
int bottom;
|
28
|
+
int left;
|
29
|
+
int right;
|
30
|
+
};
|
31
|
+
|
20
32
|
class JSI_EXPORT UnistylesRuntime : public jsi::HostObject {
|
21
33
|
private:
|
22
34
|
std::function<void(std::string)> onThemeChangeCallback;
|
23
|
-
std::function<void(std::string breakpoint, std::string orientation,
|
35
|
+
std::function<void(std::string breakpoint, std::string orientation, Dimensions& screen, Dimensions& statusBar, Insets& insets, Dimensions& navigationBar)> onLayoutChangeCallback;
|
24
36
|
std::function<void(std::string)> onContentSizeCategoryChangeCallback;
|
25
37
|
std::function<void()> onPluginChangeCallback;
|
26
38
|
|
27
|
-
|
28
|
-
|
39
|
+
Dimensions screen;
|
40
|
+
Dimensions statusBar;
|
41
|
+
Dimensions navigationBar;
|
42
|
+
Insets insets;
|
29
43
|
std::string colorScheme;
|
30
44
|
std::string contentSizeCategory;
|
31
45
|
|
32
46
|
public:
|
33
47
|
UnistylesRuntime(
|
34
|
-
|
35
|
-
int screenHeight,
|
48
|
+
Dimensions screen,
|
36
49
|
std::string colorScheme,
|
37
50
|
std::string contentSizeCategory,
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
51
|
+
Insets insets,
|
52
|
+
Dimensions statusBar,
|
53
|
+
Dimensions navigationBar
|
54
|
+
): screen(screen),
|
55
|
+
colorScheme(colorScheme),
|
56
|
+
contentSizeCategory(contentSizeCategory),
|
57
|
+
insets(insets),
|
58
|
+
statusBar(statusBar),
|
59
|
+
navigationBar(navigationBar) {}
|
46
60
|
|
47
61
|
bool hasAdaptiveThemes;
|
48
62
|
bool supportsAutomaticColorScheme;
|
@@ -52,22 +66,19 @@ public:
|
|
52
66
|
std::vector<std::string> pluginNames;
|
53
67
|
std::vector<std::string> themes;
|
54
68
|
std::vector<std::pair<std::string, double>> sortedBreakpointPairs;
|
55
|
-
|
56
|
-
std::map<std::string, int> insets;
|
57
|
-
std::map<std::string, int> statusBar;
|
58
69
|
|
59
70
|
void onThemeChange(std::function<void(std::string)> callback) {
|
60
71
|
this->onThemeChangeCallback = callback;
|
61
72
|
}
|
62
73
|
|
63
|
-
void onLayoutChange(std::function<void(std::string breakpoint, std::string orientation,
|
74
|
+
void onLayoutChange(std::function<void(std::string breakpoint, std::string orientation, Dimensions& screen, Dimensions& statusBar, Insets& insets, Dimensions& navigationBar)> callback) {
|
64
75
|
this->onLayoutChangeCallback = callback;
|
65
76
|
}
|
66
77
|
|
67
78
|
void onPluginChange(std::function<void()> callback) {
|
68
79
|
this->onPluginChangeCallback = callback;
|
69
80
|
}
|
70
|
-
|
81
|
+
|
71
82
|
void onContentSizeCategoryChange(std::function<void(std::string)> callback) {
|
72
83
|
this->onContentSizeCategoryChangeCallback = callback;
|
73
84
|
}
|
@@ -76,7 +87,7 @@ public:
|
|
76
87
|
void set(jsi::Runtime& runtime, const jsi::PropNameID& propNameId, const jsi::Value& value) override;
|
77
88
|
std::vector<jsi::PropNameID> getPropertyNames(jsi::Runtime& runtime) override;
|
78
89
|
|
79
|
-
void handleScreenSizeChange(
|
90
|
+
void handleScreenSizeChange(Dimensions& screen, Insets& insets, Dimensions& statusBar, Dimensions& navigationBar);
|
80
91
|
void handleAppearanceChange(std::string colorScheme);
|
81
92
|
void handleContentSizeCategoryChange(std::string contentSizeCategory);
|
82
93
|
|
package/ios/UnistylesModule.mm
CHANGED
@@ -72,12 +72,12 @@ RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(install) {
|
|
72
72
|
|
73
73
|
void registerUnistylesHostObject(jsi::Runtime &runtime, UnistylesModule* weakSelf) {
|
74
74
|
auto unistylesRuntime = std::make_shared<UnistylesRuntime>(
|
75
|
-
|
76
|
-
(int)weakSelf.platform.initialHeight,
|
75
|
+
weakSelf.platform.initialScreen,
|
77
76
|
weakSelf.platform.initialColorScheme,
|
78
77
|
weakSelf.platform.initialContentSizeCategory,
|
79
78
|
weakSelf.platform.initialInsets,
|
80
|
-
weakSelf.platform.initialStatusBar
|
79
|
+
weakSelf.platform.initialStatusBar,
|
80
|
+
weakSelf.platform.initialNavigationBar
|
81
81
|
);
|
82
82
|
|
83
83
|
unistylesRuntime.get()->onThemeChange([=](std::string theme) {
|
@@ -91,15 +91,29 @@ void registerUnistylesHostObject(jsi::Runtime &runtime, UnistylesModule* weakSel
|
|
91
91
|
[weakSelf emitEvent:@"__unistylesOnChange" withBody:body];
|
92
92
|
});
|
93
93
|
|
94
|
-
unistylesRuntime.get()->onLayoutChange([=](std::string breakpoint, std::string orientation,
|
94
|
+
unistylesRuntime.get()->onLayoutChange([=](std::string breakpoint, std::string orientation, Dimensions& screen, Dimensions& statusBar, Insets& insets, Dimensions& navigationBar) {
|
95
95
|
NSDictionary *body = @{
|
96
96
|
@"type": @"layout",
|
97
97
|
@"payload": @{
|
98
98
|
@"breakpoint": cxxStringToNSString(breakpoint),
|
99
99
|
@"orientation": cxxStringToNSString(orientation),
|
100
100
|
@"screen": @{
|
101
|
-
@"width": @(width),
|
102
|
-
@"height": @(height)
|
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)
|
103
117
|
}
|
104
118
|
}
|
105
119
|
};
|
@@ -1,14 +1,15 @@
|
|
1
1
|
#include <string>
|
2
2
|
#include <map>
|
3
|
+
#include <UnistylesRuntime.h>
|
3
4
|
|
4
5
|
@interface Platform : NSObject
|
5
6
|
|
6
|
-
@property (nonatomic, assign)
|
7
|
-
@property (nonatomic, assign) CGFloat initialHeight;
|
7
|
+
@property (nonatomic, assign) Dimensions initialScreen;
|
8
8
|
@property (nonatomic, assign) std::string initialColorScheme;
|
9
9
|
@property (nonatomic, assign) std::string initialContentSizeCategory;
|
10
|
-
@property (nonatomic, assign)
|
11
|
-
@property (nonatomic, assign)
|
10
|
+
@property (nonatomic, assign) Insets initialInsets;
|
11
|
+
@property (nonatomic, assign) Dimensions initialStatusBar;
|
12
|
+
@property (nonatomic, assign) Dimensions initialNavigationBar;
|
12
13
|
@property (nonatomic, assign) void* unistylesRuntime;
|
13
14
|
|
14
15
|
- (instancetype)init;
|