This commit is contained in:
Yamozha
2021-04-02 02:24:13 +03:00
parent c23950b545
commit 7256d79e2c
31493 changed files with 3036630 additions and 0 deletions

View File

@ -0,0 +1,122 @@
load("@fbsource//tools/build_defs/apple:flag_defs.bzl", "get_preprocessor_flags_for_build_mode")
load(
"//tools/build_defs/oss:rn_defs.bzl",
"ANDROID",
"APPLE",
"CXX",
"YOGA_CXX_TARGET",
"fb_xplat_cxx_test",
"get_apple_compiler_flags",
"get_apple_inspector_flags",
"react_native_target",
"react_native_xplat_target",
"rn_xplat_cxx_library",
"subdir_glob",
)
APPLE_COMPILER_FLAGS = get_apple_compiler_flags()
rn_xplat_cxx_library(
name = "slider",
srcs = glob(
["**/*.cpp"],
exclude = glob([
"tests/**/*.cpp",
"platform/**/*.cpp",
]),
),
headers = [],
header_namespace = "",
exported_headers = subdir_glob(
[
("", "*.h"),
],
prefix = "react/components/slider",
),
compiler_flags = [
"-fexceptions",
"-frtti",
"-std=c++14",
"-Wall",
],
cxx_tests = [":tests"],
fbandroid_deps = [
react_native_target("jni/react/jni:jni"),
],
fbandroid_exported_headers = subdir_glob(
[
("", "*.h"),
("platform/android", "*.h"),
],
prefix = "react/components/slider",
),
fbandroid_headers = glob(
["platform/android/*.h"],
),
fbandroid_srcs = glob(
["platform/android/*.cpp"],
),
fbobjc_compiler_flags = APPLE_COMPILER_FLAGS,
fbobjc_labels = ["supermodule:ios/default/public.react_native.infra"],
fbobjc_preprocessor_flags = get_preprocessor_flags_for_build_mode() + get_apple_inspector_flags(),
force_static = True,
ios_exported_headers = subdir_glob(
[
("", "*.h"),
("platform/ios", "*.h"),
],
prefix = "react/components/slider",
),
ios_headers = glob(
["platform/ios/*.h"],
),
ios_srcs = glob(
["platform/ios/*.cpp"],
),
platforms = (ANDROID, APPLE, CXX),
preprocessor_flags = [
"-DLOG_TAG=\"ReactNative\"",
"-DWITH_FBSYSTRACE=1",
],
visibility = ["PUBLIC"],
deps = [
"//xplat/fbsystrace:fbsystrace",
"//xplat/folly:headers_only",
"//xplat/folly:memory",
"//xplat/folly:molly",
"//xplat/third-party/glog:glog",
YOGA_CXX_TARGET,
react_native_xplat_target("fabric/debug:debug"),
react_native_xplat_target("fabric/core:core"),
react_native_xplat_target("fabric/components/image:image"),
react_native_xplat_target("fabric/components/view:view"),
react_native_xplat_target("fabric/graphics:graphics"),
react_native_xplat_target("fabric/imagemanager:imagemanager"),
react_native_xplat_target("fabric/uimanager:uimanager"),
"//xplat/js/react-native-github:generated_components-rncore",
],
)
fb_xplat_cxx_test(
name = "tests",
srcs = glob(["tests/**/*.cpp"]),
headers = glob(["tests/**/*.h"]),
compiler_flags = [
"-fexceptions",
"-frtti",
"-std=c++14",
"-Wall",
],
contacts = ["oncall+react_native@xmail.facebook.com"],
platforms = (
# `Apple` and `Android` flavors are disabled because the module (built with those flavors) requires Emulator/Simulator (which is expensive and slow). At the same time, we don't really have tests here.
# ANDROID,
# APPLE,
CXX,
),
deps = [
":slider",
"//xplat/folly:molly",
"//xplat/third-party/gmock:gtest",
],
)

View File

