react-native-unistyles 3.0.0-alpha.2 → 3.0.0-alpha.4
Sign up to get free protection for your applications and to get access to all the features.
- package/Unistyles.podspec +3 -2
- package/cxx/core/HostStyle.cpp +11 -9
- package/cxx/core/StyleSheetRegistry.cpp +6 -1
- package/cxx/core/StyleSheetRegistry.h +2 -1
- package/cxx/core/UnistylesCommitHook.cpp +3 -6
- package/cxx/core/UnistylesCommitHook.h +6 -1
- package/cxx/core/UnistylesRegistry.cpp +6 -6
- package/cxx/core/UnistylesRegistry.h +1 -1
- package/cxx/core/UnistylesState.cpp +4 -4
- package/cxx/hybridObjects/HybridShadowRegistry.cpp +4 -0
- package/cxx/hybridObjects/HybridStyleSheet.cpp +23 -32
- package/cxx/hybridObjects/HybridStyleSheet.h +6 -6
- package/cxx/hybridObjects/HybridUnistylesRuntime.cpp +17 -13
- package/cxx/parser/Parser.cpp +12 -8
- package/ios/UnistylesModuleOnLoad.h +2 -0
- package/ios/UnistylesModuleOnLoad.mm +8 -1
- package/package.json +1 -1
package/Unistyles.podspec
CHANGED
@@ -10,7 +10,7 @@ Pod::Spec.new do |s|
|
|
10
10
|
s.license = package["license"]
|
11
11
|
s.authors = package["author"]
|
12
12
|
|
13
|
-
s.platforms = { :ios => min_ios_version_supported
|
13
|
+
s.platforms = { :ios => min_ios_version_supported }
|
14
14
|
s.source = { :git => package["repository"], :tag => "#{s.version}" }
|
15
15
|
|
16
16
|
s.source_files = [
|
@@ -18,7 +18,8 @@ Pod::Spec.new do |s|
|
|
18
18
|
"cxx/**/*.{h,cpp,hpp}"
|
19
19
|
]
|
20
20
|
s.pod_target_xcconfig = {
|
21
|
-
"CLANG_CXX_LANGUAGE_STANDARD" => "c++20"
|
21
|
+
"CLANG_CXX_LANGUAGE_STANDARD" => "c++20",
|
22
|
+
"GCC_PREPROCESSOR_DEFINITIONS" => "$(inherited) FOLLY_NO_CONFIG FOLLY_CFG_NO_COROUTINES"
|
22
23
|
}
|
23
24
|
|
24
25
|
s.public_header_files = [
|
package/cxx/core/HostStyle.cpp
CHANGED
@@ -7,10 +7,12 @@ using namespace facebook;
|
|
7
7
|
std::vector<jsi::PropNameID> HostStyle::getPropertyNames(jsi::Runtime& rt) {
|
8
8
|
auto propertyNames = std::vector<jsi::PropNameID> {};
|
9
9
|
|
10
|
+
propertyNames.reserve(8);
|
11
|
+
|
10
12
|
for (const auto& pair : this->_styleSheet->unistyles) {
|
11
13
|
propertyNames.emplace_back(jsi::PropNameID::forUtf8(rt, pair.first));
|
12
14
|
}
|
13
|
-
|
15
|
+
|
14
16
|
return propertyNames;
|
15
17
|
}
|
16
18
|
|
@@ -20,7 +22,7 @@ jsi::Value HostStyle::get(jsi::Runtime& rt, const jsi::PropNameID& propNameId) {
|
|
20
22
|
if (propertyName == helpers::UNISTYLES_ID) {
|
21
23
|
return jsi::Value(this->_styleSheet->tag);
|
22
24
|
}
|
23
|
-
|
25
|
+
|
24
26
|
if (propertyName == helpers::ADD_VARIANTS_FN) {
|
25
27
|
return this->createAddVariantsProxyFunction(rt);
|
26
28
|
}
|
@@ -28,7 +30,7 @@ jsi::Value HostStyle::get(jsi::Runtime& rt, const jsi::PropNameID& propNameId) {
|
|
28
30
|
if (this->_styleSheet->unistyles.contains(propertyName)) {
|
29
31
|
return valueFromUnistyle(rt, this->_styleSheet->unistyles[propertyName]);
|
30
32
|
}
|
31
|
-
|
33
|
+
|
32
34
|
return jsi::Value::undefined();
|
33
35
|
}
|
34
36
|
|
@@ -36,20 +38,20 @@ jsi::Function HostStyle::createAddVariantsProxyFunction(jsi::Runtime& rt) {
|
|
36
38
|
auto useVariantsFnName = jsi::PropNameID::forUtf8(rt, helpers::ADD_VARIANTS_FN);
|
37
39
|
|
38
40
|
return jsi::Function::createFromHostFunction(rt, useVariantsFnName, 1, [&](jsi::Runtime &rt, const jsi::Value &thisVal, const jsi::Value *arguments, size_t count){
|
39
|
-
helpers::assertThat(rt, count == 1, "useVariants expected to be called with one argument.");
|
40
|
-
helpers::assertThat(rt, arguments[0].isObject(), "useVariants expected to be called with object.");
|
41
|
+
helpers::assertThat(rt, count == 1, "Unistyles: useVariants expected to be called with one argument.");
|
42
|
+
helpers::assertThat(rt, arguments[0].isObject(), "Unistyles: useVariants expected to be called with object.");
|
41
43
|
|
42
44
|
auto parser = parser::Parser(this->_unistylesRuntime);
|
43
45
|
auto pairs = parser.variantsToPairs(rt, arguments[0].asObject(rt));
|
44
|
-
|
46
|
+
|
45
47
|
if (pairs == this->_styleSheet->variants) {
|
46
48
|
return jsi::Value::undefined();
|
47
49
|
}
|
48
|
-
|
50
|
+
|
49
51
|
this->_styleSheet->variants = pairs;
|
50
|
-
|
52
|
+
|
51
53
|
parser.rebuildUnistylesWithVariants(rt, this->_styleSheet);
|
52
|
-
|
54
|
+
|
53
55
|
return jsi::Value::undefined();
|
54
56
|
});
|
55
57
|
}
|
@@ -17,7 +17,7 @@ std::shared_ptr<StyleSheet> StyleSheetRegistry::addStyleSheetFromValue(jsi::Runt
|
|
17
17
|
std::shared_ptr<StyleSheet> StyleSheetRegistry::addFromFunction(jsi::Runtime& rt, unsigned int tag, jsi::Function styleSheetFn) {
|
18
18
|
auto numberOfArgs = styleSheetFn.getProperty(rt, "length").getNumber();
|
19
19
|
|
20
|
-
helpers::assertThat(rt, numberOfArgs <= 2, "expected up to 2 arguments.");
|
20
|
+
helpers::assertThat(rt, numberOfArgs <= 2, "StyleSheet.create expected up to 2 arguments.");
|
21
21
|
|
22
22
|
auto& registry = UnistylesRegistry::get();
|
23
23
|
|
@@ -43,3 +43,8 @@ std::shared_ptr<StyleSheet> StyleSheetRegistry::addFromObject(jsi::Runtime& rt,
|
|
43
43
|
return registry.addStyleSheet(tag, core::StyleSheetType::Static, std::move(rawStyleSheet));
|
44
44
|
}
|
45
45
|
|
46
|
+
void StyleSheetRegistry::removeStyleSheetByTag(unsigned int tag) {
|
47
|
+
auto& registry = UnistylesRegistry::get();
|
48
|
+
|
49
|
+
registry.removeStyleSheet(tag);
|
50
|
+
}
|
@@ -17,7 +17,8 @@ struct StyleSheetRegistry {
|
|
17
17
|
StyleSheetRegistry(StyleSheetRegistry&&) = delete;
|
18
18
|
|
19
19
|
virtual std::shared_ptr<StyleSheet> addStyleSheetFromValue(jsi::Runtime& rt, jsi::Object rawStyleSheet);
|
20
|
-
|
20
|
+
virtual void removeStyleSheetByTag(unsigned int tag);
|
21
|
+
|
21
22
|
private:
|
22
23
|
virtual std::shared_ptr<StyleSheet> addFromFunction(jsi::Runtime& rt, unsigned int tag, jsi::Function styleSheetFn);
|
23
24
|
virtual std::shared_ptr<StyleSheet> addFromObject(jsi::Runtime& rt, unsigned int tag, jsi::Object rawStyleSheet);
|
@@ -3,12 +3,9 @@
|
|
3
3
|
using namespace margelo::nitro::unistyles;
|
4
4
|
using namespace facebook::react;
|
5
5
|
|
6
|
-
core::UnistylesCommitHook
|
7
|
-
|
8
|
-
|
9
|
-
};
|
10
|
-
|
11
|
-
core::UnistylesCommitHook::~UnistylesCommitHook() noexcept {}
|
6
|
+
core::UnistylesCommitHook::~UnistylesCommitHook() noexcept {
|
7
|
+
_uiManager->unregisterCommitHook(*this);
|
8
|
+
}
|
12
9
|
|
13
10
|
void core::UnistylesCommitHook::commitHookWasRegistered(const UIManager &uiManager) noexcept {}
|
14
11
|
void core::UnistylesCommitHook::commitHookWasUnregistered(const UIManager &uiManager) noexcept {}
|
@@ -11,7 +11,11 @@ namespace margelo::nitro::unistyles::core {
|
|
11
11
|
using namespace facebook::react;
|
12
12
|
|
13
13
|
struct UnistylesCommitHook : public UIManagerCommitHook {
|
14
|
-
UnistylesCommitHook(UIManager
|
14
|
+
UnistylesCommitHook(std::shared_ptr<UIManager> uiManager, std::shared_ptr<HybridUnistylesRuntime> unistylesRuntime)
|
15
|
+
: _unistylesRuntime{unistylesRuntime}, _uiManager{uiManager} {
|
16
|
+
_uiManager->registerCommitHook(*this);
|
17
|
+
}
|
18
|
+
|
15
19
|
~UnistylesCommitHook() noexcept override;
|
16
20
|
|
17
21
|
void commitHookWasRegistered(const UIManager &uiManager) noexcept override;
|
@@ -22,6 +26,7 @@ struct UnistylesCommitHook : public UIManagerCommitHook {
|
|
22
26
|
|
23
27
|
private:
|
24
28
|
std::shared_ptr<HybridUnistylesRuntime> _unistylesRuntime;
|
29
|
+
std::shared_ptr<UIManager> _uiManager;
|
25
30
|
};
|
26
31
|
|
27
32
|
}
|
@@ -63,15 +63,15 @@ void core::UnistylesRegistry::updateTheme(jsi::Runtime& rt, std::string& themeNa
|
|
63
63
|
auto& state = this->getState(rt);
|
64
64
|
auto it = state._jsThemes.find(themeName);
|
65
65
|
|
66
|
-
helpers::assertThat(rt, it != state._jsThemes.end(), "
|
66
|
+
helpers::assertThat(rt, it != state._jsThemes.end(), "Unistyles: You're trying to update theme '" + themeName + "' but it wasn't registered.");
|
67
67
|
|
68
68
|
auto currentThemeValue = it->second.lock(rt);
|
69
69
|
|
70
|
-
helpers::assertThat(rt, currentThemeValue.isObject(), "
|
70
|
+
helpers::assertThat(rt, currentThemeValue.isObject(), "Unistyles: Unable to update your theme from C++. It was already garbage collected.");
|
71
71
|
|
72
72
|
auto result = callback.call(rt, currentThemeValue.asObject(rt));
|
73
73
|
|
74
|
-
helpers::assertThat(rt, result.isObject(), "
|
74
|
+
helpers::assertThat(rt, result.isObject(), "Unistyles: Returned theme is not an object. Please check your updateTheme function.");
|
75
75
|
|
76
76
|
it->second = jsi::WeakObject(rt, result.asObject(rt));
|
77
77
|
}
|
@@ -99,7 +99,7 @@ std::shared_ptr<core::StyleSheet> core::UnistylesRegistry::addStyleSheet(int tag
|
|
99
99
|
return this->_styleSheetRegistry.back();
|
100
100
|
}
|
101
101
|
|
102
|
-
|
102
|
+
void core::UnistylesRegistry::removeStyleSheet(int tag) {
|
103
103
|
auto it = std::find_if(
|
104
104
|
this->_styleSheetRegistry.begin(),
|
105
105
|
this->_styleSheetRegistry.end(),
|
@@ -111,8 +111,8 @@ std::shared_ptr<core::StyleSheet> core::UnistylesRegistry::getStyleSheetById(int
|
|
111
111
|
if (it == this->_styleSheetRegistry.cend()) {
|
112
112
|
throw std::runtime_error("stylesheet with tag: " + std::to_string(tag) + " cannot be found.");
|
113
113
|
}
|
114
|
-
|
115
|
-
|
114
|
+
|
115
|
+
this->_styleSheetRegistry.erase(it);
|
116
116
|
}
|
117
117
|
|
118
118
|
DependencyMap core::UnistylesRegistry::buildDependencyMap(std::vector<UnistyleDependency>& deps) {
|
@@ -38,7 +38,7 @@ struct UnistylesRegistry: public StyleSheetRegistry {
|
|
38
38
|
void linkShadowNodeWithUnistyle(const ShadowNodeFamily*, const core::Unistyle::Shared);
|
39
39
|
void unlinkShadowNodeWithUnistyle(const ShadowNodeFamily*, const core::Unistyle::Shared);
|
40
40
|
std::shared_ptr<core::StyleSheet> addStyleSheet(int tag, core::StyleSheetType type, jsi::Object&& rawValue);
|
41
|
-
|
41
|
+
void removeStyleSheet(int tag);
|
42
42
|
DependencyMap buildDependencyMap(std::vector<UnistyleDependency>& deps);
|
43
43
|
DependencyMap buildDependencyMap();
|
44
44
|
|
@@ -12,7 +12,7 @@ bool core::UnistylesState::hasAdaptiveThemes() {
|
|
12
12
|
}
|
13
13
|
|
14
14
|
void core::UnistylesState::setTheme(std::string themeName) {
|
15
|
-
helpers::assertThat(*_rt, helpers::vecContainsKeys(this->_registeredThemeNames, {themeName}), "You're trying to set theme to: '" + std::string(themeName) + "', but it wasn't registered.");
|
15
|
+
helpers::assertThat(*_rt, helpers::vecContainsKeys(this->_registeredThemeNames, {themeName}), "Unistyles: You're trying to set theme to: '" + std::string(themeName) + "', but it wasn't registered.");
|
16
16
|
|
17
17
|
if (themeName != this->_currentThemeName) {
|
18
18
|
this->_currentThemeName = themeName;
|
@@ -31,15 +31,15 @@ jsi::Object core::UnistylesState::getJSTheme() {
|
|
31
31
|
return jsi::Object(*_rt);
|
32
32
|
}
|
33
33
|
|
34
|
-
helpers::assertThat(*_rt, _currentThemeName.has_value(), "
|
34
|
+
helpers::assertThat(*_rt, _currentThemeName.has_value(), "Unistyles: One of your stylesheets is trying to get the theme, but no theme has been selected yet. Did you forget to select an initial theme?");
|
35
35
|
|
36
36
|
auto it = this->_jsThemes.find(_currentThemeName.value());
|
37
37
|
|
38
|
-
helpers::assertThat(*_rt, it != this->_jsThemes.end(), "
|
38
|
+
helpers::assertThat(*_rt, it != this->_jsThemes.end(), "Unistyles: You're trying to get theme '" + _currentThemeName.value() + "', but it was not registered. Did you forget to register it with StyleSheet.configure?");
|
39
39
|
|
40
40
|
auto maybeTheme = it->second.lock(*_rt);
|
41
41
|
|
42
|
-
helpers::assertThat(*_rt, maybeTheme.isObject(), "
|
42
|
+
helpers::assertThat(*_rt, maybeTheme.isObject(), "Unistyles: Unable to retrieve your theme from C++ as it has already been garbage collected, likely due to multiple hot reloads. Please live reload the app.");
|
43
43
|
|
44
44
|
return maybeTheme.asObject(*_rt);
|
45
45
|
}
|
@@ -4,6 +4,8 @@ using namespace margelo::nitro::unistyles;
|
|
4
4
|
using namespace facebook::react;
|
5
5
|
|
6
6
|
jsi::Value HybridShadowRegistry::link(jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
|
7
|
+
helpers::assertThat(rt, count == 2, "Unistyles: Invalid babel transform 'ShadowRegistry link' expected two arguments.");
|
8
|
+
|
7
9
|
ShadowNode::Shared shadowNodeWrapper = shadowNodeFromValue(rt, args[0]);
|
8
10
|
core::Unistyle::Shared unistyleWrapper = core::unistyleFromValue(rt, args[1]);
|
9
11
|
|
@@ -15,6 +17,8 @@ jsi::Value HybridShadowRegistry::link(jsi::Runtime &rt, const jsi::Value &thisVa
|
|
15
17
|
}
|
16
18
|
|
17
19
|
jsi::Value HybridShadowRegistry::unlink(jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
|
20
|
+
helpers::assertThat(rt, count == 2, "Unistyles: Invalid babel transform 'ShadowRegistry unlink' expected two arguments.");
|
21
|
+
|
18
22
|
ShadowNode::Shared shadowNodeWrapper = shadowNodeFromValue(rt, args[0]);
|
19
23
|
core::Unistyle::Shared unistyleWrapper = core::unistyleFromValue(rt, args[1]);
|
20
24
|
|
@@ -14,18 +14,15 @@ double HybridStyleSheet::get___unid() {
|
|
14
14
|
}
|
15
15
|
|
16
16
|
jsi::Value HybridStyleSheet::create(jsi::Runtime &rt, const jsi::Value &thisVal, const jsi::Value *arguments, size_t count) {
|
17
|
-
helpers::assertThat(rt,
|
17
|
+
helpers::assertThat(rt, count == 1, "StyleSheet.create expected to be called with one argument.");
|
18
|
+
helpers::assertThat(rt, arguments[0].isObject(), "StyleSheet.create expected to be called with object or function.");
|
18
19
|
|
19
20
|
auto thisStyleSheet = thisVal.asObject(rt);
|
20
21
|
auto& registry = core::UnistylesRegistry::get();
|
21
22
|
|
22
23
|
// this might happen only when hot reloading
|
23
24
|
if (this->__unid != -1) {
|
24
|
-
|
25
|
-
auto style = std::make_shared<core::HostStyle>(registeredStyleSheet, this->_unistylesRuntime);
|
26
|
-
auto styleHostObject = jsi::Object::createFromHostObject(rt, style);
|
27
|
-
|
28
|
-
return styleHostObject;
|
25
|
+
registry.removeStyleSheet(this->__unid);
|
29
26
|
}
|
30
27
|
|
31
28
|
jsi::Object rawStyleSheet = arguments[0].asObject(rt);
|
@@ -45,7 +42,8 @@ jsi::Value HybridStyleSheet::create(jsi::Runtime &rt, const jsi::Value &thisVal,
|
|
45
42
|
}
|
46
43
|
|
47
44
|
jsi::Value HybridStyleSheet::configure(jsi::Runtime &rt, const jsi::Value &thisVal, const jsi::Value *arguments, size_t count) {
|
48
|
-
helpers::assertThat(rt,
|
45
|
+
helpers::assertThat(rt, count == 1, "StyleSheet.configure expected to be called with one argument.");
|
46
|
+
helpers::assertThat(rt, arguments[0].isObject(), "StyleSheet.configure expected to be called with object.");
|
49
47
|
|
50
48
|
// create new state
|
51
49
|
auto config = arguments[0].asObject(rt);
|
@@ -55,29 +53,28 @@ jsi::Value HybridStyleSheet::configure(jsi::Runtime &rt, const jsi::Value &thisV
|
|
55
53
|
|
56
54
|
helpers::enumerateJSIObject(rt, config, [&](const std::string& propertyName, jsi::Value& propertyValue){
|
57
55
|
if (propertyName == "settings") {
|
58
|
-
helpers::assertThat(rt, propertyValue.isObject(), "settings must be an object.");
|
56
|
+
helpers::assertThat(rt, propertyValue.isObject(), "StyleSheet.configure's settings must be an object.");
|
59
57
|
|
60
58
|
return this->parseSettings(rt, propertyValue.asObject(rt));
|
61
59
|
}
|
62
60
|
|
63
61
|
if (propertyName == "breakpoints") {
|
64
|
-
helpers::assertThat(rt, propertyValue.isObject(), "breakpoints must be an object.");
|
62
|
+
helpers::assertThat(rt, propertyValue.isObject(), "StyleSheet.configure's breakpoints must be an object.");
|
65
63
|
|
66
64
|
return this->parseBreakpoints(rt, propertyValue.asObject(rt));
|
67
65
|
}
|
68
66
|
|
69
67
|
if (propertyName == "themes") {
|
70
|
-
helpers::assertThat(rt, propertyValue.isObject(), "themes must be an object.");
|
68
|
+
helpers::assertThat(rt, propertyValue.isObject(), "StyleSheet.configure's themes must be an object.");
|
71
69
|
|
72
70
|
return this->parseThemes(rt, propertyValue.asObject(rt));
|
73
71
|
}
|
74
72
|
|
75
|
-
helpers::assertThat(rt, false, "received unexpected key: '" + std::string(propertyName) + "'.");
|
73
|
+
helpers::assertThat(rt, false, "StyleSheet.configure received unexpected key: '" + std::string(propertyName) + "'.");
|
76
74
|
});
|
77
75
|
|
78
76
|
verifyAndSelectTheme(rt);
|
79
77
|
loadExternalMethods(thisVal, rt);
|
80
|
-
registerCommitHook(rt);
|
81
78
|
|
82
79
|
return jsi::Value::undefined();
|
83
80
|
}
|
@@ -87,7 +84,7 @@ void HybridStyleSheet::parseSettings(jsi::Runtime &rt, jsi::Object settings) {
|
|
87
84
|
|
88
85
|
helpers::enumerateJSIObject(rt, settings, [&](const std::string& propertyName, jsi::Value& propertyValue){
|
89
86
|
if (propertyName == "adaptiveThemes") {
|
90
|
-
helpers::assertThat(rt, propertyValue.isBool(), "adaptiveThemes
|
87
|
+
helpers::assertThat(rt, propertyValue.isBool(), "StyleSheet.configure's adaptiveThemes must be of boolean type.");
|
91
88
|
|
92
89
|
registry.setPrefersAdaptiveThemes(rt, propertyValue.asBool());
|
93
90
|
|
@@ -96,31 +93,31 @@ void HybridStyleSheet::parseSettings(jsi::Runtime &rt, jsi::Object settings) {
|
|
96
93
|
|
97
94
|
if (propertyName == "initialTheme") {
|
98
95
|
if (propertyValue.isObject()) {
|
99
|
-
helpers::assertThat(rt, propertyValue.asObject(rt).isFunction(rt), "initialTheme
|
96
|
+
helpers::assertThat(rt, propertyValue.asObject(rt).isFunction(rt), "StyleSheet.configure's initialTheme must be either a string or a function.");
|
100
97
|
|
101
98
|
auto result = propertyValue.asObject(rt).asFunction(rt).call(rt);
|
102
99
|
|
103
|
-
helpers::assertThat(rt, result.isString(), "initialTheme resolved from function is not a string. Please check your initialTheme function.");
|
100
|
+
helpers::assertThat(rt, result.isString(), "StyleSheet.configure's initialTheme resolved from function is not a string. Please check your initialTheme function.");
|
104
101
|
|
105
102
|
return registry.setInitialThemeName(rt, result.asString(rt).utf8(rt));
|
106
103
|
}
|
107
104
|
|
108
|
-
helpers::assertThat(rt, propertyValue.isString(), "initialTheme
|
105
|
+
helpers::assertThat(rt, propertyValue.isString(), "StyleSheet.configure's initialTheme must be either a string or a function.");
|
109
106
|
|
110
107
|
registry.setInitialThemeName(rt, propertyValue.asString(rt).utf8(rt));
|
111
108
|
|
112
109
|
return;
|
113
110
|
}
|
114
111
|
|
115
|
-
helpers::assertThat(rt, false, "settings received unexpected key: '" + std::string(propertyName) + "'");
|
112
|
+
helpers::assertThat(rt, false, "StyleSheet.configure's settings received unexpected key: '" + std::string(propertyName) + "'");
|
116
113
|
});
|
117
114
|
}
|
118
115
|
|
119
116
|
void HybridStyleSheet::parseBreakpoints(jsi::Runtime &rt, jsi::Object breakpoints){
|
120
117
|
helpers::Breakpoints sortedBreakpoints = helpers::jsiBreakpointsToVecPairs(rt, std::move(breakpoints));
|
121
118
|
|
122
|
-
helpers::assertThat(rt, sortedBreakpoints.size() > 0, "
|
123
|
-
helpers::assertThat(rt, sortedBreakpoints.front().second == 0, "first breakpoint must start from 0.");
|
119
|
+
helpers::assertThat(rt, sortedBreakpoints.size() > 0, "StyleSheet.configure's breakpoints can't be empty.");
|
120
|
+
helpers::assertThat(rt, sortedBreakpoints.front().second == 0, "StyleSheet.configure's first breakpoint must start from 0.");
|
124
121
|
|
125
122
|
auto& registry = core::UnistylesRegistry::get();
|
126
123
|
auto& state = registry.getState(rt);
|
@@ -133,7 +130,7 @@ void HybridStyleSheet::parseThemes(jsi::Runtime &rt, jsi::Object themes) {
|
|
133
130
|
auto& registry = core::UnistylesRegistry::get();
|
134
131
|
|
135
132
|
helpers::enumerateJSIObject(rt, themes, [&](const std::string& propertyName, jsi::Value& propertyValue){
|
136
|
-
helpers::assertThat(rt, propertyValue.isObject(), "registered theme '" + propertyName + "' must be an object.");
|
133
|
+
helpers::assertThat(rt, propertyValue.isObject(), "StyleSheet.configure's registered theme '" + propertyName + "' must be an object.");
|
137
134
|
|
138
135
|
registry.registerTheme(rt, propertyName, propertyValue.asObject(rt));
|
139
136
|
});
|
@@ -151,7 +148,7 @@ void HybridStyleSheet::verifyAndSelectTheme(jsi::Runtime &rt) {
|
|
151
148
|
|
152
149
|
// user tries to enable adaptive themes, but didn't register both 'light' and 'dark' themes
|
153
150
|
if (prefersAdaptiveThemes && !hasAdaptiveThemes) {
|
154
|
-
helpers::assertThat(rt, false, "
|
151
|
+
helpers::assertThat(rt, false, "Unistyles: You're trying to enable adaptiveThemes, but you didn't register both 'light' and 'dark' themes.");
|
155
152
|
}
|
156
153
|
|
157
154
|
// user didn't select initial theme nor can have adaptive themes, and registered more than 1 theme
|
@@ -175,14 +172,14 @@ void HybridStyleSheet::verifyAndSelectTheme(jsi::Runtime &rt) {
|
|
175
172
|
// user selected both initial theme and adaptive themes
|
176
173
|
// we should throw an error as these options are mutually exclusive
|
177
174
|
if (hasInitialTheme && hasAdaptiveThemes) {
|
178
|
-
helpers::assertThat(rt, false, "
|
175
|
+
helpers::assertThat(rt, false, "Unistyles: You're trying to set initial theme and enable adaptiveThemes, but these options are mutually exclusive.");
|
179
176
|
}
|
180
177
|
|
181
178
|
// user only selected initial theme
|
182
179
|
// validate if following theme exist
|
183
180
|
std::string selectedTheme = state.getInitialTheme().value();
|
184
181
|
|
185
|
-
helpers::assertThat(rt, state.hasTheme(selectedTheme), "
|
182
|
+
helpers::assertThat(rt, state.hasTheme(selectedTheme), "Unistyles: You're trying to select theme '" + selectedTheme + "' but it wasn't registered.");
|
186
183
|
|
187
184
|
state.setTheme(selectedTheme);
|
188
185
|
}
|
@@ -201,18 +198,18 @@ void HybridStyleSheet::setThemeFromColorScheme(jsi::Runtime& rt) {
|
|
201
198
|
|
202
199
|
return;
|
203
200
|
default:
|
204
|
-
throw std::runtime_error("
|
201
|
+
throw std::runtime_error("Unistyles: Unable to set adaptive theme as your device doesn't support it.");
|
205
202
|
}
|
206
203
|
}
|
207
204
|
|
208
205
|
void HybridStyleSheet::loadExternalMethods(const jsi::Value& thisValue, jsi::Runtime& rt) {
|
209
206
|
auto jsMethods = thisValue.getObject(rt).getProperty(rt, "jsMethods");
|
210
207
|
|
211
|
-
helpers::assertThat(rt, jsMethods.isObject(), "
|
208
|
+
helpers::assertThat(rt, jsMethods.isObject(), "Unistyles: Can't find jsMethods.");
|
212
209
|
|
213
210
|
auto maybeProcessColorFn = jsMethods.asObject(rt).getProperty(rt, "processColor");
|
214
211
|
|
215
|
-
helpers::assertThat(rt, maybeProcessColorFn.isObject(), "
|
212
|
+
helpers::assertThat(rt, maybeProcessColorFn.isObject(), "Unistyles: Can't load processColor function from JS.");
|
216
213
|
|
217
214
|
auto processColorFn = maybeProcessColorFn.asObject(rt).asFunction(rt);
|
218
215
|
auto& registry = core::UnistylesRegistry::get();
|
@@ -237,9 +234,3 @@ void HybridStyleSheet::onPlatformDependenciesChange(std::vector<UnistyleDependen
|
|
237
234
|
|
238
235
|
shadow::ShadowTreeManager::updateShadowTree(rt, shadowLeafUpdates);
|
239
236
|
}
|
240
|
-
|
241
|
-
void HybridStyleSheet::registerCommitHook(jsi::Runtime &rt) {
|
242
|
-
UIManager& uiManager = const_cast<UIManager&>(UIManagerBinding::getBinding(rt)->getUIManager());
|
243
|
-
|
244
|
-
this->_unistylesCommitHook = std::make_shared<core::UnistylesCommitHook>(uiManager, this->_unistylesRuntime);
|
245
|
-
}
|
@@ -16,11 +16,12 @@ using namespace margelo::nitro::unistyles;
|
|
16
16
|
using namespace facebook::react;
|
17
17
|
|
18
18
|
struct HybridStyleSheet: public HybridUnistylesStyleSheetSpec {
|
19
|
-
HybridStyleSheet(std::shared_ptr<HybridUnistylesRuntime> unistylesRuntime)
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
19
|
+
HybridStyleSheet(std::shared_ptr<HybridUnistylesRuntime> unistylesRuntime, std::shared_ptr<UIManager> uiManager)
|
20
|
+
: HybridObject(TAG), _unistylesRuntime{unistylesRuntime} {
|
21
|
+
this->_unistylesCommitHook = std::make_shared<core::UnistylesCommitHook>(uiManager, unistylesRuntime);
|
22
|
+
this->_unistylesRuntime->registerPlatformListener(
|
23
|
+
std::bind(&HybridStyleSheet::onPlatformDependenciesChange, this, std::placeholders::_1)
|
24
|
+
);
|
24
25
|
}
|
25
26
|
|
26
27
|
jsi::Value create(jsi::Runtime& rt,
|
@@ -52,7 +53,6 @@ private:
|
|
52
53
|
void setThemeFromColorScheme(jsi::Runtime& rt);
|
53
54
|
void loadExternalMethods(const jsi::Value& thisValue, jsi::Runtime& rt);
|
54
55
|
void onPlatformDependenciesChange(std::vector<UnistyleDependency> dependencies);
|
55
|
-
void registerCommitHook(jsi::Runtime& rt);
|
56
56
|
|
57
57
|
double __unid = -1;
|
58
58
|
std::shared_ptr<HybridUnistylesRuntime> _unistylesRuntime;
|
@@ -45,7 +45,7 @@ Insets HybridUnistylesRuntime::getInsets() {
|
|
45
45
|
|
46
46
|
Orientation HybridUnistylesRuntime::getOrientation() {
|
47
47
|
int orientation = this->_nativePlatform.getOrientation();
|
48
|
-
|
48
|
+
|
49
49
|
return static_cast<Orientation>(orientation);
|
50
50
|
};
|
51
51
|
|
@@ -58,7 +58,7 @@ double HybridUnistylesRuntime::getFontScale() {
|
|
58
58
|
};
|
59
59
|
|
60
60
|
void HybridUnistylesRuntime::setTheme(const std::string &themeName) {
|
61
|
-
helpers::assertThat(*_rt, !this->getHasAdaptiveThemes(), "You're trying to set theme to: '" + themeName + "', but adaptiveThemes are enabled.");
|
61
|
+
helpers::assertThat(*_rt, !this->getHasAdaptiveThemes(), "Unistyles: You're trying to set theme to: '" + themeName + "', but adaptiveThemes are enabled.");
|
62
62
|
|
63
63
|
auto& state = core::UnistylesRegistry::get().getState(*_rt);
|
64
64
|
|
@@ -68,14 +68,17 @@ void HybridUnistylesRuntime::setTheme(const std::string &themeName) {
|
|
68
68
|
|
69
69
|
void HybridUnistylesRuntime::setAdaptiveThemes(bool isEnabled) {
|
70
70
|
auto& registry = core::UnistylesRegistry::get();
|
71
|
-
|
71
|
+
|
72
72
|
std::vector<UnistyleDependency> changedDependencies{};
|
73
|
+
|
74
|
+
changedDependencies.reserve(5);
|
75
|
+
|
73
76
|
bool hadAdaptiveThemes = this->getHasAdaptiveThemes();
|
74
77
|
|
75
78
|
registry.setPrefersAdaptiveThemes(*_rt, isEnabled);
|
76
|
-
|
79
|
+
|
77
80
|
bool haveAdaptiveThemes = this->getHasAdaptiveThemes();
|
78
|
-
|
81
|
+
|
79
82
|
if (hadAdaptiveThemes != haveAdaptiveThemes) {
|
80
83
|
changedDependencies.push_back(UnistyleDependency::ADAPTIVETHEMES);
|
81
84
|
}
|
@@ -83,7 +86,7 @@ void HybridUnistylesRuntime::setAdaptiveThemes(bool isEnabled) {
|
|
83
86
|
// if user disabled it, or can't have adaptive themes, do nothing
|
84
87
|
if (!this->getHasAdaptiveThemes()) {
|
85
88
|
this->_onDependenciesChange(changedDependencies);
|
86
|
-
|
89
|
+
|
87
90
|
return;
|
88
91
|
}
|
89
92
|
|
@@ -99,24 +102,25 @@ void HybridUnistylesRuntime::setAdaptiveThemes(bool isEnabled) {
|
|
99
102
|
if (!currentThemeName.has_value() || nextTheme != currentThemeName.value()) {
|
100
103
|
changedDependencies.push_back(UnistyleDependency::THEME);
|
101
104
|
changedDependencies.push_back(UnistyleDependency::THEMENAME);
|
102
|
-
|
105
|
+
|
103
106
|
state.setTheme(nextTheme);
|
104
107
|
}
|
105
|
-
|
108
|
+
|
106
109
|
this->_onDependenciesChange(changedDependencies);
|
107
110
|
};
|
108
111
|
|
109
112
|
jsi::Value HybridUnistylesRuntime::updateTheme(jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
|
110
|
-
helpers::assertThat(rt,
|
111
|
-
helpers::assertThat(rt, args[
|
113
|
+
helpers::assertThat(rt, count == 2, "UnistylesRuntime.updateTheme expected to be called with 2 arguments.");
|
114
|
+
helpers::assertThat(rt, args[0].isString(), "UnistylesRuntime.updateTheme expected first argument to be a string.");
|
115
|
+
helpers::assertThat(rt, args[1].isObject(), "UnistylesRuntime.updateTheme expected first argument to be a function.");
|
112
116
|
|
113
117
|
auto& registry = core::UnistylesRegistry::get();
|
114
118
|
auto themeName = args[0].asString(rt).utf8(rt);
|
115
119
|
|
116
|
-
helpers::assertThat(rt, args[1].asObject(rt).isFunction(rt), "second argument
|
120
|
+
helpers::assertThat(rt, args[1].asObject(rt).isFunction(rt), "UnistylesRuntime.updateTheme expected second argument to be a function.");
|
117
121
|
|
118
122
|
registry.updateTheme(rt, themeName, args[1].asObject(rt).asFunction(rt));
|
119
|
-
|
123
|
+
|
120
124
|
this->_onDependenciesChange({UnistyleDependency::THEME});
|
121
125
|
|
122
126
|
return jsi::Value::undefined();
|
@@ -163,7 +167,7 @@ UnistylesCxxMiniRuntime HybridUnistylesRuntime::getMiniRuntime() {
|
|
163
167
|
nativeMiniRuntime.statusBar,
|
164
168
|
nativeMiniRuntime.navigationBar
|
165
169
|
};
|
166
|
-
|
170
|
+
|
167
171
|
return cxxMiniRuntime;
|
168
172
|
}
|
169
173
|
|
package/cxx/parser/Parser.cpp
CHANGED
@@ -11,7 +11,7 @@ void parser::Parser::buildUnistyles(jsi::Runtime& rt, std::shared_ptr<StyleSheet
|
|
11
11
|
jsi::Object unwrappedStyleSheet = this->unwrapStyleSheet(rt, styleSheet);
|
12
12
|
|
13
13
|
helpers::enumerateJSIObject(rt, unwrappedStyleSheet, [&](const std::string& styleKey, jsi::Value& propertyValue){
|
14
|
-
helpers::assertThat(rt, propertyValue.isObject(), "
|
14
|
+
helpers::assertThat(rt, propertyValue.isObject(), "Unistyles: Style with name '" + styleKey + "' is not a function or object.");
|
15
15
|
|
16
16
|
jsi::Object styleValue = propertyValue.asObject(rt);
|
17
17
|
|
@@ -128,11 +128,13 @@ void parser::Parser::rebuildUnistyle(jsi::Runtime& rt, std::shared_ptr<StyleShee
|
|
128
128
|
auto unistyleFn = std::dynamic_pointer_cast<UnistyleDynamicFunction>(unistyle);
|
129
129
|
auto maybeMetadata = unistyleFn->dynamicFunctionMetadata;
|
130
130
|
|
131
|
-
helpers::assertThat(rt, maybeMetadata.has_value(), "Your dynamic function '" + unistyleFn->styleKey + "' has no metadata and can't be processed.");
|
131
|
+
helpers::assertThat(rt, maybeMetadata.has_value(), "Unistyles: Your dynamic function '" + unistyleFn->styleKey + "' has no metadata and can't be processed.");
|
132
132
|
|
133
133
|
// convert arguments to jsi::Value
|
134
134
|
auto metadata = unistyleFn->dynamicFunctionMetadata.value();
|
135
135
|
std::vector<jsi::Value> args{};
|
136
|
+
|
137
|
+
args.reserve(3);
|
136
138
|
|
137
139
|
for (int i = 0; i < metadata.count; i++) {
|
138
140
|
folly::dynamic& arg = metadata.arguments.at(i);
|
@@ -177,6 +179,8 @@ shadow::ShadowLeafUpdates parser::Parser::dependencyMapToShadowLeafUpdates(Depen
|
|
177
179
|
// convert jsi::Value arguments to folly::dynamic
|
178
180
|
std::vector<folly::dynamic> parser::Parser::parseDynamicFunctionArguments(jsi::Runtime& rt, size_t count, const jsi::Value* arguments) {
|
179
181
|
std::vector<folly::dynamic> parsedArgument{};
|
182
|
+
|
183
|
+
parsedArgument.reserve(3);
|
180
184
|
|
181
185
|
for (size_t i = 0; i < count; i++) {
|
182
186
|
auto& arg = arguments[i];
|
@@ -312,10 +316,6 @@ jsi::Object parser::Parser::parseFirstLevel(jsi::Runtime& rt, Unistyle::Shared u
|
|
312
316
|
|
313
317
|
parsedStyle.setProperty(rt, jsi::PropNameID::forUtf8(rt, propertyName), this->parseSecondLevel(rt, unistyle, valueFromBreakpoint));
|
314
318
|
});
|
315
|
-
|
316
|
-
if (shouldParseVariants) {
|
317
|
-
unistyle->addDependency(UnistyleDependency::VARIANTS);
|
318
|
-
}
|
319
319
|
|
320
320
|
if (shouldParseVariants && !variants.empty()) {
|
321
321
|
auto propertyValueObject = style.getProperty(rt, "variants").asObject(rt);
|
@@ -367,9 +367,11 @@ jsi::Function parser::Parser::createDynamicFunctionProxy(jsi::Runtime& rt, Unist
|
|
367
367
|
|
368
368
|
// function convert babel generated dependencies to C++ dependencies
|
369
369
|
std::vector<UnistyleDependency> parser::Parser::parseDependencies(jsi::Runtime &rt, jsi::Object&& dependencies) {
|
370
|
-
helpers::assertThat(rt, dependencies.isArray(rt), "
|
370
|
+
helpers::assertThat(rt, dependencies.isArray(rt), "Unistyles: Babel transform is invalid - unexpected type for dependencies.");
|
371
371
|
|
372
|
-
std::vector<UnistyleDependency> parsedDependencies;
|
372
|
+
std::vector<UnistyleDependency> parsedDependencies{};
|
373
|
+
|
374
|
+
parsedDependencies.reserve(5);
|
373
375
|
|
374
376
|
helpers::iterateJSIArray(rt, dependencies.asArray(rt), [&](size_t i, jsi::Value& value){
|
375
377
|
auto dependency = static_cast<UnistyleDependency>(value.asNumber());
|
@@ -387,6 +389,8 @@ jsi::Value parser::Parser::parseTransforms(jsi::Runtime& rt, Unistyle::Shared un
|
|
387
389
|
}
|
388
390
|
|
389
391
|
std::vector<jsi::Value> parsedTransforms{};
|
392
|
+
|
393
|
+
parsedTransforms.reserve(2);
|
390
394
|
|
391
395
|
helpers::iterateJSIArray(rt, obj.asArray(rt), [&](size_t i, jsi::Value& value){
|
392
396
|
if (!value.isObject()) {
|
@@ -3,6 +3,8 @@
|
|
3
3
|
#import <React/RCTEventEmitter.h>
|
4
4
|
#import <ReactCommon/RCTTurboModuleWithJSIBindings.h>
|
5
5
|
#import "TurboUnistyles/TurboUnistyles.h"
|
6
|
+
#import <React/RCTSurfacePresenter.h>
|
7
|
+
#import <React/RCTScheduler.h>
|
6
8
|
|
7
9
|
@interface UnistylesModule: RCTEventEmitter<NativeTurboUnistylesSpec>
|
8
10
|
@end
|
@@ -10,10 +10,16 @@ using namespace margelo::nitro;
|
|
10
10
|
|
11
11
|
RCT_EXPORT_MODULE(Unistyles)
|
12
12
|
|
13
|
+
__weak RCTSurfacePresenter* _surfacePresenter;
|
14
|
+
|
13
15
|
+ (BOOL)requiresMainQueueSetup {
|
14
16
|
return YES;
|
15
17
|
}
|
16
18
|
|
19
|
+
- (void)setSurfacePresenter:(id<RCTSurfacePresenterStub>)surfacePresenter {
|
20
|
+
_surfacePresenter = surfacePresenter;
|
21
|
+
}
|
22
|
+
|
17
23
|
- (void)installJSIBindingsWithRuntime:(jsi::Runtime&)rt {
|
18
24
|
// function is called on: first init and every live reload
|
19
25
|
// check if this is live reload, if so let's replace UnistylesRuntime with new runtime
|
@@ -31,7 +37,8 @@ RCT_EXPORT_MODULE(Unistyles)
|
|
31
37
|
- (void)createHybrids:(jsi::Runtime&)rt {
|
32
38
|
auto nativePlatform = Unistyles::NativePlatform::create();
|
33
39
|
auto unistylesRuntime = std::make_shared<HybridUnistylesRuntime>(nativePlatform, rt);
|
34
|
-
auto
|
40
|
+
auto uiManager = [_surfacePresenter scheduler].uiManager;
|
41
|
+
auto styleSheet = std::make_shared<HybridStyleSheet>(unistylesRuntime, uiManager);
|
35
42
|
|
36
43
|
HybridObjectRegistry::registerHybridObjectConstructor("UnistylesRuntime", [unistylesRuntime]() -> std::shared_ptr<HybridObject>{
|
37
44
|
return unistylesRuntime;
|