Skip to content

Commit b482409

Browse files
dmytrorykunfacebook-github-bot
authored andcommitted
Add Android-specific ImageRequestParams
Differential Revision: D66453306
1 parent 1d4a933 commit b482409

File tree

3 files changed

+184
-3
lines changed

3 files changed

+184
-3
lines changed

packages/react-native/ReactCommon/react/renderer/components/image/ImageShadowNode.cpp

+18-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,24 @@ void ImageShadowNode::updateStateIfNeeded() {
3030
auto newImageSource = getImageSource();
3131
const auto& oldImageRequestParams = savedState.getImageRequestParams();
3232
const auto& imageProps = getConcreteProps();
33-
const auto& newImageRequestParams = ImageRequestParams(imageProps.blurRadius);
33+
const auto& newImageRequestParams = ImageRequestParams(
34+
imageProps.blurRadius
35+
#ifdef ANDROID
36+
,
37+
imageProps.defaultSource,
38+
imageProps.resizeMode,
39+
imageProps.resizeMethod,
40+
// TODO: should we resizeMultiplier * imageSource.scale ?
41+
imageProps.resizeMultiplier,
42+
imageProps.shouldNotify,
43+
imageProps.overlayColor,
44+
imageProps.tintColor,
45+
imageProps.fadeDuration,
46+
imageProps.progressiveRenderingEnabled,
47+
imageProps.loadingIndicatorSource,
48+
imageProps.internal_analyticTag
49+
#endif
50+
);
3451

3552
if (oldImageSource == newImageSource &&
3653
oldImageRequestParams == newImageRequestParams) {

packages/react-native/ReactCommon/react/renderer/imagemanager/platform/android/ImageRequestParams.h

+66-2
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,83 @@
77

88
#pragma once
99

10+
#include <utility>
11+
12+
#include <react/renderer/graphics/Color.h>
1013
#include <react/renderer/graphics/Float.h>
14+
#include <react/renderer/imagemanager/primitives.h>
1115

1216
namespace facebook::react {
1317

1418
class ImageRequestParams {
1519
public:
1620
ImageRequestParams() = default;
17-
ImageRequestParams(Float blurRadius) : blurRadius(blurRadius) {}
21+
ImageRequestParams(
22+
Float blurRadius,
23+
ImageSource defaultSource,
24+
ImageResizeMode resizeMode,
25+
std::string resizeMethod,
26+
Float resizeMultiplier,
27+
bool shouldNotify,
28+
SharedColor overlayColor,
29+
SharedColor tintColor,
30+
Float fadeDuration,
31+
bool progressiveRenderingEnabled,
32+
ImageSource loadingIndicatorSource,
33+
std::string analyticTag)
34+
: blurRadius(blurRadius),
35+
defaultSource(std::move(defaultSource)),
36+
resizeMode(resizeMode),
37+
resizeMethod(std::move(resizeMethod)),
38+
resizeMultiplier(resizeMultiplier),
39+
shouldNotify(shouldNotify),
40+
overlayColor(overlayColor),
41+
tintColor(tintColor),
42+
fadeDuration(fadeDuration),
43+
progressiveRenderingEnabled(progressiveRenderingEnabled),
44+
loadingIndicatorSource(std::move(loadingIndicatorSource)),
45+
analyticTag(std::move(analyticTag)) {}
1846

1947
Float blurRadius{};
48+
ImageSource defaultSource{};
49+
ImageResizeMode resizeMode{ImageResizeMode::Stretch};
50+
std::string resizeMethod{};
51+
Float resizeMultiplier{};
52+
bool shouldNotify{};
53+
SharedColor overlayColor{};
54+
SharedColor tintColor{};
55+
Float fadeDuration{};
56+
bool progressiveRenderingEnabled{};
57+
ImageSource loadingIndicatorSource{};
58+
std::string analyticTag{};
2059

2160
bool operator==(const ImageRequestParams& rhs) const {
22-
return this->blurRadius == rhs.blurRadius;
61+
return std::tie(
62+
this->blurRadius,
63+
this->defaultSource,
64+
this->resizeMode,
65+
this->resizeMethod,
66+
this->resizeMultiplier,
67+
this->shouldNotify,
68+
this->overlayColor,
69+
this->tintColor,
70+
this->fadeDuration,
71+
this->progressiveRenderingEnabled,
72+
this->loadingIndicatorSource,
73+
this->analyticTag) ==
74+
std::tie(
75+
rhs.blurRadius,
76+
rhs.defaultSource,
77+
rhs.resizeMode,
78+
rhs.resizeMethod,
79+
rhs.resizeMultiplier,
80+
rhs.shouldNotify,
81+
rhs.overlayColor,
82+
rhs.tintColor,
83+
rhs.fadeDuration,
84+
rhs.progressiveRenderingEnabled,
85+
rhs.loadingIndicatorSource,
86+
rhs.analyticTag);
2387
}
2488

2589
bool operator!=(const ImageRequestParams& rhs) const {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
#pragma once
9+
10+
#include <string>
11+
12+
#include <react/renderer/core/graphicsConversions.h>
13+
#include <react/renderer/imagemanager/ImageRequestParams.h>
14+
#include <react/renderer/imagemanager/primitives.h>
15+
#include <react/renderer/mapbuffer/MapBuffer.h>
16+
#include <react/renderer/mapbuffer/MapBufferBuilder.h>
17+
18+
namespace facebook::react {
19+
20+
inline std::string toString(const ImageResizeMode& value) {
21+
switch (value) {
22+
case ImageResizeMode::Cover:
23+
return "cover";
24+
case ImageResizeMode::Contain:
25+
return "contain";
26+
case ImageResizeMode::Stretch:
27+
return "stretch";
28+
case ImageResizeMode::Center:
29+
return "center";
30+
case ImageResizeMode::Repeat:
31+
return "repeat";
32+
case ImageResizeMode::None:
33+
return "none";
34+
}
35+
}
36+
37+
constexpr static MapBuffer::Key IS_KEY_URI = 0;
38+
constexpr static MapBuffer::Key IS_KEY_DEFAULT_SRC = 1;
39+
constexpr static MapBuffer::Key IS_KEY_RESIZE_MODE = 2;
40+
constexpr static MapBuffer::Key IS_KEY_RESIZE_METHOD = 3;
41+
constexpr static MapBuffer::Key IS_KEY_BLUR_RADIUS = 4;
42+
constexpr static MapBuffer::Key IS_KEY_VIEW_WIDTH = 5;
43+
constexpr static MapBuffer::Key IS_KEY_VIEW_HEIGHT = 6;
44+
constexpr static MapBuffer::Key IS_KEY_RESIZE_MULTIPLIER = 7;
45+
constexpr static MapBuffer::Key IS_KEY_SHOULD_NOTIFY_LOAD_EVENTS = 8;
46+
constexpr static MapBuffer::Key IS_KEY_OVERLAY_COLOR = 9;
47+
constexpr static MapBuffer::Key IS_KEY_TINT_COLOR = 10;
48+
constexpr static MapBuffer::Key IS_KEY_FADE_DURATION = 11;
49+
constexpr static MapBuffer::Key IS_KEY_PROGRESSIVE_RENDERING_ENABLED = 12;
50+
constexpr static MapBuffer::Key IS_KEY_LOADING_INDICATOR_SRC = 13;
51+
constexpr static MapBuffer::Key IS_KEY_ANALYTIC_TAG = 14;
52+
53+
inline void serializeImageSource(
54+
MapBufferBuilder& builder,
55+
const ImageSource& imageSource) {
56+
builder.putString(IS_KEY_URI, imageSource.uri);
57+
builder.putDouble(IS_KEY_VIEW_WIDTH, imageSource.size.width);
58+
builder.putDouble(IS_KEY_VIEW_HEIGHT, imageSource.size.height);
59+
}
60+
61+
inline void serializeImageRequestParams(
62+
MapBufferBuilder& builder,
63+
const ImageRequestParams& imageRequestParams) {
64+
builder.putString(IS_KEY_DEFAULT_SRC, imageRequestParams.defaultSource.uri);
65+
builder.putString(
66+
IS_KEY_RESIZE_MODE, toString(imageRequestParams.resizeMode));
67+
builder.putString(IS_KEY_RESIZE_METHOD, imageRequestParams.resizeMethod);
68+
builder.putDouble(IS_KEY_BLUR_RADIUS, imageRequestParams.blurRadius);
69+
builder.putDouble(
70+
IS_KEY_RESIZE_MULTIPLIER, imageRequestParams.resizeMultiplier);
71+
builder.putBool(
72+
IS_KEY_SHOULD_NOTIFY_LOAD_EVENTS, imageRequestParams.shouldNotify);
73+
if (isColorMeaningful(imageRequestParams.overlayColor)) {
74+
builder.putInt(
75+
IS_KEY_OVERLAY_COLOR, toAndroidRepr(imageRequestParams.overlayColor));
76+
}
77+
if (isColorMeaningful(imageRequestParams.tintColor)) {
78+
builder.putInt(
79+
IS_KEY_TINT_COLOR, toAndroidRepr(imageRequestParams.tintColor));
80+
}
81+
builder.putDouble(IS_KEY_FADE_DURATION, imageRequestParams.fadeDuration);
82+
builder.putBool(
83+
IS_KEY_PROGRESSIVE_RENDERING_ENABLED,
84+
imageRequestParams.progressiveRenderingEnabled);
85+
builder.putString(
86+
IS_KEY_LOADING_INDICATOR_SRC,
87+
imageRequestParams.loadingIndicatorSource.uri);
88+
builder.putString(IS_KEY_ANALYTIC_TAG, imageRequestParams.analyticTag);
89+
}
90+
91+
inline MapBuffer serializeImageRequest(
92+
const ImageSource& imageSource,
93+
const ImageRequestParams& imageRequestParams) {
94+
auto builder = MapBufferBuilder();
95+
serializeImageSource(builder, imageSource);
96+
serializeImageRequestParams(builder, imageRequestParams);
97+
return builder.build();
98+
}
99+
100+
} // namespace facebook::react

0 commit comments

Comments
 (0)