Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions packages/dear-imgui/cpp/app/src/xframes_commit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,26 @@ CommitResult XFrames::ApplyCommitOperations(CommitBatch& batch) {
}
}
operationIndex.reset();
// Disconnect every previous Yoga owner before attaching any
// candidate child. Moving out of a removed ancestor retains the
// exact Element, Yoga node, internal subject and populated data.
// Only parents whose child list changed are re-linked. Re-linking an
// unchanged parent marks its Yoga subtree dirty and makes the next
// frame lay it out again. A child that moves leaves a parent whose
// list changes too, or one that is destroyed; both are disconnected
// before any candidate child is attached. Moving out of a removed
// ancestor retains the exact Element, Yoga node, internal subject and
// populated data.
std::unordered_set<int> relink;
for (const auto& [parent, children] : plan.children) {
if (parent == 0) continue;
const auto previous = m_hierarchy.find(parent);
if (!m_publicationOwnedIds.contains(parent) || previous == m_hierarchy.end() || previous->second != children)
relink.insert(parent);
}
for (int id : m_publicationOwnedIds)
YGNodeRemoveAllChildren(m_elements.at(id)->m_layoutNode->m_node);
if (relink.contains(id) || !plan.ownedIds.contains(id))
YGNodeRemoveAllChildren(m_elements.at(id)->m_layoutNode->m_node);
for (const auto& [parent, children] : plan.children) {
m_hierarchy[parent] = children;
if (parent == 0) continue;
if (parent == 0 || !relink.contains(parent)) continue;
auto* element = m_elements.at(parent).get();
for (size_t index = 0; index < children.size(); ++index)
element->m_layoutNode->InsertChild(m_elements.at(children[index])->m_layoutNode.get(), index);
Expand Down
19 changes: 19 additions & 0 deletions packages/dear-imgui/cpp/tests/xframes_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,25 @@ TEST_F(XFramesTest, PublicationReorderAndInsertionKeepHierarchyAndYogaConsistent
EXPECT_EQ(Node(state, 1)["yogaChildren"], json::array({3, 4, 2}));
EXPECT_EQ(Node(state, 4)["yogaParent"], 1);
}
// Re-linking a parent marks its Yoga subtree dirty, so the next frame lays it out
// again. A publication re-links only parents whose child list changed: a sibling
// subtree that did not change keeps its layout.
TEST_F(XFramesTest, PublicationRelinksOnlyParentsWhoseChildrenChanged) {
Publish({{0, {1}}, {1, {2, 3}}, {2, {4}}, {3, {5}}, {4, {}}, {5, {}}},
{Create(1), Create(2), Create(3), Create(4), Create(5)});
const auto yoga = [&](int id) { return ElementAt(id)->m_layoutNode->m_node; };
YGNodeCalculateLayout(yoga(1), 100, 100, YGDirectionLTR);
ASSERT_FALSE(YGNodeIsDirty(yoga(3)));
EXPECT_EQ(Publish({{0, {1}}, {1, {2, 3}}, {2, {6}}, {3, {5}}, {6, {}}, {5, {}}}, {Create(6)}).destroyedIds,
(std::vector<int>{4}));
EXPECT_TRUE(YGNodeIsDirty(yoga(2)));
EXPECT_FALSE(YGNodeIsDirty(yoga(3)));
EXPECT_FALSE(YGNodeIsDirty(yoga(5)));
const auto state = xf->GetDiagnosticsState();
EXPECT_EQ(Node(state, 2)["yogaChildren"], json::array({6}));
EXPECT_EQ(Node(state, 3)["yogaChildren"], json::array({5}));
EXPECT_EQ(Node(state, 1)["yogaChildren"], json::array({2, 3}));
}
TEST_F(XFramesTest, PublicationIdenticalAndEmptyTreesAdvanceExactlyOnce) {
EXPECT_EQ(Publish().nativeRevision, 1);
EXPECT_EQ(Publish({{0, {1}}, {1, {}}}, {Create(1)}).nativeRevision, 2);
Expand Down
Loading