ScrollViewShadowNode.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. * Copyright (c) Facebook, Inc. and its 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. #include "ScrollViewShadowNode.h"
  8. #include <react/core/LayoutMetrics.h>
  9. namespace facebook {
  10. namespace react {
  11. const char ScrollViewComponentName[] = "ScrollView";
  12. void ScrollViewShadowNode::updateStateIfNeeded() {
  13. ensureUnsealed();
  14. auto contentBoundingRect = Rect{};
  15. for (const auto &childNode : getLayoutableChildNodes()) {
  16. contentBoundingRect.unionInPlace(childNode->getLayoutMetrics().frame);
  17. }
  18. auto state = getStateData();
  19. if (state.contentBoundingRect != contentBoundingRect) {
  20. state.contentBoundingRect = contentBoundingRect;
  21. setStateData(std::move(state));
  22. }
  23. }
  24. #pragma mark - LayoutableShadowNode
  25. void ScrollViewShadowNode::layout(LayoutContext layoutContext) {
  26. ConcreteViewShadowNode::layout(layoutContext);
  27. updateStateIfNeeded();
  28. }
  29. Transform ScrollViewShadowNode::getTransform() const {
  30. auto transform = ConcreteViewShadowNode::getTransform();
  31. auto contentOffset = getStateData().contentOffset;
  32. return transform *
  33. Transform::Translate(-contentOffset.x, -contentOffset.y, 0);
  34. }
  35. } // namespace react
  36. } // namespace facebook