Skip to content

Commit eed82a6

Browse files
committed
capi base implement
1 parent f444688 commit eed82a6

13 files changed

+785
-48
lines changed

harmony/smart_refresh_layout.har

6.13 KB
Binary file not shown.
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
//
2+
// Created on 10/3/2024.
3+
//
4+
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
5+
// please include "napi/native_api.h".
6+
7+
#ifndef HARMONY_ANIMATION_H
8+
#define HARMONY_ANIMATION_H
9+
#include <iostream>
10+
#include <thread>
11+
#include <atomic>
12+
#include <chrono>
13+
#include <condition_variable>
14+
#include <glog/logging.h>
15+
16+
class Animation {
17+
public:
18+
Animation(std::chrono::milliseconds durationSeconds, double startValue, double endValue,
19+
std::function<void(double)> callback)
20+
: duration_(durationSeconds), currentValue_(startValue), targetValue_(endValue), startValue_(startValue),
21+
startTime_(std::chrono::steady_clock::now()), callback_(callback), updateThread_(nullptr) {
22+
if (!callback_) {
23+
throw std::invalid_argument("Callback cannot be null");
24+
}
25+
}
26+
27+
28+
~Animation() {
29+
// 等待线程结束
30+
if (updateThread_ != nullptr) {
31+
updateThread_->join();
32+
delete updateThread_; // 如果使用new分配,则需要delete释放
33+
updateThread_ = nullptr;
34+
}
35+
}
36+
37+
double GetCurrentValue() const { return currentValue_; }
38+
39+
void Start() {
40+
startTime_ = std::chrono::steady_clock::now();
41+
// 启动更新线程
42+
updateThread_ = new std::thread(&Animation::UpdateLoop, this);
43+
// updateThread_ = new std::thread(&Animation::Animate, this);
44+
}
45+
46+
47+
void UpdateLoop() {
48+
LOG(INFO) << "[clx] UpdateLoop begin";
49+
const double p0 = 0.42, p1 = 0.0, p2 = 0.58, p3 = 1.0; // CubicBezier控制点
50+
auto startTime = std::chrono::high_resolution_clock::now();
51+
// std::chrono::milliseconds stepTime(10); // 每步的时间间隔(毫秒)
52+
// std::chrono::milliseconds elapsedTime(0);
53+
// while (elapsedTime < duration_) {
54+
// double elapsedTimeSeconds = static_cast<double>(elapsedTime.count()) / 1000.0;
55+
// }
56+
for (double t = 0.0; t <= 1.0; t += 0.01) { // 逐步增加t的值来模拟动画进度
57+
double progress = cubicBezier(t, p0, p1, p2, p3); // 计算CubicBezier曲线上的点作为进度
58+
currentValue_ = startValue_ + (targetValue_ - startValue_) * progress; // 根据进度计算当前值
59+
callback_(currentValue_); // 调用回调函数处理当前值
60+
// 暂停以匹配动画的持续时间
61+
auto currentTime = std::chrono::high_resolution_clock::now();
62+
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - startTime);
63+
if (elapsed < duration_ * progress) {
64+
std::this_thread::sleep_for((duration_ * progress - elapsed) * 0.1);
65+
}
66+
startTime = currentTime; // 重置开始时间以计算下一帧的延迟
67+
}
68+
callback_(targetValue_);
69+
}
70+
71+
float Animate() {
72+
std::chrono::milliseconds stepTime(10); // 每步的时间间隔(毫秒)
73+
double totalDurationMilliseconds = static_cast<double>(duration_.count());
74+
for (double elapsedTimeMilliseconds = 0; elapsedTimeMilliseconds < totalDurationMilliseconds;
75+
elapsedTimeMilliseconds += stepTime.count()) {
76+
// 计算当前时间比例
77+
double t = static_cast<double>(elapsedTimeMilliseconds) / totalDurationMilliseconds;
78+
79+
// 使用CubicBezier计算当前值
80+
double progress = cubicBezier(t, 0.42, 0.0, 0.58, 1.0);
81+
currentValue_ = startValue_ + (targetValue_ - startValue_) * progress;
82+
callback_(currentValue_);
83+
// 等待一段时间以模拟动画的逐步进行
84+
std::this_thread::sleep_for(stepTime);
85+
}
86+
callback_(targetValue_);
87+
}
88+
89+
90+
private:
91+
std::atomic<double> startValue_;
92+
std::atomic<double> currentValue_;
93+
double targetValue_;
94+
std::chrono::milliseconds duration_;
95+
std::chrono::steady_clock::time_point startTime_;
96+
std::thread *updateThread_; // 使用指针是为了能够动态分配线程对象
97+
std::function<void(double)> callback_;
98+
99+
double cubicBezier(double t, double p0, double p1, double p2, double p3) {
100+
return pow(1 - t, 3) * p0 + 3 * pow(1 - t, 2) * t * p1 + 3 * (1 - t) * pow(t, 2) * p2 + pow(t, 3) * p3;
101+
}
102+
};
103+
#endif // HARMONY_ANIMATION_H
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
//
2+
// Created on 2024/3/8.
3+
//
4+
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
5+
// please include "napi/native_api.h".
6+
7+
#ifndef HARMONY_PULLTOREFRESHCONFIGURATOR_H
8+
#define HARMONY_PULLTOREFRESHCONFIGURATOR_H
9+
10+
#include "boost/cstdint.hpp"
11+
#include "react/renderer/graphics/Color.h"
12+
#include "react/renderer/graphics/Float.h"
13+
#include <string>
14+
namespace rnoh {
15+
class PullToRefreshConfigurator {
16+
private:
17+
bool hasRefresh = true; // 是否具有下拉刷新功能
18+
bool hasLoadMore = true; // 是否具有上拉加载功能
19+
uint32_t maxTranslate = 100; // 可下拉上拉的最大距离
20+
facebook::react::Float sensitivity = 0.7; // 下拉上拉灵敏度
21+
bool listIsPlacement = true; // 滑动结束后列表是否归位
22+
23+
24+
uint32_t animDuration = 150; // 滑动结束后,回弹动画执行时间
25+
26+
uint32_t refreshHeight = 30; // 下拉动画高度
27+
28+
facebook::react::SharedColor refreshColor = 0xFF6495ED; // 下拉动画颜色
29+
30+
facebook::react::SharedColor refreshBackgroundColor = 0xFF64811D; // 下拉动画区域背景色
31+
//
32+
// refreshTextColor ?: ResourceColor = '#999999'; // 下拉加载完毕后提示文本的字体颜色
33+
//
34+
// refreshTextSize ?: number | string | Resource = 18; // 下拉加载完毕后提示文本的字体大小
35+
36+
uint32_t refreshAnimDuration = 1000; // 下拉动画执行一次的时间
37+
uint32_t loadImgHeight = 30; // 上拉图片高度
38+
39+
std::string loadTextPullUp1 = "正在上拉刷新..."; // 上拉1阶段文本
40+
41+
std::string loadTextPullUp2 = "放开刷新"; // 上拉2阶段文本
42+
43+
std::string loadTextLoading = "正在玩命加载中..."; // 上拉加载更多中时的文本
44+
45+
uint32_t finishDelay = 500; // 刷新完成时,延迟回弹的时间
46+
47+
public:
48+
PullToRefreshConfigurator setHasRefresh(const bool hasRefresh) {
49+
this->hasRefresh = hasRefresh;
50+
return *this;
51+
}
52+
bool getHasRefresh() { return this->hasRefresh; }
53+
PullToRefreshConfigurator setHasLoadMore(const bool hasLoadMore) {
54+
this->hasLoadMore = hasLoadMore;
55+
return *this;
56+
}
57+
bool getHasLoadMore() { return this->hasLoadMore; }
58+
PullToRefreshConfigurator setMaxTranslate(const float maxTranslate) {
59+
this->maxTranslate = maxTranslate;
60+
return *this;
61+
}
62+
uint32_t getMaxTranslate() { return this->maxTranslate; }
63+
PullToRefreshConfigurator setSensitivity(const float sensitivity) {
64+
this->sensitivity = sensitivity;
65+
return *this;
66+
}
67+
facebook::react::Float getSensitivity() { return this->sensitivity; }
68+
PullToRefreshConfigurator setListIsPlacement(const bool listIsPlacement) {
69+
this->listIsPlacement = listIsPlacement;
70+
return *this;
71+
}
72+
bool getListIsPlacement() { return this->listIsPlacement; }
73+
PullToRefreshConfigurator setAnimDuration(const uint32_t animDuration) {
74+
this->animDuration = animDuration;
75+
return *this;
76+
}
77+
uint32_t getAnimDuration() { return this->animDuration; }
78+
float getRefreshWidth() {
79+
if (this->refreshHeight == 0) {
80+
return this->refreshHeight / 3 * 4;
81+
}
82+
return 0.0;
83+
}
84+
PullToRefreshConfigurator setRefreshHeight(const uint32_t refreshHeight) {
85+
this->refreshHeight = refreshHeight;
86+
return *this;
87+
}
88+
uint32_t getRefreshHeight() { return this->refreshHeight; }
89+
PullToRefreshConfigurator setRefreshColor(const facebook::react::SharedColor refreshColor) {
90+
this->refreshColor = refreshColor;
91+
return *this;
92+
}
93+
std::string getRefreshColor() { this->refreshColor; }
94+
PullToRefreshConfigurator setRefreshBackgroundColor(facebook::react::SharedColor refreshBackgroundColor) {
95+
this->refreshBackgroundColor = refreshBackgroundColor;
96+
}
97+
facebook::react::SharedColor getRefreshBackgroundColor() { return this->refreshBackgroundColor; }
98+
// PullToRefreshConfigurator setRefreshTextColor(bool listIsPlacement);
99+
// bool getRefreshTextColor();
100+
// PullToRefreshConfigurator setRefreshTextSize(bool listIsPlacement);
101+
// bool getRefreshTextSize();
102+
// PullToRefreshConfigurator setLoadBackgroundColor(bool listIsPlacement);
103+
// bool getLoadBackgroundColor();
104+
// PullToRefreshConfigurator setLoadTextColor(bool listIsPlacement);
105+
// bool getLoadTextColor();
106+
// PullToRefreshConfigurator setLoadTextSize(bool listIsPlacement);
107+
// bool getLoadTextSize();
108+
PullToRefreshConfigurator setRefreshAnimDuration(const uint32_t refreshAnimDuration) {
109+
this->refreshAnimDuration = refreshAnimDuration;
110+
return *this;
111+
}
112+
uint32_t getRefreshAnimDuration() { return this->refreshAnimDuration; }
113+
PullToRefreshConfigurator setLoadImgHeight(const float loadImgHeight) {
114+
this->loadImgHeight = loadImgHeight;
115+
return *this;
116+
}
117+
float getLoadImgHeight() { return this->loadImgHeight; }
118+
PullToRefreshConfigurator setLoadTextPullUp1(const std::string loadTextPullUp1) {
119+
this->loadTextPullUp1 = loadTextPullUp1;
120+
return *this;
121+
}
122+
std::string getLoadTextPullUp1() { return this->loadTextPullUp1; }
123+
PullToRefreshConfigurator setLoadTextPullUp2(const std::string loadTextPullUp2) {
124+
this->loadTextPullUp2 = loadTextPullUp2;
125+
return *this;
126+
}
127+
std::string getLoadTextPullUp2() { return this->loadTextPullUp2; }
128+
PullToRefreshConfigurator setLoadTextLoading(const std::string loadTextLoading) {
129+
this->loadTextLoading = loadTextLoading;
130+
return *this;
131+
}
132+
std::string getLoadTextLoading() { return this->loadTextLoading; }
133+
PullToRefreshConfigurator setFinishDelay(const uint32_t finishDelay) {
134+
this->finishDelay = finishDelay;
135+
return *this;
136+
}
137+
uint32_t getFinishDelay() { return this->finishDelay; }
138+
};
139+
} // namespace rnoh
140+
141+
#endif // HARMONY_PULLTOREFRESHCONFIGURATOR_H

0 commit comments

Comments
 (0)