From 8a9758a2cc8dfa7fb978696da699525229247c6b Mon Sep 17 00:00:00 2001 From: Willson Haw Date: Thu, 21 Mar 2024 17:36:36 -0700 Subject: [PATCH] Add JavaScript bindings for YGHasNewLayout (#1631) Summary: Adds JavaScript bindings for YGHasNewLayout which introduces two new node methods: `hasNewLayout()` and `markLayoutSeen()`. Closes https://github.com/facebook/yoga/issues/681 Pull Request resolved: https://github.com/facebook/yoga/pull/1631 Reviewed By: joevilches Differential Revision: D55213296 Pulled By: NickGerleman fbshipit-source-id: 161288c3f54c2b82a6b2b842387916fe8713c2c9 --- javascript/src/Node.cpp | 8 +++ javascript/src/Node.h | 2 + javascript/src/embind.cpp | 3 + javascript/src/wrapAssembly.ts | 2 + javascript/tests/YGHasNewLayout.test.ts | 81 +++++++++++++++++++++++++ 5 files changed, 96 insertions(+) create mode 100644 javascript/tests/YGHasNewLayout.test.ts diff --git a/javascript/src/Node.cpp b/javascript/src/Node.cpp index 5a5a185968..b1645d09cf 100644 --- a/javascript/src/Node.cpp +++ b/javascript/src/Node.cpp @@ -413,6 +413,14 @@ bool Node::isDirty(void) const { return YGNodeIsDirty(m_node); } +void Node::markLayoutSeen() { + YGNodeSetHasNewLayout(m_node, false); +} + +bool Node::hasNewLayout(void) const { + return YGNodeGetHasNewLayout(m_node); +} + void Node::calculateLayout(double width, double height, int direction) { YGNodeCalculateLayout( m_node, width, height, static_cast(direction)); diff --git a/javascript/src/Node.h b/javascript/src/Node.h index 373af92f33..2f8e7d340b 100644 --- a/javascript/src/Node.h +++ b/javascript/src/Node.h @@ -195,6 +195,8 @@ class Node { public: // Dirtiness accessors void markDirty(void); bool isDirty(void) const; + void markLayoutSeen(); + bool hasNewLayout(void) const; public: // Layout mutators void calculateLayout(double width, double height, int direction); diff --git a/javascript/src/embind.cpp b/javascript/src/embind.cpp index 4809365225..083a1ffed9 100644 --- a/javascript/src/embind.cpp +++ b/javascript/src/embind.cpp @@ -175,6 +175,9 @@ EMSCRIPTEN_BINDINGS(YOGA_LAYOUT) { .function("markDirty", &Node::markDirty) .function("isDirty", &Node::isDirty) + .function("markLayoutSeen", &Node::markLayoutSeen) + .function("hasNewLayout", &Node::hasNewLayout) + .function("calculateLayout", &Node::calculateLayout) .function("getComputedLeft", &Node::getComputedLeft) diff --git a/javascript/src/wrapAssembly.ts b/javascript/src/wrapAssembly.ts index 691daa9345..34fa304ce2 100644 --- a/javascript/src/wrapAssembly.ts +++ b/javascript/src/wrapAssembly.ts @@ -119,6 +119,8 @@ export type Node = { isDirty(): boolean; isReferenceBaseline(): boolean; markDirty(): void; + hasNewLayout(): boolean; + markLayoutSeen(): void; removeChild(child: Node): void; reset(): void; setAlignContent(alignContent: Align): void; diff --git a/javascript/tests/YGHasNewLayout.test.ts b/javascript/tests/YGHasNewLayout.test.ts new file mode 100644 index 0000000000..643823c37d --- /dev/null +++ b/javascript/tests/YGHasNewLayout.test.ts @@ -0,0 +1,81 @@ +/** + * 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. + */ + +import Yoga from 'yoga-layout'; + +test('new_layout_can_be_marked_seen', () => { + const root = Yoga.Node.create(); + root.markLayoutSeen(); + expect(root.hasNewLayout()).toBe(false); +}); + +test('new_layout_calculating_layout_marks_layout_as_unseen', () => { + const root = Yoga.Node.create(); + root.markLayoutSeen(); + root.calculateLayout(undefined, undefined); + expect(root.hasNewLayout()).toBe(true); +}); + +test('new_layout_calculated_layout_can_be_marked_seen', () => { + const root = Yoga.Node.create(); + root.calculateLayout(undefined, undefined); + root.markLayoutSeen(); + expect(root.hasNewLayout()).toBe(false); +}); + +test('new_layout_recalculating_layout_does_mark_as_unseen', () => { + const root = Yoga.Node.create(); + root.calculateLayout(undefined, undefined); + root.markLayoutSeen(); + root.calculateLayout(undefined, undefined); + expect(root.hasNewLayout()).toBe(true); +}); + +test('new_layout_reset_also_resets_layout_seen', () => { + const root = Yoga.Node.create(); + root.markLayoutSeen(); + root.reset(); + expect(root.hasNewLayout()).toBe(true); +}); + +test('new_layout_children_sets_new_layout', () => { + const root = Yoga.Node.create(); + root.setAlignItems(Yoga.ALIGN_FLEX_START); + root.setWidth(100); + root.setHeight(100); + + const root_child0 = Yoga.Node.create(); + root_child0.setAlignItems(Yoga.ALIGN_FLEX_START); + root_child0.setWidth(50); + root_child0.setHeight(20); + root.insertChild(root_child0, 0); + + const root_child1 = Yoga.Node.create(); + root_child1.setAlignItems(Yoga.ALIGN_FLEX_START); + root_child1.setWidth(50); + root_child1.setHeight(20); + root.insertChild(root_child1, 0); + + expect(root.hasNewLayout()).toEqual(true); + expect(root_child0.hasNewLayout()).toEqual(true); + expect(root_child1.hasNewLayout()).toEqual(true); + + root.markLayoutSeen(); + root_child0.markLayoutSeen(); + root_child1.markLayoutSeen(); + + expect(root.hasNewLayout()).toEqual(false); + expect(root_child0.hasNewLayout()).toEqual(false); + expect(root_child1.hasNewLayout()).toEqual(false); + + root_child1.setHeight(30); + root.calculateLayout(undefined, undefined); + + expect(root.hasNewLayout()).toEqual(true); + expect(root_child0.hasNewLayout()).toEqual(true); + expect(root_child1.hasNewLayout()).toEqual(true); +});