react-native-unistyles 2.8.0-beta.1 → 2.8.0-beta.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,407 +1,36 @@
1
1
  #include "UnistylesRuntime.h"
2
-
2
+ #include "Macros.h"
3
3
  #include <string>
4
- #include <vector>
5
- #include <algorithm>
6
4
 
7
- #pragma region HostObject
5
+ using namespace facebook;
8
6
 
9
- std::vector<jsi::PropNameID> UnistylesRuntime::getPropertyNames(jsi::Runtime& runtime) {
7
+ std::vector<jsi::PropNameID> UnistylesRuntime::getPropertyNames(jsi::Runtime& rt) {
10
8
  std::vector<jsi::PropNameID> properties;
9
+ std::for_each(this->getters.begin(), this->getters.end(), [&](const auto& it){
10
+ properties.push_back(jsi::PropNameID::forUtf8(rt, std::string(it.first)));
11
+ });
11
12
 
12
- // getters
13
- properties.push_back(jsi::PropNameID::forUtf8(runtime, std::string("screenWidth")));
14
- properties.push_back(jsi::PropNameID::forUtf8(runtime, std::string("screenHeight")));
15
- properties.push_back(jsi::PropNameID::forUtf8(runtime, std::string("contentSizeCategory")));
16
- properties.push_back(jsi::PropNameID::forUtf8(runtime, std::string("hasAdaptiveThemes")));
17
- properties.push_back(jsi::PropNameID::forUtf8(runtime, std::string("themeName")));
18
- properties.push_back(jsi::PropNameID::forUtf8(runtime, std::string("breakpoint")));
19
- properties.push_back(jsi::PropNameID::forUtf8(runtime, std::string("colorScheme")));
20
- properties.push_back(jsi::PropNameID::forUtf8(runtime, std::string("sortedBreakpointPairs")));
21
- properties.push_back(jsi::PropNameID::forUtf8(runtime, std::string("useBreakpoints")));
22
- properties.push_back(jsi::PropNameID::forUtf8(runtime, std::string("useTheme")));
23
- properties.push_back(jsi::PropNameID::forUtf8(runtime, std::string("updateTheme")));
24
- properties.push_back(jsi::PropNameID::forUtf8(runtime, std::string("useAdaptiveThemes")));
25
- properties.push_back(jsi::PropNameID::forUtf8(runtime, std::string("addPlugin")));
26
- properties.push_back(jsi::PropNameID::forUtf8(runtime, std::string("removePlugin")));
27
- properties.push_back(jsi::PropNameID::forUtf8(runtime, std::string("enabledPlugins")));
28
- properties.push_back(jsi::PropNameID::forUtf8(runtime, std::string("insets")));
29
- properties.push_back(jsi::PropNameID::forUtf8(runtime, std::string("statusBar")));
30
- properties.push_back(jsi::PropNameID::forUtf8(runtime, std::string("navigationBar")));
31
-
32
- // setters
33
- properties.push_back(jsi::PropNameID::forUtf8(runtime, std::string("themes")));
13
+ std::for_each(this->setters.begin(), this->setters.end(), [&](const auto& it){
14
+ properties.push_back(jsi::PropNameID::forUtf8(rt, std::string(it.first)));
15
+ });
34
16
 
35
17
  return properties;
36
18
  }
37
19
 
38
- jsi::Value UnistylesRuntime::get(jsi::Runtime& runtime, const jsi::PropNameID& propNameId) {
39
- std::string propName = propNameId.utf8(runtime);
40
-
41
- if (propName == "screenWidth") {
42
- return jsi::Value(this->screen.width);
43
- }
44
-
45
- if (propName == "screenHeight") {
46
- return jsi::Value(this->screen.height);
47
- }
48
-
49
- if (propName == "hasAdaptiveThemes") {
50
- return jsi::Value(this->hasAdaptiveThemes);
51
- }
52
-
53
- if (propName == "contentSizeCategory") {
54
- return jsi::Value(jsi::String::createFromUtf8(runtime, this->contentSizeCategory));
55
- }
56
-
57
- if (propName == "themeName") {
58
- return !this->themeName.empty()
59
- ? jsi::Value(jsi::String::createFromUtf8(runtime, this->themeName))
60
- : this->getThemeOrFail(runtime);
61
- }
62
-
63
- if (propName == "breakpoint") {
64
- return !this->breakpoint.empty()
65
- ? jsi::Value(jsi::String::createFromUtf8(runtime, this->breakpoint))
66
- : jsi::Value::undefined();
67
- }
68
-
69
- if (propName == "colorScheme") {
70
- return jsi::Value(jsi::String::createFromUtf8(runtime, this->colorScheme));
71
- }
72
-
73
- if (propName == "enabledPlugins") {
74
- auto jsiArray = facebook::jsi::Array(runtime, this->pluginNames.size());
75
-
76
- for (size_t i = 0; i < this->pluginNames.size(); i++) {
77
- jsiArray.setValueAtIndex(runtime, i, facebook::jsi::String::createFromUtf8(runtime, this->pluginNames[i]));
78
- }
79
-
80
- return jsiArray;
81
- }
82
-
83
- if (propName == "sortedBreakpointPairs") {
84
- std::unique_ptr<jsi::Array> sortedBreakpointEntriesArray = std::make_unique<jsi::Array>(runtime, this->sortedBreakpointPairs.size());
85
-
86
- for (size_t i = 0; i < this->sortedBreakpointPairs.size(); ++i) {
87
- std::unique_ptr<jsi::Array> pairArray = std::make_unique<jsi::Array>(runtime, 2);
88
- jsi::String nameValue = jsi::String::createFromUtf8(runtime, this->sortedBreakpointPairs[i].first);
89
-
90
- pairArray->setValueAtIndex(runtime, 0, nameValue);
91
- pairArray->setValueAtIndex(runtime, 1, jsi::Value(this->sortedBreakpointPairs[i].second));
92
- sortedBreakpointEntriesArray->setValueAtIndex(runtime, i, *pairArray);
93
- }
94
-
95
- return jsi::Value(runtime, *sortedBreakpointEntriesArray);
96
- }
97
-
98
- if (propName == "addPlugin") {
99
- return jsi::Function::createFromHostFunction(
100
- runtime,
101
- jsi::PropNameID::forAscii(runtime, "addPlugin"),
102
- 1,
103
- [this](jsi::Runtime &runtime, const jsi::Value &thisVal, const jsi::Value *arguments, size_t count) -> jsi::Value {
104
- std::string pluginName = arguments[0].asString(runtime).utf8(runtime);
105
- bool notify = arguments[1].asBool();
106
-
107
- this->pluginNames.push_back(pluginName);
108
-
109
- // registry enabled plugins won't notify listeners
110
- if (notify) {
111
- this->onPluginChangeCallback();
112
- }
20
+ jsi::Value UnistylesRuntime::get(jsi::Runtime& rt, const jsi::PropNameID& propNameId) {
21
+ auto method = this->getters.find(propNameId.utf8(rt));
113
22
 
114
- return jsi::Value::undefined();
115
- }
116
- );
117
- }
118
-
119
- if (propName == "removePlugin") {
120
- return jsi::Function::createFromHostFunction(
121
- runtime,
122
- jsi::PropNameID::forAscii(runtime, "removePlugin"),
123
- 1,
124
- [this](jsi::Runtime &runtime, const jsi::Value &thisVal, const jsi::Value *arguments, size_t count) -> jsi::Value {
125
- std::string pluginName = arguments[0].asString(runtime).utf8(runtime);
126
-
127
- auto it = std::find(this->pluginNames.begin(), this->pluginNames.end(), pluginName);
128
-
129
- if (it != this->pluginNames.end()) {
130
- this->pluginNames.erase(it);
131
- this->onPluginChangeCallback();
132
- }
133
-
134
- return jsi::Value::undefined();
135
- }
136
- );
137
- }
138
-
139
- if (propName == "useBreakpoints") {
140
- return jsi::Function::createFromHostFunction(
141
- runtime,
142
- jsi::PropNameID::forAscii(runtime, "useBreakpoints"),
143
- 1,
144
- [this](jsi::Runtime &runtime, const jsi::Value &thisVal, const jsi::Value *arguments, size_t count) -> jsi::Value {
145
- jsi::Object breakpointsObj = arguments[0].asObject(runtime);
146
- jsi::Array propertyNames = breakpointsObj.getPropertyNames(runtime);
147
- std::vector<std::pair<std::string, double>> sortedBreakpointEntriesVec;
148
-
149
- for (size_t i = 0; i < propertyNames.size(runtime); ++i) {
150
- jsi::Value propNameValue = propertyNames.getValueAtIndex(runtime, i);
151
- std::string name = propNameValue.asString(runtime).utf8(runtime);
152
- jsi::PropNameID propNameID = jsi::PropNameID::forUtf8(runtime, name);
153
- jsi::Value value = breakpointsObj.getProperty(runtime, propNameID);
154
-
155
- if (value.isNumber()) {
156
- double breakpointValue = value.asNumber();
157
- sortedBreakpointEntriesVec.push_back(std::make_pair(name, breakpointValue));
158
- }
159
- }
160
-
161
- std::sort(sortedBreakpointEntriesVec.begin(), sortedBreakpointEntriesVec.end(), [](const std::pair<std::string, double>& a, const std::pair<std::string, double>& b) {
162
- return a.second < b.second;
163
- });
164
-
165
- if (sortedBreakpointEntriesVec.size() == 0) {
166
- throw jsi::JSError(runtime, UnistylesErrorBreakpointsCannotBeEmpty);
167
- }
168
-
169
- if (sortedBreakpointEntriesVec.at(0).second != 0) {
170
- throw jsi::JSError(runtime, UnistylesErrorBreakpointsMustStartFromZero);
171
- }
172
-
173
- this->sortedBreakpointPairs = sortedBreakpointEntriesVec;
174
-
175
- std::string breakpoint = this->getBreakpointFromScreenWidth(this->screen.width, sortedBreakpointEntriesVec);
176
-
177
- this->breakpoint = breakpoint;
178
-
179
- return jsi::Value::undefined();
180
- }
181
- );
182
- }
183
-
184
- if (propName == "useTheme") {
185
- return jsi::Function::createFromHostFunction(runtime,
186
- jsi::PropNameID::forAscii(runtime, "useTheme"),
187
- 1,
188
- [this](jsi::Runtime &runtime, const jsi::Value &thisVal, const jsi::Value *arguments, size_t count) -> jsi::Value {
189
- std::string themeName = arguments[0].asString(runtime).utf8(runtime);
190
-
191
- if (this->themeName != themeName) {
192
- this->themeName = themeName;
193
- this->onThemeChangeCallback(themeName);
194
- }
195
-
196
- return jsi::Value::undefined();
197
- }
198
- );
199
- }
200
-
201
- if (propName == "updateTheme") {
202
- return jsi::Function::createFromHostFunction(runtime,
203
- jsi::PropNameID::forAscii(runtime, "updateTheme"),
204
- 1,
205
- [this](jsi::Runtime &runtime, const jsi::Value &thisVal, const jsi::Value *arguments, size_t count) -> jsi::Value {
206
- std::string themeName = arguments[0].asString(runtime).utf8(runtime);
207
-
208
- if (this->themeName == themeName) {
209
- this->onThemeChangeCallback(themeName);
210
- }
211
-
212
- return jsi::Value::undefined();
213
- }
214
- );
215
- }
216
-
217
- if (propName == "useAdaptiveThemes") {
218
- return jsi::Function::createFromHostFunction(runtime,
219
- jsi::PropNameID::forAscii(runtime, "useAdaptiveThemes"),
220
- 1,
221
- [this](jsi::Runtime &runtime, const jsi::Value &thisVal, const jsi::Value *arguments, size_t count) -> jsi::Value {
222
- bool enableAdaptiveThemes = arguments[0].asBool();
223
-
224
- if (enableAdaptiveThemes && this->colorScheme == UnistylesUnspecifiedScheme) {
225
- throw jsi::JSError(runtime, UnistylesErrorAdaptiveThemesNotSupported);
226
- }
227
-
228
- this->hasAdaptiveThemes = enableAdaptiveThemes;
229
-
230
- if (!enableAdaptiveThemes || !this->supportsAutomaticColorScheme) {
231
- return jsi::Value::undefined();
232
- }
233
-
234
- if (this->themeName != this->colorScheme) {
235
- this->themeName = this->colorScheme;
236
- this->onThemeChangeCallback(this->themeName);
237
- }
238
-
239
- return jsi::Value::undefined();
240
- }
241
- );
242
- }
243
-
244
- if (propName == "insets") {
245
- auto insets = jsi::Object(runtime);
246
-
247
- insets.setProperty(runtime, "top", this->insets.top);
248
- insets.setProperty(runtime, "bottom", this->insets.bottom);
249
- insets.setProperty(runtime, "left", this->insets.left);
250
- insets.setProperty(runtime, "right", this->insets.right);
251
-
252
- return insets;
253
- }
254
-
255
- if (propName == "statusBar") {
256
- auto statusBar = jsi::Object(runtime);
257
- auto setStatusBarColorFunction = jsi::Function::createFromHostFunction(runtime,
258
- jsi::PropNameID::forAscii(runtime, "setColor"),
259
- 1,
260
- [this](jsi::Runtime &runtime, const jsi::Value &thisVal, const jsi::Value *arguments, size_t count) -> jsi::Value {
261
- std::string color = arguments[0].asString(runtime).utf8(runtime);
262
-
263
- if (this->onSetStatusBarColorCallback.has_value()) {
264
- this->onSetStatusBarColorCallback.value()(color);
265
- }
266
-
267
- return jsi::Value::undefined();
268
- }
269
- );
270
-
271
- statusBar.setProperty(runtime, "width", this->statusBar.width);
272
- statusBar.setProperty(runtime, "height", this->statusBar.height);
273
- statusBar.setProperty(runtime, "setColor", setStatusBarColorFunction);
274
-
275
- return statusBar;
276
- }
277
-
278
- if (propName == "navigationBar") {
279
- auto navigationBarValue = jsi::Object(runtime);
280
- auto setNavigationBarColorFunction = jsi::Function::createFromHostFunction(runtime,
281
- jsi::PropNameID::forAscii(runtime, "setColor"),
282
- 1,
283
- [this](jsi::Runtime &runtime, const jsi::Value &thisVal, const jsi::Value *arguments, size_t count) -> jsi::Value {
284
- std::string color = arguments[0].asString(runtime).utf8(runtime);
285
-
286
- if (this->onSetStatusBarColorCallback.has_value()) {
287
- this->onSetNavigationBarColorCallback.value()(color);
288
- }
289
-
290
- return jsi::Value::undefined();
291
- }
292
- );
293
-
294
- navigationBarValue.setProperty(runtime, "width", this->navigationBar.width);
295
- navigationBarValue.setProperty(runtime, "height", this->navigationBar.height);
296
- navigationBarValue.setProperty(runtime, "setColor", setNavigationBarColorFunction);
297
-
298
- return navigationBarValue;
23
+ if (method != this->getters.cend()) {
24
+ return method->second(rt, method->first);
299
25
  }
300
26
 
301
27
  return jsi::Value::undefined();
302
28
  }
303
29
 
304
- void UnistylesRuntime::set(jsi::Runtime& runtime, const jsi::PropNameID& propNameId, const jsi::Value& value) {
305
- std::string propName = propNameId.utf8(runtime);
306
-
307
- if (propName == "themes" && value.isObject()) {
308
- jsi::Array themes = value.asObject(runtime).asArray(runtime);
309
- std::vector<std::string> themesVector;
310
- size_t length = themes.size(runtime);
311
-
312
- for (size_t i = 0; i < length; ++i) {
313
- jsi::Value element = themes.getValueAtIndex(runtime, i);
314
-
315
- if (element.isString()) {
316
- std::string theme = element.asString(runtime).utf8(runtime);
317
- themesVector.push_back(theme);
318
- }
319
- }
320
-
321
- if (themesVector.size() == 0) {
322
- throw jsi::JSError(runtime, UnistylesErrorThemesCannotBeEmpty);
323
- }
324
-
325
- this->themes = themesVector;
326
- this->themeName = "";
327
-
328
- bool hasLightTheme = std::find(themesVector.begin(), themesVector.end(), "light") != themesVector.end();
329
- bool hasDarkTheme = std::find(themesVector.begin(), themesVector.end(), "dark") != themesVector.end();
330
-
331
- this->supportsAutomaticColorScheme = hasLightTheme && hasDarkTheme;
332
-
333
- return;
334
- }
335
- }
336
-
337
- #pragma endregion
338
- #pragma region Helpers
339
-
340
- std::string UnistylesRuntime::getBreakpointFromScreenWidth(int width, const std::vector<std::pair<std::string, double>>& sortedBreakpointPairs) {
341
- for (size_t i = 0; i < sortedBreakpointPairs.size(); ++i) {
342
- const auto& [key, value] = sortedBreakpointPairs[i];
343
- const double maxVal = (i + 1 < sortedBreakpointPairs.size()) ? sortedBreakpointPairs[i + 1].second : std::numeric_limits<double>::infinity();
344
-
345
- if (width >= value && width < maxVal) {
346
- return key;
347
- }
348
- }
349
-
350
- return sortedBreakpointPairs.empty() ? "" : sortedBreakpointPairs.back().first;
351
- }
352
-
353
- void UnistylesRuntime::handleScreenSizeChange(Dimensions& screen, Insets& insets, Dimensions& statusBar, Dimensions& navigationBar) {
354
- std::string breakpoint = this->getBreakpointFromScreenWidth(screen.width, this->sortedBreakpointPairs);
355
- bool hasDifferentBreakpoint = this->breakpoint != breakpoint;
356
- bool hasDifferentScreenDimensions = this->screen.width != screen.width || this->screen.height != screen.height;
357
- bool hasDifferentInsets = this->insets.top != insets.top || this->insets.bottom != insets.bottom || this->insets.left != insets.left || this->insets.right != insets.right;
358
-
359
- // we don't need to check statusBar/navigationBar as they will only change on orientation change witch is equal to hasDifferentScreenDimensions
360
- bool shouldNotify = hasDifferentBreakpoint || hasDifferentScreenDimensions || hasDifferentInsets;
361
-
362
- this->breakpoint = breakpoint;
363
- this->screen = {screen.width, screen.height};
364
- this->insets = {insets.top, insets.bottom, insets.left, insets.right};
365
- this->statusBar = {statusBar.width, statusBar.height};
366
- this->navigationBar = {navigationBar.width, navigationBar.height};
30
+ void UnistylesRuntime::set(jsi::Runtime& rt, const jsi::PropNameID& propNameId, const jsi::Value& value) {
31
+ auto method = this->setters.find(propNameId.utf8(rt));
367
32
 
368
- std::string orientation = screen.width > screen.height
369
- ? UnistylesOrientationLandscape
370
- : UnistylesOrientationPortrait;
371
-
372
- if (shouldNotify) {
373
- this->onLayoutChangeCallback(breakpoint, orientation, screen, statusBar, insets, navigationBar);
33
+ if (method != this->setters.cend()) {
34
+ method->second(rt, value);
374
35
  }
375
36
  }
376
-
377
- void UnistylesRuntime::handleAppearanceChange(std::string colorScheme) {
378
- this->colorScheme = colorScheme;
379
-
380
- if (!this->supportsAutomaticColorScheme || !this->hasAdaptiveThemes) {
381
- return;
382
- }
383
-
384
- if (this->themeName != this->colorScheme) {
385
- this->onThemeChangeCallback(this->colorScheme);
386
- this->themeName = this->colorScheme;
387
- }
388
- }
389
-
390
- void UnistylesRuntime::handleContentSizeCategoryChange(std::string contentSizeCategory) {
391
- this->contentSizeCategory = contentSizeCategory;
392
- this->onContentSizeCategoryChangeCallback(contentSizeCategory);
393
- }
394
-
395
- jsi::Value UnistylesRuntime::getThemeOrFail(jsi::Runtime& runtime) {
396
- if (this->themes.size() == 1) {
397
- std::string themeName = this->themes.at(0);
398
-
399
- this->themeName = themeName;
400
-
401
- return jsi::String::createFromUtf8(runtime, themeName);
402
- }
403
-
404
- return jsi::Value().undefined();
405
- }
406
-
407
- #pragma endregion
@@ -1,108 +1,69 @@
1
1
  #pragma once
2
2
 
3
+ #include "UnistylesModel.h"
4
+ #include "Macros.h"
5
+ #include <ReactCommon/CallInvoker.h>
3
6
  #include <jsi/jsi.h>
4
- #include <vector>
5
- #include <map>
6
- #include <optional>
7
7
 
8
8
  using namespace facebook;
9
9
 
10
- const std::string UnistylesOrientationPortrait = "portrait";
11
- const std::string UnistylesOrientationLandscape = "landscape";
10
+ using Getter = std::function<jsi::Value(jsi::Runtime& rt, std::string)>;
11
+ using Setter = std::function<std::optional<jsi::Value>(jsi::Runtime& rt, const jsi::Value&)>;
12
12
 
13
- const std::string UnistylesDarkScheme = "dark";
14
- const std::string UnistylesLightScheme = "light";
15
- const std::string UnistylesUnspecifiedScheme = "unspecified";
13
+ struct JSI_EXPORT UnistylesRuntime : public jsi::HostObject, UnistylesModel {
14
+ UnistylesRuntime(jsi::Runtime& rt, std::shared_ptr<react::CallInvoker> callInvoker) : UnistylesModel(rt, callInvoker) {
15
+ this->getters = {
16
+ {"screenWidth", BIND_FN(getScreenWidth)},
17
+ {"screenHeight", BIND_FN(getScreenHeight)},
18
+ {"contentSizeCategory", BIND_FN(getContentSizeCategory)},
19
+ {"hasAdaptiveThemes", BIND_FN(hasEnabledAdaptiveThemes)},
20
+ {"themeName", BIND_FN(getThemeName)},
21
+ {"breakpoint", BIND_FN(getCurrentBreakpoint)},
22
+ {"colorScheme", BIND_FN(getColorScheme)},
23
+ {"sortedBreakpointPairs", BIND_FN(getSortedBreakpointPairs)},
24
+ {"useBreakpoints", BIND_FN(setBreakpoints)},
25
+ {"useTheme", BIND_FN(setActiveTheme)},
26
+ {"updateTheme", BIND_FN(updateTheme)},
27
+ {"useAdaptiveThemes", BIND_FN(useAdaptiveThemes)},
28
+ {"addPlugin", BIND_FN(addPlugin)},
29
+ {"removePlugin", BIND_FN(removePlugin)},
30
+ {"enabledPlugins", BIND_FN(getEnabledPlugins)},
31
+ {"insets", BIND_FN(getInsets)},
32
+ {"statusBar", BIND_FN(getStatusBar)},
33
+ {"navigationBar", BIND_FN(getNavigationBar)}
34
+ };
16
35
 
17
- const std::string UnistylesErrorBreakpointsCannotBeEmpty = "You are trying to register empty breakpoints object";
18
- const std::string UnistylesErrorBreakpointsMustStartFromZero = "You are trying to register breakpoints that don't start from 0";
19
- const std::string UnistylesErrorThemesCannotBeEmpty = "You are trying to register empty themes object";
20
- const std::string UnistylesErrorAdaptiveThemesNotSupported = "Your platform doesn't support adaptive themes";
36
+ this->setters = {
37
+ {"themes", BIND_FN(setThemes)}
38
+ };
39
+ };
21
40
 
22
- struct Dimensions {
23
- int width;
24
- int height;
25
- };
26
-
27
- struct Insets {
28
- int top;
29
- int bottom;
30
- int left;
31
- int right;
32
- };
41
+ jsi::Value getScreenWidth(jsi::Runtime&, std::string);
42
+ jsi::Value getScreenHeight(jsi::Runtime&, std::string);
43
+ jsi::Value getContentSizeCategory(jsi::Runtime&, std::string);
44
+ jsi::Value hasEnabledAdaptiveThemes(jsi::Runtime&, std::string);
45
+ jsi::Value getThemeName(jsi::Runtime&, std::string);
46
+ jsi::Value getCurrentBreakpoint(jsi::Runtime&, std::string);
47
+ jsi::Value getColorScheme(jsi::Runtime&, std::string);
48
+ jsi::Value getSortedBreakpointPairs(jsi::Runtime&, std::string);
49
+ jsi::Value setBreakpoints(jsi::Runtime&, std::string);
50
+ jsi::Value setActiveTheme(jsi::Runtime&, std::string);
51
+ jsi::Value updateTheme(jsi::Runtime&, std::string);
52
+ jsi::Value useAdaptiveThemes(jsi::Runtime&, std::string);
53
+ jsi::Value addPlugin(jsi::Runtime&, std::string);
54
+ jsi::Value removePlugin(jsi::Runtime&, std::string);
55
+ jsi::Value getEnabledPlugins(jsi::Runtime&, std::string);
56
+ jsi::Value getInsets(jsi::Runtime&, std::string);
57
+ jsi::Value getStatusBar(jsi::Runtime&, std::string);
58
+ jsi::Value getNavigationBar(jsi::Runtime&, std::string);
33
59
 
34
- class JSI_EXPORT UnistylesRuntime : public jsi::HostObject {
35
- private:
36
- std::function<void(std::string)> onThemeChangeCallback;
37
- std::function<void(std::string breakpoint, std::string orientation, Dimensions& screen, Dimensions& statusBar, Insets& insets, Dimensions& navigationBar)> onLayoutChangeCallback;
38
- std::function<void(std::string)> onContentSizeCategoryChangeCallback;
39
- std::function<void()> onPluginChangeCallback;
40
- std::optional<std::function<void(std::string)>> onSetStatusBarColorCallback;
41
- std::optional<std::function<void(std::string)>> onSetNavigationBarColorCallback;
42
-
43
- Dimensions screen;
44
- Dimensions statusBar;
45
- Dimensions navigationBar;
46
- Insets insets;
47
- std::string colorScheme;
48
- std::string contentSizeCategory;
49
-
50
- public:
51
- UnistylesRuntime(
52
- Dimensions screen,
53
- std::string colorScheme,
54
- std::string contentSizeCategory,
55
- Insets insets,
56
- Dimensions statusBar,
57
- Dimensions navigationBar
58
- ): screen(screen),
59
- colorScheme(colorScheme),
60
- contentSizeCategory(contentSizeCategory),
61
- insets(insets),
62
- statusBar(statusBar),
63
- navigationBar(navigationBar) {}
60
+ std::optional<jsi::Value> setThemes(jsi::Runtime&, const jsi::Value&);
64
61
 
65
- bool hasAdaptiveThemes;
66
- bool supportsAutomaticColorScheme;
67
-
68
- std::string themeName;
69
- std::string breakpoint;
70
- std::vector<std::string> pluginNames;
71
- std::vector<std::string> themes;
72
- std::vector<std::pair<std::string, double>> sortedBreakpointPairs;
73
-
74
- void onThemeChange(std::function<void(std::string)> callback) {
75
- this->onThemeChangeCallback = callback;
76
- }
77
-
78
- void onLayoutChange(std::function<void(std::string breakpoint, std::string orientation, Dimensions& screen, Dimensions& statusBar, Insets& insets, Dimensions& navigationBar)> callback) {
79
- this->onLayoutChangeCallback = callback;
80
- }
81
-
82
- void onPluginChange(std::function<void()> callback) {
83
- this->onPluginChangeCallback = callback;
84
- }
85
-
86
- void onContentSizeCategoryChange(std::function<void(std::string)> callback) {
87
- this->onContentSizeCategoryChangeCallback = callback;
88
- }
89
-
90
- void onSetStatusBarColor(std::function<void(std::string color)> callback) {
91
- this->onSetStatusBarColorCallback = callback;
92
- }
93
-
94
- void onSetNavigationBarColor(std::function<void(std::string color)> callback) {
95
- this->onSetNavigationBarColorCallback = callback;
96
- }
62
+ jsi::Value get(jsi::Runtime&, const jsi::PropNameID&) override;
63
+ void set(jsi::Runtime&, const jsi::PropNameID&, const jsi::Value&) override;
64
+ std::vector<jsi::PropNameID> getPropertyNames(jsi::Runtime&) override;
97
65
 
98
- jsi::Value get(jsi::Runtime&, const jsi::PropNameID& name) override;
99
- void set(jsi::Runtime& runtime, const jsi::PropNameID& propNameId, const jsi::Value& value) override;
100
- std::vector<jsi::PropNameID> getPropertyNames(jsi::Runtime& runtime) override;
101
-
102
- void handleScreenSizeChange(Dimensions& screen, Insets& insets, Dimensions& statusBar, Dimensions& navigationBar);
103
- void handleAppearanceChange(std::string colorScheme);
104
- void handleContentSizeCategoryChange(std::string contentSizeCategory);
105
-
106
- jsi::Value getThemeOrFail(jsi::Runtime&);
107
- std::string getBreakpointFromScreenWidth(int width, const std::vector<std::pair<std::string, double>>& sortedBreakpointEntries);
66
+ private:
67
+ std::map<std::string, Getter> getters;
68
+ std::map<std::string, Setter> setters;
108
69
  };
@@ -1,5 +1,6 @@
1
1
  #import <React/RCTBridgeModule.h>
2
2
  #import <React/RCTEventEmitter.h>
3
+ #import <ReactCommon/RCTRuntimeExecutor.h>
3
4
  #import <string>
4
5
 
5
6
  #if TARGET_OS_OSX
@@ -12,6 +13,13 @@
12
13
  #import "Platform_visionOS.h"
13
14
  #endif
14
15
 
16
+ @interface RCTBridge (BridgeWithRuntime)
17
+
18
+ - (void *)runtime;
19
+ - (std::shared_ptr<facebook::react::CallInvoker>)jsCallInvoker;
20
+
21
+ @end
22
+
15
23
  @interface UnistylesModule : RCTEventEmitter<RCTBridgeModule>
16
24
 
17
25
  @property (nonatomic, assign) BOOL hasListeners;