-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRNCMaterialHeaderComponentInstance.cpp
149 lines (129 loc) · 7.67 KB
/
RNCMaterialHeaderComponentInstance.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//
// Created on 2024/3/30.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".
#include "RNCMaterialHeaderComponentInstance.h"
#include "RNOH/arkui/NativeNodeApi.h"
#include "TaskCommonThread.h"
#include "react/renderer/imagemanager/primitives.h"
#include "SmartRefreshState.h"
#include <bits/alltypes.h>
namespace rnoh {
RNCMaterialHeaderComponentInstance::RNCMaterialHeaderComponentInstance(Context context)
: CppComponentInstance(std::move(context)) {}
void RNCMaterialHeaderComponentInstance::addHeader(int32_t screenWidth, int32_t index, ArkUINode *arkUI_Node) {
mWindowWidth = screenWidth;
progressNode.setLoadingProgressNodeColor(0x53658461);
progressNode.setLoadingProgressNodeAnimating(true);
progressNode.setSize({45, 45});
ArkUI_NumberValue heightArray[] = {{.f32 = 45}};
ArkUI_AttributeItem heightValue[] = {heightArray, 1};
NativeNodeApi::getInstance()->setAttribute(imageStack.getArkUINodeHandle(), NODE_HEIGHT, heightValue);
ArkUI_NumberValue widthArray[] = {{.f32 = 45}};
ArkUI_AttributeItem widthValue[] = {widthArray, 1};
NativeNodeApi::getInstance()->setAttribute(imageStack.getArkUINodeHandle(), NODE_WIDTH, widthValue);
ArkUI_NumberValue borderStyArray[] = {0.2};
ArkUI_AttributeItem borderStyValue[] = {borderStyArray, 1};
NativeNodeApi::getInstance()->setAttribute(imageStack.getArkUINodeHandle(), NODE_BORDER_WIDTH, borderStyValue);
ArkUI_NumberValue radiusArray[] = {{.f32 = 45}};
ArkUI_AttributeItem radiusValue[] = {radiusArray, 1};
NativeNodeApi::getInstance()->setAttribute(imageStack.getArkUINodeHandle(), NODE_BORDER_RADIUS, radiusValue);
imageStack.setBackgroundColor(0xFFfafafa);
ArkUI_NumberValue borderColorArray[] = {
{.u32 = 0xFFaaaaaa}, {.u32 = 0xFFaaaaaa}, {.u32 = 0xFFaaaaaa}, {.u32 = 0xFFaaaaaa}, {.u32 = 0xFFaaaaaa}};
ArkUI_AttributeItem borderColorValue[] = {borderColorArray, 4};
NativeNodeApi::getInstance()->setAttribute(imageStack.getArkUINodeHandle(), NODE_BORDER_COLOR,
borderColorValue);
uint32_t shadowColorValue = 0xffaaaaaa;
uint32_t alpha = static_cast<uint32_t>((float)(shadowColorValue >> 24 & (0xff)) * 1.0);
shadowColorValue = (alpha << 24) + (shadowColorValue & 0xffffff);
ArkUI_NumberValue shadowValue[] = {
{.f32 = 2}, {.i32 = 0}, {.f32 = 1}, {.f32 = 1}, {.i32 = 0}, {.u32 = shadowColorValue}, {.u32 = 0}};
ArkUI_AttributeItem shadowItem = {.value = shadowValue,
.size = sizeof(shadowValue) / sizeof(ArkUI_NumberValue)};
NativeNodeApi::getInstance()->setAttribute(imageStack.getArkUINodeHandle(), NODE_CUSTOM_SHADOW, &shadowItem);
ArkUI_NumberValue z_indexArray[] = {{.f32 = 1000}};
ArkUI_AttributeItem z_indexValue[] = {z_indexArray, 1};
NativeNodeApi::getInstance()->setAttribute(imageStack.getArkUINodeHandle(), NODE_Z_INDEX, z_indexValue);
NativeNodeApi::getInstance()->insertChildAt(imageStack.getArkUINodeHandle(), progressNode.getArkUINodeHandle(),
0);
NativeNodeApi::getInstance()->insertChildAt(arkUI_Node->getArkUINodeHandle(), imageStack.getArkUINodeHandle(),
index);
ArkUI_NumberValue positionArray[] = {{.f32 = static_cast<float>((screenWidth - 46) / 2.0)}, {.f32 = -46}};
ArkUI_AttributeItem positionValue[] = {positionArray, 2};
NativeNodeApi::getInstance()->setAttribute(imageStack.getArkUINodeHandle(), NODE_POSITION, positionValue);
}
void RNCMaterialHeaderComponentInstance::onHeaderMove(float dur) {
if (isRefreshed) {
return;
}
ArkUI_NumberValue positionArray[] = {{.f32 = static_cast<float>((mWindowWidth - 46) / 2.0)}, {.f32 = dur - 46}};
ArkUI_AttributeItem positionValue[] = {positionArray, 2};
NativeNodeApi::getInstance()->setAttribute(imageStack.getArkUINodeHandle(), NODE_POSITION, positionValue);
}
void RNCMaterialHeaderComponentInstance::setScaleAnimate(int32_t dur) {
TaskCommonThread *task = new TaskCommonThread();
task->setTaskParams(static_cast<std::chrono::milliseconds>(dur), [this](double v) {
float value = 1.0 - static_cast<float>(v);
ArkUI_NumberValue scaleArray[] = {{.f32 = value}, {.f32 = value}};
ArkUI_AttributeItem scaleValue[] = {scaleArray, 2};
NativeNodeApi::getInstance()->setAttribute(imageStack.getArkUINodeHandle(), NODE_SCALE, scaleValue);
if (std::abs(value - 0.01) < 1e-6) {
ArkUI_NumberValue scaleArray[] = {{.f32 = 1.0}, {.f32 = 1.0}};
ArkUI_AttributeItem scaleValue[] = {scaleArray, 2};
NativeNodeApi::getInstance()->setAttribute(imageStack.getArkUINodeHandle(), NODE_SCALE, scaleValue);
}
});
task->execute();
}
void RNCMaterialHeaderComponentInstance::finalizeUpdates() {
auto rnInstancePtr = this->m_deps->rnInstance.lock();
if (rnInstancePtr != nullptr) {
auto turboModule = rnInstancePtr->getTurboModule("RNCSmartRefreshContext");
auto arkTsTurboModule = std::dynamic_pointer_cast<rnoh::ArkTSTurboModule>(turboModule);
folly::dynamic result = arkTsTurboModule->callSync("cvp2px", {getLayoutMetrics().frame.size.width});
folly::dynamic result1 = arkTsTurboModule->callSync("cvp2px", {60});
m_stackNode.setLayoutRect({0, 0}, {result["values"].asDouble(), result1["values"].asDouble()}, 1.0);
}
m_stackNode.setAlignment(ARKUI_ALIGNMENT_BOTTOM);
}
facebook::react::SharedColor RNCMaterialHeaderComponentInstance::GetPrimaryColor() { return -1; }
void RNCMaterialHeaderComponentInstance::onChildInserted(ComponentInstance::Shared const &childComponentInstance,
std::size_t index) {
CppComponentInstance::onChildInserted(childComponentInstance, index);
m_stackNode.insertChild(childComponentInstance->getLocalRootArkUINode(), index);
getParent().lock()->getComponentName();
}
void RNCMaterialHeaderComponentInstance::onChildRemoved(ComponentInstance::Shared const &childComponentInstance) {
CppComponentInstance::onChildRemoved(childComponentInstance);
m_stackNode.removeChild(childComponentInstance->getLocalRootArkUINode());
};
SmartStackNode &RNCMaterialHeaderComponentInstance::getLocalRootArkUINode() { return m_stackNode; }
void RNCMaterialHeaderComponentInstance::onRefreshStatusChange(int32_t status) {
switch (status) {
case IS_FREE:
{
float x = static_cast<float>((mWindowWidth - 46) / 2.0);
float y = -46.0;
ArkUI_NumberValue positionArray[] = {{.f32 = x}, {.f32 = y}};
ArkUI_AttributeItem positionValue[] = {positionArray, 2};
NativeNodeApi::getInstance()->setAttribute(imageStack.getArkUINodeHandle(), NODE_POSITION, positionValue);
}
case IS_PULL_DOWN_1:
case IS_PULL_DOWN_2: {
isRefreshed = false;
progressNode.setLoadingProgressNodeColor(0xff0099cc);
} break;
case IS_REFRESHING: {
progressNode.setLoadingProgressNodeColor(0xffff4444);
}
break;
case IS_REFRESHED: {
isRefreshed = true;
progressNode.setLoadingProgressNodeColor(0xffaa66cc);
setScaleAnimate(1000);
} break;
}
}
} // namespace rnoh