From c15868ff9894d342d2b12b2269217cd11b5da8a1 Mon Sep 17 00:00:00 2001 From: caocan Date: Sun, 23 Apr 2023 18:57:20 +0800 Subject: [PATCH] fixed 794d2cc from https://gitee.com/cc520bf/arkui_ace_engine/pulls/12495 fix width and height in preview Signed-off-by: caocan Change-Id: I779a3eb33124e6a8a0a780ded8ad7bce3558dad3 --- .../core/components_ng/base/frame_node.cpp | 40 ++++++++++++++++++- .../core/components_ng/base/frame_node.h | 1 + .../components_ng/property/measure_property.h | 15 +++++++ 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/frameworks/core/components_ng/base/frame_node.cpp b/frameworks/core/components_ng/base/frame_node.cpp index eb20524ff36..099ff63ca12 100644 --- a/frameworks/core/components_ng/base/frame_node.cpp +++ b/frameworks/core/components_ng/base/frame_node.cpp @@ -42,6 +42,10 @@ namespace { constexpr double VISIBLE_RATIO_MIN = 0.0; constexpr double VISIBLE_RATIO_MAX = 1.0; +#if defined(PREVIEW) +constexpr int32_t SUBSTR_LENGTH = 3; +const char DIMENSION_UNIT_VP[] = "vp"; +#endif } // namespace namespace OHOS::Ace::NG { FrameNode::FrameNode(const std::string& tag, int32_t nodeId, const RefPtr& pattern, bool isRoot) @@ -241,6 +245,40 @@ void FrameNode::TouchToJsonValue(std::unique_ptr& json) const json->Put("responseRegion", jsArr); } +void FrameNode::GeometryNodeToJsonValue(std::unique_ptr& json) const +{ +#if defined(PREVIEW) + bool hasIdealWidth = false; + bool hasIdealHeight = false; + if (layoutProperty_ && layoutProperty_->GetCalcLayoutConstraint()) { + auto selfIdealSize = layoutProperty_->GetCalcLayoutConstraint()->selfIdealSize; + hasIdealWidth = selfIdealSize.has_value() && selfIdealSize.value().Width().has_value(); + hasIdealHeight = selfIdealSize.has_value() && selfIdealSize.value().Height().has_value(); + } + + auto jsonSize = json->GetValue("size"); + if (!hasIdealWidth) { + auto idealWidthVpStr = std::to_string(Dimension(geometryNode_->GetFrameSize().Width()).ConvertToVp()); + auto widthStr = + (idealWidthVpStr.substr(0, idealWidthVpStr.find(".") + SUBSTR_LENGTH) + DIMENSION_UNIT_VP).c_str(); + json->Put("width", widthStr); + if (jsonSize) { + jsonSize->Put("width", widthStr); + } + } + + if (!hasIdealHeight) { + auto idealHeightVpStr = std::to_string(Dimension(geometryNode_->GetFrameSize().Height()).ConvertToVp()); + auto heightStr = + (idealHeightVpStr.substr(0, idealHeightVpStr.find(".") + SUBSTR_LENGTH) + DIMENSION_UNIT_VP).c_str(); + json->Put("height", heightStr); + if (jsonSize) { + jsonSize->Put("height", heightStr); + } + } +#endif +} + void FrameNode::ToJsonValue(std::unique_ptr& json) const { if (renderContext_) { @@ -251,13 +289,13 @@ void FrameNode::ToJsonValue(std::unique_ptr& json) const ACE_PROPERTY_TO_JSON_VALUE(layoutProperty_, LayoutProperty); ACE_PROPERTY_TO_JSON_VALUE(paintProperty_, PaintProperty); ACE_PROPERTY_TO_JSON_VALUE(pattern_, Pattern); - ACE_PROPERTY_TO_JSON_VALUE(geometryNode_, Pattern); if (eventHub_) { eventHub_->ToJsonValue(json); } FocusToJsonValue(json); MouseToJsonValue(json); TouchToJsonValue(json); + GeometryNodeToJsonValue(json); json->Put("id", propInspectorId_.value_or("").c_str()); } diff --git a/frameworks/core/components_ng/base/frame_node.h b/frameworks/core/components_ng/base/frame_node.h index 1d91b1b18ea..521a713076a 100644 --- a/frameworks/core/components_ng/base/frame_node.h +++ b/frameworks/core/components_ng/base/frame_node.h @@ -336,6 +336,7 @@ private: void FocusToJsonValue(std::unique_ptr& json) const; void MouseToJsonValue(std::unique_ptr& json) const; void TouchToJsonValue(std::unique_ptr& json) const; + void GeometryNodeToJsonValue(std::unique_ptr& json) const; HitTestMode GetHitTestMode() const override; bool GetTouchable() const; diff --git a/frameworks/core/components_ng/property/measure_property.h b/frameworks/core/components_ng/property/measure_property.h index a8e6bfeb4e1..649034ac6fa 100644 --- a/frameworks/core/components_ng/property/measure_property.h +++ b/frameworks/core/components_ng/property/measure_property.h @@ -229,6 +229,21 @@ struct MeasureProperty { jsonSize->Put("width", width.c_str()); jsonSize->Put("height", height.c_str()); json->Put("size", jsonSize); +#else + auto jsonSize = JsonUtil::Create(true); + if (selfIdealSize.has_value()) { + if (selfIdealSize.value().Width().has_value()) { + auto widthStr = selfIdealSize.value().Width().value().ToString(); + json->Put("width", widthStr.c_str()); + jsonSize->Put("width", widthStr.c_str()); + } + if (selfIdealSize.value().Height().has_value()) { + auto heightStr = selfIdealSize.value().Height().value().ToString(); + json->Put("height", heightStr.c_str()); + jsonSize->Put("height", heightStr.c_str()); + } + } + json->Put("size", jsonSize); #endif auto jsonConstraintSize = JsonUtil::Create(true); -- Gitee