|
11 | 11 | #include <cmath> |
12 | 12 |
|
13 | 13 | #include "Effect.hpp" |
14 | | -#include "StyleRule.hpp" |
| 14 | +#include "HybridStyleRule.hpp" |
15 | 15 | #include "Environment.hpp" |
16 | 16 | #include "Helpers.hpp" |
| 17 | +#include <NitroModules/AnyMap.hpp> |
17 | 18 |
|
18 | 19 | namespace margelo::nitro::cssnitro { |
19 | 20 |
|
20 | | - // Trait to detect std::vector<T, Alloc> |
21 | | - template<typename T> |
22 | | - struct is_std_vector : std::false_type { |
23 | | - }; |
24 | | - template<typename T, typename Alloc> |
25 | | - struct is_std_vector<std::vector<T, Alloc>> : std::true_type { |
26 | | - }; |
| 21 | + using AnyMap = ::margelo::nitro::AnyMap; |
| 22 | + using AnyArray = ::margelo::nitro::AnyArray; |
| 23 | + using AnyValue = ::margelo::nitro::AnyValue; |
27 | 24 |
|
28 | 25 | class Rules { |
29 | 26 | public: |
30 | | - static bool testRule(const StyleRule &rule, reactnativecss::Effect::GetProxy &get) { |
31 | | - if (rule.m && !rule.m->empty() && !testMediaQuery(*rule.m, get)) { |
32 | | - return false; |
| 27 | + static bool testRule(const HybridStyleRule &rule, reactnativecss::Effect::GetProxy &get) { |
| 28 | + // If m is not defined, return true |
| 29 | + if (!rule.m.has_value() || !rule.m.value()) { |
| 30 | + return true; |
33 | 31 | } |
34 | | - return true; |
35 | | - } |
36 | 32 |
|
37 | | - template<class Cond> |
38 | | - static bool |
39 | | - testMediaQuery(const std::vector<Cond> &media, reactnativecss::Effect::GetProxy &get) { |
40 | | - for (const auto &cond: media) { |
41 | | - if (!testMediaConditionDispatch(cond, get)) { |
42 | | - return false; |
43 | | - } |
| 33 | + auto &mediaMap = *rule.m.value(); |
| 34 | + |
| 35 | + // Get all keys to check if empty |
| 36 | + auto keys = mediaMap.getAllKeys(); |
| 37 | + if (keys.empty()) { |
| 38 | + return true; |
44 | 39 | } |
45 | | - return true; |
46 | | - } |
47 | 40 |
|
48 | | - template<class L, class R> |
49 | | - static bool testMediaComparison(const std::string &op, const L &left, const R &right, |
50 | | - reactnativecss::Effect::GetProxy &get) { |
51 | | - using helpers::lower; |
52 | | - using helpers::toNumber; |
53 | | - using helpers::toString; |
| 41 | + // Check for $$op to determine logic mode |
| 42 | + std::string logicOp = "and"; // default is "and" |
| 43 | + bool negate = false; |
54 | 44 |
|
55 | | - // Accessors for environment |
56 | | - auto getWidth = [&]() -> double { return get(reactnativecss::env::windowWidth()); }; |
57 | | - auto getHeight = [&]() -> double { return get(reactnativecss::env::windowHeight()); }; |
| 45 | + if (mediaMap.contains("$$op")) { |
| 46 | + if (mediaMap.isString("$$op")) { |
| 47 | + logicOp = mediaMap.getString("$$op"); |
| 48 | + if (logicOp == "not") { |
| 49 | + negate = true; |
| 50 | + logicOp = "and"; // "not" just negates the result, logic is still "and" |
| 51 | + } |
| 52 | + } |
| 53 | + } |
58 | 54 |
|
59 | | - // Special cases when left is a string keyword |
60 | | - if (auto leftStr = toString(left)) { |
61 | | - const std::string key = lower(*leftStr); |
| 55 | + // Track test results |
| 56 | + std::vector<bool> results; |
62 | 57 |
|
63 | | - if (key == "min-width") { |
64 | | - auto v = toNumber(right); |
65 | | - return v.has_value() && getWidth() >= *v; |
66 | | - } |
67 | | - if (key == "max-width") { |
68 | | - auto v = toNumber(right); |
69 | | - return v.has_value() && getWidth() <= *v; |
| 58 | + // Loop over all keys |
| 59 | + for (const auto &key: keys) { |
| 60 | + // Skip the $$op key |
| 61 | + if (key == "$$op") { |
| 62 | + continue; |
70 | 63 | } |
71 | | - if (key == "min-height") { |
72 | | - auto v = toNumber(right); |
73 | | - return v.has_value() && getHeight() >= *v; |
| 64 | + |
| 65 | + // Value should be an array with [operator, expectedValue] |
| 66 | + if (!mediaMap.isArray(key)) { |
| 67 | + continue; |
74 | 68 | } |
75 | | - if (key == "max-height") { |
76 | | - auto v = toNumber(right); |
77 | | - return v.has_value() && getHeight() <= *v; |
| 69 | + |
| 70 | + AnyArray valueArray = mediaMap.getArray(key); |
| 71 | + if (valueArray.size() < 2) { |
| 72 | + continue; |
78 | 73 | } |
79 | | - if (key == "orientation") { |
80 | | - auto vStr = toString(right); |
81 | | - const double w = getWidth(); |
82 | | - const double h = getHeight(); |
83 | | - return vStr.has_value() && lower(*vStr) == "landscape" ? (h < w) : (h >= w); |
| 74 | + |
| 75 | + // Extract operator and expected value |
| 76 | + std::string op; |
| 77 | + if (std::holds_alternative<std::string>(valueArray[0])) { |
| 78 | + op = std::get<std::string>(valueArray[0]); |
84 | 79 | } |
| 80 | + |
| 81 | + bool testResult = testMediaQuery(key, op, valueArray[1], get); |
| 82 | + results.push_back(testResult); |
85 | 83 | } |
86 | 84 |
|
87 | | - // For general comparisons, right must be numeric |
88 | | - auto rightValOpt = toNumber(right); |
89 | | - if (!rightValOpt.has_value()) { |
90 | | - return false; |
| 85 | + // If no tests were run, return true |
| 86 | + if (results.empty()) { |
| 87 | + return true; |
91 | 88 | } |
92 | 89 |
|
93 | | - // Left can be numeric or a string referring to width/height |
94 | | - std::optional<double> leftValOpt = toNumber(left); |
95 | | - if (!leftValOpt.has_value()) { |
96 | | - if (auto leftStr = toString(left)) { |
97 | | - const std::string key = lower(*leftStr); |
98 | | - if (key == "width") { |
99 | | - leftValOpt = getWidth(); |
100 | | - } else if (key == "height") { |
101 | | - leftValOpt = getHeight(); |
102 | | - } else { |
103 | | - return false; |
| 90 | + // Apply logic |
| 91 | + bool finalResult; |
| 92 | + if (logicOp == "or") { |
| 93 | + // "or" - at least one must pass |
| 94 | + finalResult = false; |
| 95 | + for (bool result: results) { |
| 96 | + if (result) { |
| 97 | + finalResult = true; |
| 98 | + break; |
| 99 | + } |
| 100 | + } |
| 101 | + } else { |
| 102 | + // "and" - all must pass (default) |
| 103 | + finalResult = true; |
| 104 | + for (bool result: results) { |
| 105 | + if (!result) { |
| 106 | + finalResult = false; |
| 107 | + break; |
104 | 108 | } |
105 | | - } else { |
106 | | - return false; |
107 | 109 | } |
108 | 110 | } |
109 | 111 |
|
110 | | - const double a = *leftValOpt; |
111 | | - const double b = *rightValOpt; |
| 112 | + // Apply negation if needed |
| 113 | + if (negate) { |
| 114 | + finalResult = !finalResult; |
| 115 | + } |
112 | 116 |
|
113 | | - if (op == "=") return nearlyEqual(a, b); |
114 | | - if (op == ">") return a > b; |
115 | | - if (op == ">=") return a > b || nearlyEqual(a, b); |
116 | | - if (op == "<") return a < b; |
117 | | - if (op == "<=") return a < b || nearlyEqual(a, b); |
118 | | - return false; |
| 117 | + return finalResult; |
119 | 118 | } |
120 | 119 |
|
121 | | - template<class Elem> |
122 | | - static bool testMediaCondition(const std::vector<Elem> &condition, |
123 | | - reactnativecss::Effect::GetProxy &get) { |
124 | | - if (condition.empty()) return true; |
125 | | - const std::string &op = condition[0]; |
126 | | - if (op == "[]" || op == "!!") return false; |
127 | | - if (op == "=" || op == ">" || op == ">=" || op == "<" || op == "<=") { |
128 | | - if (condition.size() >= 3) { |
129 | | - const auto &left = condition[1]; |
130 | | - const auto &right = condition[2]; |
131 | | - return testMediaComparison(op, left, right, get); |
| 120 | + private: |
| 121 | + static bool |
| 122 | + testMediaQuery(const std::string &key, const std::string &op, const AnyValue &value, |
| 123 | + reactnativecss::Effect::GetProxy &get) { |
| 124 | + if (op == "=") { |
| 125 | + if (key == "min-width") { |
| 126 | + if (std::holds_alternative<double>(value)) { |
| 127 | + double vw = get(reactnativecss::env::windowWidth()); |
| 128 | + return vw >= std::get<double>(value); |
| 129 | + } |
| 130 | + return false; |
| 131 | + } |
| 132 | + if (key == "max-width") { |
| 133 | + if (std::holds_alternative<double>(value)) { |
| 134 | + double vw = get(reactnativecss::env::windowWidth()); |
| 135 | + return vw <= std::get<double>(value); |
| 136 | + } |
| 137 | + return false; |
| 138 | + } |
| 139 | + if (key == "min-height") { |
| 140 | + if (std::holds_alternative<double>(value)) { |
| 141 | + double vh = get(reactnativecss::env::windowHeight()); |
| 142 | + return vh >= std::get<double>(value); |
| 143 | + } |
| 144 | + return false; |
| 145 | + } |
| 146 | + if (key == "max-height") { |
| 147 | + if (std::holds_alternative<double>(value)) { |
| 148 | + double vh = get(reactnativecss::env::windowHeight()); |
| 149 | + return vh <= std::get<double>(value); |
| 150 | + } |
| 151 | + return false; |
| 152 | + } |
| 153 | + if (key == "orientation") { |
| 154 | + if (std::holds_alternative<std::string>(value)) { |
| 155 | + std::string orientation = std::get<std::string>(value); |
| 156 | + double vw = get(reactnativecss::env::windowWidth()); |
| 157 | + double vh = get(reactnativecss::env::windowHeight()); |
| 158 | + if (orientation == "landscape") { |
| 159 | + return vh < vw; |
| 160 | + } else { |
| 161 | + return vh >= vw; |
| 162 | + } |
| 163 | + } |
| 164 | + return false; |
132 | 165 | } |
133 | | - return false; |
134 | 166 | } |
135 | | - return false; |
136 | | - } |
137 | 167 |
|
138 | | - private: |
139 | | - static bool nearlyEqual(double a, double b, double eps = 1e-9) { |
140 | | - return std::abs(a - b) <= eps * std::max(1.0, std::max(std::abs(a), std::abs(b))); |
141 | | - } |
| 168 | + // For other operators, value must be a number |
| 169 | + if (!std::holds_alternative<double>(value)) { |
| 170 | + return false; |
| 171 | + } |
142 | 172 |
|
143 | | - template<class Cond> |
144 | | - static bool |
145 | | - testMediaConditionDispatch(const Cond &cond, reactnativecss::Effect::GetProxy &get) { |
146 | | - if constexpr (is_std_vector<std::decay_t<Cond>>::value) { |
147 | | - return testMediaCondition(cond, get); |
| 173 | + double right = std::get<double>(value); |
| 174 | + double left = 0.0; |
| 175 | + |
| 176 | + // Determine left value based on key and fetch only what's needed |
| 177 | + if (key == "width") { |
| 178 | + left = get(reactnativecss::env::windowWidth()); |
| 179 | + } else if (key == "height") { |
| 180 | + left = get(reactnativecss::env::windowHeight()); |
| 181 | + } else if (key == "resolution") { |
| 182 | + // TODO: Need to get PixelRatio - for now return 1.0 |
| 183 | + left = 1.0; // PixelRatio.get() |
148 | 184 | } else { |
149 | | - return true; |
| 185 | + return false; |
150 | 186 | } |
151 | | - } |
152 | 187 |
|
| 188 | + // Apply operator |
| 189 | + if (op == "=") { |
| 190 | + return left == right; |
| 191 | + } else if (op == ">") { |
| 192 | + return left > right; |
| 193 | + } else if (op == ">=") { |
| 194 | + return left >= right; |
| 195 | + } else if (op == "<") { |
| 196 | + return left < right; |
| 197 | + } else if (op == "<=") { |
| 198 | + return left <= right; |
| 199 | + } |
| 200 | + |
| 201 | + return false; |
| 202 | + } |
153 | 203 | }; |
154 | 204 |
|
155 | 205 | } // namespace margelo::nitro::cssnitro |
0 commit comments