@amazon-devices/expo-linear-gradient 2.0.1758683737
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.js +2 -0
- package/CHANGELOG.md +209 -0
- package/CMakeLists.txt +52 -0
- package/LICENSE +7 -0
- package/README.kepler.md +69 -0
- package/README.md +41 -0
- package/THIRD_PARTY_NOTICES.txt +24 -0
- package/android/build.gradle +107 -0
- package/android/src/main/AndroidManifest.xml +3 -0
- package/android/src/main/java/expo/modules/lineargradient/LinearGradientModule.kt +36 -0
- package/android/src/main/java/expo/modules/lineargradient/LinearGradientView.java +117 -0
- package/build/LinearGradient.d.ts +60 -0
- package/build/LinearGradient.d.ts.map +1 -0
- package/build/LinearGradient.js +31 -0
- package/build/LinearGradient.js.map +1 -0
- package/build/NativeLinearGradient.android.d.ts +4 -0
- package/build/NativeLinearGradient.android.d.ts.map +1 -0
- package/build/NativeLinearGradient.android.js +27 -0
- package/build/NativeLinearGradient.android.js.map +1 -0
- package/build/NativeLinearGradient.d.ts +4 -0
- package/build/NativeLinearGradient.d.ts.map +1 -0
- package/build/NativeLinearGradient.ios.d.ts +5 -0
- package/build/NativeLinearGradient.ios.d.ts.map +1 -0
- package/build/NativeLinearGradient.ios.js +4 -0
- package/build/NativeLinearGradient.ios.js.map +1 -0
- package/build/NativeLinearGradient.js +10 -0
- package/build/NativeLinearGradient.js.map +1 -0
- package/build/NativeLinearGradient.kepler.d.ts +23 -0
- package/build/NativeLinearGradient.kepler.d.ts.map +1 -0
- package/build/NativeLinearGradient.kepler.js +23 -0
- package/build/NativeLinearGradient.kepler.js.map +1 -0
- package/build/NativeLinearGradient.types.d.ts +11 -0
- package/build/NativeLinearGradient.types.d.ts.map +1 -0
- package/build/NativeLinearGradient.types.js +2 -0
- package/build/NativeLinearGradient.types.js.map +1 -0
- package/build/NativeLinearGradient.web.d.ts +8 -0
- package/build/NativeLinearGradient.web.d.ts.map +1 -0
- package/build/NativeLinearGradient.web.js +79 -0
- package/build/NativeLinearGradient.web.js.map +1 -0
- package/build/normalizeColor.d.ts +2 -0
- package/build/normalizeColor.d.ts.map +1 -0
- package/build/normalizeColor.js +29 -0
- package/build/normalizeColor.js.map +1 -0
- package/expo-module.config.json +10 -0
- package/ios/ExpoLinearGradient.podspec +32 -0
- package/ios/LinearGradientLayer.swift +107 -0
- package/ios/LinearGradientModule.swift +28 -0
- package/ios/LinearGradientView.swift +14 -0
- package/kepler/ExpoLinearGradientDrawableLayer.cpp +85 -0
- package/kepler/ExpoLinearGradientDrawableLayer.h +81 -0
- package/kepler/ExpoLinearGradientView.cpp +92 -0
- package/kepler/ExpoLinearGradientView.h +37 -0
- package/kepler/ExpoLinearGradientViewManager.cpp +28 -0
- package/kepler/ExpoLinearGradientViewManager.h +19 -0
- package/kepler/KeplerInit.cpp +28 -0
- package/kepler/test/ExpoLinearGradientDrawableLayer.cpp +134 -0
- package/kepler/test/ExpoLinearGradientView.cpp +86 -0
- package/kepler/test/ExpoLinearGradientViewManager.cpp +37 -0
- package/package.json +1 -0
- package/react-native.config.js +21 -0
- package/src/LinearGradient.tsx +103 -0
- package/src/NativeLinearGradient.android.tsx +49 -0
- package/src/NativeLinearGradient.ios.tsx +10 -0
- package/src/NativeLinearGradient.kepler.tsx +36 -0
- package/src/NativeLinearGradient.tsx +12 -0
- package/src/NativeLinearGradient.types.ts +21 -0
- package/src/NativeLinearGradient.web.tsx +115 -0
- package/src/normalizeColor.ts +32 -0
- package/tsconfig.json +9 -0
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Copyright 2021-present 650 Industries. All rights reserved.
|
|
2
|
+
|
|
3
|
+
import UIKit
|
|
4
|
+
import ExpoModulesCore
|
|
5
|
+
|
|
6
|
+
final class LinearGradientView: ExpoView {
|
|
7
|
+
override class var layerClass: AnyClass {
|
|
8
|
+
return LinearGradientLayer.self
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
public var gradientLayer: LinearGradientLayer {
|
|
12
|
+
return layer as! LinearGradientLayer
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright (c) 2023 Amazon.com, Inc. or its affiliates. All rights reserved.
|
|
3
|
+
//
|
|
4
|
+
#include "ExpoLinearGradientDrawableLayer.h"
|
|
5
|
+
|
|
6
|
+
#include <KeplerScript/logger/KPLog.h>
|
|
7
|
+
#include <graphics/AutoCanvasRestore.h>
|
|
8
|
+
#include <graphics/Color.h>
|
|
9
|
+
#include <graphics/Paint.h>
|
|
10
|
+
|
|
11
|
+
#include <optional>
|
|
12
|
+
|
|
13
|
+
namespace volta {
|
|
14
|
+
namespace react {
|
|
15
|
+
|
|
16
|
+
void ExpoLinearGradientDrawableLayer::onDraw(volta::graphics::Canvas& canvas) {
|
|
17
|
+
calculateGradient();
|
|
18
|
+
volta::graphics::Paint paint{};
|
|
19
|
+
paint.setShader(linearGradientShader_);
|
|
20
|
+
paint.setStyle(volta::graphics::Paint::Style::Fill);
|
|
21
|
+
paint.setAntiAlias(true);
|
|
22
|
+
|
|
23
|
+
if (!path_.isEmpty()) {
|
|
24
|
+
canvas.drawPath(path_, paint);
|
|
25
|
+
} else if (!rect_.empty()) {
|
|
26
|
+
canvas.drawRect(rect_, paint);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
void ExpoLinearGradientDrawableLayer::setColors(const std::vector<uint32_t> colors) {
|
|
31
|
+
colors_ = colors;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
void ExpoLinearGradientDrawableLayer::setStart(const graphics::Point& start) {
|
|
35
|
+
start_ = start;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
void ExpoLinearGradientDrawableLayer::setEnd(const graphics::Point& end) {
|
|
39
|
+
end_ = end;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
void ExpoLinearGradientDrawableLayer::setLocations(const std::vector<float>& locations) {
|
|
43
|
+
locations_ = locations;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
void ExpoLinearGradientDrawableLayer::setRect(const volta::graphics::Rect& rect) {
|
|
47
|
+
rect_ = rect;
|
|
48
|
+
setDimensions({rect.right(), rect.bottom()});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
void ExpoLinearGradientDrawableLayer::setPath(const volta::graphics::Path& path) {
|
|
52
|
+
path_ = path;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
void ExpoLinearGradientDrawableLayer::setSize(const float width, const float height) {
|
|
56
|
+
size_.set(width, height);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
void ExpoLinearGradientDrawableLayer::calculateGradient() {
|
|
60
|
+
// guard against crashes happening while multiple properties are updated
|
|
61
|
+
const bool noColors = colors_.empty();
|
|
62
|
+
const bool mismatchedLocationsWithColors = !locations_.empty() && colors_.size() != locations_.size();
|
|
63
|
+
if (noColors || mismatchedLocationsWithColors) return;
|
|
64
|
+
|
|
65
|
+
const graphics::Point startPos = graphics::Point{start_.x() * size_.x(), start_.y() * size_.y()};
|
|
66
|
+
const graphics::Point endPos = graphics::Point{end_.x() * size_.x(), end_.y() * size_.y()};
|
|
67
|
+
|
|
68
|
+
if (!locations_.empty()) {
|
|
69
|
+
linearGradientShader_ = volta::graphics::LinearGradientShader::makeLinearGradientShader(
|
|
70
|
+
startPos,
|
|
71
|
+
endPos,
|
|
72
|
+
colors_,
|
|
73
|
+
locations_,
|
|
74
|
+
volta::graphics::TileMode::Clamp);
|
|
75
|
+
} else {
|
|
76
|
+
linearGradientShader_ = volta::graphics::LinearGradientShader::makeLinearGradientShader(
|
|
77
|
+
startPos,
|
|
78
|
+
endPos,
|
|
79
|
+
colors_,
|
|
80
|
+
volta::graphics::TileMode::Clamp);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
} // namespace react
|
|
85
|
+
} // namespace volta
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright (c) 2023 Amazon.com, Inc. or its affiliates. All rights reserved.
|
|
3
|
+
//
|
|
4
|
+
#pragma once
|
|
5
|
+
|
|
6
|
+
#include <JsonContainer/JsonContainer.h>
|
|
7
|
+
#include <graphics/DrawableLayer.h>
|
|
8
|
+
#include <graphics/Path.h>
|
|
9
|
+
#include <graphics/Point.h>
|
|
10
|
+
#include <graphics/Rect.h>
|
|
11
|
+
#include <graphics/Shader.h>
|
|
12
|
+
|
|
13
|
+
namespace volta {
|
|
14
|
+
namespace react {
|
|
15
|
+
|
|
16
|
+
class ExpoLinearGradientDrawableLayer final : public volta::graphics::DrawableLayer {
|
|
17
|
+
public:
|
|
18
|
+
void onDraw(volta::graphics::Canvas& canvas) override;
|
|
19
|
+
void setColors(const std::vector<uint32_t> colors);
|
|
20
|
+
void setStart(const graphics::Point& start);
|
|
21
|
+
void setEnd(const graphics::Point& end);
|
|
22
|
+
void setLocations(const std::vector<float>& locations);
|
|
23
|
+
void setRect(const volta::graphics::Rect& rect);
|
|
24
|
+
void setPath(const volta::graphics::Path& path);
|
|
25
|
+
void setSize(const float width, const float height);
|
|
26
|
+
|
|
27
|
+
#ifndef UNIT_TEST
|
|
28
|
+
private:
|
|
29
|
+
#endif
|
|
30
|
+
/**
|
|
31
|
+
* @brief Calculate the gradientShader value using startPoint, endPoint, locations etc.
|
|
32
|
+
*/
|
|
33
|
+
void calculateGradient();
|
|
34
|
+
|
|
35
|
+
graphics::Point size_ = {0, 0};
|
|
36
|
+
volta::graphics::Rect rect_;
|
|
37
|
+
volta::graphics::Path path_;
|
|
38
|
+
std::shared_ptr<graphics::Shader> linearGradientShader_;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* An array of colors that represent stops in the gradient. At least two colors are required
|
|
42
|
+
* (for a single-color background, use the `style.backgroundColor` prop on a `View` component).
|
|
43
|
+
*/
|
|
44
|
+
std::vector<uint32_t> colors_;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* For example, `{ x: 0.1, y: 0.2 }` means that the gradient will start `10%` from the left and `20%` from the top.
|
|
48
|
+
*
|
|
49
|
+
* **On web**, this only changes the angle of the gradient because CSS gradients don't support changing the starting
|
|
50
|
+
* position.
|
|
51
|
+
* @default { x: 0.5, y: 0.0 }
|
|
52
|
+
*/
|
|
53
|
+
graphics::Point start_ = {0.5, 0};
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* For example, `{ x: 0.1, y: 0.2 }` means that the gradient will end `10%` from the left and `20%` from the bottom.
|
|
57
|
+
*
|
|
58
|
+
* **On web**, this only changes the angle of the gradient because CSS gradients don't support changing the end
|
|
59
|
+
* position.
|
|
60
|
+
* @default { x: 0.5, y: 1.0 }
|
|
61
|
+
*/
|
|
62
|
+
graphics::Point end_ = {0.5, 1};
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* An array that contains `number`s ranging from `0` to `1`, inclusive, and is the same length as the `colors`
|
|
66
|
+
* property. Each number indicates a color-stop location where each respective color should be located. If not
|
|
67
|
+
* specified, the colors will be distributed evenly across the gradient.
|
|
68
|
+
*
|
|
69
|
+
* For example, `[0.5, 0.8]` would render:
|
|
70
|
+
* - the first color, solid, from the beginning of the gradient view to 50% through (the middle);
|
|
71
|
+
* - a gradient from the first color to the second from the 50% point to the 80% point; and
|
|
72
|
+
* - the second color, solid, from the 80% point to the end of the gradient view.
|
|
73
|
+
*
|
|
74
|
+
* > The color-stop locations must be ascending from least to greatest.
|
|
75
|
+
* @default []
|
|
76
|
+
*/
|
|
77
|
+
std::vector<float> locations_;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
} // namespace react
|
|
81
|
+
} // namespace volta
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright (c) 2023 Amazon.com, Inc. or its affiliates. All rights reserved.
|
|
3
|
+
//
|
|
4
|
+
#include "ExpoLinearGradientView.h"
|
|
5
|
+
|
|
6
|
+
#include <KeplerScript/logger/KPLog.h>
|
|
7
|
+
#include <graphics/Color.h>
|
|
8
|
+
#include <graphics/Point.h>
|
|
9
|
+
|
|
10
|
+
#include <utility>
|
|
11
|
+
|
|
12
|
+
namespace {
|
|
13
|
+
|
|
14
|
+
template<typename T = float>
|
|
15
|
+
std::vector<T> toVector(const utils::json::JsonContainer& json) {
|
|
16
|
+
auto container = utils::json::JsonContainer(json);
|
|
17
|
+
const bool isArray = container.getType() == utils::json::JsonContainer::Type::ARRAY;
|
|
18
|
+
|
|
19
|
+
if (isArray) {
|
|
20
|
+
return container.getValuesToVector<T>();
|
|
21
|
+
}
|
|
22
|
+
return {};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
} // namespace
|
|
26
|
+
|
|
27
|
+
namespace volta {
|
|
28
|
+
namespace react {
|
|
29
|
+
|
|
30
|
+
ExpoLinearGradientView::ExpoLinearGradientView() {
|
|
31
|
+
opacityLayer_.removeAllChildren();
|
|
32
|
+
opacityLayer_.addChild(linearGradientDrawableLayer_);
|
|
33
|
+
opacityLayer_.addChild(borderDrawableLayer_);
|
|
34
|
+
opacityLayer_.addChild(clipRRectLayer_);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
void ExpoLinearGradientView::setColors(const utils::json::JsonContainer& newValue) {
|
|
38
|
+
const auto vector = toVector<double>(newValue);
|
|
39
|
+
std::vector<uint32_t> colors(vector.size());
|
|
40
|
+
for (size_t i = 0; i < vector.size(); i++) {
|
|
41
|
+
colors[i] = graphics::Color(vector[i]).argb();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
linearGradientDrawableLayer_.setColors(colors);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
void ExpoLinearGradientView::setStart(const utils::json::JsonContainer& newValue) {
|
|
48
|
+
const auto vector = toVector(newValue);
|
|
49
|
+
if (vector.size() != 2) {
|
|
50
|
+
KPLog.warn("Parameter passed to ExpoLinearGradientView::setStart must contain 2 values and has {}", vector.size());
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
linearGradientDrawableLayer_.setStart(graphics::Point{vector[0], vector[1]});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
void ExpoLinearGradientView::setEnd(const utils::json::JsonContainer& newValue) {
|
|
58
|
+
const auto vector = toVector(newValue);
|
|
59
|
+
if (vector.size() != 2) {
|
|
60
|
+
KPLog.warn("Parameter passed to ExpoLinearGradientView::setEnd must contain 2 values and has {}", vector.size());
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
linearGradientDrawableLayer_.setEnd(graphics::Point{vector[0], vector[1]});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
void ExpoLinearGradientView::setLocations(const utils::json::JsonContainer& newValue) {
|
|
68
|
+
const auto locations = toVector(newValue);
|
|
69
|
+
linearGradientDrawableLayer_.setLocations(locations);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
void ExpoLinearGradientView::updateLayoutMetrics(
|
|
73
|
+
const std::shared_ptr<KeplerViewComponentLayoutMetrics>& oldLayoutMetrics,
|
|
74
|
+
const std::shared_ptr<KeplerViewComponentLayoutMetrics>& newLayoutMetrics) {
|
|
75
|
+
if (oldLayoutMetrics == newLayoutMetrics) return;
|
|
76
|
+
|
|
77
|
+
KeplerViewComponent::updateLayoutMetrics(oldLayoutMetrics, newLayoutMetrics);
|
|
78
|
+
|
|
79
|
+
auto& topLeftCorner = borderDrawableLayer_.getBorderCalculatedMetrics().innerTopLeftCorner();
|
|
80
|
+
auto& bottomRightCorner = borderDrawableLayer_.getBorderCalculatedMetrics().innerBottomRightCorner();
|
|
81
|
+
|
|
82
|
+
linearGradientDrawableLayer_.setRect({topLeftCorner, bottomRightCorner});
|
|
83
|
+
linearGradientDrawableLayer_.setPath(
|
|
84
|
+
borderDrawableLayer_.getBorderCalculatedMetrics().innerClipPathForBorderRadius());
|
|
85
|
+
linearGradientDrawableLayer_.setSize(
|
|
86
|
+
bottomRightCorner.x() - topLeftCorner.x(),
|
|
87
|
+
bottomRightCorner.y() - topLeftCorner.y());
|
|
88
|
+
linearGradientDrawableLayer_.invalidate();
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
} // namespace react
|
|
92
|
+
} // namespace volta
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright (c) 2023 Amazon.com, Inc. or its affiliates. All rights reserved.
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
#pragma once
|
|
6
|
+
|
|
7
|
+
#include <KeplerScript/components/KeplerViewComponent.h>
|
|
8
|
+
|
|
9
|
+
#include "ExpoLinearGradientDrawableLayer.h"
|
|
10
|
+
|
|
11
|
+
namespace volta {
|
|
12
|
+
namespace react {
|
|
13
|
+
|
|
14
|
+
class ExpoLinearGradientView : public KeplerViewComponent {
|
|
15
|
+
public:
|
|
16
|
+
ExpoLinearGradientView();
|
|
17
|
+
void setColors(const utils::json::JsonContainer& newValue);
|
|
18
|
+
void setStart(const utils::json::JsonContainer& newValue);
|
|
19
|
+
void setEnd(const utils::json::JsonContainer& newValue);
|
|
20
|
+
void setLocations(const utils::json::JsonContainer& newValue);
|
|
21
|
+
|
|
22
|
+
#ifndef UNIT_TEST
|
|
23
|
+
protected:
|
|
24
|
+
#endif
|
|
25
|
+
void updateLayoutMetrics(
|
|
26
|
+
const std::shared_ptr<KeplerViewComponentLayoutMetrics>& oldLayoutMetrics,
|
|
27
|
+
const std::shared_ptr<KeplerViewComponentLayoutMetrics>& newLayoutMetrics) override;
|
|
28
|
+
|
|
29
|
+
private:
|
|
30
|
+
#ifdef UNIT_TEST
|
|
31
|
+
public:
|
|
32
|
+
#endif
|
|
33
|
+
ExpoLinearGradientDrawableLayer linearGradientDrawableLayer_;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
} // namespace react
|
|
37
|
+
} // namespace volta
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright (c) 2023 Amazon.com, Inc. or its affiliates. All rights reserved.
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
#include "ExpoLinearGradientViewManager.h"
|
|
6
|
+
|
|
7
|
+
#include <Volta/trace/Tracing.h>
|
|
8
|
+
|
|
9
|
+
#include "ExpoLinearGradientView.h"
|
|
10
|
+
|
|
11
|
+
namespace volta {
|
|
12
|
+
namespace react {
|
|
13
|
+
|
|
14
|
+
std::shared_ptr<KeplerViewComponent> ExpoLinearGradientViewManager::createComponentView() {
|
|
15
|
+
VOLTA_TRACE_SECTION(ExpoLinearGradientViewManager::createComponentView);
|
|
16
|
+
return RenderObject::create<ExpoLinearGradientView>();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
void ExpoLinearGradientViewManager::aggregatePropSetters(PropSetterAggregator& aggregator) {
|
|
20
|
+
ViewComponentViewManager::aggregatePropSetters(aggregator);
|
|
21
|
+
aggregator.addPropSetter("colors", &ExpoLinearGradientView::setColors, utils::json::JsonContainer());
|
|
22
|
+
aggregator.addPropSetter("startPoint", &ExpoLinearGradientView::setStart, utils::json::JsonContainer());
|
|
23
|
+
aggregator.addPropSetter("endPoint", &ExpoLinearGradientView::setEnd, utils::json::JsonContainer());
|
|
24
|
+
aggregator.addPropSetter("locations", &ExpoLinearGradientView::setLocations, utils::json::JsonContainer());
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
} // namespace react
|
|
28
|
+
} // namespace volta
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright (c) 2023 Amazon.com, Inc. or its affiliates. All rights reserved.
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
#pragma once
|
|
6
|
+
|
|
7
|
+
#include <KeplerScript/components/ViewComponentViewManager.h>
|
|
8
|
+
|
|
9
|
+
namespace volta {
|
|
10
|
+
namespace react {
|
|
11
|
+
|
|
12
|
+
class ExpoLinearGradientViewManager : public ViewComponentViewManager {
|
|
13
|
+
public:
|
|
14
|
+
void aggregatePropSetters(PropSetterAggregator& aggregator) override;
|
|
15
|
+
std::shared_ptr<KeplerViewComponent> createComponentView() override;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
} // namespace react
|
|
19
|
+
} // namespace volta
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
3
|
+
*
|
|
4
|
+
* PROPRIETARY/CONFIDENTIAL
|
|
5
|
+
*
|
|
6
|
+
* Use is subject to license terms.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
#include <KeplerScript/utils/autolink/KeplerAutoLinkInfo.h>
|
|
10
|
+
#include <keplerui-072/keplerscript-export.h>
|
|
11
|
+
|
|
12
|
+
#include "ExpoLinearGradientViewManager.h"
|
|
13
|
+
|
|
14
|
+
extern "C" {
|
|
15
|
+
KEPLERSCRIPT_EXPORT void keplerAutoLinkInitV1(const kepler::autolink::v1::KeplerAutoLinkInfo* autoLinkInfo) noexcept {
|
|
16
|
+
volta::react::KPLog.debug("keplerAutoLinkInitV1 for ExpoLinearGradient");
|
|
17
|
+
|
|
18
|
+
autoLinkInfo->customViewManagerDelegate->registerComponentViewManager("ExpoLinearGradient", []() {
|
|
19
|
+
volta::react::KPLog.debug("registerComponentViewManager {}", "ExpoLinearGradient");
|
|
20
|
+
|
|
21
|
+
return std::make_unique<volta::react::ExpoLinearGradientViewManager>();
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
int main() {
|
|
27
|
+
return 0;
|
|
28
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
3
|
+
*
|
|
4
|
+
* PROPRIETARY/CONFIDENTIAL
|
|
5
|
+
*
|
|
6
|
+
* Use is subject to license terms.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
#include <gmock/gmock-actions.h>
|
|
10
|
+
#include <gmock/gmock.h>
|
|
11
|
+
#include <gtest/gtest.h>
|
|
12
|
+
|
|
13
|
+
#include "catch.hpp"
|
|
14
|
+
|
|
15
|
+
#include "../ExpoLinearGradientDrawableLayer.h"
|
|
16
|
+
|
|
17
|
+
using namespace volta::react;
|
|
18
|
+
|
|
19
|
+
TEST_CASE("ExpoLinearGradientDrawableLayer") {
|
|
20
|
+
std::vector<uint32_t> colors{0xFF0000, 0x00FF00, 0x0000FF};
|
|
21
|
+
std::vector<float> locations{10, 20, 30};
|
|
22
|
+
volta::graphics::Point point1{1, 2};
|
|
23
|
+
volta::graphics::Point point2{4, 6};
|
|
24
|
+
volta::graphics::Rect rect{point1, point2};
|
|
25
|
+
volta::graphics::Path path;
|
|
26
|
+
path.addRect(rect);
|
|
27
|
+
|
|
28
|
+
// TODO: PROJVEGA-211833 - Fix these test cases - removing volta::graphics::Canvas mocks
|
|
29
|
+
// SECTION("Test ExpoLinearGradientDrawableLayer::onDraw - No metrics set") {
|
|
30
|
+
// ExpoLinearGradientDrawableLayer testExpoLinearGradientDrawableLayer{};
|
|
31
|
+
// volta::graphics::Canvas canvas{};
|
|
32
|
+
// EXPECT_CALL(canvas, drawPath(::testing::_, ::testing::_)).Times(0);
|
|
33
|
+
// EXPECT_CALL(canvas, drawRect(::testing::_, ::testing::_)).Times(0);
|
|
34
|
+
// testExpoLinearGradientDrawableLayer.onDraw(canvas);
|
|
35
|
+
// }
|
|
36
|
+
|
|
37
|
+
// SECTION("Test ExpoLinearGradientDrawableLayer::onDraw - Rect not set") {
|
|
38
|
+
// ExpoLinearGradientDrawableLayer testExpoLinearGradientDrawableLayer{};
|
|
39
|
+
// volta::graphics::Canvas canvas{};
|
|
40
|
+
// testExpoLinearGradientDrawableLayer.setRect(rect);
|
|
41
|
+
// EXPECT_CALL(canvas, drawPath(::testing::_, ::testing::_)).Times(0);
|
|
42
|
+
// EXPECT_CALL(canvas, drawRect(::testing::_, ::testing::_)).Times(1);
|
|
43
|
+
// testExpoLinearGradientDrawableLayer.onDraw(canvas);
|
|
44
|
+
// }
|
|
45
|
+
|
|
46
|
+
// SECTION("Test ExpoLinearGradientDrawableLayer::onDraw - Path not set") {
|
|
47
|
+
// ExpoLinearGradientDrawableLayer testExpoLinearGradientDrawableLayer{};
|
|
48
|
+
// volta::graphics::Canvas canvas{};
|
|
49
|
+
// testExpoLinearGradientDrawableLayer.setPath(path);
|
|
50
|
+
// EXPECT_CALL(canvas, drawPath(::testing::_, ::testing::_)).Times(1);
|
|
51
|
+
// EXPECT_CALL(canvas, drawRect(::testing::_, ::testing::_)).Times(0);
|
|
52
|
+
// testExpoLinearGradientDrawableLayer.onDraw(canvas);
|
|
53
|
+
// }
|
|
54
|
+
|
|
55
|
+
SECTION("Test ExpoLinearGradientDrawableLayer::calculateGradient - Colors are empty") {
|
|
56
|
+
ExpoLinearGradientDrawableLayer testExpoLinearGradientDrawableLayer{};
|
|
57
|
+
testExpoLinearGradientDrawableLayer.calculateGradient();
|
|
58
|
+
REQUIRE(testExpoLinearGradientDrawableLayer.linearGradientShader_ == nullptr);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
SECTION("Test ExpoLinearGradientDrawableLayer::calculateGradient - MismatchedLocationsWithColors") {
|
|
62
|
+
ExpoLinearGradientDrawableLayer testExpoLinearGradientDrawableLayer{};
|
|
63
|
+
std::vector<uint32_t> colors_more{0xFF0000, 0x00FF00, 0x0000FF, 0x000000};
|
|
64
|
+
testExpoLinearGradientDrawableLayer.setColors(colors_more);
|
|
65
|
+
testExpoLinearGradientDrawableLayer.setLocations(locations);
|
|
66
|
+
testExpoLinearGradientDrawableLayer.calculateGradient();
|
|
67
|
+
REQUIRE(testExpoLinearGradientDrawableLayer.linearGradientShader_ == nullptr);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
SECTION("Test ExpoLinearGradientDrawableLayer::calculateGradient - Locations are empty") {
|
|
71
|
+
ExpoLinearGradientDrawableLayer testExpoLinearGradientDrawableLayer{};
|
|
72
|
+
REQUIRE(testExpoLinearGradientDrawableLayer.linearGradientShader_ == nullptr);
|
|
73
|
+
testExpoLinearGradientDrawableLayer.setColors(colors);
|
|
74
|
+
testExpoLinearGradientDrawableLayer.setStart(point1);
|
|
75
|
+
testExpoLinearGradientDrawableLayer.setStart(point2);
|
|
76
|
+
testExpoLinearGradientDrawableLayer.calculateGradient();
|
|
77
|
+
REQUIRE(testExpoLinearGradientDrawableLayer.linearGradientShader_ != nullptr);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
SECTION("Test ExpoLinearGradientDrawableLayer::calculateGradient - Locations are not empty") {
|
|
81
|
+
ExpoLinearGradientDrawableLayer testExpoLinearGradientDrawableLayer{};
|
|
82
|
+
REQUIRE(testExpoLinearGradientDrawableLayer.linearGradientShader_ == nullptr);
|
|
83
|
+
testExpoLinearGradientDrawableLayer.setColors(colors);
|
|
84
|
+
testExpoLinearGradientDrawableLayer.setStart(point1);
|
|
85
|
+
testExpoLinearGradientDrawableLayer.setStart(point2);
|
|
86
|
+
testExpoLinearGradientDrawableLayer.setLocations(locations);
|
|
87
|
+
testExpoLinearGradientDrawableLayer.calculateGradient();
|
|
88
|
+
REQUIRE(testExpoLinearGradientDrawableLayer.linearGradientShader_ != nullptr);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
SECTION("Test ExpoLinearGradientDrawableLayer::setColors") {
|
|
92
|
+
ExpoLinearGradientDrawableLayer testExpoLinearGradientDrawableLayer{};
|
|
93
|
+
testExpoLinearGradientDrawableLayer.setColors(colors);
|
|
94
|
+
REQUIRE(testExpoLinearGradientDrawableLayer.colors_ == colors);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
SECTION("Test ExpoLinearGradientDrawableLayer::setStart") {
|
|
98
|
+
ExpoLinearGradientDrawableLayer testExpoLinearGradientDrawableLayer{};
|
|
99
|
+
testExpoLinearGradientDrawableLayer.setStart(point1);
|
|
100
|
+
REQUIRE(testExpoLinearGradientDrawableLayer.start_ == point1);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
SECTION("Test ExpoLinearGradientDrawableLayer::setEnd") {
|
|
104
|
+
ExpoLinearGradientDrawableLayer testExpoLinearGradientDrawableLayer{};
|
|
105
|
+
testExpoLinearGradientDrawableLayer.setEnd(point1);
|
|
106
|
+
REQUIRE(testExpoLinearGradientDrawableLayer.end_ == point1);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
SECTION("Test ExpoLinearGradientDrawableLayer::setLocations") {
|
|
110
|
+
ExpoLinearGradientDrawableLayer testExpoLinearGradientDrawableLayer{};
|
|
111
|
+
testExpoLinearGradientDrawableLayer.setLocations(locations);
|
|
112
|
+
REQUIRE(testExpoLinearGradientDrawableLayer.locations_ == locations);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
SECTION("Test ExpoLinearGradientDrawableLayer::setRect") {
|
|
116
|
+
ExpoLinearGradientDrawableLayer testExpoLinearGradientDrawableLayer{};
|
|
117
|
+
testExpoLinearGradientDrawableLayer.setRect(rect);
|
|
118
|
+
REQUIRE(testExpoLinearGradientDrawableLayer.rect_ == rect);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
SECTION("Test ExpoLinearGradientDrawableLayer::setPath") {
|
|
122
|
+
ExpoLinearGradientDrawableLayer testExpoLinearGradientDrawableLayer{};
|
|
123
|
+
path.addRect(rect);
|
|
124
|
+
testExpoLinearGradientDrawableLayer.setPath(path);
|
|
125
|
+
REQUIRE(!testExpoLinearGradientDrawableLayer.path_.isEmpty());
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
SECTION("Test ExpoLinearGradientDrawableLayer::setSize") {
|
|
129
|
+
ExpoLinearGradientDrawableLayer testExpoLinearGradientDrawableLayer{};
|
|
130
|
+
float w=10.0f, h=20.0f;
|
|
131
|
+
testExpoLinearGradientDrawableLayer.setSize(w, h);
|
|
132
|
+
REQUIRE(testExpoLinearGradientDrawableLayer.size_ == volta::graphics::Point{w, h});
|
|
133
|
+
}
|
|
134
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
3
|
+
*
|
|
4
|
+
* PROPRIETARY/CONFIDENTIAL
|
|
5
|
+
*
|
|
6
|
+
* Use is subject to license terms.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
#include "../ExpoLinearGradientView.h"
|
|
10
|
+
|
|
11
|
+
#include <gmock/gmock-actions.h>
|
|
12
|
+
#include <gmock/gmock.h>
|
|
13
|
+
#include <gtest/gtest.h>
|
|
14
|
+
|
|
15
|
+
#include "catch.hpp"
|
|
16
|
+
|
|
17
|
+
using namespace volta::react;
|
|
18
|
+
|
|
19
|
+
template<typename T>
|
|
20
|
+
utils::json::JsonContainer initializeJsonArray(std::vector<T>& list) {
|
|
21
|
+
auto arr = utils::json::JsonContainer::createJsonArray();
|
|
22
|
+
for (auto element : list) {
|
|
23
|
+
arr.insert(element);
|
|
24
|
+
}
|
|
25
|
+
return arr;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
TEST_CASE("ExpoLinearGradientView") {
|
|
29
|
+
SECTION("Test ExpoLinearGradientView::setColors") {
|
|
30
|
+
ExpoLinearGradientView testExpoLinearGradientView{};
|
|
31
|
+
std::vector<uint32_t> colors{0xFF0000, 0x00FF00, 0x0000FF};
|
|
32
|
+
auto arr = initializeJsonArray(colors);
|
|
33
|
+
testExpoLinearGradientView.setColors(arr);
|
|
34
|
+
REQUIRE(testExpoLinearGradientView.linearGradientDrawableLayer_.colors_ == colors);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
SECTION("Test ExpoLinearGradientView::setStart") {
|
|
38
|
+
ExpoLinearGradientView testExpoLinearGradientView{};
|
|
39
|
+
std::vector<float> coords{1, 2};
|
|
40
|
+
auto arr = initializeJsonArray(coords);
|
|
41
|
+
testExpoLinearGradientView.setStart(arr);
|
|
42
|
+
REQUIRE(testExpoLinearGradientView.linearGradientDrawableLayer_.start_ == volta::graphics::Point{1, 2});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
SECTION("Test ExpoLinearGradientView::setEnd") {
|
|
46
|
+
ExpoLinearGradientView testExpoLinearGradientView{};
|
|
47
|
+
std::vector<float> coords{1, 2};
|
|
48
|
+
auto arr = initializeJsonArray(coords);
|
|
49
|
+
testExpoLinearGradientView.setEnd(arr);
|
|
50
|
+
REQUIRE(testExpoLinearGradientView.linearGradientDrawableLayer_.end_ == volta::graphics::Point{1, 2});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
SECTION("Test ExpoLinearGradientView::setLocations") {
|
|
54
|
+
ExpoLinearGradientView testExpoLinearGradientView{};
|
|
55
|
+
std::vector<float> locations{10, 20, 30};
|
|
56
|
+
auto arr = initializeJsonArray(locations);
|
|
57
|
+
testExpoLinearGradientView.setLocations(arr);
|
|
58
|
+
REQUIRE(testExpoLinearGradientView.linearGradientDrawableLayer_.locations_ == locations);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
SECTION("Test ExpoLinearGradientView::updateLayoutMetrics") {
|
|
62
|
+
ExpoLinearGradientView testExpoLinearGradientView{};
|
|
63
|
+
auto oldLayoutMetrics = std::make_shared<KeplerViewComponentLayoutMetrics>();
|
|
64
|
+
auto newLayoutMetrics = std::make_shared<KeplerViewComponentLayoutMetrics>();
|
|
65
|
+
testExpoLinearGradientView.updateLayoutMetrics(oldLayoutMetrics, newLayoutMetrics);
|
|
66
|
+
REQUIRE(testExpoLinearGradientView.linearGradientDrawableLayer_.path_.isEmpty());
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
SECTION("Test ExpoLinearGradientView::updateLayoutMetrics ") {
|
|
70
|
+
ExpoLinearGradientView testExpoLinearGradientView{};
|
|
71
|
+
auto oldSize = testExpoLinearGradientView.linearGradientDrawableLayer_.size_;
|
|
72
|
+
auto oldProps = std::make_shared<KeplerViewComponentProps>();
|
|
73
|
+
auto newProps = std::make_shared<KeplerViewComponentProps>();
|
|
74
|
+
auto oldLayoutMetrics = std::make_shared<KeplerViewComponentLayoutMetrics>();
|
|
75
|
+
auto newLayoutMetrics = std::make_shared<KeplerViewComponentLayoutMetrics>();
|
|
76
|
+
volta::react::Rect oldFrame = {.size = {100, 200}};
|
|
77
|
+
volta::react::Rect newFrame = {.size = {300, 400}};
|
|
78
|
+
oldLayoutMetrics->frame = oldFrame;
|
|
79
|
+
newLayoutMetrics->frame = newFrame;
|
|
80
|
+
|
|
81
|
+
testExpoLinearGradientView.updateProps(oldProps, newProps);
|
|
82
|
+
testExpoLinearGradientView.updateLayoutMetrics(oldLayoutMetrics, newLayoutMetrics);
|
|
83
|
+
|
|
84
|
+
REQUIRE(testExpoLinearGradientView.linearGradientDrawableLayer_.size_ != oldSize);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
3
|
+
*
|
|
4
|
+
* PROPRIETARY/CONFIDENTIAL
|
|
5
|
+
*
|
|
6
|
+
* Use is subject to license terms.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
#include <gmock/gmock-actions.h>
|
|
10
|
+
#include <gmock/gmock.h>
|
|
11
|
+
#include <gtest/gtest.h>
|
|
12
|
+
|
|
13
|
+
#include "catch.hpp"
|
|
14
|
+
|
|
15
|
+
#include "../ExpoLinearGradientView.h"
|
|
16
|
+
#include "../ExpoLinearGradientViewManager.h"
|
|
17
|
+
|
|
18
|
+
using namespace volta::react;
|
|
19
|
+
|
|
20
|
+
TEST_CASE("ExpoLinearGradientViewManager") {
|
|
21
|
+
SECTION("Test ExpoLinearGradientViewManager::reateComponentView") {
|
|
22
|
+
ExpoLinearGradientViewManager testExpoLinearGradientViewManager{};
|
|
23
|
+
auto component = testExpoLinearGradientViewManager.createComponentView();
|
|
24
|
+
auto ExpoLinearGradientViewComponent = std::dynamic_pointer_cast<ExpoLinearGradientView>(component);
|
|
25
|
+
REQUIRE(ExpoLinearGradientViewComponent != nullptr);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
SECTION("Test ExpoLinearGradientViewManager::aggregatePropSetter") {
|
|
29
|
+
ExpoLinearGradientViewManager testExpoLinearGradientViewManager{};
|
|
30
|
+
PropSetterAggregator testAggregator{};
|
|
31
|
+
testExpoLinearGradientViewManager.aggregatePropSetters(testAggregator);
|
|
32
|
+
std::vector<std::string> propNames = {"colors", "startPoint", "endPoint", "locations"};
|
|
33
|
+
for (auto& propName : propNames) {
|
|
34
|
+
REQUIRE(testAggregator.propSetterMap_.find(propName) != testAggregator.propSetterMap_.end());
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"name": "@amazon-devices/expo-linear-gradient", "version": "2.0.1758683737+12.6.0", "description": "Provides a React component that renders a gradient view.", "main": "build/LinearGradient.js", "types": "build/LinearGradient.d.ts", "sideEffects": false, "scripts": {"build": "expo-module build", "clean": "expo-module clean", "lint": "expo-module lint", "test": "expo-module test", "expo-module": "expo-module"}, "kepler": {"temporary-alias-for-package": {"name": "expo-linear-gradient", "version": "12.6.0"}}, "keywords": ["react-native", "expo", "gradient"], "jest": {"preset": "expo-module-scripts"}, "repository": {"type": "git", "url": "https://github.com/expo/expo.git", "directory": "packages/expo-linear-gradient"}, "bugs": {"url": "https://github.com/expo/expo/issues"}, "author": "650 Industries, Inc.", "license": "See license in LICENSE", "homepage": "https://docs.expo.dev/versions/latest/sdk/linear-gradient/", "devDependencies": {"@testing-library/jest-dom": "^5.16.5", "@testing-library/react": "^13.4.0", "@testing-library/react-native": "^11.3.0", "expo-module-scripts": "^3.0.0"}, "peerDependencies": {"expo": "*"}}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
dependencies: {
|
|
3
|
+
'@amazon-devices/react-native-kepler': {
|
|
4
|
+
platforms: {
|
|
5
|
+
kepler: null, // do not use platform definition from @react-native-kepler
|
|
6
|
+
},
|
|
7
|
+
},
|
|
8
|
+
},
|
|
9
|
+
dependency: {
|
|
10
|
+
platforms: {
|
|
11
|
+
kepler: {
|
|
12
|
+
autolink: {
|
|
13
|
+
ExpoLinearGradientLib: {
|
|
14
|
+
provider: 'system',
|
|
15
|
+
components: ['ExpoLinearGradient'],
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
};
|