@ -0,0 +1,59 @@
/*
* 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.
*/
#pragma once
#include <react/components/slider/SliderMeasurementsManager.h>
#include <react/components/slider/SliderShadowNode.h>
#include <react/core/ConcreteComponentDescriptor.h>
namespace facebook {
namespace react {
/*
* Descriptor for <Slider> component.
*/
class SliderComponentDescriptor final
: public ConcreteComponentDescriptor<SliderShadowNode> {
public:
SliderComponentDescriptor(ComponentDescriptorParameters const &parameters)
: ConcreteComponentDescriptor(parameters),
imageManager_(std::make_shared<ImageManager>(contextContainer_)),
measurementsManager_(
SliderMeasurementsManager::shouldMeasureSlider()
? std::make_shared<SliderMeasurementsManager>(contextContainer_)
: nullptr) {}
void adopt(UnsharedShadowNode shadowNode) const override {
ConcreteComponentDescriptor::adopt(shadowNode);
assert(std::dynamic_pointer_cast<SliderShadowNode>(shadowNode));
auto sliderShadowNode =
std::static_pointer_cast<SliderShadowNode>(shadowNode);
// `SliderShadowNode` uses `ImageManager` to initiate image loading and
// communicate the loading state and results to mounting layer.
sliderShadowNode->setImageManager(imageManager_);
if (measurementsManager_) {
// `SliderShadowNode` uses `SliderMeasurementsManager` to
// provide measurements to Yoga.
sliderShadowNode->setSliderMeasurementsManager(measurementsManager_);
// All `SliderShadowNode`s must have leaf Yoga nodes with properly
// setup measure function.
sliderShadowNode->enableMeasurement();
}
}
private:
const SharedImageManager imageManager_;
const std::shared_ptr<SliderMeasurementsManager> measurementsManager_;
};
} // namespace react
} // namespace facebook

View File

@ -0,0 +1,100 @@
/*
* 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 "SliderShadowNode.h"
#include <react/core/LayoutContext.h>
namespace facebook {
namespace react {
extern const char SliderComponentName[] = "Slider";
void SliderShadowNode::setImageManager(const SharedImageManager &imageManager) {
ensureUnsealed();
imageManager_ = imageManager;
}
void SliderShadowNode::setSliderMeasurementsManager(
const std::shared_ptr<SliderMeasurementsManager> &measurementsManager) {
ensureUnsealed();
measurementsManager_ = measurementsManager;
}
void SliderShadowNode::updateStateIfNeeded() {
const auto &newTrackImageSource = getTrackImageSource();
const auto &newMinimumTrackImageSource = getMinimumTrackImageSource();
const auto &newMaximumTrackImageSource = getMaximumTrackImageSource();
const auto &newThumbImageSource = getThumbImageSource();
auto const &currentState = getStateData();
auto trackImageSource = currentState.getTrackImageSource();
auto minimumTrackImageSource = currentState.getMinimumTrackImageSource();
auto maximumTrackImageSource = currentState.getMaximumTrackImageSource();
auto thumbImageSource = currentState.getThumbImageSource();
bool anyChanged = newTrackImageSource != trackImageSource ||
newMinimumTrackImageSource != minimumTrackImageSource ||
newMaximumTrackImageSource != maximumTrackImageSource ||
newThumbImageSource != thumbImageSource;
if (!anyChanged) {
return;
}
// Now we are about to mutate the Shadow Node.
ensureUnsealed();
// It is not possible to copy or move image requests from SliderLocalData,
// so instead we recreate any image requests (that may already be in-flight?)
// TODO: check if multiple requests are cached or if it's a net loss
auto state = SliderState{
newTrackImageSource,
imageManager_->requestImage(newTrackImageSource, getSurfaceId()),
newMinimumTrackImageSource,
imageManager_->requestImage(newMinimumTrackImageSource, getSurfaceId()),
newMaximumTrackImageSource,
imageManager_->requestImage(newMaximumTrackImageSource, getSurfaceId()),
newThumbImageSource,
imageManager_->requestImage(newThumbImageSource, getSurfaceId())};
setStateData(std::move(state));
}
ImageSource SliderShadowNode::getTrackImageSource() const {
return getConcreteProps().trackImage;
}
ImageSource SliderShadowNode::getMinimumTrackImageSource() const {
return getConcreteProps().minimumTrackImage;
}
ImageSource SliderShadowNode::getMaximumTrackImageSource() const {
return getConcreteProps().maximumTrackImage;
}
ImageSource SliderShadowNode::getThumbImageSource() const {
return getConcreteProps().thumbImage;
}
#pragma mark - LayoutableShadowNode
Size SliderShadowNode::measure(LayoutConstraints layoutConstraints) const {
if (SliderMeasurementsManager::shouldMeasureSlider()) {
return measurementsManager_->measure(getSurfaceId(), layoutConstraints);
}
return {};
}
void SliderShadowNode::layout(LayoutContext layoutContext) {
updateStateIfNeeded();
ConcreteViewShadowNode::layout(layoutContext);
}
} // namespace react
} // namespace facebook

View File

@ -0,0 +1,74 @@
/*
* 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.
*/
#pragma once
#include <react/components/rncore/EventEmitters.h>
#include <react/components/rncore/Props.h>
#include <react/components/slider/SliderMeasurementsManager.h>
#include <react/components/slider/SliderState.h>
#include <react/components/view/ConcreteViewShadowNode.h>
#include <react/imagemanager/ImageManager.h>
#include <react/imagemanager/primitives.h>
namespace facebook {
namespace react {
extern const char SliderComponentName[];
/*
* `ShadowNode` for <Slider> component.
*/
class SliderShadowNode final : public ConcreteViewShadowNode<
SliderComponentName,
SliderProps,
SliderEventEmitter,
SliderState> {
public:
using ConcreteViewShadowNode::ConcreteViewShadowNode;
// Associates a shared `ImageManager` with the node.
void setImageManager(const SharedImageManager &imageManager);
// Associates a shared `SliderMeasurementsManager` with the node.
void setSliderMeasurementsManager(
const std::shared_ptr<SliderMeasurementsManager> &measurementsManager);
static SliderState initialStateData(
ShadowNodeFragment const &fragment,
SurfaceId const surfaceId,
ComponentDescriptor const &componentDescriptor) {
auto imageSource = ImageSource{ImageSource::Type::Invalid};
return {imageSource,
{imageSource, nullptr},
imageSource,
{imageSource, nullptr},
imageSource,
{imageSource, nullptr},
imageSource,
{imageSource, nullptr}};
}
#pragma mark - LayoutableShadowNode
Size measure(LayoutConstraints layoutConstraints) const override;
void layout(LayoutContext layoutContext) override;
private:
void updateStateIfNeeded();
ImageSource getTrackImageSource() const;
ImageSource getMinimumTrackImageSource() const;
ImageSource getMaximumTrackImageSource() const;
ImageSource getThumbImageSource() const;
SharedImageManager imageManager_;
std::shared_ptr<SliderMeasurementsManager> measurementsManager_;
};
} // namespace react
} // namespace facebook

