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 "core/common/ace_application_info.h"
23 #include "core/components/box/box_element.h"
24 #include "core/components/common/layout/constants.h"
25 #include "core/components/coverage/render_coverage.h"
26 #include "core/components/display/render_display.h"
27 #include "core/components/flex/flex_item_element.h"
28 #include "core/components/flex/render_flex_item.h"
29 #include "core/components/focusable/focusable_element.h"
30 #include "core/components/popup/popup_element_v2.h"
31 #include "core/components/text/render_text.h"
32 #include "core/components/text/text_element.h"
33 #include "core/components/transform/transform_element.h"
34 #include "core/components_v2/inspector/inspector_composed_component.h"
35 #include "core/components_v2/inspector/inspector_constants.h"
36 #include "core/components_v2/inspector/utils.h"
37 #include "core/pipeline/base/component.h"
38 #include "core/pipeline/base/composed_component.h"
39
40 namespace OHOS::Ace::V2 {
41
42 namespace {
43
44 std::atomic<int32_t> g_currentInspectorId(0);
45
GetCurrentInspectorId()46 int32_t GetCurrentInspectorId()
47 {
48 return g_currentInspectorId.fetch_add(1, std::memory_order_relaxed) + 1;
49 }
50
51 constexpr uint32_t WINDOW_BLUR_STYLE_ENUM_OFFSET = 100;
52
53 const char* VISIBLE_TYPE[] = { "Visibility.Visible", "Visibility.Hidden", "Visibility.None" };
54
55 const char* ITEM_ALIGN[] = { "ItemAlign.Auto", "ItemAlign.Start", "ItemAlign.Center", "ItemAlign.End",
56 "ItemAlign.Stretch", "ItemAlign.Baseline" };
57
58 // NONE translate to Solid
59 const char* BORDER_STYLE[] = {
60 "BorderStyle.Solid",
61 "BorderStyle.Dashed",
62 "BorderStyle.Dotted",
63 "BorderStyle.Solid",
64 };
65
66 const char* WINDOW_BLUR_STYLE[] = { "BlurStyle.SmallLight", "BlurStyle.MediumLight", "BlurStyle.LargeLight",
67 "BlurStyle.XlargeLight", "BlurStyle.SmallDark", "BlurStyle.MediumDark", "BlurStyle.LargeDark",
68 "BlurStyle.XlargeDark" };
69
70 const char* ALIGNMENT_TYPE[3][3] = { { "Alignment.TopStart", "Alignment.Start", "Alignment.BottomStart" },
71 { "Alignment.Top", "Alignment.Center", "Alignment.Bottom" },
72 { "Alignment.TopEnd", "Alignment.End", "Alignment.BottomEnd" } };
73
74 const char* GRID_SIZE_TYPE[] = { "default", "sx", "sm", "md", "lg" };
75
76 constexpr const char* TEXT_DIRECTION[] = { "Direction.Ltr", "Direction.Rtl", "Direction.Inherit", "Direction.Auto" };
77
78 constexpr const char* BASIC_SHAPE_TYPE[] { "None", "Inset", "Circle", "Ellipse", "Polygon", "Path", "Rect" };
79
80 const std::unordered_map<std::string, DoubleJsonFunc> CREATE_JSON_DOUBLE_MAP {
__anone729286d0202() 81 { "opacity", [](const InspectorNode& inspector) { return inspector.GetOpacity(); } },
__anone729286d0302() 82 { "flexGrow", [](const InspectorNode& inspector) { return inspector.GetFlexGrow(); } },
__anone729286d0402() 83 { "flexShrink", [](const InspectorNode& inspector) { return inspector.GetFlexShrink(); } },
__anone729286d0502() 84 { "gridOffset", [](const InspectorNode& inspector) { return inspector.GetGridOffset(); } },
__anone729286d0602() 85 { "blur", [](const InspectorNode& inspector) { return inspector.GetBlur(); } },
__anone729286d0702() 86 { "backdropBlur", [](const InspectorNode& inspector) { return inspector.GetBackDropBlur(); } },
__anone729286d0802() 87 { "aspectRatio", [](const InspectorNode& inspector) { return inspector.GetAspectRatio(); } },
__anone729286d0902() 88 { "brightness", [](const InspectorNode& inspector) { return inspector.GetBrightness(); } },
__anone729286d0a02() 89 { "saturate", [](const InspectorNode& inspector) { return inspector.GetSaturate(); } },
__anone729286d0b02() 90 { "contrast", [](const InspectorNode& inspector) { return inspector.GetContrast(); } },
__anone729286d0c02() 91 { "invert", [](const InspectorNode& inspector) { return inspector.GetInvert(); } },
__anone729286d0d02() 92 { "sepia", [](const InspectorNode& inspector) { return inspector.GetSepia(); } },
__anone729286d0e02() 93 { "grayscale", [](const InspectorNode& inspector) { return inspector.GetGrayScale(); } },
__anone729286d0f02() 94 { "hueRotate", [](const InspectorNode& inspector) { return inspector.GetHueRotate(); } },
95 };
96
97 const std::unordered_map<std::string, StringJsonFunc> CREATE_JSON_STRING_MAP {
__anone729286d1002() 98 { "visibility", [](const InspectorNode& inspector) { return inspector.GetVisibility(); } },
__anone729286d1102() 99 { "alignSelf", [](const InspectorNode& inspector) { return inspector.GetAlignSelf(); } },
__anone729286d1202() 100 { "clip", [](const InspectorNode& inspector) { return inspector.GetClip(); } },
__anone729286d1302() 101 { "constraintSize", [](const InspectorNode& inspector) { return inspector.GetConstraintSize(); } },
__anone729286d1402() 102 { "borderColor", [](const InspectorNode& inspector) { return inspector.GetBorderColor(); } },
__anone729286d1502() 103 { "borderStyle", [](const InspectorNode& inspector) { return inspector.GetBorderStyle(); } },
__anone729286d1602() 104 { "borderWidth", [](const InspectorNode& inspector) { return inspector.GetBorderWidth(); } },
__anone729286d1702() 105 { "borderRadius", [](const InspectorNode& inspector) { return inspector.GetBorderRadius(); } },
__anone729286d1802() 106 { "backgroundImage", [](const InspectorNode& inspector) { return inspector.GetBackgroundImage(); } },
__anone729286d1902() 107 { "backgroundColor", [](const InspectorNode& inspector) { return inspector.GetBackgroundColor(); } },
__anone729286d1a02() 108 { "flexBasis", [](const InspectorNode& inspector) { return inspector.GetFlexBasis(); } },
__anone729286d1b02() 109 { "width", [](const InspectorNode& inspector) { return inspector.GetWidth(); } },
__anone729286d1c02() 110 { "height", [](const InspectorNode& inspector) { return inspector.GetHeight(); } },
__anone729286d1d02() 111 { "align", [](const InspectorNode& inspector) { return inspector.GetAlign(); } },
__anone729286d1e02() 112 { "direction", [](const InspectorNode& inspector) { return inspector.GetDirectionStr(); } },
__anone729286d1f02() 113 { "bindPopup", [](const InspectorNode& inspector) { return inspector.GetBindPopup(); } },
__anone729286d2002() 114 { "bindContextMenu", [](const InspectorNode& inspector) { return inspector.GetBindContextMenu(); } },
__anone729286d2102() 115 { "colorBlend", [](const InspectorNode& inspector) { return inspector.GetColorBlend(); } },
__anone729286d2202() 116 { "backgroundImageSize", [](const InspectorNode& inspector) { return inspector.GetBackgroundImageSize(); } },
117 { "backgroundImagePosition",
__anone729286d2302() 118 [](const InspectorNode& inspector) { return inspector.GetBackgroundImagePosition(); } },
__anone729286d2402() 119 { "padding", [](const InspectorNode& inspector) { return inspector.GetPadding(); } },
__anone729286d2502() 120 { "margin", [](const InspectorNode& inspector) { return inspector.GetAllMargin(); } },
121 };
122
123 const std::unordered_map<std::string, BoolJsonFunc> CREATE_JSON_BOOL_MAP {
__anone729286d2602() 124 { "enabled", [](const InspectorNode& inspector) { return inspector.GetEnabled(); } },
__anone729286d2702() 125 { "focusable", [](const InspectorNode& inspector) { return inspector.GetFocusable(); } },
126 };
127
128 const std::unordered_map<std::string, IntJsonFunc> CREATE_JSON_INT_MAP {
__anone729286d2802() 129 { "zIndex", [](const InspectorNode& inspector) { return inspector.GetZIndex(); } },
__anone729286d2902() 130 { "gridSpan", [](const InspectorNode& inspector) { return inspector.GetGridSpan(); } },
__anone729286d2a02() 131 { "layoutWeight", [](const InspectorNode& inspector) { return inspector.GetLayoutWeight(); } },
__anone729286d2b02() 132 { "displayPriority", [](const InspectorNode& inspector) { return inspector.GetDisplayPriority(); } },
133 };
134
135 const std::unordered_map<std::string, JsonValueJsonFunc> CREATE_JSON_JSON_VALUE_MAP {
__anone729286d2c02() 136 { "shadow", [](const InspectorNode& inspector) { return inspector.GetShadow(); } },
__anone729286d2d02() 137 { "position", [](const InspectorNode& inspector) { return inspector.GetPosition(); } },
__anone729286d2e02() 138 { "offset", [](const InspectorNode& inspector) { return inspector.GetOffset(); } },
__anone729286d2f02() 139 { "size", [](const InspectorNode& inspector) { return inspector.GetSize(); } },
__anone729286d3002() 140 { "useSizeType", [](const InspectorNode& inspector) { return inspector.GetUseSizeType(); } },
__anone729286d3102() 141 { "rotate", [](const InspectorNode& inspector) { return inspector.GetRotate(); } },
__anone729286d3202() 142 { "scale", [](const InspectorNode& inspector) { return inspector.GetScale(); } },
__anone729286d3302() 143 { "transform", [](const InspectorNode& inspector) { return inspector.GetTransform(); } },
__anone729286d3402() 144 { "translate", [](const InspectorNode& inspector) { return inspector.GetTranslate(); } },
__anone729286d3502() 145 { "markAnchor", [](const InspectorNode& inspector) { return inspector.GetMarkAnchor(); } },
__anone729286d3602() 146 { "mask", [](const InspectorNode& inspector) { return inspector.GetMask(); } },
__anone729286d3702() 147 { "overlay", [](const InspectorNode& inspector) { return inspector.GetOverlay(); } },
__anone729286d3802() 148 { "border", [](const InspectorNode& inspector) { return inspector.GetUnifyBorder(); } },
__anone729286d3902() 149 { "linearGradient", [](const InspectorNode& inspector) { return inspector.GetLinearGradient(); } },
__anone729286d3a02() 150 { "sweepGradient", [](const InspectorNode& inspector) { return inspector.GetSweepGradient(); } },
__anone729286d3b02() 151 { "radialGradient", [](const InspectorNode& inspector) { return inspector.GetRadialGradient(); } },
152 };
153
154 const std::unordered_map<std::string, BoolJsonFunc> CREATE_XTS_BOOL_MAP {
__anone729286d3c02() 155 { "clickable", [](const InspectorNode& inspector) { return inspector.GetClickable(); } },
__anone729286d3d02() 156 { "checkable", [](const InspectorNode& inspector) { return inspector.GetCheckable(); } },
__anone729286d3e02() 157 { "scrollable", [](const InspectorNode& inspector) { return inspector.GetScrollable(); } },
__anone729286d3f02() 158 { "long-clickable", [](const InspectorNode& inspector) { return inspector.GetLongClickable(); } },
__anone729286d4002() 159 { "selected", [](const InspectorNode& inspector) { return inspector.IsSelected(); } },
__anone729286d4102() 160 { "password", [](const InspectorNode& inspector) { return inspector.IsPassword(); } },
__anone729286d4202() 161 { "checked", [](const InspectorNode& inspector) { return inspector.IsChecked(); } },
__anone729286d4302() 162 { "focused", [](const InspectorNode& inspector) { return inspector.IsFocused(); } },
163 };
164
165 const std::unordered_map<std::string, IntJsonFunc> CREATE_XTS_INT_MAP {
__anone729286d4402() 166 { "layoutPriority", [](const InspectorNode& inspector) { return inspector.GetLayoutPriority(); } },
167 };
168
169 const std::unordered_map<std::string, JsonValueJsonFunc> CREATE_XTS_JSON_VALUE_MAP {
__anone729286d4502() 170 { "windowBlur", [](const InspectorNode& inspector) { return inspector.GetWindowBlur(); } },
__anone729286d4602() 171 { "useAlign", [](const InspectorNode& inspector) { return inspector.GetUseAlign(); } },
172 };
173
174 }; // namespace
175
InspectorComposedElement(const ComposeId & id)176 InspectorComposedElement::InspectorComposedElement(const ComposeId& id) : ComposedElement(id) {}
177
~InspectorComposedElement()178 InspectorComposedElement::~InspectorComposedElement()
179 {
180 if (inspectorId_ == -1) {
181 return;
182 }
183 accessibilityNode_.Reset();
184 RemoveInspectorNode(inspectorId_);
185 }
186
OnInactive()187 void InspectorComposedElement::OnInactive()
188 {
189 accessibilityNode_.Reset();
190 RemoveInspectorNode(inspectorId_);
191 inspectorId_ = -1;
192 }
193
OnActive()194 void InspectorComposedElement::OnActive()
195 {
196 inspectorId_ = GetCurrentInspectorId();
197 }
198
GetElementChildBySlot(const RefPtr<Element> & element,int32_t & slot) const199 RefPtr<Element> InspectorComposedElement::GetElementChildBySlot(const RefPtr<Element>& element, int32_t& slot) const
200 {
201 if (!element) {
202 return nullptr;
203 }
204 auto child = element->GetChildBySlot(slot);
205 if (!child) {
206 slot = DEFAULT_ELEMENT_SLOT;
207 child = element->GetChildBySlot(slot);
208 }
209 return child;
210 }
211
GetInspectorComposedElementParent(const RefPtr<Element> & element) const212 RefPtr<Element> InspectorComposedElement::GetInspectorComposedElementParent(const RefPtr<Element>& element) const
213 {
214 if (!element) {
215 return nullptr;
216 }
217 for (const auto& child : element->GetChildren()) {
218 auto inspectorComposedElement = AceType::DynamicCast<InspectorComposedElement>(child);
219 if (inspectorComposedElement) {
220 return element;
221 }
222 auto element_ = GetInspectorComposedElementParent(child);
223 if (element_) {
224 return element_;
225 }
226 }
227 return nullptr;
228 }
229
ToJsonObject() const230 std::unique_ptr<JsonValue> InspectorComposedElement::ToJsonObject() const
231 {
232 auto resultJson = JsonUtil::Create(true);
233 for (const auto& value : CREATE_JSON_DOUBLE_MAP) {
234 resultJson->Put(value.first.c_str(), value.second(*this));
235 }
236 for (const auto& value : CREATE_JSON_STRING_MAP) {
237 resultJson->Put(value.first.c_str(), value.second(*this).c_str());
238 }
239 for (const auto& value : CREATE_JSON_BOOL_MAP) {
240 resultJson->Put(value.first.c_str(), value.second(*this));
241 }
242 for (const auto& value : CREATE_JSON_INT_MAP) {
243 resultJson->Put(value.first.c_str(), value.second(*this));
244 }
245
246 for (const auto& value : CREATE_JSON_JSON_VALUE_MAP) {
247 resultJson->Put(value.first.c_str(), value.second(*this));
248 }
249 #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM)
250 for (const auto& value : CREATE_XTS_BOOL_MAP) {
251 resultJson->Put(value.first.c_str(), value.second(*this));
252 }
253 for (const auto& value : CREATE_XTS_INT_MAP) {
254 resultJson->Put(value.first.c_str(), value.second(*this));
255 }
256 for (const auto& value : CREATE_XTS_JSON_VALUE_MAP) {
257 resultJson->Put(value.first.c_str(), value.second(*this));
258 }
259 #endif
260 return resultJson;
261 }
262
Prepare(const WeakPtr<Element> & weakParent)263 void InspectorComposedElement::Prepare(const WeakPtr<Element>& weakParent)
264 {
265 accessibilityEnabled_ = false;
266 #if defined(WINDOWS_PLATFORM) || defined(MAC_PLATFORM)
267 accessibilityEnabled_ = true;
268 #else
269 if (AceApplicationInfo::GetInstance().IsAccessibilityEnabled() || SystemProperties::GetAccessibilityEnabled()) {
270 accessibilityEnabled_ = true;
271 }
272 #endif
273 if (accessibilityEnabled_) {
274 auto parent = weakParent.Upgrade();
275 RefPtr<InspectorComposedElement> inspectorParent;
276 while (parent) {
277 inspectorParent = DynamicCast<InspectorComposedElement>(parent);
278 if (inspectorParent) {
279 break;
280 }
281 parent = parent->GetElementParent().Upgrade();
282 }
283 if (inspectorParent) {
284 inspectorParentId_ = inspectorParent->inspectorId_;
285 }
286 }
287 AddComposedComponentId();
288 }
289
Update()290 void InspectorComposedElement::Update()
291 {
292 ComposedElement::Update();
293 auto component = DynamicCast<V2::InspectorComposedComponent>(component_);
294 if (!component) {
295 LOGE("fail to update due to component is null");
296 return;
297 }
298 auto inspectorFunctionImpl = component->GetInspectorFunctionImpl();
299 inspectorFunctionImpl->SetUpdateEventInfoImpl([weak = WeakClaim(this)](BaseEventInfo& info) {
300 auto composedElement = weak.Upgrade();
301 if (composedElement) {
302 composedElement->UpdateEventTarget(info);
303 }
304 });
305 if (accessibilityNode_) {
306 accessibilityNode_->SetAccessible(component->IsAccessibilityGroup());
307 accessibilityNode_->SetAccessibilityLabel(component->GetAccessibilitytext());
308 accessibilityNode_->SetAccessibilityHint(component->GetAccessibilityDescription());
309 accessibilityNode_->SetImportantForAccessibility(component->GetAccessibilityImportance());
310 accessibilityNode_->SetFocusChangeEventMarker(component->GetAccessibilityEvent());
311 }
312 }
313
CanUpdate(const RefPtr<Component> & newComponent)314 bool InspectorComposedElement::CanUpdate(const RefPtr<Component>& newComponent)
315 {
316 return Element::CanUpdate(newComponent);
317 }
318
AddComposedComponentId()319 void InspectorComposedElement::AddComposedComponentId()
320 {
321 auto context = context_.Upgrade();
322 if (context == nullptr) {
323 LOGW("get context failed");
324 return;
325 }
326 auto accessibilityManager = context->GetAccessibilityManager();
327 if (!accessibilityManager) {
328 LOGW("get AccessibilityManager failed");
329 return;
330 }
331 accessibilityManager->AddComposedElement(std::to_string(inspectorId_), AceType::Claim(this));
332 if (accessibilityEnabled_) {
333 accessibilityNode_ = InspectorComposedComponent::CreateAccessibilityNode(
334 inspectorTag_, inspectorId_, inspectorParentId_, GetRenderSlot());
335 accessibilityNode_->SetJsComponentId(key_);
336 }
337 }
338
RemoveInspectorNode(int32_t id)339 void InspectorComposedElement::RemoveInspectorNode(int32_t id)
340 {
341 auto context = context_.Upgrade();
342 if (context == nullptr) {
343 LOGW("get context failed");
344 return;
345 }
346 auto accessibilityManager = context->GetAccessibilityManager();
347 if (!accessibilityManager) {
348 LOGW("get AccessibilityManager failed");
349 return;
350 }
351 accessibilityManager->RemoveComposedElementById(std::to_string(id));
352 if (accessibilityEnabled_) {
353 accessibilityManager->RemoveAccessibilityNodeById(id);
354 }
355 }
356
GetInspectorNode(IdType typeId,bool isForward) const357 RefPtr<RenderNode> InspectorComposedElement::GetInspectorNode(IdType typeId, bool isForward) const
358 {
359 if (isForward) {
360 auto parent = GetElementParent().Upgrade();
361 while (parent) {
362 if (AceType::TypeId(parent) == typeId) {
363 return parent->GetRenderNode();
364 }
365 parent = parent->GetElementParent().Upgrade();
366 }
367 return nullptr;
368 }
369 auto child = children_.empty() ? nullptr : children_.front();
370 while (child) {
371 if (AceType::TypeId(child) == typeId) {
372 return child->GetRenderNode();
373 }
374 child = child->GetChildren().empty() ? nullptr : child->GetChildren().front();
375 }
376 return nullptr;
377 }
378
GetAccessibilityNode() const379 RefPtr<AccessibilityNode> InspectorComposedElement::GetAccessibilityNode() const
380 {
381 auto context = context_.Upgrade();
382 if (context == nullptr) {
383 LOGW("get context failed");
384 return nullptr;
385 }
386 auto accessibilityManager = context->GetAccessibilityManager();
387 if (!accessibilityManager) {
388 LOGW("get AccessibilityManager failed");
389 return nullptr;
390 }
391 return accessibilityManager->GetAccessibilityNodeById(StringUtils::StringToInt(id_));
392 }
393
GetRenderBox() const394 RefPtr<RenderBox> InspectorComposedElement::GetRenderBox() const
395 {
396 auto node = GetInspectorNode(BoxElement::TypeId());
397 if (!node) {
398 return nullptr;
399 }
400 return AceType::DynamicCast<RenderBox>(node);
401 }
402
GetWidth() const403 std::string InspectorComposedElement::GetWidth() const
404 {
405 auto render = GetRenderBox();
406 if (render) {
407 Dimension value = render->GetWidthDimension();
408 if (value.Value() == -1) {
409 return "-";
410 }
411 return value.ToString();
412 }
413 return "-";
414 }
415
GetHeight() const416 std::string InspectorComposedElement::GetHeight() const
417 {
418 auto render = GetRenderBox();
419 if (render) {
420 Dimension value = render->GetHeightDimension();
421 if (value.Value() == -1) {
422 return "-";
423 }
424 return value.ToString();
425 }
426 return "-";
427 }
428
GetSize() const429 std::unique_ptr<JsonValue> InspectorComposedElement::GetSize() const
430 {
431 auto jsonValue = JsonUtil::Create(true);
432 jsonValue->Put("width", GetWidth().c_str());
433 jsonValue->Put("height", GetHeight().c_str());
434 return jsonValue;
435 }
436
GetPadding() const437 std::string InspectorComposedElement::GetPadding() const
438 {
439 auto render = GetRenderBox();
440 if (render) {
441 auto top = render->GetPadding(DimensionHelper(&Edge::SetTop, &Edge::Top));
442 auto right = render->GetPadding(DimensionHelper(&Edge::SetRight, &Edge::Right));
443 auto bottom = render->GetPadding(DimensionHelper(&Edge::SetBottom, &Edge::Bottom));
444 auto left = render->GetPadding(DimensionHelper(&Edge::SetLeft, &Edge::Left));
445 if (top == right && right == bottom && bottom == left) {
446 return top.ToString();
447 } else {
448 auto jsonValue = JsonUtil::Create(true);
449 jsonValue->Put("top", top.ToString().c_str());
450 jsonValue->Put("right", right.ToString().c_str());
451 jsonValue->Put("bottom", bottom.ToString().c_str());
452 jsonValue->Put("left", left.ToString().c_str());
453 return jsonValue->ToString();
454 }
455 }
456 return "0.0";
457 }
458
GetAllMargin() const459 std::string InspectorComposedElement::GetAllMargin() const
460 {
461 auto render = GetRenderBox();
462 if (render) {
463 auto top = render->GetMargin(DimensionHelper(&Edge::SetTop, &Edge::Top));
464 auto right = render->GetMargin(DimensionHelper(&Edge::SetRight, &Edge::Right));
465 auto bottom = render->GetMargin(DimensionHelper(&Edge::SetBottom, &Edge::Bottom));
466 auto left = render->GetMargin(DimensionHelper(&Edge::SetLeft, &Edge::Left));
467 if (top == right && right == bottom && bottom == left) {
468 return top.ToString().c_str();
469 } else {
470 auto jsonValue = JsonUtil::Create(true);
471 jsonValue->Put("top", top.ToString().c_str());
472 jsonValue->Put("right", right.ToString().c_str());
473 jsonValue->Put("bottom", bottom.ToString().c_str());
474 jsonValue->Put("left", left.ToString().c_str());
475 return jsonValue->ToString();
476 }
477 }
478 return "0.0";
479 }
480
GetMargin(OHOS::Ace::AnimatableType type) const481 Dimension InspectorComposedElement::GetMargin(OHOS::Ace::AnimatableType type) const
482 {
483 auto render = GetRenderBox();
484 if (render) {
485 if (type == AnimatableType::PROPERTY_MARGIN_LEFT) {
486 return render->GetMargin(DimensionHelper(&Edge::SetLeft, &Edge::Left));
487 } else if (type == AnimatableType::PROPERTY_MARGIN_TOP) {
488 return render->GetMargin(DimensionHelper(&Edge::SetTop, &Edge::Top));
489 } else if (type == AnimatableType::PROPERTY_MARGIN_RIGHT) {
490 return render->GetMargin(DimensionHelper(&Edge::SetRight, &Edge::Right));
491 } else if (type == AnimatableType::PROPERTY_MARGIN_BOTTOM) {
492 return render->GetMargin(DimensionHelper(&Edge::SetBottom, &Edge::Bottom));
493 }
494 }
495 return Dimension();
496 }
497
GetConstraintSize() const498 std::string InspectorComposedElement::GetConstraintSize() const
499 {
500 LayoutParam layoutParam = LayoutParam(Size(), Size());
501 auto render = GetRenderBox();
502 if (render) {
503 layoutParam = render->GetConstraints();
504 }
505 auto jsonStr = JsonUtil::Create(true);
506 Dimension minWidth = Dimension(SystemProperties::Px2Vp(layoutParam.GetMinSize().Width()), DimensionUnit::VP);
507 Dimension minHeight = Dimension(SystemProperties::Px2Vp(layoutParam.GetMinSize().Height()), DimensionUnit::VP);
508 Dimension maxWidth = Dimension(SystemProperties::Px2Vp(layoutParam.GetMaxSize().Width()), DimensionUnit::VP);
509 Dimension maxHeight = Dimension(SystemProperties::Px2Vp(layoutParam.GetMaxSize().Height()), DimensionUnit::VP);
510 jsonStr->Put("minWidth", minWidth.ToString().c_str());
511 jsonStr->Put("minHeight", minHeight.ToString().c_str());
512 jsonStr->Put("maxWidth", maxWidth.ToString().c_str());
513 jsonStr->Put("maxHeight", maxHeight.ToString().c_str());
514 return jsonStr->ToString();
515 }
516
GetLayoutPriority() const517 int32_t InspectorComposedElement::GetLayoutPriority() const
518 {
519 auto render = GetRenderBox();
520 if (render) {
521 return render->GetDisplayIndex();
522 }
523 return 0;
524 }
525
GetLayoutWeight() const526 int32_t InspectorComposedElement::GetLayoutWeight() const
527 {
528 auto node = GetInspectorNode(FlexItemElement::TypeId());
529 if (!node) {
530 return 0;
531 }
532 auto render = AceType::DynamicCast<RenderFlexItem>(node);
533 if (render) {
534 return render->GetFlexWeight();
535 }
536 return 0;
537 }
538
GetAlign() const539 std::string InspectorComposedElement::GetAlign() const
540 {
541 auto render = GetRenderBox();
542 if (render) {
543 auto align = render->GetAlign();
544 int32_t h = align.GetHorizontal() + 1;
545 int32_t v = align.GetVertical() + 1;
546 return ALIGNMENT_TYPE[h][v];
547 }
548 return ALIGNMENT_TYPE[1][1];
549 }
550
GetDirectionStr() const551 std::string InspectorComposedElement::GetDirectionStr() const
552 {
553 auto render = GetRenderBox();
554 if (!render) {
555 return TEXT_DIRECTION[3];
556 }
557 auto value = static_cast<int32_t>(render->GetInspectorDirection());
558 auto length = static_cast<int32_t>(sizeof(TEXT_DIRECTION) / sizeof(TEXT_DIRECTION[0]));
559 if (value < length) {
560 return TEXT_DIRECTION[value];
561 }
562 return TEXT_DIRECTION[3];
563 }
564
GetDirection() const565 TextDirection InspectorComposedElement::GetDirection() const
566 {
567 auto render = GetRenderBox();
568 if (render) {
569 return render->GetTextDirection();
570 }
571 return TextDirection::AUTO;
572 }
573
GetBorderRadius() const574 std::string InspectorComposedElement::GetBorderRadius() const
575 {
576 auto value = GetBorder().TopLeftRadius().GetX().Value();
577 if (value == 0.0) {
578 return "0.0vp";
579 }
580 return GetBorder().TopLeftRadius().GetX().ToString();
581 }
582
GetUnifyBorder() const583 std::unique_ptr<JsonValue> InspectorComposedElement::GetUnifyBorder() const
584 {
585 auto jsonValue = JsonUtil::Create(true);
586 jsonValue->Put("width", GetBorderWidth().c_str());
587 jsonValue->Put("color", GetBorderColor().c_str());
588 jsonValue->Put("radius", GetBorderRadius().c_str());
589 jsonValue->Put("style", GetBorderStyle().c_str());
590 return jsonValue;
591 }
592
GetPosition() const593 std::unique_ptr<JsonValue> InspectorComposedElement::GetPosition() const
594 {
595 auto jsonValue = JsonUtil::Create(true);
596 auto node = GetInspectorNode(FlexItemElement::TypeId());
597 if (!node) {
598 jsonValue->Put("x", "0.0px");
599 jsonValue->Put("y", "0.0px");
600 return jsonValue;
601 }
602 auto render = AceType::DynamicCast<RenderFlexItem>(node);
603 if (render) {
604 PositionType type = render->GetPositionType();
605 if (type == PositionType::ABSOLUTE) {
606 jsonValue->Put("x", render->GetLeft().ToString().c_str());
607 jsonValue->Put("y", render->GetTop().ToString().c_str());
608 return jsonValue;
609 }
610 }
611 jsonValue->Put("x", "0.0px");
612 jsonValue->Put("y", "0.0px");
613 return jsonValue;
614 }
615
GetMarkAnchor() const616 std::unique_ptr<JsonValue> InspectorComposedElement::GetMarkAnchor() const
617 {
618 auto jsonValue = JsonUtil::Create(true);
619 auto node = GetInspectorNode(FlexItemElement::TypeId());
620 if (!node) {
621 jsonValue->Put("x", "0.0px");
622 jsonValue->Put("y", "0.0px");
623 return jsonValue;
624 }
625 auto render = AceType::DynamicCast<RenderFlexItem>(node);
626 if (render) {
627 jsonValue->Put("x", render->GetAnchorX().ToString().c_str());
628 jsonValue->Put("y", render->GetAnchorY().ToString().c_str());
629 return jsonValue;
630 }
631 jsonValue->Put("x", "0.0px");
632 jsonValue->Put("y", "0.0px");
633 return jsonValue;
634 }
635
GetOffset() const636 std::unique_ptr<JsonValue> InspectorComposedElement::GetOffset() const
637 {
638 auto jsonValue = JsonUtil::Create(true);
639 auto node = GetInspectorNode(FlexItemElement::TypeId());
640 if (!node) {
641 jsonValue->Put("x", "0.0px");
642 jsonValue->Put("y", "0.0px");
643 return jsonValue;
644 }
645 auto render = AceType::DynamicCast<RenderFlexItem>(node);
646 if (render) {
647 PositionType type = render->GetPositionType();
648 if (type == PositionType::OFFSET) {
649 jsonValue->Put("x", render->GetLeft().ToString().c_str());
650 jsonValue->Put("y", render->GetTop().ToString().c_str());
651 return jsonValue;
652 }
653 }
654 jsonValue->Put("x", "0.0px");
655 jsonValue->Put("y", "0.0px");
656 return jsonValue;
657 }
658
GetRect()659 std::string InspectorComposedElement::GetRect()
660 {
661 std::string strRec;
662 Rect rect = GetRenderRect();
663
664 if (accessibilityNode_ && accessibilityNode_->GetParentNode()) {
665 auto parent = accessibilityNode_->GetParentNode();
666 if (parent->GetClipFlag()) {
667 rect = rect.Constrain(parent->GetRect());
668 }
669 }
670 if (GetClipFlag()) {
671 accessibilityNode_->SetClipFlagToChild(true);
672 }
673
674 isRectValid_ = rect.IsValid();
675 if (!isRectValid_) {
676 rect.SetRect(0, 0, 0, 0);
677 }
678
679 if (accessibilityNode_) {
680 auto render = AceType::DynamicCast<RenderTransform>(GetInspectorNode(TransformElement::TypeId()));
681 if (render || accessibilityNode_->GetMatrix4Flag()) {
682 if (render) {
683 auto transformNow = render->GetTransformMatrix(render->GetTransitionPaintRect().GetOffset());
684 accessibilityNode_->SetTransformToChild(transformNow);
685 }
686 Matrix4 transform = accessibilityNode_->GetMatrix4();
687 rect = accessibilityNode_->GetRectWithTransform(rect, transform);
688 }
689 }
690
691 strRec = std::to_string(rect.Left())
692 .append(",")
693 .append(std::to_string(rect.Top()))
694 .append(",")
695 .append(std::to_string(rect.Width()))
696 .append(",")
697 .append(std::to_string(rect.Height()));
698 return strRec;
699 }
700
GetParentRect() const701 Rect InspectorComposedElement::GetParentRect() const
702 {
703 auto parent = GetElementParent().Upgrade();
704 if (!parent) {
705 return Rect();
706 }
707 Rect parentRect = parent->GetRenderRect();
708 return parentRect;
709 }
710
GetAspectRatio() const711 double InspectorComposedElement::GetAspectRatio() const
712 {
713 auto render = GetRenderBox();
714 if (render) {
715 return render->GetAspectRatio();
716 }
717 return 0.0;
718 }
719
GetDisplayPriority() const720 int32_t InspectorComposedElement::GetDisplayPriority() const
721 {
722 auto node = GetInspectorNode(FlexItemElement::TypeId());
723 if (!node) {
724 return 1;
725 }
726 auto render = AceType::DynamicCast<RenderFlexItem>(node);
727 if (render) {
728 return render->GetDisplayIndex();
729 }
730 return 1;
731 }
732
GetFlexBasis() const733 std::string InspectorComposedElement::GetFlexBasis() const
734 {
735 auto render = AceType::DynamicCast<RenderFlexItem>(GetInspectorNode(FlexItemElement::TypeId()));
736 if (render) {
737 auto flexBasis = render->GetFlexBasis();
738 return flexBasis.IsValid() ? render->GetFlexBasis().ToString() : "auto";
739 }
740 return "auto";
741 }
742
GetFlexGrow() const743 double InspectorComposedElement::GetFlexGrow() const
744 {
745 auto render = AceType::DynamicCast<RenderFlexItem>(GetInspectorNode(FlexItemElement::TypeId()));
746 if (render) {
747 return render->GetFlexGrow();
748 }
749 return 0.0;
750 }
751
GetFlexShrink() const752 double InspectorComposedElement::GetFlexShrink() const
753 {
754 auto render = AceType::DynamicCast<RenderFlexItem>(GetInspectorNode(FlexItemElement::TypeId()));
755 if (render) {
756 return render->GetFlexShrink();
757 }
758 return 0.0;
759 }
760
GetAlignSelf() const761 std::string InspectorComposedElement::GetAlignSelf() const
762 {
763 auto render = AceType::DynamicCast<RenderFlexItem>(GetInspectorNode(FlexItemElement::TypeId()));
764 if (render) {
765 return ITEM_ALIGN[static_cast<int32_t>(render->GetAlignSelf())];
766 }
767 return ITEM_ALIGN[0];
768 }
769
GetBorder() const770 Border InspectorComposedElement::GetBorder() const
771 {
772 auto render = GetRenderBox();
773 if (!render) {
774 return Border();
775 }
776 auto decoration = render->GetBackDecoration();
777 if (decoration) {
778 return decoration->GetBorder();
779 }
780 return Border();
781 }
782
GetBorderStyle() const783 std::string InspectorComposedElement::GetBorderStyle() const
784 {
785 auto border = GetBorder();
786 int32_t style = static_cast<int32_t>(border.Left().GetBorderStyle());
787 return BORDER_STYLE[style];
788 }
789
GetBorderWidth() const790 std::string InspectorComposedElement::GetBorderWidth() const
791 {
792 auto border = GetBorder();
793 auto borderWidth = border.Left().GetWidth();
794 if (NearZero(borderWidth.Value())) {
795 return "0.00vp";
796 }
797 return borderWidth.ToString();
798 }
799
GetBorderColor() const800 std::string InspectorComposedElement::GetBorderColor() const
801 {
802 auto border = GetBorder();
803 return border.Left().GetColor().ColorToString();
804 }
805
GetBackDecoration() const806 RefPtr<Decoration> InspectorComposedElement::GetBackDecoration() const
807 {
808 auto render = GetRenderBox();
809 if (!render) {
810 return nullptr;
811 }
812 return render->GetBackDecoration();
813 }
814
GetBackgroundImage() const815 std::string InspectorComposedElement::GetBackgroundImage() const
816 {
817 auto backDecoration = GetBackDecoration();
818 if (!backDecoration) {
819 return "NONE";
820 }
821 auto image = backDecoration->GetImage();
822 if (!image) {
823 return "NONE";
824 }
825 auto imageRepeat = image->GetImageRepeat();
826 if (imageRepeat == ImageRepeat::REPEATX) {
827 return image->GetSrc() + ", ImageRepeat.X";
828 } else if (imageRepeat == ImageRepeat::REPEATY) {
829 return image->GetSrc() + ", ImageRepeat.Y";
830 } else if (imageRepeat == ImageRepeat::REPEAT) {
831 return image->GetSrc() + ", ImageRepeat.XY";
832 }
833 return image->GetSrc() + ", ImageRepeat.NoRepeat";
834 }
835
GetBackgroundColor() const836 std::string InspectorComposedElement::GetBackgroundColor() const
837 {
838 auto jsonValue = JsonUtil::Create(true);
839 auto backDecoration = GetBackDecoration();
840 if (!backDecoration) {
841 return "NONE";
842 }
843 auto color = backDecoration->GetBackgroundColor();
844 return color.ColorToString();
845 }
846
GetBackgroundImageSize() const847 std::string InspectorComposedElement::GetBackgroundImageSize() const
848 {
849 auto backDecoration = GetBackDecoration();
850 if (!backDecoration) {
851 return "ImageSize.Auto";
852 }
853 auto image = backDecoration->GetImage();
854 if (!image) {
855 return "ImageSize.Auto";
856 }
857 auto widthType = image->GetImageSize().GetSizeTypeX();
858 if (widthType == BackgroundImageSizeType::CONTAIN) {
859 return "ImageSize.Contain";
860 } else if (widthType == BackgroundImageSizeType::COVER) {
861 return "ImageSize.Cover";
862 } else if (widthType == BackgroundImageSizeType::AUTO) {
863 return "ImageSize.Auto";
864 }
865 auto jsonValue = JsonUtil::Create(true);
866 Dimension width = Dimension((image->GetImageSize().GetSizeValueX()), DimensionUnit::VP);
867 Dimension height = Dimension((image->GetImageSize().GetSizeValueY()), DimensionUnit::VP);
868 jsonValue->Put("width", width.ToString().c_str());
869 jsonValue->Put("height", height.ToString().c_str());
870 return jsonValue->ToString();
871 }
872
GetBackgroundImagePosition() const873 std::string InspectorComposedElement::GetBackgroundImagePosition() const
874 {
875 auto jsonValue = JsonUtil::Create(true);
876 auto backDecoration = GetBackDecoration();
877 if (!backDecoration) {
878 jsonValue->Put("x", 0.0);
879 jsonValue->Put("y", 0.0);
880 return jsonValue->ToString();
881 }
882 auto image = backDecoration->GetImage();
883 if (!image) {
884 jsonValue->Put("x", 0.0);
885 jsonValue->Put("y", 0.0);
886 return jsonValue->ToString();
887 }
888 if (image->GetImagePosition().GetSizeTypeX() == BackgroundImagePositionType::PX) {
889 auto width = image->GetImagePosition().GetSizeValueX();
890 auto height = image->GetImagePosition().GetSizeValueY();
891 jsonValue->Put("x", width);
892 jsonValue->Put("y", height);
893 return jsonValue->ToString();
894 } else {
895 auto width = image->GetImagePosition().GetSizeValueX();
896 auto height = image->GetImagePosition().GetSizeValueY();
897 return GetAlignmentType(width, height);
898 }
899 }
900
GetAlignmentType(double width,double height) const901 std::string InspectorComposedElement::GetAlignmentType(double width, double height) const
902 {
903 auto jsonValue = JsonUtil::Create(true);
904 if (NearZero(width)) {
905 if (NearZero(height)) {
906 return "Alignment.TopStart";
907 } else if (NearEqual(height, 50.0)) { // Determine whether the vertical element is centered
908 return "Alignment.Start";
909 } else {
910 return "Alignment.BottomStart";
911 }
912 } else if (NearEqual(width, 50.0)) { // Judge whether the horizontal element is centered
913 if (NearZero(height)) {
914 return "Alignment.Top";
915 } else if (NearEqual(height, 50)) {
916 return "Alignment.Center";
917 } else {
918 return "Alignment.Bottom";
919 }
920 } else {
921 if (NearZero(height)) {
922 return "Alignment.TopEnd";
923 } else if (NearEqual(height, 50.0)) {
924 return "Alignment.BottomEnd";
925 } else {
926 return "Alignment.BottomEnd";
927 }
928 }
929 }
930
GetFrontDecoration() const931 RefPtr<Decoration> InspectorComposedElement::GetFrontDecoration() const
932 {
933 auto render = GetRenderBox();
934 if (!render) {
935 return nullptr;
936 }
937 return render->GetFrontDecoration();
938 }
939
GetOpacity() const940 double InspectorComposedElement::GetOpacity() const
941 {
942 auto node = GetInspectorNode(DisplayElement::TypeId());
943 if (!node) {
944 return 1.0;
945 }
946 auto render = AceType::DynamicCast<RenderDisplay>(node);
947 if (!render) {
948 return 1.0;
949 }
950 return render->GetTransitionOpacity();
951 }
952
GetVisibility() const953 std::string InspectorComposedElement::GetVisibility() const
954 {
955 auto node = GetInspectorNode(DisplayElement::TypeId());
956 if (!node) {
957 return VISIBLE_TYPE[static_cast<int32_t>(VisibleType::VISIBLE)];
958 }
959 auto render = AceType::DynamicCast<RenderDisplay>(node);
960 if (!render) {
961 return VISIBLE_TYPE[static_cast<int32_t>(VisibleType::VISIBLE)];
962 }
963 return VISIBLE_TYPE[static_cast<int32_t>(render->GetVisibleType())];
964 }
965
GetClip() const966 std::string InspectorComposedElement::GetClip() const
967 {
968 auto render = GetRenderBox();
969 if (!render) {
970 return "false";
971 }
972 auto clipPath = render->GetClipPath();
973 auto jsonValue = JsonUtil::Create(true);
974 if (clipPath && clipPath->GetBasicShape()) {
975 int32_t shapeType = static_cast<int32_t>(clipPath->GetBasicShape()->GetBasicShapeType());
976 int32_t size = static_cast<int32_t>(sizeof(BASIC_SHAPE_TYPE) / sizeof(BASIC_SHAPE_TYPE[0]));
977 if (shapeType < size) {
978 jsonValue->Put("shape", BASIC_SHAPE_TYPE[shapeType]);
979 }
980 } else {
981 if (render->GetBoxClipFlag() == true) {
982 return "true";
983 } else {
984 return "false";
985 }
986 }
987 return jsonValue->ToString();
988 }
989
GetClipFlag() const990 bool InspectorComposedElement::GetClipFlag() const
991 {
992 auto render = GetRenderBox();
993 if (!render) {
994 return false;
995 }
996
997 return render->GetBoxClipFlag();
998 }
999
GetEnabled() const1000 bool InspectorComposedElement::GetEnabled() const
1001 {
1002 auto node = GetInspectorNode(GetTargetTypeId());
1003 if (!node) {
1004 return true;
1005 }
1006 return !node->IsDisabled();
1007 }
1008
GetZIndex() const1009 int32_t InspectorComposedElement::GetZIndex() const
1010 {
1011 auto node = GetInspectorNode(GetTargetTypeId());
1012 if (!node) {
1013 return 0;
1014 }
1015 return node->GetZIndex();
1016 }
1017
GetOriginPoint() const1018 DimensionOffset InspectorComposedElement::GetOriginPoint() const
1019 {
1020 auto node = GetInspectorNode(TransformElement::TypeId());
1021 if (!node) {
1022 return DimensionOffset(OHOS::Ace::HALF_PERCENT, OHOS::Ace::HALF_PERCENT);
1023 }
1024 auto render = AceType::DynamicCast<RenderTransform>(node);
1025 if (!render) {
1026 return DimensionOffset(OHOS::Ace::HALF_PERCENT, OHOS::Ace::HALF_PERCENT);
1027 }
1028 return render->GetTransformOrigin();
1029 }
1030
GetRotate() const1031 std::unique_ptr<JsonValue> InspectorComposedElement::GetRotate() const
1032 {
1033 auto render = AceType::DynamicCast<RenderTransform>(GetInspectorNode(TransformElement::TypeId()));
1034 auto jsonValue = JsonUtil::Create(true);
1035 if (!render) {
1036 return jsonValue;
1037 }
1038 for (const auto& operation : render->GetTransformEffects().GetOperations()) {
1039 if (operation.type_ == TransformOperationType::ROTATE) {
1040 const auto& rotate = operation.rotateOperation_;
1041 jsonValue->Put("x", std::to_string(rotate.dx).c_str());
1042 jsonValue->Put("y", std::to_string(rotate.dy).c_str());
1043 jsonValue->Put("z", std::to_string(rotate.dz).c_str());
1044 jsonValue->Put("angle", std::to_string(rotate.angle).c_str());
1045 jsonValue->Put("centerX", render->GetOriginX().ToString().c_str());
1046 jsonValue->Put("centerY", render->GetOriginY().ToString().c_str());
1047 break;
1048 }
1049 }
1050 return jsonValue;
1051 }
1052
GetScale() const1053 std::unique_ptr<JsonValue> InspectorComposedElement::GetScale() const
1054 {
1055 auto render = AceType::DynamicCast<RenderTransform>(GetInspectorNode(TransformElement::TypeId()));
1056 auto jsonValue = JsonUtil::Create(true);
1057 if (!render) {
1058 return jsonValue;
1059 }
1060 for (const auto& operation : render->GetTransformEffects().GetOperations()) {
1061 if (operation.type_ == TransformOperationType::SCALE) {
1062 const auto& scale = operation.scaleOperation_;
1063 jsonValue->Put("x", std::to_string(scale.scaleX).c_str());
1064 jsonValue->Put("y", std::to_string(scale.scaleY).c_str());
1065 jsonValue->Put("z", std::to_string(scale.scaleZ).c_str());
1066 jsonValue->Put("centerX", render->GetOriginX().ToString().c_str());
1067 jsonValue->Put("centerY", render->GetOriginY().ToString().c_str());
1068 break;
1069 }
1070 }
1071 return jsonValue;
1072 }
1073
GetTransform() const1074 std::unique_ptr<JsonValue> InspectorComposedElement::GetTransform() const
1075 {
1076 auto render = AceType::DynamicCast<RenderTransform>(GetInspectorNode(TransformElement::TypeId()));
1077 auto jsonValue = JsonUtil::Create(true);
1078 if (!render) {
1079 return jsonValue;
1080 }
1081 for (const auto& operation : render->GetTransformEffects().GetOperations()) {
1082 if (operation.type_ == TransformOperationType::MATRIX) {
1083 const auto& matrix = operation.matrix4_;
1084 jsonValue->Put("type", "matrix");
1085 auto matrixString = matrix.ToString();
1086 while (matrixString.find("\n") != std::string::npos) {
1087 auto num = matrixString.find("\n");
1088 matrixString.replace(num, 1, "");
1089 }
1090 jsonValue->Put("matrix", matrixString.c_str());
1091 break;
1092 }
1093 }
1094 return jsonValue;
1095 }
1096
GetTranslate() const1097 std::unique_ptr<JsonValue> InspectorComposedElement::GetTranslate() const
1098 {
1099 auto render = AceType::DynamicCast<RenderTransform>(GetInspectorNode(TransformElement::TypeId()));
1100 auto jsonValue = JsonUtil::Create(true);
1101 if (!render) {
1102 return jsonValue;
1103 }
1104 for (const auto& operation : render->GetTransformEffects().GetOperations()) {
1105 if (operation.type_ == TransformOperationType::TRANSLATE) {
1106 const auto& translate = operation.translateOperation_;
1107 jsonValue->Put("x", translate.dx.ToString().c_str());
1108 jsonValue->Put("y", translate.dy.ToString().c_str());
1109 jsonValue->Put("z", translate.dz.ToString().c_str());
1110 break;
1111 }
1112 }
1113 return jsonValue;
1114 }
1115
GetBlur() const1116 double InspectorComposedElement::GetBlur() const
1117 {
1118 auto render = AceType::DynamicCast<RenderBox>(GetInspectorNode(BoxElement::TypeId()));
1119 if (!render) {
1120 return 0.0;
1121 }
1122 return render->GetBlurRadius().Value();
1123 }
1124
GetBackDropBlur() const1125 double InspectorComposedElement::GetBackDropBlur() const
1126 {
1127 auto render = AceType::DynamicCast<RenderBox>(GetInspectorNode(BoxElement::TypeId()));
1128 if (!render) {
1129 return 0.0;
1130 }
1131 return render->GetBackdropRadius().Value();
1132 }
1133
GetBrightness() const1134 double InspectorComposedElement::GetBrightness() const
1135 {
1136 auto render = GetRenderBox();
1137 if (render) {
1138 return render->GetBrightness();
1139 }
1140 return 1.0;
1141 }
1142
GetSaturate() const1143 double InspectorComposedElement::GetSaturate() const
1144 {
1145 auto render = GetRenderBox();
1146 if (render) {
1147 return render->GetSaturate();
1148 }
1149 return 1.0;
1150 }
1151
GetContrast() const1152 double InspectorComposedElement::GetContrast() const
1153 {
1154 auto render = GetRenderBox();
1155 if (render) {
1156 return render->GetContrast();
1157 }
1158 return 1.0;
1159 }
1160
GetInvert() const1161 double InspectorComposedElement::GetInvert() const
1162 {
1163 auto render = GetRenderBox();
1164 if (render) {
1165 return render->GetInvert();
1166 }
1167 return 0.0;
1168 }
1169
GetSepia() const1170 double InspectorComposedElement::GetSepia() const
1171 {
1172 auto render = GetRenderBox();
1173 if (render) {
1174 return render->GetSepia();
1175 }
1176 return 0.0;
1177 }
1178
GetGrayScale() const1179 double InspectorComposedElement::GetGrayScale() const
1180 {
1181 auto render = GetRenderBox();
1182 if (render) {
1183 return render->GetGrayScale();
1184 }
1185 return 0.0;
1186 }
1187
GetHueRotate() const1188 double InspectorComposedElement::GetHueRotate() const
1189 {
1190 auto render = GetRenderBox();
1191 if (render) {
1192 return render->GetHueRotate();
1193 }
1194 return 0.0;
1195 }
1196
GetWindowBlur() const1197 std::unique_ptr<JsonValue> InspectorComposedElement::GetWindowBlur() const
1198 {
1199 auto render = AceType::DynamicCast<RenderBox>(GetInspectorNode(BoxElement::TypeId()));
1200 auto jsonValue = JsonUtil::Create(true);
1201 if (!render) {
1202 return jsonValue;
1203 }
1204 jsonValue->Put("percent", std::to_string(render->GetWindowBlurProgress()).c_str());
1205 jsonValue->Put(
1206 "style", WINDOW_BLUR_STYLE[static_cast<int32_t>(render->GetWindowBlurStyle()) - WINDOW_BLUR_STYLE_ENUM_OFFSET]);
1207 return jsonValue;
1208 }
1209
GetShadow() const1210 std::unique_ptr<JsonValue> InspectorComposedElement::GetShadow() const
1211 {
1212 auto render = AceType::DynamicCast<RenderBox>(GetInspectorNode(BoxElement::TypeId()));
1213 auto jsonValue = JsonUtil::Create(true);
1214 if (!render) {
1215 return jsonValue;
1216 }
1217 Shadow shadow = render->GetShadow();
1218 jsonValue->Put("radius", std::to_string(shadow.GetBlurRadius()).c_str());
1219 jsonValue->Put("color", ConvertColorToString(shadow.GetColor()).c_str());
1220 jsonValue->Put("offsetX", std::to_string(shadow.GetOffset().GetX()).c_str());
1221 jsonValue->Put("offsetY", std::to_string(shadow.GetOffset().GetY()).c_str());
1222 return jsonValue;
1223 }
1224
GetOverlay() const1225 std::unique_ptr<JsonValue> InspectorComposedElement::GetOverlay() const
1226 {
1227 auto jsonValue = JsonUtil::Create(true);
1228 // Since CoverageComponent is inherited from ComponentGroup, but Coverage does not have Element,
1229 // ComponentGroupElement is called.
1230 auto coverage = GetInspectorElement<RenderCoverage>(ComponentGroupElement::TypeId());
1231 if (!coverage) {
1232 jsonValue->Put("options", "{align: Alignment.Center, offset: {x: 0, y: 0}}");
1233 return jsonValue;
1234 }
1235 auto title = coverage->GetTextVal();
1236 auto alignment = coverage->GetAlignment();
1237 auto jsonAlign = JsonUtil::Create(true);
1238 if (alignment == Alignment::TOP_LEFT) {
1239 jsonAlign->Put("align", "Alignment.TopStart");
1240 } else if (alignment == Alignment::TOP_CENTER) {
1241 jsonAlign->Put("align", "Alignment.Top");
1242 } else if (alignment == Alignment::TOP_RIGHT) {
1243 jsonAlign->Put("align", "Alignment.TopEnd");
1244 } else if (alignment == Alignment::CENTER_LEFT) {
1245 jsonAlign->Put("align", "Alignment.Start");
1246 } else if (alignment == Alignment::CENTER_RIGHT) {
1247 jsonAlign->Put("align", "Alignment.End");
1248 } else if (alignment == Alignment::BOTTOM_LEFT) {
1249 jsonAlign->Put("align", "Alignment.BottomStart");
1250 } else if (alignment == Alignment::BOTTOM_CENTER) {
1251 jsonAlign->Put("align", "Alignment.Bottom");
1252 } else if (alignment == Alignment::BOTTOM_RIGHT) {
1253 jsonAlign->Put("align", "Alignment.BottomEnd");
1254 } else {
1255 jsonAlign->Put("align", "Alignment.Center");
1256 }
1257 auto offsetJson = JsonUtil::Create(true);
1258 offsetJson->Put("x", coverage->GetX().ToString().c_str());
1259 offsetJson->Put("y", coverage->GetY().ToString().c_str());
1260 jsonAlign->Put("offset", offsetJson);
1261 jsonValue->Put("title", title.c_str());
1262 jsonValue->Put("options", jsonAlign);
1263 return jsonValue;
1264 }
1265
GetMask() const1266 std::unique_ptr<JsonValue> InspectorComposedElement::GetMask() const
1267 {
1268 auto render = GetRenderBox();
1269 auto jsonValue = JsonUtil::Create(true);
1270 if (!render) {
1271 return jsonValue;
1272 }
1273 auto mask = render->GetMask();
1274 if (mask && mask->GetMaskPath() && mask->GetMaskPath()->GetBasicShape()) {
1275 auto shape = mask->GetMaskPath()->GetBasicShape();
1276 int32_t shapeType = static_cast<int32_t>(shape->GetBasicShapeType());
1277 int32_t size = static_cast<int32_t>(sizeof(BASIC_SHAPE_TYPE) / sizeof(BASIC_SHAPE_TYPE[0]));
1278 if (shapeType < size) {
1279 jsonValue->Put("shape", BASIC_SHAPE_TYPE[shapeType]);
1280 }
1281 }
1282 return jsonValue;
1283 }
1284
GetGridColumnInfo() const1285 RefPtr<GridColumnInfo> InspectorComposedElement::GetGridColumnInfo() const
1286 {
1287 auto render = GetRenderBox();
1288 if (!render) {
1289 return nullptr;
1290 }
1291 auto columnInfo = render->GetGridColumnInfo();
1292 if (!columnInfo) {
1293 return nullptr;
1294 }
1295 return columnInfo;
1296 }
1297
GetGridSpan() const1298 int32_t InspectorComposedElement::GetGridSpan() const
1299 {
1300 auto columnInfo = GetGridColumnInfo();
1301 if (columnInfo) {
1302 return columnInfo->GetColumns();
1303 }
1304 return 1;
1305 }
1306
GetGridOffset() const1307 int32_t InspectorComposedElement::GetGridOffset() const
1308 {
1309 auto columnInfo = GetGridColumnInfo();
1310 if (columnInfo) {
1311 if (columnInfo->GetOffset(GridSizeType::UNDEFINED) == -1) {
1312 return 0;
1313 }
1314 return columnInfo->GetOffset(GridSizeType::UNDEFINED);
1315 }
1316 return 0;
1317 }
1318
GetUseSizeType() const1319 std::unique_ptr<JsonValue> InspectorComposedElement::GetUseSizeType() const
1320 {
1321 auto columnInfo = GetGridColumnInfo();
1322 auto jsonRoot = JsonUtil::Create(true);
1323 if (!columnInfo) {
1324 return jsonRoot;
1325 }
1326 int32_t index = static_cast<int32_t>(GridSizeType::XS);
1327 for (; index < static_cast<int32_t>(GridSizeType::XL); index++) {
1328 auto jsonValue = JsonUtil::Create(true);
1329 GridSizeType type = static_cast<GridSizeType>(index);
1330 jsonValue->Put("span", static_cast<int32_t>(columnInfo->GetColumns(type)));
1331 jsonValue->Put("offset", columnInfo->GetOffset(type));
1332 jsonRoot->Put(GRID_SIZE_TYPE[index], jsonValue);
1333 }
1334 return jsonRoot;
1335 }
1336
GetUseAlign() const1337 std::unique_ptr<JsonValue> InspectorComposedElement::GetUseAlign() const
1338 {
1339 auto render = GetRenderBox();
1340 auto jsonValue = JsonUtil::Create(true);
1341 if (!render) {
1342 return jsonValue;
1343 }
1344 jsonValue->Put("edge", ConvertSideToString(render->GetUseAlignSide()).c_str());
1345 jsonValue->Put("offset", render->GetUseAlignOffset().ToString().c_str());
1346 return jsonValue;
1347 }
1348
GetBindPopup() const1349 std::string InspectorComposedElement::GetBindPopup() const
1350 {
1351 auto resultJson = JsonUtil::Create(true);
1352 auto coverageElement = GetContentElement<ComponentGroupElement>(ComponentGroupElement::TypeId(), false);
1353 RefPtr<PopupElementV2> popupElement = nullptr;
1354 if (coverageElement) {
1355 for (const auto& element : coverageElement->GetChildren()) {
1356 if (AceType::DynamicCast<PopupElementV2>(element)) {
1357 popupElement = AceType::DynamicCast<PopupElementV2>(element);
1358 }
1359 }
1360 }
1361 if (!popupElement) {
1362 return "";
1363 }
1364 std::string show;
1365 if (popupElement->IsShow()) {
1366 show = "true";
1367 } else {
1368 show = "false";
1369 }
1370 auto popupJson = JsonUtil::Create(true);
1371 popupJson->Put("message", popupElement->GetMessage().c_str());
1372 popupJson->Put("placementOnTop", popupElement->GetPlacementOnTop());
1373 auto primaryButtonJson = JsonUtil::Create(true);
1374 primaryButtonJson->Put("value", popupElement->GetPrimaryButtonValue().c_str());
1375 auto secondaryButtonJson = JsonUtil::Create(true);
1376 secondaryButtonJson->Put("value", popupElement->GetSecondaryButtonValue().c_str());
1377
1378 popupJson->Put("primaryButton", primaryButtonJson);
1379 popupJson->Put("secondaryButton", secondaryButtonJson);
1380 return show + ", " + popupJson->ToString();
1381 }
1382
GetBindContextMenu() const1383 std::string InspectorComposedElement::GetBindContextMenu() const
1384 {
1385 auto node = GetInspectorNode(BoxElement::TypeId());
1386 if (!node) {
1387 return "-";
1388 }
1389 auto responseType = AceType::DynamicCast<RenderBox>(node);
1390 if (responseType) {
1391 if (responseType->GetOnMouseId()) {
1392 return "ResponseType.RightClick";
1393 } else if (responseType->GetOnLongPress()) {
1394 return "ResponseType.Longpress";
1395 } else {
1396 return "-";
1397 }
1398 }
1399 return "-";
1400 }
1401
GetColorBlend() const1402 std::string InspectorComposedElement::GetColorBlend() const
1403 {
1404 auto node = GetRenderBox();
1405 if (!node) {
1406 return "";
1407 }
1408 auto colorBlend = node->GetColorBlend();
1409 return colorBlend.ColorToString();
1410 }
1411
UpdateEventTarget(BaseEventInfo & info) const1412 void InspectorComposedElement::UpdateEventTarget(BaseEventInfo& info) const
1413 {
1414 auto area = GetCurrentRectAndOrigin();
1415 auto& target = info.GetTargetWichModify();
1416 target.area.SetOffset(area.first.GetOffset());
1417 target.area.SetHeight(Dimension(area.first.GetSize().Height()));
1418 target.area.SetWidth(Dimension(area.first.GetSize().Width()));
1419 target.origin = DimensionOffset(area.second);
1420 }
1421
GetCurrentRectAndOrigin() const1422 std::pair<Rect, Offset> InspectorComposedElement::GetCurrentRectAndOrigin() const
1423 {
1424 auto rectInLocal = GetRenderRectInLocal();
1425 auto rectInGlobal = GetRenderRect();
1426 auto marginLeft = GetMargin(AnimatableType::PROPERTY_MARGIN_LEFT).ConvertToPx();
1427 auto marginRight = GetMargin(AnimatableType::PROPERTY_MARGIN_RIGHT).ConvertToPx();
1428 auto marginTop = GetMargin(AnimatableType::PROPERTY_MARGIN_TOP).ConvertToPx();
1429 auto marginBottom = GetMargin(AnimatableType::PROPERTY_MARGIN_BOTTOM).ConvertToPx();
1430 auto Localoffset = rectInLocal.GetOffset();
1431 auto offset = Offset(Localoffset.GetX() + marginLeft, Localoffset.GetY() + marginTop);
1432 auto size = Size(rectInLocal.Width() - marginLeft - marginRight, rectInLocal.Height() - marginTop - marginBottom);
1433 auto globalOffset = rectInGlobal.GetOffset();
1434 return { { offset, size }, { globalOffset.GetX() - Localoffset.GetX(), globalOffset.GetY() - Localoffset.GetY() } };
1435 }
1436
GetColorsAndRepeating(std::unique_ptr<JsonValue> & resultJson,const Gradient & gradient) const1437 void InspectorComposedElement::GetColorsAndRepeating(
1438 std::unique_ptr<JsonValue>& resultJson, const Gradient& gradient) const
1439 {
1440 auto jsoncolorArray = JsonUtil::CreateArray(true);
1441 auto colors = gradient.GetColors();
1442 for (size_t i = 0; i < colors.size(); ++i) {
1443 auto temp = JsonUtil::CreateArray(true);
1444 auto value = std::to_string(colors[i].GetDimension().Value() / 100.0);
1445 auto color = colors[i].GetColor().ColorToString();
1446 temp->Put("0", color.c_str());
1447 temp->Put("1", value.c_str());
1448 auto index = std::to_string(i);
1449 jsoncolorArray->Put(index.c_str(), temp);
1450 }
1451 resultJson->Put("colors", jsoncolorArray);
1452 auto repeat = ConvertBoolToString(gradient.GetRepeat());
1453 resultJson->Put("repeating", repeat.c_str());
1454 }
1455
GetLinearGradient() const1456 std::unique_ptr<JsonValue> InspectorComposedElement::GetLinearGradient() const
1457 {
1458 auto resultJson = JsonUtil::Create(true);
1459 auto node = GetRenderBox();
1460 if (!node) {
1461 return resultJson;
1462 }
1463 auto decoration = node->GetBackDecoration();
1464 if (decoration) {
1465 auto lineGradient = decoration->GetGradient();
1466 if (GradientType::LINEAR != lineGradient.GetType()) {
1467 return resultJson;
1468 }
1469 if (lineGradient.GetLinearGradient().angle) {
1470 resultJson->Put("angle", lineGradient.GetLinearGradient().angle->ToString().c_str());
1471 }
1472
1473 auto linearX = lineGradient.GetLinearGradient().linearX;
1474 auto linearY = lineGradient.GetLinearGradient().linearY;
1475 if (linearX == GradientDirection::LEFT) {
1476 if (linearY == GradientDirection::TOP) {
1477 resultJson->Put("direction", "GradientDirection.LeftTop");
1478 } else if (linearY == GradientDirection::BOTTOM) {
1479 resultJson->Put("direction", "GradientDirection.LeftBottom");
1480 } else {
1481 resultJson->Put("direction", "GradientDirection.Left");
1482 }
1483 } else if (linearX == GradientDirection::RIGHT) {
1484 if (linearY == GradientDirection::TOP) {
1485 resultJson->Put("direction", "GradientDirection.RightTop");
1486 } else if (linearY == GradientDirection::BOTTOM) {
1487 resultJson->Put("direction", "GradientDirection.RightBottom");
1488 } else {
1489 resultJson->Put("direction", "GradientDirection.Right");
1490 }
1491 } else {
1492 if (linearY == GradientDirection::TOP) {
1493 resultJson->Put("direction", "GradientDirection.Top");
1494 } else if (linearY == GradientDirection::BOTTOM) {
1495 resultJson->Put("direction", "GradientDirection.Bottom");
1496 } else {
1497 resultJson->Put("direction", "GradientDirection.None");
1498 }
1499 }
1500 GetColorsAndRepeating(resultJson, lineGradient);
1501 }
1502 return resultJson;
1503 }
1504
GetSweepGradient() const1505 std::unique_ptr<JsonValue> InspectorComposedElement::GetSweepGradient() const
1506 {
1507 auto resultJson = JsonUtil::Create(true);
1508 auto node = GetRenderBox();
1509 if (!node) {
1510 return resultJson;
1511 }
1512 auto decoration = node->GetBackDecoration();
1513 if (decoration) {
1514 auto sweepGradient = decoration->GetGradient();
1515 if (GradientType::SWEEP != sweepGradient.GetType()) {
1516 return resultJson;
1517 }
1518 auto radialCenterX = sweepGradient.GetSweepGradient().centerX;
1519 auto radialCenterY = sweepGradient.GetSweepGradient().centerY;
1520 if (radialCenterX && radialCenterY) {
1521 auto jsPoint = JsonUtil::CreateArray(true);
1522 jsPoint->Put("0", radialCenterX->ToString().c_str());
1523 jsPoint->Put("1", radialCenterY->ToString().c_str());
1524 resultJson->Put("center", jsPoint);
1525 }
1526
1527 auto startAngle = sweepGradient.GetSweepGradient().startAngle;
1528 auto endAngle = sweepGradient.GetSweepGradient().endAngle;
1529 if (startAngle) {
1530 resultJson->Put("start", startAngle->ToString().c_str());
1531 }
1532 if (endAngle) {
1533 resultJson->Put("end", endAngle->ToString().c_str());
1534 }
1535
1536 GetColorsAndRepeating(resultJson, sweepGradient);
1537 }
1538 return resultJson;
1539 }
1540
GetRadialGradient() const1541 std::unique_ptr<JsonValue> InspectorComposedElement::GetRadialGradient() const
1542 {
1543 auto resultJson = JsonUtil::Create(true);
1544 auto node = GetRenderBox();
1545 if (!node) {
1546 return resultJson;
1547 }
1548 auto decoration = node->GetBackDecoration();
1549 if (decoration) {
1550 auto radialGradient = decoration->GetGradient();
1551 if (GradientType::RADIAL != radialGradient.GetType()) {
1552 return resultJson;
1553 }
1554
1555 auto radialCenterX = radialGradient.GetRadialGradient().radialCenterX;
1556 auto radialCenterY = radialGradient.GetRadialGradient().radialCenterY;
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 radius = radialGradient.GetRadialGradient().radialVerticalSize;
1565 if (radius) {
1566 resultJson->Put("radius", radius->ToString().c_str());
1567 }
1568
1569 GetColorsAndRepeating(resultJson, radialGradient);
1570 }
1571 return resultJson;
1572 }
1573
GetTag() const1574 const std::string& InspectorComposedElement::GetTag() const
1575 {
1576 auto iter = COMPONENT_TAG_TO_ETS_TAG_MAP.find(name_);
1577 return iter != COMPONENT_TAG_TO_ETS_TAG_MAP.end() ? iter->second : name_;
1578 }
1579
GetClickable() const1580 bool InspectorComposedElement::GetClickable() const
1581 {
1582 auto node = GetAccessibilityNode();
1583 if (!node) {
1584 return false;
1585 }
1586 return node->GetClickableState();
1587 }
GetCheckable() const1588 bool InspectorComposedElement::GetCheckable() const
1589 {
1590 auto node = GetAccessibilityNode();
1591 if (!node) {
1592 return false;
1593 }
1594 return node->GetCheckableState();
1595 }
GetFocusable() const1596 bool InspectorComposedElement::GetFocusable() const
1597 {
1598 auto focusableElement = GetContentElement<FocusableElement>(FocusableElement::TypeId(), false);
1599 if (!focusableElement) {
1600 return false;
1601 }
1602 return focusableElement->IsFocusable();
1603 }
GetScrollable() const1604 bool InspectorComposedElement::GetScrollable() const
1605 {
1606 auto node = GetAccessibilityNode();
1607 if (!node) {
1608 return false;
1609 }
1610 return node->GetScrollableState();
1611 }
GetLongClickable() const1612 bool InspectorComposedElement::GetLongClickable() const
1613 {
1614 auto node = GetAccessibilityNode();
1615 if (!node) {
1616 return false;
1617 }
1618 return node->GetLongClickableState();
1619 }
IsSelected() const1620 bool InspectorComposedElement::IsSelected() const
1621 {
1622 auto node = GetAccessibilityNode();
1623 if (!node) {
1624 return false;
1625 }
1626 return node->GetSelectedState();
1627 }
IsPassword() const1628 bool InspectorComposedElement::IsPassword() const
1629 {
1630 auto node = GetAccessibilityNode();
1631 if (!node) {
1632 return false;
1633 }
1634 return node->GetIsPassword();
1635 }
IsChecked() const1636 bool InspectorComposedElement::IsChecked() const
1637 {
1638 auto node = GetAccessibilityNode();
1639 if (!node) {
1640 return false;
1641 }
1642 return node->GetCheckedState();
1643 }
IsFocused() const1644 bool InspectorComposedElement::IsFocused() const
1645 {
1646 auto node = GetAccessibilityNode();
1647 if (!node) {
1648 return false;
1649 }
1650 return node->GetFocusedState();
1651 }
1652
1653 } // namespace OHOS::Ace::V2
1654