Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -233,75 +233,14 @@ Size SurfaceHandler::measure(
return rootShadowNode->getLayoutMetrics().frame.size;
}

std::shared_ptr<const ShadowNode> SurfaceHandler::dirtyMeasurableNodesRecursive(
std::shared_ptr<const ShadowNode> node) const {
const auto nodeHasChildren = !node->getChildren().empty();
const auto isMeasurableYogaNode =
node->getTraits().check(ShadowNodeTraits::Trait::MeasurableYogaNode);

// Node is not measurable and has no children, its layout will not be affected
if (!nodeHasChildren && !isMeasurableYogaNode) {
return nullptr;
void SurfaceHandler::dirtyMeasurableNodes(std::shared_ptr<const ShadowNode> node) const {
if (auto* layoutableNode = dynamic_cast<const YogaLayoutableShadowNode*>(node.get())) {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might check the traits before trying the dynamic cast - I believe this would be faster than trying to call dynamic_cast if we'd like to optimize this further (I left optimization out since this is a thing that doesn't happen that often)

// Only cast away const where needed

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll remove this comment, it is not valid.

const_cast<YogaLayoutableShadowNode*>(layoutableNode)->dirtyLayout();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if it's safe to dirty a potentially sealed node.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this was what was done previously as well? I couldn't find a place where we checked for sealed or not in the previous code.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously, the node was cloned before marking it as dirty - the new node is always unsealed.

You also can't really check if a node is sealed or not, since that information is preserved only in debug builds.

}

ShadowNode::SharedListOfShared newChildren =
ShadowNodeFragment::childrenPlaceholder();

if (nodeHasChildren) {
std::shared_ptr<std::vector<std::shared_ptr<const ShadowNode>>>
newChildrenMutable = nullptr;
for (size_t i = 0; i < node->getChildren().size(); i++) {
const auto& child = node->getChildren()[i];

if (const auto& layoutableNode =
std::dynamic_pointer_cast<const YogaLayoutableShadowNode>(
child)) {
auto newChild = dirtyMeasurableNodesRecursive(layoutableNode);

if (newChild != nullptr) {
if (newChildrenMutable == nullptr) {
newChildrenMutable = std::make_shared<
std::vector<std::shared_ptr<const ShadowNode>>>(
node->getChildren());
newChildren = newChildrenMutable;
}

(*newChildrenMutable)[i] = newChild;
}
}
}

// Node is not measurable and its children were not dirtied, its layout will
// not be affected
if (!isMeasurableYogaNode && newChildrenMutable == nullptr) {
return nullptr;
}
}

const auto newNode = node->getComponentDescriptor().cloneShadowNode(
*node,
{
.children = newChildren,
// Preserve the original state of the node
.state = node->getState(),
});

if (isMeasurableYogaNode) {
std::static_pointer_cast<YogaLayoutableShadowNode>(newNode)->dirtyLayout();
}

return newNode;
}

void SurfaceHandler::dirtyMeasurableNodes(ShadowNode& root) const {
for (const auto& child : root.getChildren()) {
if (const auto& layoutableNode =
std::dynamic_pointer_cast<const YogaLayoutableShadowNode>(child)) {
const auto newChild = dirtyMeasurableNodesRecursive(layoutableNode);
if (newChild != nullptr) {
root.replaceChild(*child, newChild);
}
}
for (const auto& child : node->getChildren()) {
dirtyMeasurableNodes(child);
}
}

Expand Down Expand Up @@ -344,7 +283,7 @@ void SurfaceHandler::constraintLayout(
layoutContext.fontSizeMultiplier !=
oldRootShadowNode.getConcreteProps()
.layoutContext.fontSizeMultiplier) {
dirtyMeasurableNodes(*newRoot);
dirtyMeasurableNodes(newRoot);
}

return newRoot;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,9 @@ class SurfaceHandler {
void applyDisplayMode(DisplayMode displayMode) const;

/*
* An utility for dirtying all measurable shadow nodes present in the tree.
* Marks all measurable shadow nodes in the given tree as dirty
*/
void dirtyMeasurableNodes(ShadowNode& root) const;
std::shared_ptr<const ShadowNode> dirtyMeasurableNodesRecursive(
std::shared_ptr<const ShadowNode> node) const;
void dirtyMeasurableNodes(std::shared_ptr<const ShadowNode> node) const;

#pragma mark - Link & Parameters

Expand Down