1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "core/components_v2/inspector/inspector_composed_element.h"
17 
18 #include <atomic>
19 
20 #include "base/log/dump_log.h"
21 #include "base/utils/system_properties.h"
22 #include "base/utils/utils.h"
23 #include "core/common/ace_application_info.h"
24 #include "core/components/box/box_element.h"
25 #include "core/components/common/layout/constants.h"
26 #include "core/components/coverage/render_coverage.h"
27 #include "core/components/display/render_display.h"
28 #include "core/components/flex/flex_item_element.h"
29 #include "core/components/flex/render_flex_item.h"
30 #include "core/components/focusable/focusable_element.h"
31 #include "core/components/popup/popup_element_v2.h"
32 #include "core/components/text/render_text.h"
33 #include "core/components/text/text_element.h"
34 #include "core/components/transform/transform_element.h"
35 #include "core/components_v2/inspector/inspector_composed_component.h"
36 #include "core/components_v2/inspector/inspector_constants.h"
37 #include "core/components_v2/inspector/utils.h"
38 #include "core/pipeline/base/component.h"
39 #include "core/pipeline/base/composed_component.h"
40 
41 namespace OHOS::Ace::V2 {
42 
43 namespace {
44 
45 constexpr uint32_t WINDOW_BLUR_STYLE_ENUM_OFFSET = 100;
46 
47 const char* VISIBLE_TYPE[] = { "Visibility.Visible", "Visibility.Hidden", "Visibility.None" };
48 
49 const char* ITEM_ALIGN[] = { "ItemAlign.Auto", "ItemAlign.Start", "ItemAlign.Center", "ItemAlign.End",
50     "ItemAlign.Stretch", "ItemAlign.Baseline" };
51 
52 // NONE translate to Solid
53 const char* BORDER_STYLE[] = {
54     "BorderStyle.Solid",
55     "BorderStyle.Dashed",
56     "BorderStyle.Dotted",
57     "BorderStyle.Solid",
58 };
59 
60 const char* WINDOW_BLUR_STYLE[] = { "BlurStyle.SmallLight", "BlurStyle.MediumLight", "BlurStyle.LargeLight",
61     "BlurStyle.XlargeLight", "BlurStyle.SmallDark", "BlurStyle.MediumDark", "BlurStyle.LargeDark",
62     "BlurStyle.XlargeDark" };
63 
64 const char* ALIGNMENT_TYPE[3][3] = { { "Alignment.TopStart", "Alignment.Start", "Alignment.BottomStart" },
65     { "Alignment.Top", "Alignment.Center", "Alignment.Bottom" },
66     { "Alignment.TopEnd", "Alignment.End", "Alignment.BottomEnd" } };
67 
68 const char* GRID_SIZE_TYPE[] = { "default", "sx", "sm", "md", "lg" };
69 
70 constexpr const char* TEXT_DIRECTION[] = { "Direction.Ltr", "Direction.Rtl", "Direction.Inherit", "Direction.Auto" };
71 
72 constexpr const char* BASIC_SHAPE_TYPE[] { "None", "Inset", "Circle", "Ellipse", "Polygon", "Path", "Rect" };
73 
74 constexpr const char* HIT_TEST_BEHAVIOR[] = {
75     "HitTestMode.Default",
76     "HitTestMode.Block",
77     "HitTestMode.Transparent",
78     "HitTestMode.None",
79 };
80 
81 const std::unordered_map<std::string, DoubleJsonFunc> CREATE_JSON_DOUBLE_MAP {
__anon58d2e1330202() 82     { "opacity", [](const InspectorNode& inspector) { return inspector.GetOpacity(); } },
__anon58d2e1330302() 83     { "flexGrow", [](const InspectorNode& inspector) { return inspector.GetFlexGrow(); } },
__anon58d2e1330402() 84     { "flexShrink", [](const InspectorNode& inspector) { return inspector.GetFlexShrink(); } },
__anon58d2e1330502() 85     { "gridOffset", [](const InspectorNode& inspector) { return inspector.GetGridOffset(); } },
__anon58d2e1330602() 86     { "blur", [](const InspectorNode& inspector) { return inspector.GetBlur(); } },
__anon58d2e1330702() 87     { "backdropBlur", [](const InspectorNode& inspector) { return inspector.GetBackDropBlur(); } },
__anon58d2e1330802() 88     { "aspectRatio", [](const InspectorNode& inspector) { return inspector.GetAspectRatio(); } },
__anon58d2e1330902() 89     { "brightness", [](const InspectorNode& inspector) { return inspector.GetBrightness(); } },
__anon58d2e1330a02() 90     { "saturate", [](const InspectorNode& inspector) { return inspector.GetSaturate(); } },
__anon58d2e1330b02() 91     { "contrast", [](const InspectorNode& inspector) { return inspector.GetContrast(); } },
__anon58d2e1330c02() 92     { "invert", [](const InspectorNode& inspector) { return inspector.GetInvert(); } },
__anon58d2e1330d02() 93     { "sepia", [](const InspectorNode& inspector) { return inspector.GetSepia(); } },
__anon58d2e1330e02() 94     { "grayscale", [](const InspectorNode& inspector) { return inspector.GetGrayScale(); } },
__anon58d2e1330f02() 95     { "hueRotate", [](const InspectorNode& inspector) { return inspector.GetHueRotate(); } },
96 };
97 
98 const std::unordered_map<std::string, StringJsonFunc> CREATE_JSON_STRING_MAP {
__anon58d2e1331002() 99     { "visibility", [](const InspectorNode& inspector) { return inspector.GetVisibility(); } },
__anon58d2e1331102() 100     { "alignSelf", [](const InspectorNode& inspector) { return inspector.GetAlignSelf(); } },
__anon58d2e1331202() 101     { "clip", [](const InspectorNode& inspector) { return inspector.GetClip(); } },
__anon58d2e1331302() 102     { "constraintSize", [](const InspectorNode& inspector) { return inspector.GetConstraintSize(); } },
__anon58d2e1331402() 103     { "borderColor", [](const InspectorNode& inspector) { return inspector.GetBorderColor(); } },
__anon58d2e1331502() 104     { "borderStyle", [](const InspectorNode& inspector) { return inspector.GetBorderStyle(); } },
__anon58d2e1331602() 105     { "borderWidth", [](const InspectorNode& inspector) { return inspector.GetBorderWidth(); } },
__anon58d2e1331702() 106     { "borderRadius", [](const InspectorNode& inspector) { return inspector.GetBorderRadius(); } },
__anon58d2e1331802() 107     { "backgroundImage", [](const InspectorNode& inspector) { return inspector.GetBackgroundImage(); } },
__anon58d2e1331902() 108     { "backgroundColor", [](const InspectorNode& inspector) { return inspector.GetBackgroundColor(); } },
__anon58d2e1331a02() 109     { "flexBasis", [](const InspectorNode& inspector) { return inspector.GetFlexBasis(); } },
__anon58d2e1331b02() 110     { "width", [](const InspectorNode& inspector) { return inspector.GetWidth(); } },
__anon58d2e1331c02() 111     { "height", [](const InspectorNode& inspector) { return inspector.GetHeight(); } },
__anon58d2e1331d02() 112     { "align", [](const InspectorNode& inspector) { return inspector.GetAlign(); } },
__anon58d2e1331e02() 113     { "direction", [](const InspectorNode& inspector) { return inspector.GetDirectionStr(); } },
__anon58d2e1331f02() 114     { "bindPopup", [](const InspectorNode& inspector) { return inspector.GetBindPopup(); } },
__anon58d2e1332002() 115     { "bindContextMenu", [](const InspectorNode& inspector) { return inspector.GetBindContextMenu(); } },
__anon58d2e1332102() 116     { "colorBlend", [](const InspectorNode& inspector) { return inspector.GetColorBlend(); } },
__anon58d2e1332202() 117     { "backgroundImageSize", [](const InspectorNode& inspector) { return inspector.GetBackgroundImageSize(); } },
118     { "backgroundImagePosition",
__anon58d2e1332302() 119         [](const InspectorNode& inspector) { return inspector.GetBackgroundImagePosition(); } },
__anon58d2e1332402() 120     { "padding", [](const InspectorNode& inspector) { return inspector.GetPadding(); } },
__anon58d2e1332502() 121     { "margin", [](const InspectorNode& inspector) { return inspector.GetAllMargin(); } },
__anon58d2e1332602() 122     { "hitTestBehavior", [](const InspectorNode& inspector) { return inspector.GetHitTestBehaviorStr(); } },
123 };
124 
125 const std::unordered_map<std::string, BoolJsonFunc> CREATE_JSON_BOOL_MAP {
__anon58d2e1332702() 126     { "enabled", [](const InspectorNode& inspector) { return inspector.GetEnabled(); } },
__anon58d2e1332802() 127     { "focusable", [](const InspectorNode& inspector) { return inspector.GetFocusable(); } },
__anon58d2e1332902() 128     { "touchable", [](const InspectorNode& inspector) { return inspector.GetTouchable(); } },
129 };
130 
131 const std::unordered_map<std::string, IntJsonFunc> CREATE_JSON_INT_MAP {
__anon58d2e1332a02() 132     { "zIndex", [](const InspectorNode& inspector) { return inspector.GetZIndex(); } },
__anon58d2e1332b02() 133     { "gridSpan", [](const InspectorNode& inspector) { return inspector.GetGridSpan(); } },
__anon58d2e1332c02() 134     { "layoutWeight", [](const InspectorNode& inspector) { return inspector.GetLayoutWeight(); } },
__anon58d2e1332d02() 135     { "displayPriority", [](const InspectorNode& inspector) { return inspector.GetDisplayPriority(); } },
136 };
137 
138 const std::unordered_map<std::string, JsonValueJsonFunc> CREATE_JSON_JSON_VALUE_MAP {
__anon58d2e1332e02() 139     { "shadow", [](const InspectorNode& inspector) { return inspector.GetShadow(); } },
__anon58d2e1332f02() 140     { "position", [](const InspectorNode& inspector) { return inspector.GetPosition(); } },
__anon58d2e1333002() 141     { "offset", [](const InspectorNode& inspector) { return inspector.GetOffset(); } },
__anon58d2e1333102() 142     { "size", [](const InspectorNode& inspector) { return inspector.GetSize(); } },
__anon58d2e1333202() 143     { "useSizeType", [](const InspectorNode& inspector) { return inspector.GetUseSizeType(); } },
__anon58d2e1333302() 144     { "rotate", [](const InspectorNode& inspector) { return inspector.GetRotate(); } },
__anon58d2e1333402() 145     { "scale", [](const InspectorNode& inspector) { return inspector.GetScale(); } },
__anon58d2e1333502() 146     { "transform", [](const InspectorNode& inspector) { return inspector.GetTransform(); } },
__anon58d2e1333602() 147     { "translate", [](const InspectorNode& inspector) { return inspector.GetTranslate(); } },
__anon58d2e1333702() 148     { "markAnchor", [](const InspectorNode& inspector) { return inspector.GetMarkAnchor(); } },
__anon58d2e1333802() 149     { "mask", [](const InspectorNode& inspector) { return inspector.GetMask(); } },
__anon58d2e1333902() 150     { "overlay", [](const InspectorNode& inspector) { return inspector.GetOverlay(); } },
__anon58d2e1333a02() 151     { "border", [](const InspectorNode& inspector) { return inspector.GetUnifyBorder(); } },
__anon58d2e1333b02() 152     { "linearGradient", [](const InspectorNode& inspector) { return inspector.GetLinearGradient(); } },
__anon58d2e1333c02() 153     { "sweepGradient", [](const InspectorNode& inspector) { return inspector.GetSweepGradient(); } },
__anon58d2e1333d02() 154     { "radialGradient", [](const InspectorNode& inspector) { return inspector.GetRadialGradient(); } },
155 };
156 
157 const std::unordered_map<std::string, BoolJsonFunc> CREATE_XTS_BOOL_MAP {
__anon58d2e1333e02() 158     { "clickable", [](const InspectorNode& inspector) { return inspector.GetClickable(); } },
__anon58d2e1333f02() 159     { "checkable", [](const InspectorNode& inspector) { return inspector.GetCheckable(); } },
__anon58d2e1334002() 160     { "scrollable", [](const InspectorNode& inspector) { return inspector.GetScrollable(); } },
__anon58d2e1334102() 161     { "long-clickable", [](const InspectorNode& inspector) { return inspector.GetLongClickable(); } },
__anon58d2e1334202() 162     { "selected", [](const InspectorNode& inspector) { return inspector.IsSelected(); } },
__anon58d2e1334302() 163     { "password", [](const InspectorNode& inspector) { return inspector.IsPassword(); } },
__anon58d2e1334402() 164     { "checked", [](const InspectorNode& inspector) { return inspector.IsChecked(); } },
__anon58d2e1334502() 165     { "focused", [](const InspectorNode& inspector) { return inspector.IsFocused(); } },
166 };
167 
168 const std::unordered_map<std::string, IntJsonFunc> CREATE_XTS_INT_MAP {
__anon58d2e1334602() 169     { "layoutPriority", [](const InspectorNode& inspector) { return inspector.GetLayoutPriority(); } },
170 };
171 
172 const std::unordered_map<std::string, JsonValueJsonFunc> CREATE_XTS_JSON_VALUE_MAP {
__anon58d2e1334702() 173     { "windowBlur", [](const InspectorNode& inspector) { return inspector.GetWindowBlur(); } },
__anon58d2e1334802() 174     { "useAlign", [](const InspectorNode& inspector) { return inspector.GetUseAlign(); } },
175 };
176 
177 constexpr double VISIBLE_RATIO_MIN = 0.0;
178 constexpr double VISIBLE_RATIO_MAX = 1.0;
179 
180 }; // namespace
181 
InspectorComposedElement(const ComposeId & id)182 InspectorComposedElement::InspectorComposedElement(const ComposeId& id) : ComposedElement(id) {}
183 
~InspectorComposedElement()184 InspectorComposedElement::~InspectorComposedElement()
185 {
186     auto popupElement = GetPopupElement();
187     if (popupElement && popupElement->GetPopupComponent()) {
188         auto popupComponent = popupElement->GetPopupComponent();
189         if (popupComponent && popupComponent->GetPopupController()) {
190             popupComponent->GetPopupController()->CancelPopup();
191         }
192     }
193 
194     if (inspectorId_ == -1) {
195         return;
196     }
197     accessibilityNode_.Reset();
198     RemoveInspectorNode(inspectorId_);
199 }
200 
OnInactive()201 void InspectorComposedElement::OnInactive()
202 {
203     accessibilityNode_.Reset();
204     RemoveInspectorNode(inspectorId_);
205     inspectorId_ = -1;
206 }
207 
OnActive()208 void InspectorComposedElement::OnActive()
209 {
210     inspectorId_ = std::stoi(id_);
211 }
212 
GetPopupElement() const213 RefPtr<PopupElementV2> InspectorComposedElement::GetPopupElement() const
214 {
215     auto coverageElement = GetContentElement<ComponentGroupElement>(ComponentGroupElement::TypeId(), false);
216     RefPtr<PopupElementV2> popupElement = nullptr;
217     if (coverageElement) {
218         for (const auto& element : coverageElement->GetChildren()) {
219             if (AceType::DynamicCast<PopupElementV2>(element)) {
220                 popupElement = AceType::DynamicCast<PopupElementV2>(element);
221             }
222         }
223     }
224 
225     return popupElement;
226 }
227 
GetElementChildBySlot(const RefPtr<Element> & element,int32_t & slot) const228 RefPtr<Element> InspectorComposedElement::GetElementChildBySlot(const RefPtr<Element>& element, int32_t& slot) const
229 {
230     if (!element) {
231         return nullptr;
232     }
233     auto child = element->GetChildBySlot(slot);
234     if (!child) {
235         slot = DEFAULT_ELEMENT_SLOT;
236         child = element->GetChildBySlot(slot);
237     }
238     return child;
239 }
240 
GetInspectorComposedElementParent(const RefPtr<Element> & element) const241 RefPtr<Element> InspectorComposedElement::GetInspectorComposedElementParent(const RefPtr<Element>& element) const
242 {
243     if (!element) {
244         return nullptr;
245     }
246     for (const auto& child : element->GetChildren()) {
247         auto inspectorComposedElement = AceType::DynamicCast<InspectorComposedElement>(child);
248         if (inspectorComposedElement) {
249             return element;
250         }
251         auto element_ = GetInspectorComposedElementParent(child);
252         if (element_) {
253             return element_;
254         }
255     }
256     return nullptr;
257 }
258 
ToJsonObject() const259 std::unique_ptr<JsonValue> InspectorComposedElement::ToJsonObject() const
260 {
261     auto resultJson = JsonUtil::Create(true);
262     for (const auto& value : CREATE_JSON_DOUBLE_MAP) {
263         resultJson->Put(value.first.c_str(), value.second(*this));
264     }
265     for (const auto& value : CREATE_JSON_STRING_MAP) {
266         resultJson->Put(value.first.c_str(), value.second(*this).c_str());
267     }
268     for (const auto& value : CREATE_JSON_BOOL_MAP) {
269         resultJson->Put(value.first.c_str(), value.second(*this));
270     }
271     for (const auto& value : CREATE_JSON_INT_MAP) {
272         resultJson->Put(value.first.c_str(), value.second(*this));
273     }
274 
275     for (const auto& value : CREATE_JSON_JSON_VALUE_MAP) {
276         resultJson->Put(value.first.c_str(), value.second(*this));
277     }
278 #if !defined(PREVIEW)
279     for (const auto& value : CREATE_XTS_BOOL_MAP) {
280         resultJson->Put(value.first.c_str(), value.second(*this));
281     }
282     for (const auto& value : CREATE_XTS_INT_MAP) {
283         resultJson->Put(value.first.c_str(), value.second(*this));
284     }
285 
286     for (const auto& value : CREATE_XTS_JSON_VALUE_MAP) {
287         resultJson->Put(value.first.c_str(), value.second(*this));
288     }
289 #endif
290     return resultJson;
291 }
292 
Prepare(const WeakPtr<Element> & weakParent)293 void InspectorComposedElement::Prepare(const WeakPtr<Element>& weakParent)
294 {
295     accessibilityEnabled_ = false;
296 #if defined(PREVIEW)
297     accessibilityEnabled_ = true;
298 #else
299     if (AceApplicationInfo::GetInstance().IsAccessibilityEnabled() || SystemProperties::GetAccessibilityEnabled()) {
300         accessibilityEnabled_ = true;
301     }
302 #endif
303     if (accessibilityEnabled_) {
304         auto parent = weakParent.Upgrade();
305         RefPtr<InspectorComposedElement> inspectorParent;
306         while (parent) {
307             inspectorParent = DynamicCast<InspectorComposedElement>(parent);
308             if (inspectorParent) {
309                 break;
310             }
311             parent = parent->GetElementParent().Upgrade();
312         }
313         if (inspectorParent) {
314             inspectorParentId_ = inspectorParent->inspectorId_;
315         }
316     }
317     AddComposedComponentId();
318 }
319 
Update()320 void InspectorComposedElement::Update()
321 {
322     ComposedElement::Update();
323     auto component = DynamicCast<V2::InspectorComposedComponent>(component_);
324     if (!component) {
325         LOGE("fail to update due to component is null");
326         return;
327     }
328     SetElementId(component->GetElementId());
329 
330     auto inspectorFunctionImpl = component->GetInspectorFunctionImpl();
331     inspectorFunctionImpl->SetUpdateEventInfoImpl([weak = WeakClaim(this)](BaseEventInfo& info) {
332         auto composedElement = weak.Upgrade();
333         if (composedElement) {
334             composedElement->UpdateEventTarget(info);
335         }
336     });
337     if (accessibilityNode_) {
338         accessibilityNode_->SetAccessible(component->IsAccessibilityGroup());
339         accessibilityNode_->SetAccessibilityLabel(component->GetAccessibilityText());
340         accessibilityNode_->SetAccessibilityHint(component->GetAccessibilityDescription());
341         accessibilityNode_->SetImportantForAccessibility(component->GetAccessibilityImportance());
342         accessibilityNode_->SetFocusChangeEventMarker(component->GetAccessibilityEvent());
343     }
344 }
345 
CanUpdate(const RefPtr<Component> & newComponent)346 bool InspectorComposedElement::CanUpdate(const RefPtr<Component>& newComponent)
347 {
348     auto component = AceType::DynamicCast<InspectorComposedComponent>(newComponent);
349     if (!component) {
350         return false;
351     }
352     return GetInspectorTag() == component->GetName();
353 }
354 
AddComposedComponentId()355 void InspectorComposedElement::AddComposedComponentId()
356 {
357     auto context = context_.Upgrade();
358     if (context == nullptr) {
359         LOGW("get context failed");
360         return;
361     }
362     auto accessibilityManager = context->GetAccessibilityManager();
363     if (!accessibilityManager) {
364         LOGW("get AccessibilityManager failed");
365         return;
366     }
367     accessibilityManager->AddComposedElement(std::to_string(inspectorId_), AceType::Claim(this));
368     if (accessibilityEnabled_) {
369         accessibilityNode_ = InspectorComposedComponent::CreateAccessibilityNode(
370             inspectorTag_, inspectorId_, inspectorParentId_, GetRenderSlot());
371         if (accessibilityNode_) {
372             accessibilityNode_->SetJsComponentId(key_);
373         }
374     }
375 }
376 
RemoveInspectorNode(int32_t id)377 void InspectorComposedElement::RemoveInspectorNode(int32_t id)
378 {
379     auto context = context_.Upgrade();
380     if (context == nullptr) {
381         LOGW("get context failed");
382         return;
383     }
384     auto accessibilityManager = context->GetAccessibilityManager();
385     if (!accessibilityManager) {
386         LOGW("get AccessibilityManager failed");
387         return;
388     }
389     accessibilityManager->RemoveComposedElementById(std::to_string(id));
390     if (accessibilityEnabled_) {
391         accessibilityManager->RemoveAccessibilityNodeById(id);
392     }
393 }
394 
GetInspectorNode(IdType typeId,bool isForward) const395 RefPtr<RenderNode> InspectorComposedElement::GetInspectorNode(IdType typeId, bool isForward) const
396 {
397     if (isForward) {
398         auto parent = GetElementParent().Upgrade();
399         while (parent) {
400             if (AceType::TypeId(parent) == typeId) {
401                 return parent->GetRenderNode();
402             }
403             parent = parent->GetElementParent().Upgrade();
404         }
405         return nullptr;
406     }
407     auto child = children_.empty() ? nullptr : children_.front();
408     while (child) {
409         if (AceType::TypeId(child) == typeId) {
410             return child->GetRenderNode();
411         }
412         child = child->GetChildren().empty() ? nullptr : child->GetChildren().front();
413     }
414     return nullptr;
415 }
416 
GetAccessibilityNode() const417 RefPtr<AccessibilityNode> InspectorComposedElement::GetAccessibilityNode() const
418 {
419     auto context = context_.Upgrade();
420     if (context == nullptr) {
421         LOGW("get context failed");
422         return nullptr;
423     }
424     auto accessibilityManager = context->GetAccessibilityManager();
425     if (!accessibilityManager) {
426         LOGW("get AccessibilityManager failed");
427         return nullptr;
428     }
429     return accessibilityManager->GetAccessibilityNodeById(StringUtils::StringToInt(id_));
430 }
431 
GetRenderBox() const432 RefPtr<RenderBox> InspectorComposedElement::GetRenderBox() const
433 {
434     auto node = GetInspectorNode(BoxElement::TypeId());
435     if (!node) {
436         return nullptr;
437     }
438     return AceType::DynamicCast<RenderBox>(node);
439 }
440 
GetWidth() const441 std::string InspectorComposedElement::GetWidth() const
442 {
443     auto render = GetRenderBox();
444     if (render) {
445         Dimension value = render->GetWidthDimension();
446         if (value.Value() == -1) {
447             return "-";
448         }
449         return value.ToString();
450     }
451     return "-";
452 }
453 
GetHeight() const454 std::string InspectorComposedElement::GetHeight() const
455 {
456     auto render = GetRenderBox();
457     if (render) {
458         Dimension value = render->GetHeightDimension();
459         if (value.Value() == -1) {
460             return "-";
461         }
462         return value.ToString();
463     }
464     return "-";
465 }
466 
GetSize() const467 std::unique_ptr<JsonValue> InspectorComposedElement::GetSize() const
468 {
469     auto jsonValue = JsonUtil::Create(true);
470     jsonValue->Put("width", GetWidth().c_str());
471     jsonValue->Put("height", GetHeight().c_str());
472     return jsonValue;
473 }
474 
GetPadding() const475 std::string InspectorComposedElement::GetPadding() const
476 {
477     auto render = GetRenderBox();
478     if (render) {
479         auto top = render->GetPadding(DimensionHelper(&Edge::SetTop, &Edge::Top));
480         auto right = render->GetPadding(DimensionHelper(&Edge::SetRight, &Edge::Right));
481         auto bottom = render->GetPadding(DimensionHelper(&Edge::SetBottom, &Edge::Bottom));
482         auto left = render->GetPadding(DimensionHelper(&Edge::SetLeft, &Edge::Left));
483         if (top == right && right == bottom && bottom == left) {
484             return top.ToString();
485         } else {
486             auto jsonValue = JsonUtil::Create(true);
487             jsonValue->Put("top", top.ToString().c_str());
488             jsonValue->Put("right", right.ToString().c_str());
489             jsonValue->Put("bottom", bottom.ToString().c_str());
490             jsonValue->Put("left", left.ToString().c_str());
491             return jsonValue->ToString();
492         }
493     }
494     return "0.0";
495 }
496 
GetAllMargin() const497 std::string InspectorComposedElement::GetAllMargin() const
498 {
499     auto render = GetRenderBox();
500     if (render) {
501         auto top = render->GetMargin(DimensionHelper(&Edge::SetTop, &Edge::Top));
502         auto right = render->GetMargin(DimensionHelper(&Edge::SetRight, &Edge::Right));
503         auto bottom = render->GetMargin(DimensionHelper(&Edge::SetBottom, &Edge::Bottom));
504         auto left = render->GetMargin(DimensionHelper(&Edge::SetLeft, &Edge::Left));
505         if (top == right && right == bottom && bottom == left) {
506             return top.ToString().c_str();
507         } else {
508             auto jsonValue = JsonUtil::Create(true);
509             jsonValue->Put("top", top.ToString().c_str());
510             jsonValue->Put("right", right.ToString().c_str());
511             jsonValue->Put("bottom", bottom.ToString().c_str());
512             jsonValue->Put("left", left.ToString().c_str());
513             return jsonValue->ToString();
514         }
515     }
516     return "0.0";
517 }
518 
GetMargin(OHOS::Ace::AnimatableType type) const519 Dimension InspectorComposedElement::GetMargin(OHOS::Ace::AnimatableType type) const
520 {
521     auto render = GetRenderBox();
522     if (render) {
523         if (type == AnimatableType::PROPERTY_MARGIN_LEFT) {
524             return render->GetMargin(DimensionHelper(&Edge::SetLeft, &Edge::Left));
525         } else if (type == AnimatableType::PROPERTY_MARGIN_TOP) {
526             return render->GetMargin(DimensionHelper(&Edge::SetTop, &Edge::Top));
527         } else if (type == AnimatableType::PROPERTY_MARGIN_RIGHT) {
528             return render->GetMargin(DimensionHelper(&Edge::SetRight, &Edge::Right));
529         } else if (type == AnimatableType::PROPERTY_MARGIN_BOTTOM) {
530             return render->GetMargin(DimensionHelper(&Edge::SetBottom, &Edge::Bottom));
531         }
532     }
533     return Dimension();
534 }
535 
GetConstraintSize() const536 std::string InspectorComposedElement::GetConstraintSize() const
537 {
538     LayoutParam layoutParam = LayoutParam(Size(), Size());
539     auto render = GetRenderBox();
540     if (render) {
541         layoutParam = render->GetConstraints();
542     }
543     auto jsonStr = JsonUtil::Create(true);
544     Dimension minWidth = Dimension(
545         PipelineBase::Px2VpWithCurrentDensity(layoutParam.GetMinSize().Width()), DimensionUnit::VP);
546     Dimension minHeight = Dimension(
547         PipelineBase::Px2VpWithCurrentDensity(layoutParam.GetMinSize().Height()), DimensionUnit::VP);
548     Dimension maxWidth = Dimension(
549         PipelineBase::Px2VpWithCurrentDensity(layoutParam.GetMaxSize().Width()), DimensionUnit::VP);
550     Dimension maxHeight = Dimension(
551         PipelineBase::Px2VpWithCurrentDensity(layoutParam.GetMaxSize().Height()), DimensionUnit::VP);
552     jsonStr->Put("minWidth", minWidth.ToString().c_str());
553     jsonStr->Put("minHeight", minHeight.ToString().c_str());
554     jsonStr->Put("maxWidth", maxWidth.ToString().c_str());
555     jsonStr->Put("maxHeight", maxHeight.ToString().c_str());
556     return jsonStr->ToString();
557 }
558 
GetLayoutPriority() const559 int32_t InspectorComposedElement::GetLayoutPriority() const
560 {
561     auto render = GetRenderBox();
562     if (render) {
563         return render->GetDisplayIndex();
564     }
565     return 0;
566 }
567 
GetLayoutWeight() const568 int32_t InspectorComposedElement::GetLayoutWeight() const
569 {
570     auto node = GetInspectorNode(FlexItemElement::TypeId());
571     if (!node) {
572         return 0;
573     }
574     auto render = AceType::DynamicCast<RenderFlexItem>(node);
575     if (render) {
576         return render->GetFlexWeight();
577     }
578     return 0;
579 }
580 
GetAlign() const581 std::string InspectorComposedElement::GetAlign() const
582 {
583     auto render = GetRenderBox();
584     if (render) {
585         auto align = render->GetAlign();
586         int32_t h = align.GetHorizontal() + 1;
587         int32_t v = align.GetVertical() + 1;
588         return ALIGNMENT_TYPE[h][v];
589     }
590     return ALIGNMENT_TYPE[1][1];
591 }
592 
GetDirectionStr() const593 std::string InspectorComposedElement::GetDirectionStr() const
594 {
595     auto render = GetRenderBox();
596     if (!render) {
597         return TEXT_DIRECTION[3];
598     }
599     auto value = static_cast<int32_t>(render->GetInspectorDirection());
600     auto length = static_cast<int32_t>(sizeof(TEXT_DIRECTION) / sizeof(TEXT_DIRECTION[0]));
601     if (value < length) {
602         return TEXT_DIRECTION[value];
603     }
604     return TEXT_DIRECTION[3];
605 }
606 
GetDirection() const607 TextDirection InspectorComposedElement::GetDirection() const
608 {
609     auto render = GetRenderBox();
610     if (render) {
611         return render->GetTextDirection();
612     }
613     return TextDirection::AUTO;
614 }
615 
GetBorderRadius() const616 std::string InspectorComposedElement::GetBorderRadius() const
617 {
618     auto value = GetBorder().TopLeftRadius().GetX().Value();
619     if (value == 0.0) {
620         return "0.0vp";
621     }
622     return GetBorder().TopLeftRadius().GetX().ToString();
623 }
624 
GetUnifyBorder() const625 std::unique_ptr<JsonValue> InspectorComposedElement::GetUnifyBorder() const
626 {
627     auto jsonValue = JsonUtil::Create(true);
628     jsonValue->Put("width", GetBorderWidth().c_str());
629     jsonValue->Put("color", GetBorderColor().c_str());
630     jsonValue->Put("radius", GetBorderRadius().c_str());
631     jsonValue->Put("style", GetBorderStyle().c_str());
632     return jsonValue;
633 }
634 
GetPosition() const635 std::unique_ptr<JsonValue> InspectorComposedElement::GetPosition() const
636 {
637     auto jsonValue = JsonUtil::Create(true);
638     auto node = GetInspectorNode(FlexItemElement::TypeId());
639     if (!node) {
640         jsonValue->Put("x", "0.0px");
641         jsonValue->Put("y", "0.0px");
642         return jsonValue;
643     }
644     auto render = AceType::DynamicCast<RenderFlexItem>(node);
645     if (render) {
646         PositionType type = render->GetPositionType();
647         if (type == PositionType::PTABSOLUTE) {
648             jsonValue->Put("x", render->GetLeft().ToString().c_str());
649             jsonValue->Put("y", render->GetTop().ToString().c_str());
650             return jsonValue;
651         }
652     }
653     jsonValue->Put("x", "0.0px");
654     jsonValue->Put("y", "0.0px");
655     return jsonValue;
656 }
657 
GetMarkAnchor() const658 std::unique_ptr<JsonValue> InspectorComposedElement::GetMarkAnchor() const
659 {
660     auto jsonValue = JsonUtil::Create(true);
661     auto node = GetInspectorNode(FlexItemElement::TypeId());
662     if (!node) {
663         jsonValue->Put("x", "0.0px");
664         jsonValue->Put("y", "0.0px");
665         return jsonValue;
666     }
667     auto render = AceType::DynamicCast<RenderFlexItem>(node);
668     if (render) {
669         jsonValue->Put("x", render->GetAnchorX().ToString().c_str());
670         jsonValue->Put("y", render->GetAnchorY().ToString().c_str());
671         return jsonValue;
672     }
673     jsonValue->Put("x", "0.0px");
674     jsonValue->Put("y", "0.0px");
675     return jsonValue;
676 }
677 
GetOffset() const678 std::unique_ptr<JsonValue> InspectorComposedElement::GetOffset() const
679 {
680     auto jsonValue = JsonUtil::Create(true);
681     auto node = GetInspectorNode(FlexItemElement::TypeId());
682     if (!node) {
683         jsonValue->Put("x", "0.0px");
684         jsonValue->Put("y", "0.0px");
685         return jsonValue;
686     }
687     auto render = AceType::DynamicCast<RenderFlexItem>(node);
688     if (render) {
689         PositionType type = render->GetPositionType();
690         if (type == PositionType::PTOFFSET) {
691             jsonValue->Put("x", render->GetLeft().ToString().c_str());
692             jsonValue->Put("y", render->GetTop().ToString().c_str());
693             return jsonValue;
694         }
695     }
696     jsonValue->Put("x", "0.0px");
697     jsonValue->Put("y", "0.0px");
698     return jsonValue;
699 }
700 
GetRect()701 std::string InspectorComposedElement::GetRect()
702 {
703     std::string strRec;
704     Rect rect = GetRenderRect();
705 
706     if (accessibilityNode_ && accessibilityNode_->GetParentNode()) {
707         auto parent = accessibilityNode_->GetParentNode();
708         if (parent->GetClipFlag()) {
709             rect = rect.Constrain(parent->GetRect());
710         }
711     }
712     if (accessibilityNode_ && GetClipFlag()) {
713         accessibilityNode_->SetClipFlagToChild(true);
714     }
715 
716     isRectValid_ = rect.IsValid();
717     if (!isRectValid_) {
718         rect.SetRect(0, 0, 0, 0);
719     }
720 
721     if (accessibilityNode_) {
722         auto render = AceType::DynamicCast<RenderTransform>(GetInspectorNode(TransformElement::TypeId()));
723         if (render || accessibilityNode_->GetMatrix4Flag()) {
724             if (render) {
725                 auto transformNow = render->GetTransformMatrix(render->GetTransitionPaintRect().GetOffset());
726                 accessibilityNode_->SetTransformToChild(transformNow);
727             }
728             Matrix4 transform = accessibilityNode_->GetMatrix4();
729             rect = accessibilityNode_->GetRectWithTransform(rect, transform);
730         }
731     }
732 
733     strRec = std::to_string(rect.Left())
734                  .append(",")
735                  .append(std::to_string(rect.Top()))
736                  .append(",")
737                  .append(std::to_string(rect.Width()))
738                  .append(",")
739                  .append(std::to_string(rect.Height()));
740     return strRec;
741 }
742 
GetParentRect() const743 Rect InspectorComposedElement::GetParentRect() const
744 {
745     auto parent = GetElementParent().Upgrade();
746     if (!parent) {
747         return Rect();
748     }
749     Rect parentRect = parent->GetRenderRect();
750     return parentRect;
751 }
752 
GetAspectRatio() const753 double InspectorComposedElement::GetAspectRatio() const
754 {
755     auto render = GetRenderBox();
756     if (render) {
757         return render->GetAspectRatio();
758     }
759     return 0.0;
760 }
761 
GetDisplayPriority() const762 int32_t InspectorComposedElement::GetDisplayPriority() const
763 {
764     auto node = GetInspectorNode(FlexItemElement::TypeId());
765     if (!node) {
766         return 1;
767     }
768     auto render = AceType::DynamicCast<RenderFlexItem>(node);
769     if (render) {
770         return render->GetDisplayIndex();
771     }
772     return 1;
773 }
774 
GetFlexBasis() const775 std::string InspectorComposedElement::GetFlexBasis() const
776 {
777     auto render = AceType::DynamicCast<RenderFlexItem>(GetInspectorNode(FlexItemElement::TypeId()));
778     if (render) {
779         auto flexBasis = render->GetFlexBasis();
780         return flexBasis.IsValid() ? render->GetFlexBasis().ToString() : "auto";
781     }
782     return "auto";
783 }
784 
GetFlexGrow() const785 double InspectorComposedElement::GetFlexGrow() const
786 {
787     auto render = AceType::DynamicCast<RenderFlexItem>(GetInspectorNode(FlexItemElement::TypeId()));
788     if (render) {
789         return render->GetFlexGrow();
790     }
791     return 0.0;
792 }
793 
GetFlexShrink() const794 double InspectorComposedElement::GetFlexShrink() const
795 {
796     auto render = AceType::DynamicCast<RenderFlexItem>(GetInspectorNode(FlexItemElement::TypeId()));
797     if (render) {
798         return render->GetFlexShrink();
799     }
800     return 0.0;
801 }
802 
GetAlignSelf() const803 std::string InspectorComposedElement::GetAlignSelf() const
804 {
805     auto render = AceType::DynamicCast<RenderFlexItem>(GetInspectorNode(FlexItemElement::TypeId()));
806     if (render) {
807         return ITEM_ALIGN[static_cast<int32_t>(render->GetAlignSelf())];
808     }
809     return ITEM_ALIGN[0];
810 }
811 
GetBorder() const812 Border InspectorComposedElement::GetBorder() const
813 {
814     auto render = GetRenderBox();
815     if (!render) {
816         return Border();
817     }
818     auto decoration = render->GetBackDecoration();
819     if (decoration) {
820         return decoration->GetBorder();
821     }
822     return Border();
823 }
824 
GetBorderStyle() const825 std::string InspectorComposedElement::GetBorderStyle() const
826 {
827     auto border = GetBorder();
828     int32_t style = static_cast<int32_t>(border.Left().GetBorderStyle());
829     return BORDER_STYLE[style];
830 }
831 
GetBorderWidth() const832 std::string InspectorComposedElement::GetBorderWidth() const
833 {
834     auto border = GetBorder();
835     auto borderWidth = border.Left().GetWidth();
836     if (NearZero(borderWidth.Value())) {
837         return "0.00vp";
838     }
839     return borderWidth.ToString();
840 }
841 
GetBorderColor() const842 std::string InspectorComposedElement::GetBorderColor() const
843 {
844     auto border = GetBorder();
845     return border.Left().GetColor().ColorToString();
846 }
847 
GetBackDecoration() const848 RefPtr<Decoration> InspectorComposedElement::GetBackDecoration() const
849 {
850     auto render = GetRenderBox();
851     if (!render) {
852         return nullptr;
853     }
854     return render->GetBackDecoration();
855 }
856 
GetBackgroundImage() const857 std::string InspectorComposedElement::GetBackgroundImage() const
858 {
859     auto backDecoration = GetBackDecoration();
860     if (!backDecoration) {
861         return "NONE";
862     }
863     auto image = backDecoration->GetImage();
864     if (!image) {
865         return "NONE";
866     }
867     auto imageRepeat = image->GetImageRepeat();
868     if (imageRepeat == ImageRepeat::REPEAT_X) {
869         return image->GetSrc() + ", ImageRepeat.X";
870     } else if (imageRepeat == ImageRepeat::REPEAT_Y) {
871         return image->GetSrc() + ", ImageRepeat.Y";
872     } else if (imageRepeat == ImageRepeat::REPEAT) {
873         return image->GetSrc() + ", ImageRepeat.XY";
874     }
875     return image->GetSrc() + ", ImageRepeat.NoRepeat";
876 }
877 
GetBackgroundColor() const878 std::string InspectorComposedElement::GetBackgroundColor() const
879 {
880     auto jsonValue = JsonUtil::Create(true);
881     auto backDecoration = GetBackDecoration();
882     if (!backDecoration) {
883         return "NONE";
884     }
885     auto color = backDecoration->GetBackgroundColor();
886     return color.ColorToString();
887 }
888 
GetBackgroundImageSize() const889 std::string InspectorComposedElement::GetBackgroundImageSize() const
890 {
891     auto backDecoration = GetBackDecoration();
892     if (!backDecoration) {
893         return "ImageSize.Auto";
894     }
895     auto image = backDecoration->GetImage();
896     if (!image) {
897         return "ImageSize.Auto";
898     }
899 
900     return image->GetImageSize().ToString();
901 }
902 
GetBackgroundImagePosition() const903 std::string InspectorComposedElement::GetBackgroundImagePosition() const
904 {
905     auto jsonValue = JsonUtil::Create(true);
906     auto backDecoration = GetBackDecoration();
907     if (!backDecoration) {
908         jsonValue->Put("x", 0.0);
909         jsonValue->Put("y", 0.0);
910         return jsonValue->ToString();
911     }
912     auto image = backDecoration->GetImage();
913     if (!image) {
914         jsonValue->Put("x", 0.0);
915         jsonValue->Put("y", 0.0);
916         return jsonValue->ToString();
917     }
918 
919     return image->GetImagePosition().ToString();
920 }
921 
GetFrontDecoration() const922 RefPtr<Decoration> InspectorComposedElement::GetFrontDecoration() const
923 {
924     auto render = GetRenderBox();
925     if (!render) {
926         return nullptr;
927     }
928     return render->GetFrontDecoration();
929 }
930 
GetOpacity() const931 double InspectorComposedElement::GetOpacity() const
932 {
933     auto node = GetInspectorNode(DisplayElement::TypeId());
934     if (!node) {
935         return 1.0;
936     }
937     auto render = AceType::DynamicCast<RenderDisplay>(node);
938     if (!render) {
939         return 1.0;
940     }
941     return render->GetTransitionOpacity();
942 }
943 
GetVisibility() const944 std::string InspectorComposedElement::GetVisibility() const
945 {
946     auto node = GetInspectorNode(DisplayElement::TypeId());
947     if (!node) {
948         return VISIBLE_TYPE[static_cast<int32_t>(VisibleType::VISIBLE)];
949     }
950     auto render = AceType::DynamicCast<RenderDisplay>(node);
951     if (!render) {
952         return VISIBLE_TYPE[static_cast<int32_t>(VisibleType::VISIBLE)];
953     }
954     return VISIBLE_TYPE[static_cast<int32_t>(render->GetVisibleType())];
955 }
956 
GetClip() const957 std::string InspectorComposedElement::GetClip() const
958 {
959     auto render = GetRenderBox();
960     if (!render) {
961         return "false";
962     }
963     auto clipPath = render->GetClipPath();
964     auto jsonValue = JsonUtil::Create(true);
965     if (clipPath && clipPath->GetBasicShape()) {
966         int32_t shapeType = static_cast<int32_t>(clipPath->GetBasicShape()->GetBasicShapeType());
967         int32_t size = static_cast<int32_t>(sizeof(BASIC_SHAPE_TYPE) / sizeof(BASIC_SHAPE_TYPE[0]));
968         if (shapeType < size) {
969             jsonValue->Put("shape", BASIC_SHAPE_TYPE[shapeType]);
970         }
971     } else {
972         if (render->GetBoxClipFlag() == true) {
973             return "true";
974         } else {
975             return "false";
976         }
977     }
978     return jsonValue->ToString();
979 }
980 
GetClipFlag() const981 bool InspectorComposedElement::GetClipFlag() const
982 {
983     auto render = GetRenderBox();
984     if (!render) {
985         return false;
986     }
987 
988     return render->GetBoxClipFlag();
989 }
990 
GetEnabled() const991 bool InspectorComposedElement::GetEnabled() const
992 {
993     auto node = GetInspectorNode(GetTargetTypeId());
994     if (!node) {
995         return true;
996     }
997     return !node->IsDisabled();
998 }
999 
GetZIndex() const1000 int32_t InspectorComposedElement::GetZIndex() const
1001 {
1002     auto node = GetInspectorNode(GetTargetTypeId());
1003     if (!node) {
1004         return 0;
1005     }
1006     return node->GetZIndex();
1007 }
1008 
GetOriginPoint() const1009 DimensionOffset InspectorComposedElement::GetOriginPoint() const
1010 {
1011     auto node = GetInspectorNode(TransformElement::TypeId());
1012     if (!node) {
1013         return DimensionOffset(OHOS::Ace::HALF_PERCENT, OHOS::Ace::HALF_PERCENT);
1014     }
1015     auto render = AceType::DynamicCast<RenderTransform>(node);
1016     if (!render) {
1017         return DimensionOffset(OHOS::Ace::HALF_PERCENT, OHOS::Ace::HALF_PERCENT);
1018     }
1019     return render->GetTransformOrigin();
1020 }
1021 
GetRotate() const1022 std::unique_ptr<JsonValue> InspectorComposedElement::GetRotate() const
1023 {
1024     auto render = AceType::DynamicCast<RenderTransform>(GetInspectorNode(TransformElement::TypeId()));
1025     auto jsonValue = JsonUtil::Create(true);
1026     if (!render) {
1027         return jsonValue;
1028     }
1029     for (const auto& operation : render->GetTransformEffects().GetOperations()) {
1030         if (operation.type_ == TransformOperationType::ROTATE) {
1031             const auto& rotate = operation.rotateOperation_;
1032             jsonValue->Put("x", std::to_string(rotate.dx).c_str());
1033             jsonValue->Put("y", std::to_string(rotate.dy).c_str());
1034             jsonValue->Put("z", std::to_string(rotate.dz).c_str());
1035             jsonValue->Put("angle", std::to_string(rotate.angle).c_str());
1036             jsonValue->Put("centerX", render->GetOriginX().ToString().c_str());
1037             jsonValue->Put("centerY", render->GetOriginY().ToString().c_str());
1038             break;
1039         }
1040     }
1041     return jsonValue;
1042 }
1043 
GetScale() const1044 std::unique_ptr<JsonValue> InspectorComposedElement::GetScale() const
1045 {
1046     auto render = AceType::DynamicCast<RenderTransform>(GetInspectorNode(TransformElement::TypeId()));
1047     auto jsonValue = JsonUtil::Create(true);
1048     if (!render) {
1049         return jsonValue;
1050     }
1051     for (const auto& operation : render->GetTransformEffects().GetOperations()) {
1052         if (operation.type_ == TransformOperationType::SCALE) {
1053             const auto& scale = operation.scaleOperation_;
1054             jsonValue->Put("x", std::to_string(scale.scaleX).c_str());
1055             jsonValue->Put("y", std::to_string(scale.scaleY).c_str());
1056             jsonValue->Put("z", std::to_string(scale.scaleZ).c_str());
1057             jsonValue->Put("centerX", render->GetOriginX().ToString().c_str());
1058             jsonValue->Put("centerY", render->GetOriginY().ToString().c_str());
1059             break;
1060         }
1061     }
1062     return jsonValue;
1063 }
1064 
GetTransform() const1065 std::unique_ptr<JsonValue> InspectorComposedElement::GetTransform() const
1066 {
1067     auto render = AceType::DynamicCast<RenderTransform>(GetInspectorNode(TransformElement::TypeId()));
1068     auto jsonValue = JsonUtil::Create(true);
1069     if (!render) {
1070         return jsonValue;
1071     }
1072     for (const auto& operation : render->GetTransformEffects().GetOperations()) {
1073         if (operation.type_ == TransformOperationType::MATRIX) {
1074             const auto& matrix = operation.matrix4_;
1075             jsonValue->Put("type", "matrix");
1076             auto matrixString = matrix.ToString();
1077             while (matrixString.find("\n") != std::string::npos) {
1078                 auto num = matrixString.find("\n");
1079                 matrixString.replace(num, 1, "");
1080             }
1081             jsonValue->Put("matrix", matrixString.c_str());
1082             break;
1083         }
1084     }
1085     return jsonValue;
1086 }
1087 
GetTranslate() const1088 std::unique_ptr<JsonValue> InspectorComposedElement::GetTranslate() const
1089 {
1090     auto render = AceType::DynamicCast<RenderTransform>(GetInspectorNode(TransformElement::TypeId()));
1091     auto jsonValue = JsonUtil::Create(true);
1092     if (!render) {
1093         return jsonValue;
1094     }
1095     for (const auto& operation : render->GetTransformEffects().GetOperations()) {
1096         if (operation.type_ == TransformOperationType::TRANSLATE) {
1097             const auto& translate = operation.translateOperation_;
1098             jsonValue->Put("x", translate.dx.ToString().c_str());
1099             jsonValue->Put("y", translate.dy.ToString().c_str());
1100             jsonValue->Put("z", translate.dz.ToString().c_str());
1101             break;
1102         }
1103     }
1104     return jsonValue;
1105 }
1106 
GetBlur() const1107 double InspectorComposedElement::GetBlur() const
1108 {
1109     auto render = AceType::DynamicCast<RenderBox>(GetInspectorNode(BoxElement::TypeId()));
1110     if (!render) {
1111         return 0.0;
1112     }
1113     return render->GetBlurRadius().Value();
1114 }
1115 
GetBackDropBlur() const1116 double InspectorComposedElement::GetBackDropBlur() const
1117 {
1118     auto render = AceType::DynamicCast<RenderBox>(GetInspectorNode(BoxElement::TypeId()));
1119     if (!render) {
1120         return 0.0;
1121     }
1122     return render->GetBackdropRadius().Value();
1123 }
1124 
GetBrightness() const1125 double InspectorComposedElement::GetBrightness() const
1126 {
1127     auto render = GetRenderBox();
1128     if (render) {
1129         return render->GetBrightness();
1130     }
1131     return 1.0;
1132 }
1133 
GetSaturate() const1134 double InspectorComposedElement::GetSaturate() const
1135 {
1136     auto render = GetRenderBox();
1137     if (render) {
1138         return render->GetSaturate();
1139     }
1140     return 1.0;
1141 }
1142 
GetContrast() const1143 double InspectorComposedElement::GetContrast() const
1144 {
1145     auto render = GetRenderBox();
1146     if (render) {
1147         return render->GetContrast();
1148     }
1149     return 1.0;
1150 }
1151 
GetInvert() const1152 double InspectorComposedElement::GetInvert() const
1153 {
1154     auto render = GetRenderBox();
1155     if (render) {
1156         return render->GetInvert();
1157     }
1158     return 0.0;
1159 }
1160 
GetSepia() const1161 double InspectorComposedElement::GetSepia() const
1162 {
1163     auto render = GetRenderBox();
1164     if (render) {
1165         return render->GetSepia();
1166     }
1167     return 0.0;
1168 }
1169 
GetGrayScale() const1170 double InspectorComposedElement::GetGrayScale() const
1171 {
1172     auto render = GetRenderBox();
1173     if (render) {
1174         return render->GetGrayScale();
1175     }
1176     return 0.0;
1177 }
1178 
GetHueRotate() const1179 double InspectorComposedElement::GetHueRotate() const
1180 {
1181     auto render = GetRenderBox();
1182     if (render) {
1183         return render->GetHueRotate();
1184     }
1185     return 0.0;
1186 }
1187 
GetWindowBlur() const1188 std::unique_ptr<JsonValue> InspectorComposedElement::GetWindowBlur() const
1189 {
1190     auto render = AceType::DynamicCast<RenderBox>(GetInspectorNode(BoxElement::TypeId()));
1191     auto jsonValue = JsonUtil::Create(true);
1192     if (!render) {
1193         return jsonValue;
1194     }
1195     jsonValue->Put("percent", std::to_string(render->GetWindowBlurProgress()).c_str());
1196     jsonValue->Put(
1197         "style", WINDOW_BLUR_STYLE[static_cast<int32_t>(render->GetWindowBlurStyle()) - WINDOW_BLUR_STYLE_ENUM_OFFSET]);
1198     return jsonValue;
1199 }
1200 
GetShadow() const1201 std::unique_ptr<JsonValue> InspectorComposedElement::GetShadow() const
1202 {
1203     auto render = AceType::DynamicCast<RenderBox>(GetInspectorNode(BoxElement::TypeId()));
1204     auto jsonValue = JsonUtil::Create(true);
1205     if (!render) {
1206         return jsonValue;
1207     }
1208     Shadow shadow = render->GetShadow();
1209     jsonValue->Put("radius", std::to_string(shadow.GetBlurRadius()).c_str());
1210     jsonValue->Put("color", ConvertColorToString(shadow.GetColor()).c_str());
1211     jsonValue->Put("offsetX", std::to_string(shadow.GetOffset().GetX()).c_str());
1212     jsonValue->Put("offsetY", std::to_string(shadow.GetOffset().GetY()).c_str());
1213     jsonValue->Put("type", std::to_string(static_cast<int32_t>(shadow.GetShadowType())).c_str());
1214     return jsonValue;
1215 }
1216 
GetOverlay() const1217 std::unique_ptr<JsonValue> InspectorComposedElement::GetOverlay() const
1218 {
1219     auto jsonValue = JsonUtil::Create(true);
1220     // Since CoverageComponent is inherited from ComponentGroup, but Coverage does not have Element,
1221     // ComponentGroupElement is called.
1222     auto coverage = GetInspectorElement<RenderCoverage>(ComponentGroupElement::TypeId());
1223     if (!coverage) {
1224         jsonValue->Put("options", "{align: Alignment.Center, offset: {x: 0, y: 0}}");
1225         return jsonValue;
1226     }
1227     auto title = coverage->GetTextVal();
1228     auto alignment = coverage->GetAlignment();
1229     auto jsonAlign = JsonUtil::Create(true);
1230     if (alignment == Alignment::TOP_LEFT) {
1231         jsonAlign->Put("align", "Alignment.TopStart");
1232     } else if (alignment == Alignment::TOP_CENTER) {
1233         jsonAlign->Put("align", "Alignment.Top");
1234     } else if (alignment == Alignment::TOP_RIGHT) {
1235         jsonAlign->Put("align", "Alignment.TopEnd");
1236     } else if (alignment == Alignment::CENTER_LEFT) {
1237         jsonAlign->Put("align", "Alignment.Start");
1238     } else if (alignment == Alignment::CENTER_RIGHT) {
1239         jsonAlign->Put("align", "Alignment.End");
1240     } else if (alignment == Alignment::BOTTOM_LEFT) {
1241         jsonAlign->Put("align", "Alignment.BottomStart");
1242     } else if (alignment == Alignment::BOTTOM_CENTER) {
1243         jsonAlign->Put("align", "Alignment.Bottom");
1244     } else if (alignment == Alignment::BOTTOM_RIGHT) {
1245         jsonAlign->Put("align", "Alignment.BottomEnd");
1246     } else {
1247         jsonAlign->Put("align", "Alignment.Center");
1248     }
1249     auto offsetJson = JsonUtil::Create(true);
1250     offsetJson->Put("x", coverage->GetX().ToString().c_str());
1251     offsetJson->Put("y", coverage->GetY().ToString().c_str());
1252     jsonAlign->Put("offset", offsetJson);
1253     jsonValue->Put("title", title.c_str());
1254     jsonValue->Put("options", jsonAlign);
1255     return jsonValue;
1256 }
1257 
GetMask() const1258 std::unique_ptr<JsonValue> InspectorComposedElement::GetMask() const
1259 {
1260     auto render = GetRenderBox();
1261     auto jsonValue = JsonUtil::Create(true);
1262     if (!render) {
1263         return jsonValue;
1264     }
1265     auto mask = render->GetMask();
1266     if (mask && mask->GetMaskPath() && mask->GetMaskPath()->GetBasicShape()) {
1267         auto shape = mask->GetMaskPath()->GetBasicShape();
1268         int32_t shapeType = static_cast<int32_t>(shape->GetBasicShapeType());
1269         int32_t size = static_cast<int32_t>(sizeof(BASIC_SHAPE_TYPE) / sizeof(BASIC_SHAPE_TYPE[0]));
1270         if (shapeType < size) {
1271             jsonValue->Put("shape", BASIC_SHAPE_TYPE[shapeType]);
1272         }
1273     }
1274     return jsonValue;
1275 }
1276 
GetGridColumnInfo() const1277 RefPtr<GridColumnInfo> InspectorComposedElement::GetGridColumnInfo() const
1278 {
1279     auto render = GetRenderBox();
1280     if (!render) {
1281         return nullptr;
1282     }
1283     auto columnInfo = render->GetGridColumnInfo();
1284     if (!columnInfo) {
1285         return nullptr;
1286     }
1287     return columnInfo;
1288 }
1289 
GetGridSpan() const1290 int32_t InspectorComposedElement::GetGridSpan() const
1291 {
1292     auto columnInfo = GetGridColumnInfo();
1293     if (columnInfo) {
1294         return columnInfo->GetColumns();
1295     }
1296     return 1;
1297 }
1298 
GetGridOffset() const1299 int32_t InspectorComposedElement::GetGridOffset() const
1300 {
1301     auto columnInfo = GetGridColumnInfo();
1302     if (columnInfo) {
1303         if (columnInfo->GetOffset(GridSizeType::UNDEFINED) == -1) {
1304             return 0;
1305         }
1306         return columnInfo->GetOffset(GridSizeType::UNDEFINED);
1307     }
1308     return 0;
1309 }
1310 
GetUseSizeType() const1311 std::unique_ptr<JsonValue> InspectorComposedElement::GetUseSizeType() const
1312 {
1313     auto columnInfo = GetGridColumnInfo();
1314     auto jsonRoot = JsonUtil::Create(true);
1315     if (!columnInfo) {
1316         return jsonRoot;
1317     }
1318     int32_t index = static_cast<int32_t>(GridSizeType::XS);
1319     for (; index < static_cast<int32_t>(GridSizeType::XL); index++) {
1320         auto jsonValue = JsonUtil::Create(true);
1321         GridSizeType type = static_cast<GridSizeType>(index);
1322         jsonValue->Put("span", static_cast<int32_t>(columnInfo->GetColumns(type)));
1323         jsonValue->Put("offset", columnInfo->GetOffset(type));
1324         jsonRoot->Put(GRID_SIZE_TYPE[index], jsonValue);
1325     }
1326     return jsonRoot;
1327 }
1328 
GetUseAlign() const1329 std::unique_ptr<JsonValue> InspectorComposedElement::GetUseAlign() const
1330 {
1331     auto render = GetRenderBox();
1332     auto jsonValue = JsonUtil::Create(true);
1333     if (!render) {
1334         return jsonValue;
1335     }
1336     jsonValue->Put("edge", ConvertSideToString(render->GetUseAlignSide()).c_str());
1337     jsonValue->Put("offset", render->GetUseAlignOffset().ToString().c_str());
1338     return jsonValue;
1339 }
1340 
GetBindPopup() const1341 std::string InspectorComposedElement::GetBindPopup() const
1342 {
1343     auto resultJson = JsonUtil::Create(true);
1344     auto coverageElement = GetContentElement<ComponentGroupElement>(ComponentGroupElement::TypeId(), false);
1345     RefPtr<PopupElementV2> popupElement = nullptr;
1346     if (coverageElement) {
1347         for (const auto& element : coverageElement->GetChildren()) {
1348             if (AceType::DynamicCast<PopupElementV2>(element)) {
1349                 popupElement = AceType::DynamicCast<PopupElementV2>(element);
1350             }
1351         }
1352     }
1353     if (!popupElement) {
1354         return "";
1355     }
1356     std::string show;
1357     if (popupElement->IsShow()) {
1358         show = "true";
1359     } else {
1360         show = "false";
1361     }
1362     auto popupJson = JsonUtil::Create(true);
1363     popupJson->Put("message", popupElement->GetMessage().c_str());
1364     popupJson->Put("placementOnTop", popupElement->GetPlacementOnTop());
1365     auto primaryButtonJson = JsonUtil::Create(true);
1366     primaryButtonJson->Put("value", popupElement->GetPrimaryButtonValue().c_str());
1367     auto secondaryButtonJson = JsonUtil::Create(true);
1368     secondaryButtonJson->Put("value", popupElement->GetSecondaryButtonValue().c_str());
1369 
1370     popupJson->Put("primaryButton", primaryButtonJson);
1371     popupJson->Put("secondaryButton", secondaryButtonJson);
1372     return show + ", " + popupJson->ToString();
1373 }
1374 
GetBindContextMenu() const1375 std::string InspectorComposedElement::GetBindContextMenu() const
1376 {
1377     auto node = GetInspectorNode(BoxElement::TypeId());
1378     if (!node) {
1379         return "-";
1380     }
1381     auto responseType = AceType::DynamicCast<RenderBox>(node);
1382     if (responseType) {
1383         if (responseType->GetOnMouseId()) {
1384             return "ResponseType.RightClick";
1385         } else if (responseType->GetOnLongPress()) {
1386             return "ResponseType.LongPress";
1387         } else {
1388             return "-";
1389         }
1390     }
1391     return "-";
1392 }
1393 
GetColorBlend() const1394 std::string InspectorComposedElement::GetColorBlend() const
1395 {
1396     auto node = GetRenderBox();
1397     if (!node) {
1398         return "";
1399     }
1400     auto colorBlend = node->GetColorBlend();
1401     return colorBlend.ColorToString();
1402 }
1403 
AddChildWithSlot(int32_t slot,const RefPtr<Component> & newComponent)1404 void InspectorComposedElement::AddChildWithSlot(int32_t slot, const RefPtr<Component>& newComponent)
1405 {
1406     auto renderElement = GetRenderElement();
1407     CHECK_NULL_VOID(renderElement);
1408     renderElement->UpdateChildWithSlot(nullptr, newComponent, slot, slot);
1409     renderElement->MarkDirty();
1410 }
1411 
UpdateChildWithSlot(int32_t slot,const RefPtr<Component> & newComponent)1412 void InspectorComposedElement::UpdateChildWithSlot(int32_t slot, const RefPtr<Component>& newComponent)
1413 {
1414     auto renderElement = GetRenderElement();
1415     CHECK_NULL_VOID(renderElement);
1416     auto child = GetElementChildBySlot(renderElement, slot);
1417     CHECK_NULL_VOID(child);
1418     // Deals with the case where the component to be updated is a custom component.
1419     auto rootElement = renderElement;
1420     auto childComposedElement = AceType::DynamicCast<ComposedElement>(child);
1421     if (childComposedElement && childComposedElement->GetName() == "view") {
1422         rootElement = child;
1423         child = rootElement->GetChildren().empty() ? nullptr : rootElement->GetChildren().front();
1424     }
1425 
1426     // Replace the component with newComponent.
1427     auto context = rootElement->GetContext().Upgrade();
1428     auto needRebuildFocusElement = AceType::DynamicCast<Element>(rootElement->GetFocusScope());
1429     if (context && needRebuildFocusElement) {
1430         context->AddNeedRebuildFocusElement(needRebuildFocusElement);
1431     }
1432     ElementRegister::GetInstance()->RemoveItemSilently(child->GetElementId());
1433     rootElement->DeactivateChild(child);
1434     auto newChild = rootElement->InflateComponent(newComponent, child->GetSlot(), child->GetRenderSlot());
1435     ElementRegister::GetInstance()->AddElement(newChild);
1436     renderElement->MarkDirty();
1437 }
1438 
DeleteChildWithSlot(int32_t slot)1439 void InspectorComposedElement::DeleteChildWithSlot(int32_t slot)
1440 {
1441     auto renderElement = GetRenderElement();
1442     CHECK_NULL_VOID(renderElement);
1443     auto child = GetElementChildBySlot(renderElement, slot);
1444     CHECK_NULL_VOID(child);
1445     renderElement->UpdateChildWithSlot(child, nullptr, slot, slot);
1446     renderElement->MarkDirty();
1447 }
1448 
UpdateEventTarget(BaseEventInfo & info) const1449 void InspectorComposedElement::UpdateEventTarget(BaseEventInfo& info) const
1450 {
1451     auto area = GetCurrentRectAndOrigin();
1452     auto& target = info.GetTargetWithModify();
1453     target.area.SetOffset(area.first.GetOffset());
1454     target.area.SetHeight(Dimension(area.first.GetSize().Height()));
1455     target.area.SetWidth(Dimension(area.first.GetSize().Width()));
1456     target.origin = DimensionOffset(area.second);
1457 }
1458 
GetCurrentRectAndOrigin() const1459 std::pair<Rect, Offset> InspectorComposedElement::GetCurrentRectAndOrigin() const
1460 {
1461     auto rectInLocal = GetRenderRectInLocal();
1462     auto rectInGlobal = GetRenderRect();
1463     auto marginLeft = GetMargin(AnimatableType::PROPERTY_MARGIN_LEFT).ConvertToPx();
1464     auto marginRight = GetMargin(AnimatableType::PROPERTY_MARGIN_RIGHT).ConvertToPx();
1465     auto marginTop = GetMargin(AnimatableType::PROPERTY_MARGIN_TOP).ConvertToPx();
1466     auto marginBottom = GetMargin(AnimatableType::PROPERTY_MARGIN_BOTTOM).ConvertToPx();
1467     auto LocalOffset = rectInLocal.GetOffset();
1468     auto offset = Offset(LocalOffset.GetX() + marginLeft, LocalOffset.GetY() + marginTop);
1469     auto size = Size(rectInLocal.Width() - marginLeft - marginRight, rectInLocal.Height() - marginTop - marginBottom);
1470     auto globalOffset = rectInGlobal.GetOffset();
1471     return { { offset, size }, { globalOffset.GetX() - LocalOffset.GetX(), globalOffset.GetY() - LocalOffset.GetY() } };
1472 }
1473 
GetColorsAndRepeating(std::unique_ptr<JsonValue> & resultJson,const Gradient & gradient) const1474 void InspectorComposedElement::GetColorsAndRepeating(
1475     std::unique_ptr<JsonValue>& resultJson, const Gradient& gradient) const
1476 {
1477     auto jsoncolorArray = JsonUtil::CreateArray(true);
1478     auto colors = gradient.GetColors();
1479     for (size_t i = 0; i < colors.size(); ++i) {
1480         auto temp = JsonUtil::CreateArray(true);
1481         auto value = std::to_string(colors[i].GetDimension().Value() / 100.0);
1482         auto color = colors[i].GetColor().ColorToString();
1483         temp->Put("0", color.c_str());
1484         temp->Put("1", value.c_str());
1485         auto index = std::to_string(i);
1486         jsoncolorArray->Put(index.c_str(), temp);
1487     }
1488     resultJson->Put("colors", jsoncolorArray);
1489     auto repeat = ConvertBoolToString(gradient.GetRepeat());
1490     resultJson->Put("repeating", repeat.c_str());
1491 }
1492 
GetLinearGradient() const1493 std::unique_ptr<JsonValue> InspectorComposedElement::GetLinearGradient() const
1494 {
1495     auto resultJson = JsonUtil::Create(true);
1496     auto node = GetRenderBox();
1497     if (!node) {
1498         return resultJson;
1499     }
1500     auto decoration = node->GetBackDecoration();
1501     if (decoration) {
1502         auto lineGradient = decoration->GetGradient();
1503         if (GradientType::LINEAR != lineGradient.GetType()) {
1504             return resultJson;
1505         }
1506         if (lineGradient.GetLinearGradient().angle) {
1507             resultJson->Put("angle", lineGradient.GetLinearGradient().angle->ToString().c_str());
1508         }
1509 
1510         auto linearX = lineGradient.GetLinearGradient().linearX;
1511         auto linearY = lineGradient.GetLinearGradient().linearY;
1512         if (linearX == GradientDirection::LEFT) {
1513             if (linearY == GradientDirection::TOP) {
1514                 resultJson->Put("direction", "GradientDirection.LeftTop");
1515             } else if (linearY == GradientDirection::BOTTOM) {
1516                 resultJson->Put("direction", "GradientDirection.LeftBottom");
1517             } else {
1518                 resultJson->Put("direction", "GradientDirection.Left");
1519             }
1520         } else if (linearX == GradientDirection::RIGHT) {
1521             if (linearY == GradientDirection::TOP) {
1522                 resultJson->Put("direction", "GradientDirection.RightTop");
1523             } else if (linearY == GradientDirection::BOTTOM) {
1524                 resultJson->Put("direction", "GradientDirection.RightBottom");
1525             } else {
1526                 resultJson->Put("direction", "GradientDirection.Right");
1527             }
1528         } else {
1529             if (linearY == GradientDirection::TOP) {
1530                 resultJson->Put("direction", "GradientDirection.Top");
1531             } else if (linearY == GradientDirection::BOTTOM) {
1532                 resultJson->Put("direction", "GradientDirection.Bottom");
1533             } else {
1534                 resultJson->Put("direction", "GradientDirection.None");
1535             }
1536         }
1537         GetColorsAndRepeating(resultJson, lineGradient);
1538     }
1539     return resultJson;
1540 }
1541 
GetSweepGradient() const1542 std::unique_ptr<JsonValue> InspectorComposedElement::GetSweepGradient() const
1543 {
1544     auto resultJson = JsonUtil::Create(true);
1545     auto node = GetRenderBox();
1546     if (!node) {
1547         return resultJson;
1548     }
1549     auto decoration = node->GetBackDecoration();
1550     if (decoration) {
1551         auto sweepGradient = decoration->GetGradient();
1552         if (GradientType::SWEEP != sweepGradient.GetType()) {
1553             return resultJson;
1554         }
1555         auto radialCenterX = sweepGradient.GetSweepGradient().centerX;
1556         auto radialCenterY = sweepGradient.GetSweepGradient().centerY;
1557         if (radialCenterX && radialCenterY) {
1558             auto jsPoint = JsonUtil::CreateArray(true);
1559             jsPoint->Put("0", radialCenterX->ToString().c_str());
1560             jsPoint->Put("1", radialCenterY->ToString().c_str());
1561             resultJson->Put("center", jsPoint);
1562         }
1563 
1564         auto startAngle = sweepGradient.GetSweepGradient().startAngle;
1565         auto endAngle = sweepGradient.GetSweepGradient().endAngle;
1566         if (startAngle) {
1567             resultJson->Put("start", startAngle->ToString().c_str());
1568         }
1569         if (endAngle) {
1570             resultJson->Put("end", endAngle->ToString().c_str());
1571         }
1572 
1573         GetColorsAndRepeating(resultJson, sweepGradient);
1574     }
1575     return resultJson;
1576 }
1577 
GetRadialGradient() const1578 std::unique_ptr<JsonValue> InspectorComposedElement::GetRadialGradient() const
1579 {
1580     auto resultJson = JsonUtil::Create(true);
1581     auto node = GetRenderBox();
1582     if (!node) {
1583         return resultJson;
1584     }
1585     auto decoration = node->GetBackDecoration();
1586     if (decoration) {
1587         auto radialGradient = decoration->GetGradient();
1588         if (GradientType::RADIAL != radialGradient.GetType()) {
1589             return resultJson;
1590         }
1591 
1592         auto radialCenterX = radialGradient.GetRadialGradient().radialCenterX;
1593         auto radialCenterY = radialGradient.GetRadialGradient().radialCenterY;
1594         if (radialCenterX && radialCenterY) {
1595             auto jsPoint = JsonUtil::CreateArray(true);
1596             jsPoint->Put("0", radialCenterX->ToString().c_str());
1597             jsPoint->Put("1", radialCenterY->ToString().c_str());
1598             resultJson->Put("center", jsPoint);
1599         }
1600 
1601         auto radius = radialGradient.GetRadialGradient().radialVerticalSize;
1602         if (radius) {
1603             resultJson->Put("radius", radius->ToString().c_str());
1604         }
1605 
1606         GetColorsAndRepeating(resultJson, radialGradient);
1607     }
1608     return resultJson;
1609 }
1610 
GetTag() const1611 const std::string& InspectorComposedElement::GetTag() const
1612 {
1613     auto iter = COMPONENT_TAG_TO_ETS_TAG_MAP.find(name_);
1614     return iter != COMPONENT_TAG_TO_ETS_TAG_MAP.end() ? iter->second : name_;
1615 }
1616 
GetClickable() const1617 bool InspectorComposedElement::GetClickable() const
1618 {
1619     auto node = GetAccessibilityNode();
1620     if (!node) {
1621         return false;
1622     }
1623     return node->GetClickableState();
1624 }
GetCheckable() const1625 bool InspectorComposedElement::GetCheckable() const
1626 {
1627     auto node = GetAccessibilityNode();
1628     if (!node) {
1629         return false;
1630     }
1631     return node->GetCheckableState();
1632 }
GetFocusable() const1633 bool InspectorComposedElement::GetFocusable() const
1634 {
1635     auto focusableElement = GetContentElement<FocusableElement>(FocusableElement::TypeId(), false);
1636     if (!focusableElement) {
1637         return false;
1638     }
1639     return focusableElement->IsFocusable();
1640 }
GetScrollable() const1641 bool InspectorComposedElement::GetScrollable() const
1642 {
1643     auto node = GetAccessibilityNode();
1644     if (!node) {
1645         return false;
1646     }
1647     return node->GetScrollableState();
1648 }
GetLongClickable() const1649 bool InspectorComposedElement::GetLongClickable() const
1650 {
1651     auto node = GetAccessibilityNode();
1652     if (!node) {
1653         return false;
1654     }
1655     return node->GetLongClickableState();
1656 }
GetTouchable() const1657 bool InspectorComposedElement::GetTouchable() const
1658 {
1659     auto render = GetRenderBox();
1660     if (!render) {
1661         return false;
1662     }
1663     return render->IsTouchable();
1664 }
IsSelected() const1665 bool InspectorComposedElement::IsSelected() const
1666 {
1667     auto node = GetAccessibilityNode();
1668     if (!node) {
1669         return false;
1670     }
1671     return node->GetSelectedState();
1672 }
IsPassword() const1673 bool InspectorComposedElement::IsPassword() const
1674 {
1675     auto node = GetAccessibilityNode();
1676     if (!node) {
1677         return false;
1678     }
1679     return node->GetIsPassword();
1680 }
IsChecked() const1681 bool InspectorComposedElement::IsChecked() const
1682 {
1683     auto node = GetAccessibilityNode();
1684     if (!node) {
1685         return false;
1686     }
1687     return node->GetCheckedState();
1688 }
IsFocused() const1689 bool InspectorComposedElement::IsFocused() const
1690 {
1691     auto node = GetAccessibilityNode();
1692     if (!node) {
1693         return false;
1694     }
1695     return node->GetFocusedState();
1696 }
1697 
TriggerVisibleAreaChangeCallback(std::list<VisibleCallbackInfo> & callbackInfoList)1698 void InspectorComposedElement::TriggerVisibleAreaChangeCallback(std::list<VisibleCallbackInfo>& callbackInfoList)
1699 {
1700     auto renderBox = GetRenderBox();
1701     if (!renderBox) {
1702         LOGE("TriggerVisibleAreaChangeCallback: Get render box failed.");
1703         return;
1704     }
1705 
1706     auto context = context_.Upgrade();
1707     if (!context) {
1708         LOGW("get context failed");
1709         return;
1710     }
1711 
1712     const auto& renderNode = GetRenderNode();
1713     if (!renderNode) {
1714         LOGE("Get render node failed!");
1715         return;
1716     }
1717 
1718     if (!context->GetOnShow() || renderNode->GetHidden() || inspectorId_ == -1) {
1719         ProcessAllVisibleCallback(callbackInfoList, VISIBLE_RATIO_MIN);
1720         return;
1721     }
1722 
1723     auto margin = renderBox->GetMargin();
1724     auto renderRect = GetRenderRect() + Offset(margin.LeftPx(), margin.TopPx());
1725     renderRect -= margin.GetLayoutSize();
1726 
1727     Rect visibleRect = renderRect;
1728     Rect parentRect;
1729     auto parent = renderNode->GetParent().Upgrade();
1730     while (parent) {
1731         parentRect = parent->GetRectBasedWindowTopLeft();
1732         visibleRect = visibleRect.Constrain(parentRect);
1733         parent = parent->GetParent().Upgrade();
1734     }
1735 
1736     double currentVisibleRatio =
1737         std::clamp(CalculateCurrentVisibleRatio(visibleRect, renderRect), VISIBLE_RATIO_MIN, VISIBLE_RATIO_MAX);
1738     ProcessAllVisibleCallback(callbackInfoList, currentVisibleRatio);
1739 }
1740 
CalculateCurrentVisibleRatio(const Rect & visibleRect,const Rect & renderRect)1741 double InspectorComposedElement::CalculateCurrentVisibleRatio(const Rect& visibleRect, const Rect& renderRect)
1742 {
1743     if (!visibleRect.IsValid() || !renderRect.IsValid()) {
1744         return 0.0;
1745     }
1746 
1747     return visibleRect.Width() * visibleRect.Height() / (renderRect.Width() * renderRect.Height());
1748 }
1749 
ProcessAllVisibleCallback(std::list<VisibleCallbackInfo> & callbackInfoList,double currentVisibleRatio)1750 void InspectorComposedElement::ProcessAllVisibleCallback(
1751     std::list<VisibleCallbackInfo>& callbackInfoList, double currentVisibleRatio)
1752 {
1753     for (auto& nodeCallbackInfo : callbackInfoList) {
1754         if (GreatNotEqual(currentVisibleRatio, nodeCallbackInfo.visibleRatio) && !nodeCallbackInfo.isCurrentVisible) {
1755             OnVisibleAreaChangeCallback(nodeCallbackInfo, true, currentVisibleRatio);
1756             continue;
1757         }
1758 
1759         if (LessNotEqual(currentVisibleRatio, nodeCallbackInfo.visibleRatio) && nodeCallbackInfo.isCurrentVisible) {
1760             OnVisibleAreaChangeCallback(nodeCallbackInfo, false, currentVisibleRatio);
1761             continue;
1762         }
1763 
1764         if (NearEqual(currentVisibleRatio, nodeCallbackInfo.visibleRatio)) {
1765             if (NearEqual(nodeCallbackInfo.visibleRatio, VISIBLE_RATIO_MIN) && nodeCallbackInfo.isCurrentVisible) {
1766                 OnVisibleAreaChangeCallback(nodeCallbackInfo, false, VISIBLE_RATIO_MIN);
1767             }
1768 
1769             if (NearEqual(nodeCallbackInfo.visibleRatio, VISIBLE_RATIO_MAX) && !nodeCallbackInfo.isCurrentVisible) {
1770                 OnVisibleAreaChangeCallback(nodeCallbackInfo, true, VISIBLE_RATIO_MAX);
1771             }
1772         }
1773     }
1774 }
1775 
OnVisibleAreaChangeCallback(VisibleCallbackInfo & callbackInfo,bool visibleType,double currentVisibleRatio)1776 void InspectorComposedElement::OnVisibleAreaChangeCallback(
1777     VisibleCallbackInfo& callbackInfo, bool visibleType, double currentVisibleRatio)
1778 {
1779     callbackInfo.isCurrentVisible = visibleType;
1780     if (callbackInfo.callback) {
1781         callbackInfo.callback(visibleType, currentVisibleRatio);
1782     }
1783 }
1784 
GetHitTestBehaviorStr() const1785 std::string InspectorComposedElement::GetHitTestBehaviorStr() const
1786 {
1787     auto node = GetInspectorNode(GetTargetTypeId());
1788     if (!node) {
1789         return HIT_TEST_BEHAVIOR[0];
1790     }
1791     auto hitTestMode = static_cast<int32_t>(node->GetHitTestMode());
1792     return HIT_TEST_BEHAVIOR[hitTestMode];
1793 }
1794 
1795 } // namespace OHOS::Ace::V2
1796