|
10 | 10 | #include <glog/logging.h> |
11 | 11 | #include <react/debug/react_native_expect.h> |
12 | 12 | #include <react/renderer/components/view/primitives.h> |
| 13 | +#include <react/renderer/components/view/YogaStylableProps.h> |
13 | 14 | #include <react/renderer/core/LayoutMetrics.h> |
14 | 15 | #include <react/renderer/core/PropsParserContext.h> |
15 | 16 | #include <react/renderer/core/RawProps.h> |
@@ -164,22 +165,106 @@ inline LayoutMetrics layoutMetricsFromYogaNode(yoga::Node &yogaNode) |
164 | 165 | layoutMetrics.layoutDirection = YGNodeLayoutGetDirection(&yogaNode) == YGDirectionRTL ? LayoutDirection::RightToLeft |
165 | 166 | : LayoutDirection::LeftToRight; |
166 | 167 |
|
167 | | - layoutMetrics.marginInsets = EdgeInsets{ |
168 | | - floatFromYogaFloat(YGNodeLayoutGetMargin(&yogaNode, YGEdgeLeft)), |
169 | | - floatFromYogaFloat(YGNodeLayoutGetMargin(&yogaNode, YGEdgeTop)), |
170 | | - floatFromYogaFloat(YGNodeLayoutGetMargin(&yogaNode, YGEdgeRight)), |
171 | | - floatFromYogaFloat(YGNodeLayoutGetMargin(&yogaNode, YGEdgeBottom)) |
172 | | - }; |
173 | | - layoutMetrics.paddingInsets = EdgeInsets{ |
174 | | - floatFromYogaFloat(YGNodeLayoutGetPadding(&yogaNode, YGEdgeLeft)), |
175 | | - floatFromYogaFloat(YGNodeLayoutGetPadding(&yogaNode, YGEdgeTop)), |
176 | | - floatFromYogaFloat(YGNodeLayoutGetPadding(&yogaNode, YGEdgeRight)), |
177 | | - floatFromYogaFloat(YGNodeLayoutGetPadding(&yogaNode, YGEdgeBottom)) |
178 | | - }; |
179 | | - |
180 | 168 | return layoutMetrics; |
181 | 169 | } |
182 | 170 |
|
| 171 | +template <auto LayoutMember, typename... Args> |
| 172 | +float getLayoutEdgeValue(const YogaStylableProps& props, Args&&... args) { |
| 173 | + std::optional<float> result; |
| 174 | + auto tryResolve = [&](const auto &arg) -> bool { |
| 175 | + if (result.has_value()) { |
| 176 | + return false; |
| 177 | + } |
| 178 | + |
| 179 | + using T = std::decay_t<decltype(arg)>; |
| 180 | + if constexpr (std::is_same_v<T, yoga::Style::Length YogaStylableProps::*>) { |
| 181 | + if (const auto& length = props.*arg; length.isDefined()) { |
| 182 | + result = length.value().unwrap(); |
| 183 | + } |
| 184 | + } else if constexpr (std::is_same_v<T, facebook::yoga::Edge>) { |
| 185 | + if (auto edgeValue = (props.yogaStyle.*LayoutMember)(arg); edgeValue.isDefined()) { |
| 186 | + result = edgeValue.value().unwrap(); |
| 187 | + } |
| 188 | + } |
| 189 | + return result.has_value(); |
| 190 | + }; |
| 191 | + |
| 192 | + (tryResolve(std::forward<decltype(args)>(args)) || ...); |
| 193 | + return result.value_or(0.0f); |
| 194 | +} |
| 195 | + |
| 196 | +inline EdgeInsets marginInsetsFromYogaStylableProps(const YogaStylableProps &props) |
| 197 | +{ |
| 198 | + return { |
| 199 | + .left = getLayoutEdgeValue<&yoga::Style::margin>( |
| 200 | + props, |
| 201 | + &YogaStylableProps::marginInlineStart, |
| 202 | + facebook::yoga::Edge::Start, |
| 203 | + facebook::yoga::Edge::Left, |
| 204 | + &YogaStylableProps::marginInline, |
| 205 | + facebook::yoga::Edge::Horizontal, |
| 206 | + facebook::yoga::Edge::All), |
| 207 | + .top = getLayoutEdgeValue<&yoga::Style::margin>( |
| 208 | + props, |
| 209 | + facebook::yoga::Edge::Top, |
| 210 | + &YogaStylableProps::marginBlockStart, |
| 211 | + &YogaStylableProps::marginBlock, |
| 212 | + facebook::yoga::Edge::Vertical, |
| 213 | + facebook::yoga::Edge::All), |
| 214 | + .right = getLayoutEdgeValue<&yoga::Style::margin>( |
| 215 | + props, |
| 216 | + &YogaStylableProps::marginInlineEnd, |
| 217 | + facebook::yoga::Edge::End, |
| 218 | + facebook::yoga::Edge::Right, |
| 219 | + &YogaStylableProps::marginInline, |
| 220 | + facebook::yoga::Edge::Horizontal, |
| 221 | + facebook::yoga::Edge::All), |
| 222 | + .bottom = getLayoutEdgeValue<&yoga::Style::margin>( |
| 223 | + props, |
| 224 | + facebook::yoga::Edge::Bottom, |
| 225 | + &YogaStylableProps::marginBlockEnd, |
| 226 | + &YogaStylableProps::marginBlock, |
| 227 | + facebook::yoga::Edge::Vertical, |
| 228 | + facebook::yoga::Edge::All) |
| 229 | + }; |
| 230 | +} |
| 231 | + |
| 232 | +inline EdgeInsets paddingInsetsFromYogaStylableProps(const YogaStylableProps &props) |
| 233 | +{ |
| 234 | + return { |
| 235 | + .left = getLayoutEdgeValue<&yoga::Style::padding>( |
| 236 | + props, |
| 237 | + &YogaStylableProps::paddingInlineStart, |
| 238 | + facebook::yoga::Edge::Start, |
| 239 | + facebook::yoga::Edge::Left, |
| 240 | + &YogaStylableProps::paddingInline, |
| 241 | + facebook::yoga::Edge::Horizontal, |
| 242 | + facebook::yoga::Edge::All), |
| 243 | + .top = getLayoutEdgeValue<&yoga::Style::padding>( |
| 244 | + props, |
| 245 | + facebook::yoga::Edge::Top, |
| 246 | + &YogaStylableProps::paddingBlockStart, |
| 247 | + &YogaStylableProps::paddingBlock, |
| 248 | + facebook::yoga::Edge::Vertical, |
| 249 | + facebook::yoga::Edge::All), |
| 250 | + .right = getLayoutEdgeValue<&yoga::Style::padding>( |
| 251 | + props, |
| 252 | + &YogaStylableProps::paddingInlineEnd, |
| 253 | + facebook::yoga::Edge::End, |
| 254 | + facebook::yoga::Edge::Right, |
| 255 | + &YogaStylableProps::paddingInline, |
| 256 | + facebook::yoga::Edge::Horizontal, |
| 257 | + facebook::yoga::Edge::All), |
| 258 | + .bottom = getLayoutEdgeValue<&yoga::Style::padding>( |
| 259 | + props, |
| 260 | + facebook::yoga::Edge::Bottom, |
| 261 | + &YogaStylableProps::paddingBlockEnd, |
| 262 | + &YogaStylableProps::paddingBlock, |
| 263 | + facebook::yoga::Edge::Vertical, |
| 264 | + facebook::yoga::Edge::All) |
| 265 | + }; |
| 266 | +} |
| 267 | + |
183 | 268 | inline YGDirection yogaDirectionFromLayoutDirection(LayoutDirection direction) |
184 | 269 | { |
185 | 270 | switch (direction) { |
|
0 commit comments