mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
181b42bfce
Summary: We don't need to have those constants because this functionality is available in STL via `std::numeric_limits<YourParticularFloatType>::infinity()` (or `::min()` and `::max()`). At the same time usage of `kFloatMax` was replaced with `Infinity` (which is a different value). Using `max` instead of `Infinity` was an attempt to mimic iOS/UIKit model where `Infinity` and `NaN` values usually are not being used. However, now this does not seem like a good idea. This concept is not used anywhere else (even in CK which is totally incompatible with it) and de-facto in RN we use it only in few places. So, let's use Infinity in places where it's logically appropriate. Reviewed By: mdvacca Differential Revision: D15155191 fbshipit-source-id: 4d24350c7540cec074a8b040d7c13f257aa812e7
95 lines
2.5 KiB
C++
95 lines
2.5 KiB
C++
/**
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
#include <cstdlib>
|
|
#include <limits>
|
|
|
|
#include <react/components/image/ImageLocalData.h>
|
|
#include <react/components/image/ImageShadowNode.h>
|
|
#include <react/core/LayoutContext.h>
|
|
|
|
namespace facebook {
|
|
namespace react {
|
|
|
|
const char ImageComponentName[] = "Image";
|
|
|
|
void ImageShadowNode::setImageManager(const SharedImageManager &imageManager) {
|
|
ensureUnsealed();
|
|
imageManager_ = imageManager;
|
|
}
|
|
|
|
void ImageShadowNode::updateLocalData() {
|
|
const auto &imageSource = getImageSource();
|
|
const auto ¤tLocalData = getLocalData();
|
|
if (currentLocalData) {
|
|
assert(std::dynamic_pointer_cast<const ImageLocalData>(currentLocalData));
|
|
auto currentImageLocalData =
|
|
std::static_pointer_cast<const ImageLocalData>(currentLocalData);
|
|
if (currentImageLocalData->getImageSource() == imageSource) {
|
|
// Same `imageSource` is already in `localData`,
|
|
// no need to (re)request an image resource.
|
|
return;
|
|
}
|
|
}
|
|
|
|
// Now we are about to mutate the Shadow Node.
|
|
ensureUnsealed();
|
|
|
|
auto imageRequest = imageManager_->requestImage(imageSource);
|
|
auto imageLocalData =
|
|
std::make_shared<ImageLocalData>(imageSource, std::move(imageRequest));
|
|
setLocalData(imageLocalData);
|
|
}
|
|
|
|
ImageSource ImageShadowNode::getImageSource() const {
|
|
auto sources = getProps()->sources;
|
|
|
|
if (sources.size() == 0) {
|
|
return {
|
|
/* .type = */ ImageSource::Type::Invalid,
|
|
};
|
|
}
|
|
|
|
if (sources.size() == 1) {
|
|
return sources[0];
|
|
}
|
|
|
|
auto layoutMetrics = getLayoutMetrics();
|
|
auto size = layoutMetrics.getContentFrame().size;
|
|
auto scale = layoutMetrics.pointScaleFactor;
|
|
auto targetImageArea = size.width * size.height * scale * scale;
|
|
auto bestFit = std::numeric_limits<Float>::infinity();
|
|
|
|
auto bestSource = ImageSource{};
|
|
|
|
for (const auto &source : sources) {
|
|
auto sourceSize = source.size;
|
|
auto sourceScale = source.scale == 0 ? scale : source.scale;
|
|
auto sourceArea =
|
|
sourceSize.width * sourceSize.height * sourceScale * sourceScale;
|
|
|
|
auto fit = std::abs(1 - (sourceArea / targetImageArea));
|
|
|
|
if (fit < bestFit) {
|
|
bestFit = fit;
|
|
bestSource = source;
|
|
}
|
|
}
|
|
|
|
return bestSource;
|
|
}
|
|
|
|
#pragma mark - LayoutableShadowNode
|
|
|
|
void ImageShadowNode::layout(LayoutContext layoutContext) {
|
|
updateLocalData();
|
|
ConcreteViewShadowNode::layout(layoutContext);
|
|
}
|
|
|
|
} // namespace react
|
|
} // namespace facebook
|