Skip to content

Commit 8a9758a

Browse files
WillsonHawNickGerleman
authored andcommitted
Add JavaScript bindings for YGHasNewLayout (#1631)
Summary: Adds JavaScript bindings for YGHasNewLayout which introduces two new node methods: `hasNewLayout()` and `markLayoutSeen()`. Closes #681 Pull Request resolved: #1631 Reviewed By: joevilches Differential Revision: D55213296 Pulled By: NickGerleman fbshipit-source-id: 161288c3f54c2b82a6b2b842387916fe8713c2c9
1 parent 334eebc commit 8a9758a

5 files changed

Lines changed: 96 additions & 0 deletions

File tree

javascript/src/Node.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,14 @@ bool Node::isDirty(void) const {
413413
return YGNodeIsDirty(m_node);
414414
}
415415

416+
void Node::markLayoutSeen() {
417+
YGNodeSetHasNewLayout(m_node, false);
418+
}
419+
420+
bool Node::hasNewLayout(void) const {
421+
return YGNodeGetHasNewLayout(m_node);
422+
}
423+
416424
void Node::calculateLayout(double width, double height, int direction) {
417425
YGNodeCalculateLayout(
418426
m_node, width, height, static_cast<YGDirection>(direction));

javascript/src/Node.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ class Node {
195195
public: // Dirtiness accessors
196196
void markDirty(void);
197197
bool isDirty(void) const;
198+
void markLayoutSeen();
199+
bool hasNewLayout(void) const;
198200

199201
public: // Layout mutators
200202
void calculateLayout(double width, double height, int direction);

javascript/src/embind.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ EMSCRIPTEN_BINDINGS(YOGA_LAYOUT) {
175175
.function("markDirty", &Node::markDirty)
176176
.function("isDirty", &Node::isDirty)
177177

178+
.function("markLayoutSeen", &Node::markLayoutSeen)
179+
.function("hasNewLayout", &Node::hasNewLayout)
180+
178181
.function("calculateLayout", &Node::calculateLayout)
179182

180183
.function("getComputedLeft", &Node::getComputedLeft)

javascript/src/wrapAssembly.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ export type Node = {
119119
isDirty(): boolean;
120120
isReferenceBaseline(): boolean;
121121
markDirty(): void;
122+
hasNewLayout(): boolean;
123+
markLayoutSeen(): void;
122124
removeChild(child: Node): void;
123125
reset(): void;
124126
setAlignContent(alignContent: Align): void;
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import Yoga from 'yoga-layout';
9+
10+
test('new_layout_can_be_marked_seen', () => {
11+
const root = Yoga.Node.create();
12+
root.markLayoutSeen();
13+
expect(root.hasNewLayout()).toBe(false);
14+
});
15+
16+
test('new_layout_calculating_layout_marks_layout_as_unseen', () => {
17+
const root = Yoga.Node.create();
18+
root.markLayoutSeen();
19+
root.calculateLayout(undefined, undefined);
20+
expect(root.hasNewLayout()).toBe(true);
21+
});
22+
23+
test('new_layout_calculated_layout_can_be_marked_seen', () => {
24+
const root = Yoga.Node.create();
25+
root.calculateLayout(undefined, undefined);
26+
root.markLayoutSeen();
27+
expect(root.hasNewLayout()).toBe(false);
28+
});
29+
30+
test('new_layout_recalculating_layout_does_mark_as_unseen', () => {
31+
const root = Yoga.Node.create();
32+
root.calculateLayout(undefined, undefined);
33+
root.markLayoutSeen();
34+
root.calculateLayout(undefined, undefined);
35+
expect(root.hasNewLayout()).toBe(true);
36+
});
37+
38+
test('new_layout_reset_also_resets_layout_seen', () => {
39+
const root = Yoga.Node.create();
40+
root.markLayoutSeen();
41+
root.reset();
42+
expect(root.hasNewLayout()).toBe(true);
43+
});
44+
45+
test('new_layout_children_sets_new_layout', () => {
46+
const root = Yoga.Node.create();
47+
root.setAlignItems(Yoga.ALIGN_FLEX_START);
48+
root.setWidth(100);
49+
root.setHeight(100);
50+
51+
const root_child0 = Yoga.Node.create();
52+
root_child0.setAlignItems(Yoga.ALIGN_FLEX_START);
53+
root_child0.setWidth(50);
54+
root_child0.setHeight(20);
55+
root.insertChild(root_child0, 0);
56+
57+
const root_child1 = Yoga.Node.create();
58+
root_child1.setAlignItems(Yoga.ALIGN_FLEX_START);
59+
root_child1.setWidth(50);
60+
root_child1.setHeight(20);
61+
root.insertChild(root_child1, 0);
62+
63+
expect(root.hasNewLayout()).toEqual(true);
64+
expect(root_child0.hasNewLayout()).toEqual(true);
65+
expect(root_child1.hasNewLayout()).toEqual(true);
66+
67+
root.markLayoutSeen();
68+
root_child0.markLayoutSeen();
69+
root_child1.markLayoutSeen();
70+
71+
expect(root.hasNewLayout()).toEqual(false);
72+
expect(root_child0.hasNewLayout()).toEqual(false);
73+
expect(root_child1.hasNewLayout()).toEqual(false);
74+
75+
root_child1.setHeight(30);
76+
root.calculateLayout(undefined, undefined);
77+
78+
expect(root.hasNewLayout()).toEqual(true);
79+
expect(root_child0.hasNewLayout()).toEqual(true);
80+
expect(root_child1.hasNewLayout()).toEqual(true);
81+
});

0 commit comments

Comments
 (0)