View File

@ -0,0 +1,46 @@
/*
* 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 "SliderState.h"
namespace facebook {
namespace react {
ImageSource SliderState::getTrackImageSource() const {
return trackImageSource_;
}
ImageRequest const &SliderState::getTrackImageRequest() const {
return *trackImageRequest_;
}
ImageSource SliderState::getMinimumTrackImageSource() const {
return minimumTrackImageSource_;
}
ImageRequest const &SliderState::getMinimumTrackImageRequest() const {
return *minimumTrackImageRequest_;
}
ImageSource SliderState::getMaximumTrackImageSource() const {
return maximumTrackImageSource_;
}
ImageRequest const &SliderState::getMaximumTrackImageRequest() const {
return *maximumTrackImageRequest_;
}
ImageSource SliderState::getThumbImageSource() const {
return thumbImageSource_;
}
ImageRequest const &SliderState::getThumbImageRequest() const {
return *thumbImageRequest_;
}
} // namespace react
} // namespace facebook

View File

@ -0,0 +1,81 @@
/*
* 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.
*/
#pragma once
#include <folly/dynamic.h>
#include <react/imagemanager/ImageRequest.h>
#include <react/imagemanager/primitives.h>
namespace facebook {
namespace react {
/*
* State for <Slider> component.
*/
class SliderState final {
public:
SliderState(
ImageSource const &trackImageSource,
ImageRequest trackImageRequest,
ImageSource const &minimumTrackImageSource,
ImageRequest minimumTrackImageRequest,
ImageSource const &maximumTrackImageSource,
ImageRequest maximumTrackImageRequest,
ImageSource const &thumbImageSource,
ImageRequest thumbImageRequest)
: trackImageSource_(trackImageSource),
trackImageRequest_(
std::make_shared<ImageRequest>(std::move(trackImageRequest))),
minimumTrackImageSource_(minimumTrackImageSource),
minimumTrackImageRequest_(std::make_shared<ImageRequest>(
std::move(minimumTrackImageRequest))),
maximumTrackImageSource_(maximumTrackImageSource),
maximumTrackImageRequest_(std::make_shared<ImageRequest>(
std::move(maximumTrackImageRequest))),
thumbImageSource_(thumbImageSource),
thumbImageRequest_(
std::make_shared<ImageRequest>(std::move(thumbImageRequest))){};
SliderState() = default;
ImageSource getTrackImageSource() const;
ImageRequest const &getTrackImageRequest() const;
ImageSource getMinimumTrackImageSource() const;
ImageRequest const &getMinimumTrackImageRequest() const;
ImageSource getMaximumTrackImageSource() const;
ImageRequest const &getMaximumTrackImageRequest() const;
ImageSource getThumbImageSource() const;
ImageRequest const &getThumbImageRequest() const;
#ifdef ANDROID
SliderState(SliderState const &previousState, folly::dynamic data){};
/*
* Empty implementation for Android because it doesn't use this class.
*/
folly::dynamic getDynamic() const {
return {};
};
#endif
private:
ImageSource trackImageSource_;
std::shared_ptr<ImageRequest> trackImageRequest_;
ImageSource minimumTrackImageSource_;
std::shared_ptr<ImageRequest> minimumTrackImageRequest_;
ImageSource maximumTrackImageSource_;
std::shared_ptr<ImageRequest> maximumTrackImageRequest_;
ImageSource thumbImageSource_;
std::shared_ptr<ImageRequest> thumbImageRequest_;
};
} // namespace react
} // namespace facebook

