1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
|
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <algorithm>
#include <yoga/Yoga.h>
#include "./Node.hh"
#include "./Layout.hh"
#include "./Size.hh"
#include "./Config.hh"
static YGSize globalMeasureFunc(
YGNodeRef nodeRef,
float width,
YGMeasureMode widthMode,
float height,
YGMeasureMode heightMode) {
Node const& node = *reinterpret_cast<Node const*>(YGNodeGetContext(nodeRef));
Size size = node.callMeasureFunc(width, widthMode, height, heightMode);
YGSize ygSize = {
static_cast<float>(size.width), static_cast<float>(size.height)};
return ygSize;
}
static void globalDirtiedFunc(YGNodeRef nodeRef) {
Node const& node = *reinterpret_cast<Node const*>(YGNodeGetContext(nodeRef));
node.callDirtiedFunc();
}
/* static */ Node* Node::createDefault(void) {
return new Node(nullptr);
}
/* static */ Node* Node::createWithConfig(Config* config) {
return new Node(config);
}
/* static */ void Node::destroy(Node* node) {
delete node;
}
/* static */ Node* Node::fromYGNode(YGNodeRef nodeRef) {
return reinterpret_cast<Node*>(YGNodeGetContext(nodeRef));
}
Node::Node(Config* config)
: m_node(
config != nullptr ? YGNodeNewWithConfig(config->m_config)
: YGNodeNew()),
m_measureFunc(nullptr),
m_dirtiedFunc(nullptr) {
YGNodeSetContext(m_node, reinterpret_cast<void*>(this));
}
Node::~Node(void) {
YGNodeFree(m_node);
}
void Node::reset(void) {
m_measureFunc.reset(nullptr);
m_dirtiedFunc.reset(nullptr);
YGNodeReset(m_node);
}
void Node::copyStyle(Node const& other) {
YGNodeCopyStyle(m_node, other.m_node);
}
void Node::setPositionType(int positionType) {
YGNodeStyleSetPositionType(m_node, static_cast<YGPositionType>(positionType));
}
void Node::setPosition(int edge, double position) {
YGNodeStyleSetPosition(m_node, static_cast<YGEdge>(edge), position);
}
void Node::setPositionPercent(int edge, double position) {
YGNodeStyleSetPositionPercent(m_node, static_cast<YGEdge>(edge), position);
}
void Node::setAlignContent(int alignContent) {
YGNodeStyleSetAlignContent(m_node, static_cast<YGAlign>(alignContent));
}
void Node::setAlignItems(int alignItems) {
YGNodeStyleSetAlignItems(m_node, static_cast<YGAlign>(alignItems));
}
void Node::setAlignSelf(int alignSelf) {
YGNodeStyleSetAlignSelf(m_node, static_cast<YGAlign>(alignSelf));
}
void Node::setFlexDirection(int flexDirection) {
YGNodeStyleSetFlexDirection(
m_node, static_cast<YGFlexDirection>(flexDirection));
}
void Node::setFlexWrap(int flexWrap) {
YGNodeStyleSetFlexWrap(m_node, static_cast<YGWrap>(flexWrap));
}
void Node::setJustifyContent(int justifyContent) {
YGNodeStyleSetJustifyContent(m_node, static_cast<YGJustify>(justifyContent));
}
void Node::setMargin(int edge, double margin) {
YGNodeStyleSetMargin(m_node, static_cast<YGEdge>(edge), margin);
}
void Node::setMarginPercent(int edge, double margin) {
YGNodeStyleSetMarginPercent(m_node, static_cast<YGEdge>(edge), margin);
}
void Node::setMarginAuto(int edge) {
YGNodeStyleSetMarginAuto(m_node, static_cast<YGEdge>(edge));
}
void Node::setOverflow(int overflow) {
YGNodeStyleSetOverflow(m_node, static_cast<YGOverflow>(overflow));
}
void Node::setDisplay(int display) {
YGNodeStyleSetDisplay(m_node, static_cast<YGDisplay>(display));
}
void Node::setFlex(double flex) {
YGNodeStyleSetFlex(m_node, flex);
}
void Node::setFlexBasis(double flexBasis) {
YGNodeStyleSetFlexBasis(m_node, flexBasis);
}
void Node::setFlexBasisPercent(double flexBasis) {
YGNodeStyleSetFlexBasisPercent(m_node, flexBasis);
}
void Node::setFlexBasisAuto() {
YGNodeStyleSetFlexBasisAuto(m_node);
}
void Node::setFlexGrow(double flexGrow) {
YGNodeStyleSetFlexGrow(m_node, flexGrow);
}
void Node::setFlexShrink(double flexShrink) {
YGNodeStyleSetFlexShrink(m_node, flexShrink);
}
void Node::setWidth(double width) {
YGNodeStyleSetWidth(m_node, width);
}
void Node::setWidthPercent(double width) {
YGNodeStyleSetWidthPercent(m_node, width);
}
void Node::setWidthAuto() {
YGNodeStyleSetWidthAuto(m_node);
}
void Node::setHeight(double height) {
YGNodeStyleSetHeight(m_node, height);
}
void Node::setHeightPercent(double height) {
YGNodeStyleSetHeightPercent(m_node, height);
}
void Node::setHeightAuto() {
YGNodeStyleSetHeightAuto(m_node);
}
void Node::setMinWidth(double minWidth) {
YGNodeStyleSetMinWidth(m_node, minWidth);
}
void Node::setMinWidthPercent(double minWidth) {
YGNodeStyleSetMinWidthPercent(m_node, minWidth);
}
void Node::setMinHeight(double minHeight) {
YGNodeStyleSetMinHeight(m_node, minHeight);
}
void Node::setMinHeightPercent(double minHeight) {
YGNodeStyleSetMinHeightPercent(m_node, minHeight);
}
void Node::setMaxWidth(double maxWidth) {
YGNodeStyleSetMaxWidth(m_node, maxWidth);
}
void Node::setMaxWidthPercent(double maxWidth) {
YGNodeStyleSetMaxWidthPercent(m_node, maxWidth);
}
void Node::setMaxHeight(double maxHeight) {
YGNodeStyleSetMaxHeight(m_node, maxHeight);
}
void Node::setMaxHeightPercent(double maxHeight) {
YGNodeStyleSetMaxHeightPercent(m_node, maxHeight);
}
void Node::setAspectRatio(double aspectRatio) {
YGNodeStyleSetAspectRatio(m_node, aspectRatio);
}
void Node::setBorder(int edge, double border) {
YGNodeStyleSetBorder(m_node, static_cast<YGEdge>(edge), border);
}
void Node::setPadding(int edge, double padding) {
YGNodeStyleSetPadding(m_node, static_cast<YGEdge>(edge), padding);
}
void Node::setPaddingPercent(int edge, double padding) {
YGNodeStyleSetPaddingPercent(m_node, static_cast<YGEdge>(edge), padding);
}
void Node::setIsReferenceBaseline(bool isReferenceBaseline) {
YGNodeSetIsReferenceBaseline(m_node, isReferenceBaseline);
}
void Node::setGap(int gutter, double gapLength) {
YGNodeStyleSetGap(m_node, static_cast<YGGutter>(gutter), gapLength);
}
int Node::getPositionType(void) const {
return YGNodeStyleGetPositionType(m_node);
}
Value Node::getPosition(int edge) const {
return Value::fromYGValue(
YGNodeStyleGetPosition(m_node, static_cast<YGEdge>(edge)));
}
int Node::getAlignContent(void) const {
return YGNodeStyleGetAlignContent(m_node);
}
int Node::getAlignItems(void) const {
return YGNodeStyleGetAlignItems(m_node);
}
int Node::getAlignSelf(void) const {
return YGNodeStyleGetAlignSelf(m_node);
}
int Node::getFlexDirection(void) const {
return YGNodeStyleGetFlexDirection(m_node);
}
int Node::getFlexWrap(void) const {
return YGNodeStyleGetFlexWrap(m_node);
}
int Node::getJustifyContent(void) const {
return YGNodeStyleGetJustifyContent(m_node);
}
Value Node::getMargin(int edge) const {
return Value::fromYGValue(
YGNodeStyleGetMargin(m_node, static_cast<YGEdge>(edge)));
}
int Node::getOverflow(void) const {
return YGNodeStyleGetOverflow(m_node);
}
int Node::getDisplay(void) const {
return YGNodeStyleGetDisplay(m_node);
}
Value Node::getFlexBasis(void) const {
return Value::fromYGValue(YGNodeStyleGetFlexBasis(m_node));
}
double Node::getFlexGrow(void) const {
return YGNodeStyleGetFlexGrow(m_node);
}
double Node::getFlexShrink(void) const {
return YGNodeStyleGetFlexShrink(m_node);
}
Value Node::getWidth(void) const {
return Value::fromYGValue(YGNodeStyleGetWidth(m_node));
}
Value Node::getHeight(void) const {
return Value::fromYGValue(YGNodeStyleGetHeight(m_node));
}
Value Node::getMinWidth(void) const {
return Value::fromYGValue(YGNodeStyleGetMinWidth(m_node));
}
Value Node::getMinHeight(void) const {
return Value::fromYGValue(YGNodeStyleGetMinHeight(m_node));
}
Value Node::getMaxWidth(void) const {
return Value::fromYGValue(YGNodeStyleGetMaxWidth(m_node));
}
Value Node::getMaxHeight(void) const {
return Value::fromYGValue(YGNodeStyleGetMaxHeight(m_node));
}
double Node::getAspectRatio(void) const {
return YGNodeStyleGetAspectRatio(m_node);
}
double Node::getBorder(int edge) const {
return YGNodeStyleGetBorder(m_node, static_cast<YGEdge>(edge));
}
Value Node::getPadding(int edge) const {
return Value::fromYGValue(
YGNodeStyleGetPadding(m_node, static_cast<YGEdge>(edge)));
}
float Node::getGap(int gutter) {
return YGNodeStyleGetGap(m_node, static_cast<YGGutter>(gutter));
}
bool Node::isReferenceBaseline() {
return YGNodeIsReferenceBaseline(m_node);
}
void Node::insertChild(Node* child, unsigned index) {
YGNodeInsertChild(m_node, child->m_node, index);
}
void Node::removeChild(Node* child) {
YGNodeRemoveChild(m_node, child->m_node);
}
unsigned Node::getChildCount(void) const {
return YGNodeGetChildCount(m_node);
}
Node* Node::getParent(void) {
auto nodePtr = YGNodeGetParent(m_node);
if (nodePtr == nullptr)
return nullptr;
return Node::fromYGNode(nodePtr);
}
Node* Node::getChild(unsigned index) {
auto nodePtr = YGNodeGetChild(m_node, index);
if (nodePtr == nullptr)
return nullptr;
return Node::fromYGNode(nodePtr);
}
void Node::setMeasureFunc(MeasureCallback* measureFunc) {
m_measureFunc.reset(measureFunc);
YGNodeSetMeasureFunc(m_node, &globalMeasureFunc);
}
void Node::unsetMeasureFunc(void) {
m_measureFunc.reset(nullptr);
YGNodeSetMeasureFunc(m_node, nullptr);
}
Size Node::callMeasureFunc(
double width,
int widthMode,
double height,
int heightMode) const {
return m_measureFunc->measure(width, widthMode, height, heightMode);
}
void Node::setDirtiedFunc(DirtiedCallback* dirtiedFunc) {
m_dirtiedFunc.reset(dirtiedFunc);
YGNodeSetDirtiedFunc(m_node, &globalDirtiedFunc);
}
void Node::unsetDirtiedFunc(void) {
m_dirtiedFunc.reset(nullptr);
YGNodeSetDirtiedFunc(m_node, nullptr);
}
void Node::callDirtiedFunc(void) const {
m_dirtiedFunc->dirtied();
}
void Node::markDirty(void) {
YGNodeMarkDirty(m_node);
}
bool Node::isDirty(void) const {
return YGNodeIsDirty(m_node);
}
void Node::calculateLayout(double width, double height, int direction) {
YGNodeCalculateLayout(
m_node, width, height, static_cast<YGDirection>(direction));
}
double Node::getComputedLeft(void) const {
return YGNodeLayoutGetLeft(m_node);
}
double Node::getComputedRight(void) const {
return YGNodeLayoutGetRight(m_node);
}
double Node::getComputedTop(void) const {
return YGNodeLayoutGetTop(m_node);
}
double Node::getComputedBottom(void) const {
return YGNodeLayoutGetBottom(m_node);
}
double Node::getComputedWidth(void) const {
return YGNodeLayoutGetWidth(m_node);
}
double Node::getComputedHeight(void) const {
return YGNodeLayoutGetHeight(m_node);
}
Layout Node::getComputedLayout(void) const {
Layout layout;
layout.left = YGNodeLayoutGetLeft(m_node);
layout.right = YGNodeLayoutGetRight(m_node);
layout.top = YGNodeLayoutGetTop(m_node);
layout.bottom = YGNodeLayoutGetBottom(m_node);
layout.width = YGNodeLayoutGetWidth(m_node);
layout.height = YGNodeLayoutGetHeight(m_node);
return layout;
}
double Node::getComputedMargin(int edge) const {
return YGNodeLayoutGetMargin(m_node, static_cast<YGEdge>(edge));
}
double Node::getComputedBorder(int edge) const {
return YGNodeLayoutGetBorder(m_node, static_cast<YGEdge>(edge));
}
double Node::getComputedPadding(int edge) const {
return YGNodeLayoutGetPadding(m_node, static_cast<YGEdge>(edge));
}
|