feat(dom): update node size

This commit is contained in:
charleeshen
2022-03-08 15:14:40 +08:00
committed by ilikethese
parent 963c18330c
commit 4381663e81
6 changed files with 120 additions and 55 deletions
+5
View File
@@ -136,7 +136,12 @@ class DomNode : public std::enable_shared_from_this<DomNode> {
void UpdateProperties(const std::unordered_map<std::string, std::shared_ptr<DomValue>>& update_style,
const std::unordered_map<std::string, std::shared_ptr<DomValue>>& update_dom_ext);
void UpdateDomNodeStyle(const std::unordered_map<std::string, std::shared_ptr<DomValue>>& update_style);
private:
void UpdateDiff(const std::unordered_map<std::string, std::shared_ptr<DomValue>>& update_style,
const std::unordered_map<std::string, std::shared_ptr<DomValue>>& update_dom_ext);
void UpdateDomExt(const std::unordered_map<std::string, std::shared_ptr<DomValue>>& update_dom_ext);
void UpdateStyle(const std::unordered_map<std::string, std::shared_ptr<DomValue>>& update_style);
void UpdateObjectStyle(DomValue& style_map, const DomValue& update_style);
private:
+6 -1
View File
@@ -428,11 +428,16 @@ void DomManager::UpdateRenderNode(const std::shared_ptr<DomNode>& node) {
if (!render_manager) {
return;
}
TDF_BASE_DCHECK(node);
// 更新 layout tree
node->ParseLayoutStyleInfo();
// 更新属性
std::vector<std::shared_ptr<DomNode>> nodes;
nodes.push_back(node);
render_manager->UpdateRenderNode(std::move(nodes));
Layout();
NotifyRender();
}
+76 -49
View File
@@ -323,55 +323,9 @@ void DomNode::UpdateProperties(const std::unordered_map<std::string, std::shared
dom_manager->PostTask([WEAK_THIS, update_style, update_dom_ext]() {
DEFINE_AND_CHECK_SELF(DomNode)
// udpate diff
auto style_diff_value = DiffUtils::DiffProps(update_style, *self->GetStyleMap());
auto ext_diff_value = DiffUtils::DiffProps(update_style, *self->GetStyleMap());
auto style_update = std::get<0>(style_diff_value);
auto ext_update = std::get<0>(ext_diff_value);
std::shared_ptr<DomValueMap> diff_value = std::make_shared<DomValueMap>();
if (style_update) {
diff_value->insert(style_update->begin(), style_update->end());
}
if (ext_update) {
diff_value->insert(ext_update->begin(), ext_update->end());
}
self->SetDiffStyle(diff_value);
for (const auto& v : update_style) {
if (self->style_map_ == nullptr) {
self->style_map_ = std::make_shared<std::unordered_map<std::string, std::shared_ptr<DomValue>>>();
}
auto iter = self->style_map_->find(v.first);
if (iter == self->style_map_->end()) {
std::pair<std::string, std::shared_ptr<DomValue>> pair = {v.first, std::make_shared<DomValue>(std::move(*v.second))};
self->style_map_->insert(pair);
}
if (v.second->IsObject() && iter->second->IsObject()) {
self->UpdateObjectStyle(*iter->second, *v.second);
} else {
iter->second = std::make_shared<DomValue>(std::move(*v.second));
}
}
for (const auto& v : update_dom_ext) {
if (self->dom_ext_map_ == nullptr) {
self->dom_ext_map_ = std::make_shared<std::unordered_map<std::string, std::shared_ptr<DomValue>>>();
}
auto iter = self->dom_ext_map_->find(v.first);
if (iter == self->dom_ext_map_->end()) {
std::pair<std::string, std::shared_ptr<DomValue>> pair = {v.first, std::make_shared<DomValue>(std::move(*v.second))};
self->dom_ext_map_->insert(pair);
}
if (v.second->IsObject() && iter->second->IsObject()) {
self->UpdateObjectStyle(*iter->second, *v.second);
} else {
iter->second = std::make_shared<DomValue>(std::move(*v.second));
}
}
self->UpdateDiff(update_style, update_dom_ext);
self->UpdateStyle(update_style);
self->UpdateDomExt(update_dom_ext);
// update Render Node
auto dom_manager = self->dom_manager_.lock();
@@ -384,6 +338,79 @@ void DomNode::UpdateProperties(const std::unordered_map<std::string, std::shared
}
}
void DomNode::UpdateDomNodeStyle(const std::unordered_map<std::string, std::shared_ptr<DomValue>>& update_style) {
auto dom_manager = dom_manager_.lock();
TDF_BASE_DCHECK(dom_manager);
if (dom_manager) {
dom_manager->PostTask([WEAK_THIS, update_style]() {
DEFINE_AND_CHECK_SELF(DomNode)
self->UpdateStyle(update_style);
});
}
}
void DomNode::UpdateDiff(const std::unordered_map<std::string, std::shared_ptr<DomValue>>& update_style,
const std::unordered_map<std::string, std::shared_ptr<DomValue>>& update_dom_ext) {
auto style_diff_value = DiffUtils::DiffProps(*this->GetStyleMap(), update_style);
auto ext_diff_value = DiffUtils::DiffProps(*this->GetStyleMap(), update_dom_ext);
auto style_update = std::get<0>(style_diff_value);
auto ext_update = std::get<0>(ext_diff_value);
std::shared_ptr<DomValueMap> diff_value = std::make_shared<DomValueMap>();
if (style_update) {
diff_value->insert(style_update->begin(), style_update->end());
}
if (ext_update) {
diff_value->insert(ext_update->begin(), ext_update->end());
}
this->SetDiffStyle(diff_value);
}
void DomNode::UpdateDomExt(const std::unordered_map<std::string, std::shared_ptr<DomValue>>& update_dom_ext) {
if (update_dom_ext.size() == 0) return;
for (const auto& v : update_dom_ext) {
if (this->dom_ext_map_ == nullptr) {
this->dom_ext_map_ = std::make_shared<std::unordered_map<std::string, std::shared_ptr<DomValue>>>();
}
auto iter = this->dom_ext_map_->find(v.first);
if (iter == this->dom_ext_map_->end()) {
std::pair<std::string, std::shared_ptr<DomValue>> pair = {v.first, std::make_shared<DomValue>(*v.second)};
this->dom_ext_map_->insert(pair);
continue;
}
if (v.second->IsObject() && iter->second->IsObject()) {
this->UpdateObjectStyle(*iter->second, *v.second);
} else {
iter->second = std::make_shared<DomValue>(*v.second);
}
}
}
void DomNode::UpdateStyle(const std::unordered_map<std::string, std::shared_ptr<DomValue>>& update_style) {
if (update_style.size() == 0) return;
for (const auto& v : update_style) {
if (this->style_map_ == nullptr) {
this->style_map_ = std::make_shared<std::unordered_map<std::string, std::shared_ptr<DomValue>>>();
}
auto iter = this->style_map_->find(v.first);
if (iter == this->style_map_->end()) {
std::pair<std::string, std::shared_ptr<DomValue>> pair = {v.first, std::make_shared<DomValue>(*v.second)};
this->style_map_->insert(pair);
continue;
}
if (v.second->IsObject() && iter->second->IsObject()) {
this->UpdateObjectStyle(*iter->second, *v.second);
} else {
iter->second = std::make_shared<DomValue>(*v.second);
}
}
}
void DomNode::UpdateObjectStyle(DomValue& style_map, const DomValue& update_style) {
TDF_BASE_DCHECK(style_map.IsObject());
TDF_BASE_DCHECK(update_style.IsObject());
@@ -42,7 +42,7 @@ void OnDestroyNativeRenderProvider(JNIEnv* j_env,
void UpdateRootSize(JNIEnv* j_env, jobject j_obj, jint j_instance_id,
jfloat width, jfloat height);
void UpdateNodeSize(JNIEnv* j_env, jobject j_obj, jint j_node_id,
void UpdateNodeSize(JNIEnv* j_env, jobject j_obj, jint j_instance_id, jint j_node_id,
jfloat width, jfloat height);
void OnReceivedEvent(JNIEnv *j_env, jobject j_object,
@@ -53,7 +53,7 @@ REGISTER_JNI("com/tencent/renderer/NativeRenderProvider",
REGISTER_JNI("com/tencent/renderer/NativeRenderProvider",
"updateNodeSize",
"(IFF)V",
"(IIFF)V",
UpdateNodeSize)
REGISTER_JNI("com/tencent/renderer/NativeRenderProvider",
@@ -102,9 +102,36 @@ void UpdateRootSize(JNIEnv *j_env, jobject j_object, jint j_instance_id,
dom_manager->DoLayout();
}
void UpdateNodeSize(JNIEnv *j_env, jobject j_object, jint j_node_id,
void UpdateNodeSize(JNIEnv *j_env, jobject j_object, jint j_instance_id, jint j_node_id,
jfloat j_width, jfloat j_height) {
std::shared_ptr<HippyRenderManager> render_manager = HippyRenderManager::Find(
static_cast<int32_t>(j_instance_id));
if (!render_manager) {
TDF_BASE_DLOG(WARNING) << "UpdateNodeSize j_instance_id invalid";
return;
}
std::shared_ptr<DomManager> dom_manager = render_manager->GetDomManager();
if (dom_manager == nullptr) {
TDF_BASE_DLOG(WARNING) << "UpdateNodeSize dom_manager is nullptr";
return;
}
auto node = dom_manager->GetNode(hippy::base::checked_numeric_cast<jlong, uint32_t>(j_node_id));
if (node == nullptr) {
TDF_BASE_DLOG(WARNING) << "UpdateNodeSize DomNode not found for id: " << j_node_id;
return;
}
std::unordered_map<std::string, std::shared_ptr<DomValue>> update_style;
std::shared_ptr<DomValue> width =
std::make_shared<DomValue>(hippy::base::checked_numeric_cast<jfloat, double>(j_width));
std::shared_ptr<DomValue> height =
std::make_shared<DomValue>(hippy::base::checked_numeric_cast<jfloat, double>(j_height));
update_style.insert({"width", width});
update_style.insert({"height", height});
node->UpdateDomNodeStyle(update_style);
node->ParseLayoutStyleInfo();
}
void DoCallBack(JNIEnv *j_env, jobject j_object,
@@ -278,7 +278,7 @@ public class NativeRenderProvider {
}
public void onSizeChanged(int nodeId, int width, int height) {
updateNodeSize(nodeId, PixelUtil.px2dp(width), PixelUtil.px2dp(height));
updateNodeSize(mInstanceId, nodeId, PixelUtil.px2dp(width), PixelUtil.px2dp(height));
}
/**
@@ -361,11 +361,12 @@ public class NativeRenderProvider {
* Updates the size to the specified node, such as modal node, should set new window size before
* layout.
*
* @param instanceId the unique id of native (C++) render manager
* @param nodeId the dom node id
* @param width new width of node, use dp unit
* @param height new height of node, use dp unit
*/
private native void updateNodeSize(int nodeId, float width, float height);
private native void updateNodeSize(int instanceId, int nodeId, float width, float height);
/**
* Dispatch event generated by native renderer to (C++) dom manager.