View File

@ -0,0 +1,68 @@
/*
* 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 "SliderMeasurementsManager.h"
#include <fbjni/fbjni.h>
#include <react/core/conversions.h>
#include <react/jni/ReadableNativeMap.h>
using namespace facebook::jni;
namespace facebook {
namespace react {
Size SliderMeasurementsManager::measure(
SurfaceId surfaceId,
LayoutConstraints layoutConstraints) const {
{
std::lock_guard<std::mutex> lock(mutex_);
if (hasBeenMeasured_) {
return cachedMeasurement_;
}
}
const jni::global_ref<jobject> &fabricUIManager =
contextContainer_->at<jni::global_ref<jobject>>("FabricUIManager");
static auto measure =
jni::findClassStatic("com/facebook/react/fabric/FabricUIManager")
->getMethod<jlong(
jint,
jstring,
ReadableMap::javaobject,
ReadableMap::javaobject,
ReadableMap::javaobject,
jfloat,
jfloat,
jfloat,
jfloat)>("measure");
auto minimumSize = layoutConstraints.minimumSize;
auto maximumSize = layoutConstraints.maximumSize;
local_ref<JString> componentName = make_jstring("RCTSlider");
auto measurement = yogaMeassureToSize(measure(
fabricUIManager,
surfaceId,
componentName.get(),
nullptr,
nullptr,
nullptr,
minimumSize.width,
maximumSize.width,
minimumSize.height,
maximumSize.height));
std::lock_guard<std::mutex> lock(mutex_);
cachedMeasurement_ = measurement;
return measurement;
}
} // namespace react
} // namespace facebook

View File

@ -0,0 +1,40 @@
/*
* 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.
*/
#pragma once
#include <react/core/ConcreteComponentDescriptor.h>
#include <react/core/LayoutConstraints.h>
#include <react/utils/ContextContainer.h>
namespace facebook {
namespace react {
/**
* Class that manages slider measurements across platforms.
* On iOS it is a noop, since the height is passed in from JS on iOS only.
*/
class SliderMeasurementsManager {
public:
SliderMeasurementsManager(const ContextContainer::Shared &contextContainer)
: contextContainer_(contextContainer) {}
static inline bool shouldMeasureSlider() {
return true;
}
Size measure(SurfaceId surfaceId, LayoutConstraints layoutConstraints) const;
private:
const ContextContainer::Shared contextContainer_;
mutable std::mutex mutex_;
mutable bool hasBeenMeasured_ = false;
mutable Size cachedMeasurement_{};
};
} // namespace react
} // namespace facebook

View File

@ -0,0 +1,21 @@
/*
* 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 "SliderMeasurementsManager.h"
namespace facebook {
namespace react {
Size SliderMeasurementsManager::measure(
SurfaceId surfaceId,
LayoutConstraints layoutConstraints) const {
assert(false); // should never reach this point
return {};
}
} // namespace react
} // namespace facebook

View File

@ -0,0 +1,33 @@
/*
* 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.
*/
#pragma once
#include <react/core/ConcreteComponentDescriptor.h>
#include <react/core/LayoutConstraints.h>
#include <react/utils/ContextContainer.h>
namespace facebook {
namespace react {
/**
* Class that manages slider measurements across platforms.
* On iOS it is a noop, since the height is passed in from JS on iOS only.
*/
class SliderMeasurementsManager {
public:
SliderMeasurementsManager(ContextContainer::Shared const &contextContainer) {}
static inline bool shouldMeasureSlider() {
return false;
}
Size measure(SurfaceId surfaceId, LayoutConstraints layoutConstraints) const;
};
} // namespace react
} // namespace facebook

View File

@ -0,0 +1,14 @@
/*
* 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 <memory>
#include <gtest/gtest.h>
TEST(SliderTest, testSomething) {
// TODO
}