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 "core/components/box/box_element.h"
19 #include "core/components/coverage/render_coverage.h"
20 #include "core/components/display/render_display.h"
21 #include "core/components/flex/flex_item_element.h"
22 #include "core/components/flex/render_flex_item.h"
23 #include "core/components/focusable/focusable_element.h"
24 #include "core/components/transform/transform_element.h"
25 #include "core/components_v2/inspector/inspector_composed_component.h"
26 #include "core/components_v2/inspector/inspector_constants.h"
27 #include "core/components_v2/inspector/utils.h"
28
29 namespace OHOS::Ace::V2 {
30
31 namespace {
32
33 constexpr uint32_t WINDOW_BLUR_STYLE_ENUM_OFFSET = 100;
34
35 const char* VISIBLE_TYPE[] = { "Visibility.Visible", "Visibility.Hidden", "Visibility.None" };
36
37 const char* ITEM_ALIGN[] = { "ItemAlign.Auto", "ItemAlign.Start", "ItemAlign.Center", "ItemAlign.End",
38 "ItemAlign.Stretch", "ItemAlign.Baseline" };
39
40 // NONE translate to Solid
41 const char* BORDER_STYLE[] = {
42 "BorderStyle.Solid",
43 "BorderStyle.Dashed",
44 "BorderStyle.Dotted",
45 "BorderStyle.Solid",
46 };
47
48 const char* WINDOW_BLUR_STYLE[] = { "BlurStyle.SmallLight", "BlurStyle.MediumLight", "BlurStyle.LargeLight",
49 "BlurStyle.XlargeLight", "BlurStyle.SmallDark", "BlurStyle.MediumDark", "BlurStyle.LargeDark",
50 "BlurStyle.XlargeDark" };
51
52 const char* ALIGNMENT_TYPE[3][3] = { { "Alignment.TopStart", "Alignment.Start", "Alignment.BottomStart" },
53 { "Alignment.Top", "Alignment.Center", "Alignment.Bottom" },
54 { "Alignment.TopEnd", "Alignment.End", "Alignment.BottomEnd" } };
55
56 const char* GRID_SIZE_TYPE[] = { "default", "sx", "sm", "md", "lg" };
57
58 constexpr const char* TEXT_DIRECTION[] = { "Direction.Ltr", "Direction.Rtl", "Direction.Inherit", "Direction.Auto" };
59
60 constexpr const char* BASIC_SHAPE_TYPE[] { "None", "Inset", "Circle", "Ellipse", "Polygon", "Path", "Rect" };
61
62 constexpr const char* HIT_TEST_BEHAVIOR[] = {
63 "HitTestMode.Default",
64 "HitTestMode.Block",
65 "HitTestMode.Transparent",
66 "HitTestMode.None",
67 };
68
69 const std::unordered_map<std::string, DoubleJsonFunc> CREATE_JSON_DOUBLE_MAP {
__anon347d689d0202() 70 { "opacity", [](const InspectorNode& inspector) { return inspector.GetOpacity(); } },
__anon347d689d0302() 71 { "flexGrow", [](const InspectorNode& inspector) { return inspector.GetFlexGrow(); } },
__anon347d689d0402() 72 { "flexShrink", [](const InspectorNode& inspector) { return inspector.GetFlexShrink(); } },
__anon347d689d0502() 73 { "gridOffset", [](const InspectorNode& inspector) { return inspector.GetGridOffset(); } },
__anon347d689d0602() 74 { "blur", [](const InspectorNode& inspector) { return inspector.GetBlur(); } },
__anon347d689d0702() 75 { "backdropBlur", [](const InspectorNode& inspector) { return inspector.GetBackDropBlur(); } },
__anon347d689d0802() 76 { "aspectRatio", [](const InspectorNode& inspector) { return inspector.GetAspectRatio(); } },
__anon347d689d0902() 77 { "brightness", [](const InspectorNode& inspector) { return inspector.GetBrightness(); } },
__anon347d689d0a02() 78 { "saturate", [](const InspectorNode& inspector) { return inspector.GetSaturate(); } },
__anon347d689d0b02() 79 { "contrast", [](const InspectorNode& inspector) { return inspector.GetContrast(); } },
__anon347d689d0c02() 80 { "invert", [](const InspectorNode& inspector) { return inspector.GetInvert(); } },
__anon347d689d0d02() 81 { "sepia", [](const InspectorNode& inspector) { return inspector.GetSepia(); } },
__anon347d689d0e02() 82 { "grayscale", [](const InspectorNode& inspector) { return inspector.GetGrayScale(); } },
__anon347d689d0f02() 83 { "hueRotate", [](const InspectorNode& inspector) { return inspector.GetHueRotate(); } },
84 };
85
86 const std::unordered_map<std::string, StringJsonFunc> CREATE_JSON_STRING_MAP {
__anon347d689d1002() 87 { "visibility", [](const InspectorNode& inspector) { return inspector.GetVisibility(); } },
__anon347d689d1102() 88 { "alignSelf", [](const InspectorNode& inspector) { return inspector.GetAlignSelf(); } },
__anon347d689d1202() 89 { "clip", [](const InspectorNode& inspector) { return inspector.GetClip(); } },
__anon347d689d1302() 90 { "constraintSize", [](const InspectorNode& inspector) { return inspector.GetConstraintSize(); } },
__anon347d689d1402() 91 { "borderColor", [](const InspectorNode& inspector) { return inspector.GetBorderColor(); } },
__anon347d689d1502() 92 { "borderStyle", [](const InspectorNode& inspector) { return inspector.GetBorderStyle(); } },
__anon347d689d1602() 93 { "borderWidth", [](const InspectorNode& inspector) { return inspector.GetBorderWidth(); } },
__anon347d689d1702() 94 { "borderRadius", [](const InspectorNode& inspector) { return inspector.GetBorderRadius(); } },
__anon347d689d1802() 95 { "backgroundImage", [](const InspectorNode& inspector) { return inspector.GetBackgroundImage(); } },
__anon347d689d1902() 96 { "backgroundColor", [](const InspectorNode& inspector) { return inspector.GetBackgroundColor(); } },
__anon347d689d1a02() 97 { "flexBasis", [](const InspectorNode& inspector) { return inspector.GetFlexBasis(); } },
__anon347d689d1b02() 98 { "width", [](const InspectorNode& inspector) { return inspector.GetWidth(); } },
__anon347d689d1c02() 99 { "height", [](const InspectorNode& inspector) { return inspector.GetHeight(); } },
__anon347d689d1d02() 100 { "align", [](const InspectorNode& inspector) { return inspector.GetAlign(); } },
__anon347d689d1e02() 101 { "direction", [](const InspectorNode& inspector) { return inspector.GetDirectionStr(); } },
__anon347d689d1f02() 102 { "bindPopup", [](const InspectorNode& inspector) { return inspector.GetBindPopup(); } },
__anon347d689d2002() 103 { "bindContextMenu", [](const InspectorNode& inspector) { return inspector.GetBindContextMenu(); } },
__anon347d689d2102() 104 { "colorBlend", [](const InspectorNode& inspector) { return inspector.GetColorBlend(); } },
__anon347d689d2202() 105 { "backgroundImageSize", [](const InspectorNode& inspector) { return inspector.GetBackgroundImageSize(); } },
106 { "backgroundImagePosition",
__anon347d689d2302() 107 [](const InspectorNode& inspector) { return inspector.GetBackgroundImagePosition(); } },
__anon347d689d2402() 108 { "padding", [](const InspectorNode& inspector) { return inspector.GetPadding(); } },
__anon347d689d2502() 109 { "margin", [](const InspectorNode& inspector) { return inspector.GetAllMargin(); } },
__anon347d689d2602() 110 { "hitTestBehavior", [](const InspectorNode& inspector) { return inspector.GetHitTestBehaviorStr(); } },
111 };
112
113 const std::unordered_map<std::string, BoolJsonFunc> CREATE_JSON_BOOL_MAP {
__anon347d689d2702() 114 { "enabled", [](const InspectorNode& inspector) { return inspector.GetEnabled(); } },
__anon347d689d2802() 115 { "focusable", [](const InspectorNode& inspector) { return inspector.GetFocusable(); } },
__anon347d689d2902() 116 { "touchable", [](const InspectorNode& inspector) { return inspector.GetTouchable(); } },
117 };
118
119 const std::unordered_map<std::string, IntJsonFunc> CREATE_JSON_INT_MAP {
__anon347d689d2a02() 120 { "zIndex", [](const InspectorNode& inspector) { return inspector.GetZIndex(); } },
__anon347d689d2b02() 121 { "gridSpan", [](const InspectorNode& inspector) { return inspector.GetGridSpan(); } },
__anon347d689d2c02() 122 { "layoutWeight", [](const InspectorNode& inspector) { return inspector.GetLayoutWeight(); } },
__anon347d689d2d02() 123 { "displayPriority", [](const InspectorNode& inspector) { return inspector.GetDisplayPriority(); } },
124 };
125
126 const std::unordered_map<std::string, JsonValueJsonFunc> CREATE_JSON_JSON_VALUE_MAP {
__anon347d689d2e02() 127 { "shadow", [](const InspectorNode& inspector) { return inspector.GetShadow(); } },
__anon347d689d2f02() 128 { "position", [](const InspectorNode& inspector) { return inspector.GetPosition(); } },
__anon347d689d3002() 129 { "offset", [](const InspectorNode& inspector) { return inspector.GetOffset(); } },
__anon347d689d3102() 130 { "size", [](const InspectorNode& inspector) { return inspector.GetSize(); } },
__anon347d689d3202() 131 { "useSizeType", [](const InspectorNode& inspector) { return inspector.GetUseSizeType(); } },
__anon347d689d3302() 132 { "rotate", [](const InspectorNode& inspector) { return inspector.GetRotate(); } },
__anon347d689d3402() 133 { "scale", [](const InspectorNode& inspector) { return inspector.GetScale(); } },
__anon347d689d3502() 134 { "transform", [](const InspectorNode& inspector) { return inspector.GetTransform(); } },
__anon347d689d3602() 135 { "translate", [](const InspectorNode& inspector) { return inspector.GetTranslate(); } },
__anon347d689d3702() 136 { "markAnchor", [](const InspectorNode& inspector) { return inspector.GetMarkAnchor(); } },
__anon347d689d3802() 137 { "mask", [](const InspectorNode& inspector) { return inspector.GetMask(); } },
__anon347d689d3902() 138 { "overlay", [](const InspectorNode& inspector) { return inspector.GetOverlay(); } },
__anon347d689d3a02() 139 { "border", [](const InspectorNode& inspector) { return inspector.GetUnifyBorder(); } },
__anon347d689d3b02() 140 { "linearGradient", [](const InspectorNode& inspector) { return inspector.GetLinearGradient(); } },
__anon347d689d3c02() 141 { "sweepGradient", [](const InspectorNode& inspector) { return inspector.GetSweepGradient(); } },
__anon347d689d3d02() 142 { "radialGradient", [](const InspectorNode& inspector) { return inspector.GetRadialGradient(); } },
143 };
144
145 const std::unordered_map<std::string, BoolJsonFunc> CREATE_XTS_BOOL_MAP {
__anon347d689d3e02() 146 { "clickable", [](const InspectorNode& inspector) { return inspector.GetClickable(); } },
__anon347d689d3f02() 147 { "checkable", [](const InspectorNode& inspector) { return inspector.GetCheckable(); } },
__anon347d689d4002() 148 { "scrollable", [](const InspectorNode& inspector) { return inspector.GetScrollable(); } },
__anon347d689d4102() 149 { "long-clickable", [](const InspectorNode& inspector) { return inspector.GetLongClickable(); } },
__anon347d689d4202() 150 { "selected", [](const InspectorNode& inspector) { return inspector.IsSelected(); } },
__anon347d689d4302() 151 { "password", [](const InspectorNode& inspector) { return inspector.IsPassword(); } },
__anon347d689d4402() 152 { "checked", [](const InspectorNode& inspector) { return inspector.IsChecked(); } },
__anon347d689d4502() 153 { "focused", [](const InspectorNode& inspector) { return inspector.IsFocused(); } },
154 };
155
156 const std::unordered_map<std::string, IntJsonFunc> CREATE_XTS_INT_MAP {
__anon347d689d4602() 157 { "layoutPriority", [](const InspectorNode& inspector) { return inspector.GetLayoutPriority(); } },
158 };
159
160 const std::unordered_map<std::string, JsonValueJsonFunc> CREATE_XTS_JSON_VALUE_MAP {
__anon347d689d4702() 161 { "windowBlur", [](const InspectorNode& inspector) { return inspector.GetWindowBlur(); } },
__anon347d689d4802() 162 { "useAlign", [](const InspectorNode& inspector) { return inspector.GetUseAlign(); } },
163 };
164
165 constexpr double VISIBLE_RATIO_MIN = 0.0;
166 constexpr double VISIBLE_RATIO_MAX = 1.0;
167
168 }; // namespace
169
InspectorComposedElement(const ComposeId & id)170 InspectorComposedElement::InspectorComposedElement(const ComposeId& id) : ComposedElement(id) {}
171
~InspectorComposedElement()172 InspectorComposedElement::~InspectorComposedElement()
173 {
174 auto popupElement = GetPopupElement();
175 if (popupElement && popupElement->GetPopupComponent()) {
176 auto popupComponent = popupElement->GetPopupComponent();
177 if (popupComponent && popupComponent->GetPopupController()) {
178 popupComponent->GetPopupController()->CancelPopup();
179 }
180 }
181
182 if (inspectorId_ == -1) {
183 return;
184 }
185 accessibilityNode_.Reset();
186 RemoveInspectorNode(inspectorId_);
187 }
188
OnInactive()189 void InspectorComposedElement::OnInactive()
190 {
191 accessibilityNode_.Reset();
192 RemoveInspectorNode(inspectorId_);
193 inspectorId_ = -1;
194 }
195
OnActive()196 void InspectorComposedElement::OnActive()
197 {
198 inspectorId_ = StringUtils::StringToInt(id_);
199 }
200
GetPopupElement() const201 RefPtr<PopupElementV2> InspectorComposedElement::GetPopupElement() const
202 {
203 auto coverageElement = GetContentElement<ComponentGroupElement>(ComponentGroupElement::TypeId(), false);
204 RefPtr<PopupElementV2> popupElement = nullptr;
205 if (coverageElement) {
206 for (const auto& element : coverageElement->GetChildren()) {
207 if (AceType::DynamicCast<PopupElementV2>(element)) {
208 popupElement = AceType::DynamicCast<PopupElementV2>(element);
209 }
210 }
211 }
212
213 return popupElement;
214 }
215
GetElementChildBySlot(const RefPtr<Element> & element,int32_t & slot) const216 RefPtr<Element> InspectorComposedElement::GetElementChildBySlot(const RefPtr<Element>& element, int32_t& slot) const
217 {
218 if (!element) {
219 return nullptr;
220 }
221 auto child = element->GetChildBySlot(slot);
222 if (!child) {
223 slot = DEFAULT_ELEMENT_SLOT;
224 child = element->GetChildBySlot(slot);
225 }
226 return child;
227 }
228
GetInspectorComposedElementParent(const RefPtr<Element> & element) const229 RefPtr<Element> InspectorComposedElement::GetInspectorComposedElementParent(const RefPtr<Element>& element) const
230 {
231 if (!element) {
232 return nullptr;
233 }
234 for (const auto& child : element->GetChildren()) {
235 auto inspectorComposedElement = AceType::DynamicCast<InspectorComposedElement>(child);
236 if (inspectorComposedElement) {
237 return element;
238 }
239 auto element_ = GetInspectorComposedElementParent(child);
240 if (element_) {
241 return element_;
242 }
243 }
244 return nullptr;
245 }
246
ToJsonObject() const247 std::unique_ptr<JsonValue> InspectorComposedElement::ToJsonObject() const
248 {
249 auto resultJson = JsonUtil::Create(true);
250 for (const auto& value : CREATE_JSON_DOUBLE_MAP) {
251 resultJson->Put(value.first.c_str(), value.second(*this));
252 }
253 for (const auto& value : CREATE_JSON_STRING_MAP) {
254 resultJson->Put(value.first.c_str(), value.second(*this).c_str());
255 }
256 for (const auto& value : CREATE_JSON_BOOL_MAP) {
257 resultJson->Put(value.first.c_str(), value.second(*this));
258 }
259 for (const auto& value : CREATE_JSON_INT_MAP) {
260 resultJson->Put(value.first.c_str(), value.second(*this));
261 }
262
263 for (const auto& value : CREATE_JSON_JSON_VALUE_MAP) {
264 resultJson->Put(value.first.c_str(), value.second(*this));
265 }
266 #if !defined(PREVIEW)
267 for (const auto& value : CREATE_XTS_BOOL_MAP) {
268 resultJson->Put(value.first.c_str(), value.second(*this));
269 }
270 for (const auto& value : CREATE_XTS_INT_MAP) {
271 resultJson->Put(value.first.c_str(), value.second(*this));
272 }
273
274 for (const auto& value : CREATE_XTS_JSON_VALUE_MAP) {
275 resultJson->Put(value.first.c_str(), value.second(*this));
276 }
277 #endif
278 return resultJson;
279 }
280
Prepare(const WeakPtr<Element> & weakParent)281 void InspectorComposedElement::Prepare(const WeakPtr<Element>& weakParent)
282 {
283 accessibilityEnabled_ = false;
284 #if defined(PREVIEW)
285 accessibilityEnabled_ = true;
286 #else
287 if (AceApplicationInfo::GetInstance().IsAccessibilityEnabled() || SystemProperties::GetAccessibilityEnabled()) {
288 accessibilityEnabled_ = true;
289 }
290 #endif
291 if (accessibilityEnabled_) {
292 auto parent = weakParent.Upgrade();
293 RefPtr<InspectorComposedElement> inspectorParent;
294 while (parent) {
295 inspectorParent = DynamicCast<InspectorComposedElement>(parent);
296 if (inspectorParent) {
297 break;
298 }
299 parent = parent->GetElementParent().Upgrade();
300 }
301 if (inspectorParent) {
302 inspectorParentId_ = inspectorParent->inspectorId_;
303 }
304 }
305 AddComposedComponentId();
306 }
307
Update()308 void InspectorComposedElement::Update()
309 {
310 ComposedElement::Update();
311 auto component = DynamicCast<V2::InspectorComposedComponent>(component_);
312 if (!component) {
313 LOGE("fail to update due to component is null");
314 return;
315 }
316 SetElementId(component->GetElementId());
317
318 auto inspectorFunctionImpl = component->GetInspectorFunctionImpl();
319 inspectorFunctionImpl->SetUpdateEventInfoImpl([weak = WeakClaim(this)](BaseEventInfo& info) {
320 auto composedElement = weak.Upgrade();
321 if (composedElement) {
322 composedElement->UpdateEventTarget(info);
323 }
324 });
325 if (accessibilityNode_) {
326 accessibilityNode_->SetAccessible(component->IsAccessibilityGroup());
327 accessibilityNode_->SetAccessibilityLabel(component->GetAccessibilityText());
328 accessibilityNode_->SetAccessibilityHint(component->GetAccessibilityDescription());
329 accessibilityNode_->SetImportantForAccessibility(component->GetAccessibilityImportance());
330 accessibilityNode_->SetFocusChangeEventMarker(component->GetAccessibilityEvent());
331 }
332 }
333
CanUpdate(const RefPtr<Component> & newComponent)334 bool InspectorComposedElement::CanUpdate(const RefPtr<Component>& newComponent)
335 {
336 auto component = AceType::DynamicCast<InspectorComposedComponent>(newComponent);
337 if (!component) {
338 return false;
339 }
340 return GetInspectorTag() == component->GetName();
341 }
342
AddComposedComponentId()343 void InspectorComposedElement::AddComposedComponentId()
344 {
345 auto context = context_.Upgrade();
346 if (context == nullptr) {
347 LOGW("get context failed");
348 return;
349 }
350 auto accessibilityManager = context->GetAccessibilityManager();
351 if (!accessibilityManager) {
352 LOGW("get AccessibilityManager failed");
353 return;
354 }
355 accessibilityManager->AddComposedElement(std::to_string(inspectorId_), AceType::Claim(this));
356 if (accessibilityEnabled_) {
357 accessibilityNode_ = InspectorComposedComponent::CreateAccessibilityNode(
358 inspectorTag_, inspectorId_, inspectorParentId_, GetRenderSlot());
359 if (accessibilityNode_) {
360 accessibilityNode_->SetJsComponentId(key_);
361 }
362 }
363 }
364
RemoveInspectorNode(int32_t id)365 void InspectorComposedElement::RemoveInspectorNode(int32_t id)
366 {
367 auto context = context_.Upgrade();
368 if (context == nullptr) {
369 LOGW("get context failed");
370 return;
371 }
372 auto accessibilityManager = context->GetAccessibilityManager();
373 if (!accessibilityManager) {
374 LOGW("get AccessibilityManager failed");
375 return;
376 }
377 accessibilityManager->RemoveComposedElementById(std::to_string(id));
378 if (accessibilityEnabled_) {
379 accessibilityManager->RemoveAccessibilityNodeById(id);
380 }
381 }
382
GetInspectorNode(IdType typeId,bool isForward) const383 RefPtr<RenderNode> InspectorComposedElement::GetInspectorNode(IdType typeId, bool isForward) const
384 {
385 if (isForward) {
386 auto parent = GetElementParent().Upgrade();
387 while (parent) {
388 if (AceType::TypeId(parent) == typeId) {
389 return parent->GetRenderNode();
390 }
391 parent = parent->GetElementParent().Upgrade();
392 }
393 return nullptr;
394 }
395 auto child = children_.empty() ? nullptr : children_.front();
396 while (child) {
397 if (AceType::TypeId(child) == typeId) {
398 return child->GetRenderNode();
399 }
400 child = child->GetChildren().empty() ? nullptr : child->GetChildren().front();
401 }
402 return nullptr;
403 }
404
GetAccessibilityNode() const405 RefPtr<AccessibilityNode> InspectorComposedElement::GetAccessibilityNode() const
406 {
407 auto context = context_.Upgrade();
408 if (context == nullptr) {
409 LOGW("get context failed");
410 return nullptr;
411 }
412 auto accessibilityManager = context->GetAccessibilityManager();
413 if (!accessibilityManager) {
414 LOGW("get AccessibilityManager failed");
415 return nullptr;
416 }
417 return accessibilityManager->GetAccessibilityNodeById(StringUtils::StringToInt(id_));
418 }
419
GetRenderBox() const420 RefPtr<RenderBox> InspectorComposedElement::GetRenderBox() const
421 {
422 auto node = GetInspectorNode(BoxElement::TypeId());
423 if (!node) {
424 return nullptr;
425 }
426 return AceType::DynamicCast<RenderBox>(node);
427 }
428
GetWidth() const429 std::string InspectorComposedElement::GetWidth() const
430 {
431 auto render = GetRenderBox();
432 if (render) {
433 Dimension value = render->GetWidthDimension();
434 if (value.Value() == -1) {
435 return "-";
436 }
437 return value.ToString();
438 }
439 return "-";
440 }
441
GetHeight() const442 std::string InspectorComposedElement::GetHeight() const
443 {
444 auto render = GetRenderBox();
445 if (render) {
446 Dimension value = render->GetHeightDimension();
447 if (value.Value() == -1) {
448 return "-";
449 }
450 return value.ToString();
451 }
452 return "-";
453 }
454
GetSize() const455 std::unique_ptr<JsonValue> InspectorComposedElement::GetSize() const
456 {
457 auto jsonValue = JsonUtil::Create(true);
458 jsonValue->Put("width", GetWidth().c_str());
459 jsonValue->Put("height", GetHeight().c_str());
460 return jsonValue;
461 }
462
GetPadding() const463 std::string InspectorComposedElement::GetPadding() const
464 {
465 auto render = GetRenderBox();
466 if (render) {
467 auto top = render->GetPadding(DimensionHelper(&Edge::SetTop, &Edge::Top));
468 auto right = render->GetPadding(DimensionHelper(&Edge::SetRight, &Edge::Right));
469 auto bottom = render->GetPadding(DimensionHelper(&Edge::SetBottom, &Edge::Bottom));
470 auto left = render->GetPadding(DimensionHelper(&Edge::SetLeft, &Edge::Left));
471 if (top == right && right == bottom && bottom == left) {
472 return top.ToString();
473 } else {
474 auto jsonValue = JsonUtil::Create(true);
475 jsonValue->Put("top", top.ToString().c_str());
476 jsonValue->Put("right", right.ToString().c_str());
477 jsonValue->Put("bottom", bottom.ToString().c_str());
478 jsonValue->Put("left", left.ToString().c_str());
479 return jsonValue->ToString();
480 }
481 }
482 return "0.0";
483 }
484
GetAllMargin() const485 std::string InspectorComposedElement::GetAllMargin() const
486 {
487 auto render = GetRenderBox();
488 if (render) {
489 auto top = render->GetMargin(DimensionHelper(&Edge::SetTop, &Edge::Top));
490 auto right = render->GetMargin(DimensionHelper(&Edge::SetRight, &Edge::Right));
491 auto bottom = render->GetMargin(DimensionHelper(&Edge::SetBottom, &Edge::Bottom));
492 auto left = render->GetMargin(DimensionHelper(&Edge::SetLeft, &Edge::Left));
493 if (top == right && right == bottom && bottom == left) {
494 return top.ToString().c_str();
495 } else {
496 auto jsonValue = JsonUtil::Create(true);
497 jsonValue->Put("top", top.ToString().c_str());
498 jsonValue->Put("right", right.ToString().c_str());
499 jsonValue->Put("bottom", bottom.ToString().c_str());
500 jsonValue->Put("left", left.ToString().c_str());
501 return jsonValue->ToString();
502 }
503 }
504 return "0.0";
505 }
506
GetMargin(OHOS::Ace::AnimatableType type) const507 Dimension InspectorComposedElement::GetMargin(OHOS::Ace::AnimatableType type) const
508 {
509 auto render = GetRenderBox();
510 if (render) {
511 if (type == AnimatableType::PROPERTY_MARGIN_LEFT) {
512 return render->GetMargin(DimensionHelper(&Edge::SetLeft, &Edge::Left));
513 } else if (type == AnimatableType::PROPERTY_MARGIN_TOP) {
514 return render->GetMargin(DimensionHelper(&Edge::SetTop, &Edge::Top));
515 } else if (type == AnimatableType::PROPERTY_MARGIN_RIGHT) {
516 return render->GetMargin(DimensionHelper(&Edge::SetRight, &Edge::Right));
517 } else if (type == AnimatableType::PROPERTY_MARGIN_BOTTOM) {
518 return render->GetMargin(DimensionHelper(&Edge::SetBottom, &Edge::Bottom));
519 }
520 }
521 return Dimension();
522 }
523
GetConstraintSize() const524 std::string InspectorComposedElement::GetConstraintSize() const
525 {
526 LayoutParam layoutParam = LayoutParam(Size(), Size());
527 auto render = GetRenderBox();
528 if (render) {
529 layoutParam = render->GetConstraints();
530 }
531 auto jsonStr = JsonUtil::Create(true);
532 Dimension minWidth = Dimension(
533 PipelineBase::Px2VpWithCurrentDensity(layoutParam.GetMinSize().Width()), DimensionUnit::VP);
534 Dimension minHeight = Dimension(
535 PipelineBase::Px2VpWithCurrentDensity(layoutParam.GetMinSize().Height()), DimensionUnit::VP);
536 Dimension maxWidth = Dimension(
537 PipelineBase::Px2VpWithCurrentDensity(layoutParam.GetMaxSize().Width()), DimensionUnit::VP);
538 Dimension maxHeight = Dimension(
539 PipelineBase::Px2VpWithCurrentDensity(layoutParam.GetMaxSize().Height()), DimensionUnit::VP);
540 jsonStr->Put("minWidth", minWidth.ToString().c_str());
541 jsonStr->Put("minHeight", minHeight.ToString().c_str());
542 jsonStr->Put("maxWidth", maxWidth.ToString().c_str());
543 jsonStr->Put("maxHeight", maxHeight.ToString().c_str());
544 return jsonStr->ToString();
545 }
546
GetLayoutPriority() const547 int32_t InspectorComposedElement::GetLayoutPriority() const
548 {
549 auto render = GetRenderBox();
550 if (render) {
551 return render->GetDisplayIndex();
552 }
553 return 0;
554 }
555
GetLayoutWeight() const556 int32_t InspectorComposedElement::GetLayoutWeight() const
557 {
558 auto node = GetInspectorNode(FlexItemElement::TypeId());
559 if (!node) {
560 return 0;
561 }
562 auto render = AceType::DynamicCast<RenderFlexItem>(node);
563 if (render) {
564 return render->GetFlexWeight();
565 }
566 return 0;
567 }
568
GetAlign() const569 std::string InspectorComposedElement::GetAlign() const
570 {
571 auto render = GetRenderBox();
572 if (render) {
573 auto align = render->GetAlign();
574 int32_t h = align.GetHorizontal() + 1;
575 int32_t v = align.GetVertical() + 1;
576 return ALIGNMENT_TYPE[h][v];
577 }
578 return ALIGNMENT_TYPE[1][1];
579 }
580
GetDirectionStr() const581 std::string InspectorComposedElement::GetDirectionStr() const
582 {
583 auto render = GetRenderBox();
584 if (!render) {
585 return TEXT_DIRECTION[3];
586 }
587 auto value = static_cast<int32_t>(render->GetInspectorDirection());
588 auto length = static_cast<int32_t>(sizeof(TEXT_DIRECTION) / sizeof(TEXT_DIRECTION[0]));
589 if (value < length) {
590 return TEXT_DIRECTION[value];
591 }
592 return TEXT_DIRECTION[3];
593 }
594
GetDirection() const595 TextDirection InspectorComposedElement::GetDirection() const
596 {
597 auto render = GetRenderBox();
598 if (render) {
599 return render->GetTextDirection();
600 }
601 return TextDirection::AUTO;
602 }
603
GetBorderRadius() const604 std::string InspectorComposedElement::GetBorderRadius() const
605 {
606 auto value = GetBorder().TopLeftRadius().GetX().Value();
607 if (value == 0.0) {
608 return "0.0vp";
609 }
610 return GetBorder().TopLeftRadius().GetX().ToString();
611 }
612
GetUnifyBorder() const613 std::unique_ptr<JsonValue> InspectorComposedElement::GetUnifyBorder() const
614 {
615 auto jsonValue = JsonUtil::Create(true);
616 jsonValue->Put("width", GetBorderWidth().c_str());
617 jsonValue->Put("color", GetBorderColor().c_str());
618 jsonValue->Put("radius", GetBorderRadius().c_str());
619 jsonValue->Put("style", GetBorderStyle().c_str());
620 return jsonValue;
621 }
622
GetPosition() const623 std::unique_ptr<JsonValue> InspectorComposedElement::GetPosition() const
624 {
625 auto jsonValue = JsonUtil::Create(true);
626 auto node = GetInspectorNode(FlexItemElement::TypeId());
627 if (!node) {
628 jsonValue->Put("x", "0.0px");
629 jsonValue->Put("y", "0.0px");
630 return jsonValue;
631 }
632 auto render = AceType::DynamicCast<RenderFlexItem>(node);
633 if (render) {
634 PositionType type = render->GetPositionType();
635 if (type == PositionType::PTABSOLUTE) {
636 jsonValue->Put("x", render->GetLeft().ToString().c_str());
637 jsonValue->Put("y", render->GetTop().ToString().c_str());
638 return jsonValue;
639 }
640 }
641 jsonValue->Put("x", "0.0px");
642 jsonValue->Put("y", "0.0px");
643 return jsonValue;
644 }
645
GetMarkAnchor() const646 std::unique_ptr<JsonValue> InspectorComposedElement::GetMarkAnchor() const
647 {
648 auto jsonValue = JsonUtil::Create(true);
649 auto node = GetInspectorNode(FlexItemElement::TypeId());
650 if (!node) {
651 jsonValue->Put("x", "0.0px");
652 jsonValue->Put("y", "0.0px");
653 return jsonValue;
654 }
655 auto render = AceType::DynamicCast<RenderFlexItem>(node);
656 if (render) {
657 jsonValue->Put("x", render->GetAnchorX().ToString().c_str());
658 jsonValue->Put("y", render->GetAnchorY().ToString().c_str());
659 return jsonValue;
660 }
661 jsonValue->Put("x", "0.0px");
662 jsonValue->Put("y", "0.0px");
663 return jsonValue;
664 }
665
GetOffset() const666 std::unique_ptr<JsonValue> InspectorComposedElement::GetOffset() const
667 {
668 auto jsonValue = JsonUtil::Create(true);
669 auto node = GetInspectorNode(FlexItemElement::TypeId());
670 if (!node) {
671 jsonValue->Put("x", "0.0px");
672 jsonValue->Put("y", "0.0px");
673 return jsonValue;
674 }
675 auto render = AceType::DynamicCast<RenderFlexItem>(node);
676 if (render) {
677 PositionType type = render->GetPositionType();
678 if (type == PositionType::PTOFFSET) {
679 jsonValue->Put("x", render->GetLeft().ToString().c_str());
680 jsonValue->Put("y", render->GetTop().ToString().c_str());
681 return jsonValue;
682 }
683 }
684 jsonValue->Put("x", "0.0px");
685 jsonValue->Put("y", "0.0px");
686 return jsonValue;
687 }
688
GetRect()689 std::string InspectorComposedElement::GetRect()
690 {
691 std::string strRec;
692 Rect rect = GetRenderRect();
693
694 if (accessibilityNode_ && accessibilityNode_->GetParentNode()) {
695 auto parent = accessibilityNode_->GetParentNode();
696 if (parent->GetClipFlag()) {
697 rect = rect.Constrain(parent->GetRect());
698 }
699 }
700 if (accessibilityNode_ && GetClipFlag()) {
701 accessibilityNode_->SetClipFlagToChild(true);
702 }
703
704 isRectValid_ = rect.IsValid();
705 if (!isRectValid_) {
706 rect.SetRect(0, 0, 0, 0);
707 }
708
709 if (accessibilityNode_) {
710 auto render = AceType::DynamicCast<RenderTransform>(GetInspectorNode(TransformElement::TypeId()));
711 if (render || accessibilityNode_->GetMatrix4Flag()) {
712 if (render) {
713 auto transformNow = render->GetTransformMatrix(render->GetTransitionPaintRect().GetOffset());
714 accessibilityNode_->SetTransformToChild(transformNow);
715 }
716 Matrix4 transform = accessibilityNode_->GetMatrix4();
717 rect = accessibilityNode_->GetRectWithTransform(rect, transform);
718 }
719 }
720
721 strRec = std::to_string(rect.Left())
722 .append(",")
723 .append(std::to_string(rect.Top()))
724 .append(",")
725 .append(std::to_string(rect.Width()))
726 .append(",")
727 .append(std::to_string(rect.Height()));
728 return strRec;
729 }
730
GetParentRect() const731 Rect InspectorComposedElement::GetParentRect() const
732 {
733 auto parent = GetElementParent().Upgrade();
734 if (!parent) {
735 return Rect();
736 }
737 Rect parentRect = parent->GetRenderRect();
738 return parentRect;
739 }
740
GetAspectRatio() const741 double InspectorComposedElement::GetAspectRatio() const
742 {
743 auto render = GetRenderBox();
744 if (render) {
745 return render->GetAspectRatio();
746 }
747 return 0.0;
748 }
749
GetDisplayPriority() const750 int32_t InspectorComposedElement::GetDisplayPriority() const
751 {
752 auto node = GetInspectorNode(FlexItemElement::TypeId());
753 if (!node) {
754 return 1;
755 }
756 auto render = AceType::DynamicCast<RenderFlexItem>(node);
757 if (render) {
758 return render->GetDisplayIndex();
759 }
760 return 1;
761 }
762
GetFlexBasis() const763 std::string InspectorComposedElement::GetFlexBasis() const
764 {
765 auto render = AceType::DynamicCast<RenderFlexItem>(GetInspectorNode(FlexItemElement::TypeId()));
766 if (render) {
767 auto flexBasis = render->GetFlexBasis();
768 return flexBasis.IsValid() ? render->GetFlexBasis().ToString() : "auto";
769 }
770 return "auto";
771 }
772
GetFlexGrow() const773 double InspectorComposedElement::GetFlexGrow() const
774 {
775 auto render = AceType::DynamicCast<RenderFlexItem>(GetInspectorNode(FlexItemElement::TypeId()));
776 if (render) {
777 return render->GetFlexGrow();
778 }
779 return 0.0;
780 }
781
GetFlexShrink() const782 double InspectorComposedElement::GetFlexShrink() const
783 {
784 auto render = AceType::DynamicCast<RenderFlexItem>(GetInspectorNode(FlexItemElement::TypeId()));
785 if (render) {
786 return render->GetFlexShrink();
787 }
788 return 0.0;
789 }
790
GetAlignSelf() const791 std::string InspectorComposedElement::GetAlignSelf() const
792 {
793 auto render = AceType::DynamicCast<RenderFlexItem>(GetInspectorNode(FlexItemElement::TypeId()));
794 if (render) {
795 return ITEM_ALIGN[static_cast<int32_t>(render->GetAlignSelf())];
796 }
797 return ITEM_ALIGN[0];
798 }
799
GetBorder() const800 Border InspectorComposedElement::GetBorder() const
801 {
802 auto render = GetRenderBox();
803 if (!render) {
804 return Border();
805 }
806 auto decoration = render->GetBackDecoration();
807 if (decoration) {
808 return decoration->GetBorder();
809 }
810 return Border();
811 }
812
GetBorderStyle() const813 std::string InspectorComposedElement::GetBorderStyle() const
814 {
815 auto border = GetBorder();
816 int32_t style = static_cast<int32_t>(border.Left().GetBorderStyle());
817 return BORDER_STYLE[style];
818 }
819
GetBorderWidth() const820 std::string InspectorComposedElement::GetBorderWidth() const
821 {
822 auto border = GetBorder();
823 auto borderWidth = border.Left().GetWidth();
824 if (NearZero(borderWidth.Value())) {
825 return "0.00vp";
826 }
827 return borderWidth.ToString();
828 }
829
GetBorderColor() const830 std::string InspectorComposedElement::GetBorderColor() const
831 {
832 auto border = GetBorder();
833 return border.Left().GetColor().ColorToString();
834 }
835
GetBackDecoration() const836 RefPtr<Decoration> InspectorComposedElement::GetBackDecoration() const
837 {
838 auto render = GetRenderBox();
839 if (!render) {
840 return nullptr;
841 }
842 return render->GetBackDecoration();
843 }
844
GetBackgroundImage() const845 std::string InspectorComposedElement::GetBackgroundImage() const
846 {
847 auto backDecoration = GetBackDecoration();
848 if (!backDecoration) {
849 return "NONE";
850 }
851 auto image = backDecoration->GetImage();
852 if (!image) {
853 return "NONE";
854 }
855 auto imageRepeat = image->GetImageRepeat();
856 if (imageRepeat == ImageRepeat::REPEAT_X) {
857 return image->GetSrc() + ", ImageRepeat.X";
858 } else if (imageRepeat == ImageRepeat::REPEAT_Y) {
859 return image->GetSrc() + ", ImageRepeat.Y";
860 } else if (imageRepeat == ImageRepeat::REPEAT) {
861 return image->GetSrc() + ", ImageRepeat.XY";
862 }
863 return image->GetSrc() + ", ImageRepeat.NoRepeat";
864 }
865
GetBackgroundColor() const866 std::string InspectorComposedElement::GetBackgroundColor() const
867 {
868 auto jsonValue = JsonUtil::Create(true);
869 auto backDecoration = GetBackDecoration();
870 if (!backDecoration) {
871 return "NONE";
872 }
873 auto color = backDecoration->GetBackgroundColor();
874 return color.ColorToString();
875 }
876
GetBackgroundImageSize() const877 std::string InspectorComposedElement::GetBackgroundImageSize() const
878 {
879 auto backDecoration = GetBackDecoration();
880 if (!backDecoration) {
881 return "ImageSize.Auto";
882 }
883 auto image = backDecoration->GetImage();
884 if (!image) {
885 return "ImageSize.Auto";
886 }
887
888 return image->GetImageSize().ToString();
889 }
890
GetBackgroundImagePosition() const891 std::string InspectorComposedElement::GetBackgroundImagePosition() const
892 {
893 auto jsonValue = JsonUtil::Create(true);
894 auto backDecoration = GetBackDecoration();
895 if (!backDecoration) {
896 jsonValue->Put("x", 0.0);
897 jsonValue->Put("y", 0.0);
898 return jsonValue->ToString();
899 }
900 auto image = backDecoration->GetImage();
901 if (!image) {
902 jsonValue->Put("x", 0.0);
903 jsonValue->Put("y", 0.0);
904 return jsonValue->ToString();
905 }
906
907 return image->GetImagePosition().ToString();
908 }
909
GetFrontDecoration() const910 RefPtr<Decoration> InspectorComposedElement::GetFrontDecoration() const
911 {
912 auto render = GetRenderBox();
913 if (!render) {
914 return nullptr;
915 }
916 return render->GetFrontDecoration();
917 }
918
GetOpacity() const919 double InspectorComposedElement::GetOpacity() const
920 {
921 auto node = GetInspectorNode(DisplayElement::TypeId());
922 if (!node) {
923 return 1.0;
924 }
925 auto render = AceType::DynamicCast<RenderDisplay>(node);
926 if (!render) {
927 return 1.0;
928 }
929 return render->GetTransitionOpacity();
930 }
931
GetVisibility() const932 std::string InspectorComposedElement::GetVisibility() const
933 {
934 auto node = GetInspectorNode(DisplayElement::TypeId());
935 if (!node) {
936 return VISIBLE_TYPE[static_cast<int32_t>(VisibleType::VISIBLE)];
937 }
938 auto render = AceType::DynamicCast<RenderDisplay>(node);
939 if (!render) {
940 return VISIBLE_TYPE[static_cast<int32_t>(VisibleType::VISIBLE)];
941 }
942 return VISIBLE_TYPE[static_cast<int32_t>(render->GetVisibleType())];
943 }
944
GetClip() const945 std::string InspectorComposedElement::GetClip() const
946 {
947 auto render = GetRenderBox();
948 if (!render) {
949 return "false";
950 }
951 auto clipPath = render->GetClipPath();
952 auto jsonValue = JsonUtil::Create(true);
953 if (clipPath && clipPath->GetBasicShape()) {
954 int32_t shapeType = static_cast<int32_t>(clipPath->GetBasicShape()->GetBasicShapeType());
955 int32_t size = static_cast<int32_t>(sizeof(BASIC_SHAPE_TYPE) / sizeof(BASIC_SHAPE_TYPE[0]));
956 if (shapeType < size) {
957 jsonValue->Put("shape", BASIC_SHAPE_TYPE[shapeType]);
958 }
959 } else {
960 if (render->GetBoxClipFlag() == true) {
961 return "true";
962 } else {
963 return "false";
964 }
965 }
966 return jsonValue->ToString();
967 }
968
GetClipFlag() const969 bool InspectorComposedElement::GetClipFlag() const
970 {
971 auto render = GetRenderBox();
972 if (!render) {
973 return false;
974 }
975
976 return render->GetBoxClipFlag();
977 }
978
GetEnabled() const979 bool InspectorComposedElement::GetEnabled() const
980 {
981 auto node = GetInspectorNode(GetTargetTypeId());
982 if (!node) {
983 return true;
984 }
985 return !node->IsDisabled();
986 }
987
GetZIndex() const988 int32_t InspectorComposedElement::GetZIndex() const
989 {
990 auto node = GetInspectorNode(GetTargetTypeId());
991 if (!node) {
992 return 0;
993 }
994 return node->GetZIndex();
995 }
996
GetOriginPoint() const997 DimensionOffset InspectorComposedElement::GetOriginPoint() const
998 {
999 auto node = GetInspectorNode(TransformElement::TypeId());
1000 if (!node) {
1001 return DimensionOffset(OHOS::Ace::HALF_PERCENT, OHOS::Ace::HALF_PERCENT);
1002 }
1003 auto render = AceType::DynamicCast<RenderTransform>(node);
1004 if (!render) {
1005 return DimensionOffset(OHOS::Ace::HALF_PERCENT, OHOS::Ace::HALF_PERCENT);
1006 }
1007 return render->GetTransformOrigin();
1008 }
1009
GetRotate() const1010 std::unique_ptr<JsonValue> InspectorComposedElement::GetRotate() const
1011 {
1012 auto render = AceType::DynamicCast<RenderTransform>(GetInspectorNode(TransformElement::TypeId()));
1013 auto jsonValue = JsonUtil::Create(true);
1014 if (!render) {
1015 return jsonValue;
1016 }
1017 for (const auto& operation : render->GetTransformEffects().GetOperations()) {
1018 if (operation.type_ == TransformOperationType::ROTATE) {
1019 const auto& rotate = operation.rotateOperation_;
1020 jsonValue->Put("x", std::to_string(rotate.dx).c_str());
1021 jsonValue->Put("y", std::to_string(rotate.dy).c_str());
1022 jsonValue->Put("z", std::to_string(rotate.dz).c_str());
1023 jsonValue->Put("angle", std::to_string(rotate.angle).c_str());
1024 jsonValue->Put("centerX", render->GetOriginX().ToString().c_str());
1025 jsonValue->Put("centerY", render->GetOriginY().ToString().c_str());
1026 break;
1027 }
1028 }
1029 return jsonValue;
1030 }
1031
GetScale() const1032 std::unique_ptr<JsonValue> InspectorComposedElement::GetScale() const
1033 {
1034 auto render = AceType::DynamicCast<RenderTransform>(GetInspectorNode(TransformElement::TypeId()));
1035 auto jsonValue = JsonUtil::Create(true);
1036 if (!render) {
1037 return jsonValue;
1038 }
1039 for (const auto& operation : render->GetTransformEffects().GetOperations()) {
1040 if (operation.type_ == TransformOperationType::SCALE) {
1041 const auto& scale = operation.scaleOperation_;
1042 jsonValue->Put("x", std::to_string(scale.scaleX).c_str());
1043 jsonValue->Put("y", std::to_string(scale.scaleY).c_str());
1044 jsonValue->Put("z", std::to_string(scale.scaleZ).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
GetTransform() const1053 std::unique_ptr<JsonValue> InspectorComposedElement::GetTransform() 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::MATRIX) {
1062 const auto& matrix = operation.matrix4_;
1063 jsonValue->Put("type", "matrix");
1064 auto matrixString = matrix.ToString();
1065 while (matrixString.find("\n") != std::string::npos) {
1066 auto num = matrixString.find("\n");
1067 matrixString.replace(num, 1, "");
1068 }
1069 jsonValue->Put("matrix", matrixString.c_str());
1070 break;
1071 }
1072 }
1073 return jsonValue;
1074 }
1075
GetTranslate() const1076 std::unique_ptr<JsonValue> InspectorComposedElement::GetTranslate() const
1077 {
1078 auto render = AceType::DynamicCast<RenderTransform>(GetInspectorNode(TransformElement::TypeId()));
1079 auto jsonValue = JsonUtil::Create(true);
1080 if (!render) {
1081 return jsonValue;
1082 }
1083 for (const auto& operation : render->GetTransformEffects().GetOperations()) {
1084 if (operation.type_ == TransformOperationType::TRANSLATE) {
1085 const auto& translate = operation.translateOperation_;
1086 jsonValue->Put("x", translate.dx.ToString().c_str());
1087 jsonValue->Put("y", translate.dy.ToString().c_str());
1088 jsonValue->Put("z", translate.dz.ToString().c_str());
1089 break;
1090 }
1091 }
1092 return jsonValue;
1093 }
1094
GetBlur() const1095 double InspectorComposedElement::GetBlur() const
1096 {
1097 auto render = AceType::DynamicCast<RenderBox>(GetInspectorNode(BoxElement::TypeId()));
1098 if (!render) {
1099 return 0.0;
1100 }
1101 return render->GetBlurRadius().Value();
1102 }
1103
GetBackDropBlur() const1104 double InspectorComposedElement::GetBackDropBlur() const
1105 {
1106 auto render = AceType::DynamicCast<RenderBox>(GetInspectorNode(BoxElement::TypeId()));
1107 if (!render) {
1108 return 0.0;
1109 }
1110 return render->GetBackdropRadius().Value();
1111 }
1112
GetBrightness() const1113 double InspectorComposedElement::GetBrightness() const
1114 {
1115 auto render = GetRenderBox();
1116 if (render) {
1117 return render->GetBrightness();
1118 }
1119 return 1.0;
1120 }
1121
GetSaturate() const1122 double InspectorComposedElement::GetSaturate() const
1123 {
1124 auto render = GetRenderBox();
1125 if (render) {
1126 return render->GetSaturate();
1127 }
1128 return 1.0;
1129 }
1130
GetContrast() const1131 double InspectorComposedElement::GetContrast() const
1132 {
1133 auto render = GetRenderBox();
1134 if (render) {
1135 return render->GetContrast();
1136 }
1137 return 1.0;
1138 }
1139
GetInvert() const1140 double InspectorComposedElement::GetInvert() const
1141 {
1142 auto render = GetRenderBox();
1143 if (render) {
1144 return render->GetInvert();
1145 }
1146 return 0.0;
1147 }
1148
GetSepia() const1149 double InspectorComposedElement::GetSepia() const
1150 {
1151 auto render = GetRenderBox();
1152 if (render) {
1153 return render->GetSepia();
1154 }
1155 return 0.0;
1156 }
1157
GetGrayScale() const1158 double InspectorComposedElement::GetGrayScale() const
1159 {
1160 auto render = GetRenderBox();
1161 if (render) {
1162 return render->GetGrayScale();
1163 }
1164 return 0.0;
1165 }
1166
GetHueRotate() const1167 double InspectorComposedElement::GetHueRotate() const
1168 {
1169 auto render = GetRenderBox();
1170 if (render) {
1171 return render->GetHueRotate();
1172 }
1173 return 0.0;
1174 }
1175
GetWindowBlur() const1176 std::unique_ptr<JsonValue> InspectorComposedElement::GetWindowBlur() const
1177 {
1178 auto render = AceType::DynamicCast<RenderBox>(GetInspectorNode(BoxElement::TypeId()));
1179 auto jsonValue = JsonUtil::Create(true);
1180 if (!render) {
1181 return jsonValue;
1182 }
1183 jsonValue->Put("percent", std::to_string(render->GetWindowBlurProgress()).c_str());
1184 jsonValue->Put(
1185 "style", WINDOW_BLUR_STYLE[static_cast<int32_t>(render->GetWindowBlurStyle()) - WINDOW_BLUR_STYLE_ENUM_OFFSET]);
1186 return jsonValue;
1187 }
1188
GetShadow() const1189 std::unique_ptr<JsonValue> InspectorComposedElement::GetShadow() const
1190 {
1191 auto render = AceType::DynamicCast<RenderBox>(GetInspectorNode(BoxElement::TypeId()));
1192 auto jsonValue = JsonUtil::Create(true);
1193 if (!render) {
1194 return jsonValue;
1195 }
1196 Shadow shadow = render->GetShadow();
1197 jsonValue->Put("radius", std::to_string(shadow.GetBlurRadius()).c_str());
1198 jsonValue->Put("color", ConvertColorToString(shadow.GetColor()).c_str());
1199 jsonValue->Put("offsetX", std::to_string(shadow.GetOffset().GetX()).c_str());
1200 jsonValue->Put("offsetY", std::to_string(shadow.GetOffset().GetY()).c_str());
1201 jsonValue->Put("type", std::to_string(static_cast<int32_t>(shadow.GetShadowType())).c_str());
1202 return jsonValue;
1203 }
1204
GetOverlay() const1205 std::unique_ptr<JsonValue> InspectorComposedElement::GetOverlay() const
1206 {
1207 auto jsonValue = JsonUtil::Create(true);
1208 // Since CoverageComponent is inherited from ComponentGroup, but Coverage does not have Element,
1209 // ComponentGroupElement is called.
1210 auto coverage = GetInspectorElement<RenderCoverage>(ComponentGroupElement::TypeId());
1211 if (!coverage) {
1212 jsonValue->Put("options", "{align: Alignment.Center, offset: {x: 0, y: 0}}");
1213 return jsonValue;
1214 }
1215 auto title = coverage->GetTextVal();
1216 auto alignment = coverage->GetAlignment();
1217 auto jsonAlign = JsonUtil::Create(true);
1218 if (alignment == Alignment::TOP_LEFT) {
1219 jsonAlign->Put("align", "Alignment.TopStart");
1220 } else if (alignment == Alignment::TOP_CENTER) {
1221 jsonAlign->Put("align", "Alignment.Top");
1222 } else if (alignment == Alignment::TOP_RIGHT) {
1223 jsonAlign->Put("align", "Alignment.TopEnd");
1224 } else if (alignment == Alignment::CENTER_LEFT) {
1225 jsonAlign->Put("align", "Alignment.Start");
1226 } else if (alignment == Alignment::CENTER_RIGHT) {
1227 jsonAlign->Put("align", "Alignment.End");
1228 } else if (alignment == Alignment::BOTTOM_LEFT) {
1229 jsonAlign->Put("align", "Alignment.BottomStart");
1230 } else if (alignment == Alignment::BOTTOM_CENTER) {
1231 jsonAlign->Put("align", "Alignment.Bottom");
1232 } else if (alignment == Alignment::BOTTOM_RIGHT) {
1233 jsonAlign->Put("align", "Alignment.BottomEnd");
1234 } else {
1235 jsonAlign->Put("align", "Alignment.Center");
1236 }
1237 auto offsetJson = JsonUtil::Create(true);
1238 offsetJson->Put("x", coverage->GetX().ToString().c_str());
1239 offsetJson->Put("y", coverage->GetY().ToString().c_str());
1240 jsonAlign->Put("offset", offsetJson);
1241 jsonValue->Put("title", title.c_str());
1242 jsonValue->Put("options", jsonAlign);
1243 return jsonValue;
1244 }
1245
GetMask() const1246 std::unique_ptr<JsonValue> InspectorComposedElement::GetMask() const
1247 {
1248 auto render = GetRenderBox();
1249 auto jsonValue = JsonUtil::Create(true);
1250 if (!render) {
1251 return jsonValue;
1252 }
1253 auto mask = render->GetMask();
1254 if (mask && mask->GetMaskPath() && mask->GetMaskPath()->GetBasicShape()) {
1255 auto shape = mask->GetMaskPath()->GetBasicShape();
1256 int32_t shapeType = static_cast<int32_t>(shape->GetBasicShapeType());
1257 int32_t size = static_cast<int32_t>(sizeof(BASIC_SHAPE_TYPE) / sizeof(BASIC_SHAPE_TYPE[0]));
1258 if (shapeType < size) {
1259 jsonValue->Put("shape", BASIC_SHAPE_TYPE[shapeType]);
1260 }
1261 }
1262 return jsonValue;
1263 }
1264
GetGridColumnInfo() const1265 RefPtr<GridColumnInfo> InspectorComposedElement::GetGridColumnInfo() const
1266 {
1267 auto render = GetRenderBox();
1268 if (!render) {
1269 return nullptr;
1270 }
1271 auto columnInfo = render->GetGridColumnInfo();
1272 if (!columnInfo) {
1273 return nullptr;
1274 }
1275 return columnInfo;
1276 }
1277
GetGridSpan() const1278 int32_t InspectorComposedElement::GetGridSpan() const
1279 {
1280 auto columnInfo = GetGridColumnInfo();
1281 if (columnInfo) {
1282 return columnInfo->GetColumns();
1283 }
1284 return 1;
1285 }
1286
GetGridOffset() const1287 int32_t InspectorComposedElement::GetGridOffset() const
1288 {
1289 auto columnInfo = GetGridColumnInfo();
1290 if (columnInfo) {
1291 if (columnInfo->GetOffset(GridSizeType::UNDEFINED) == -1) {
1292 return 0;
1293 }
1294 return columnInfo->GetOffset(GridSizeType::UNDEFINED);
1295 }
1296 return 0;
1297 }
1298
GetUseSizeType() const1299 std::unique_ptr<JsonValue> InspectorComposedElement::GetUseSizeType() const
1300 {
1301 auto columnInfo = GetGridColumnInfo();
1302 auto jsonRoot = JsonUtil::Create(true);
1303 if (!columnInfo) {
1304 return jsonRoot;
1305 }
1306 int32_t index = static_cast<int32_t>(GridSizeType::XS);
1307 for (; index < static_cast<int32_t>(GridSizeType::XL); index++) {
1308 auto jsonValue = JsonUtil::Create(true);
1309 GridSizeType type = static_cast<GridSizeType>(index);
1310 jsonValue->Put("span", static_cast<int32_t>(columnInfo->GetColumns(type)));
1311 jsonValue->Put("offset", columnInfo->GetOffset(type));
1312 jsonRoot->Put(GRID_SIZE_TYPE[index], jsonValue);
1313 }
1314 return jsonRoot;
1315 }
1316
GetUseAlign() const1317 std::unique_ptr<JsonValue> InspectorComposedElement::GetUseAlign() const
1318 {
1319 auto render = GetRenderBox();
1320 auto jsonValue = JsonUtil::Create(true);
1321 if (!render) {
1322 return jsonValue;
1323 }
1324 jsonValue->Put("edge", ConvertSideToString(render->GetUseAlignSide()).c_str());
1325 jsonValue->Put("offset", render->GetUseAlignOffset().ToString().c_str());
1326 return jsonValue;
1327 }
1328
GetBindPopup() const1329 std::string InspectorComposedElement::GetBindPopup() const
1330 {
1331 auto resultJson = JsonUtil::Create(true);
1332 auto coverageElement = GetContentElement<ComponentGroupElement>(ComponentGroupElement::TypeId(), false);
1333 RefPtr<PopupElementV2> popupElement = nullptr;
1334 if (coverageElement) {
1335 for (const auto& element : coverageElement->GetChildren()) {
1336 if (AceType::DynamicCast<PopupElementV2>(element)) {
1337 popupElement = AceType::DynamicCast<PopupElementV2>(element);
1338 }
1339 }
1340 }
1341 if (!popupElement) {
1342 return "";
1343 }
1344 std::string show;
1345 if (popupElement->IsShow()) {
1346 show = "true";
1347 } else {
1348 show = "false";
1349 }
1350 auto popupJson = JsonUtil::Create(true);
1351 popupJson->Put("message", popupElement->GetMessage().c_str());
1352 popupJson->Put("placementOnTop", popupElement->GetPlacementOnTop());
1353 auto primaryButtonJson = JsonUtil::Create(true);
1354 primaryButtonJson->Put("value", popupElement->GetPrimaryButtonValue().c_str());
1355 auto secondaryButtonJson = JsonUtil::Create(true);
1356 secondaryButtonJson->Put("value", popupElement->GetSecondaryButtonValue().c_str());
1357
1358 popupJson->Put("primaryButton", primaryButtonJson);
1359 popupJson->Put("secondaryButton", secondaryButtonJson);
1360 return show + ", " + popupJson->ToString();
1361 }
1362
GetBindContextMenu() const1363 std::string InspectorComposedElement::GetBindContextMenu() const
1364 {
1365 auto node = GetInspectorNode(BoxElement::TypeId());
1366 if (!node) {
1367 return "-";
1368 }
1369 auto responseType = AceType::DynamicCast<RenderBox>(node);
1370 if (responseType) {
1371 if (responseType->GetOnMouseId()) {
1372 return "ResponseType.RightClick";
1373 } else if (responseType->GetOnLongPress()) {
1374 return "ResponseType.LongPress";
1375 } else {
1376 return "-";
1377 }
1378 }
1379 return "-";
1380 }
1381
GetColorBlend() const1382 std::string InspectorComposedElement::GetColorBlend() const
1383 {
1384 auto node = GetRenderBox();
1385 if (!node) {
1386 return "";
1387 }
1388 auto colorBlend = node->GetColorBlend();
1389 return colorBlend.ColorToString();
1390 }
1391
AddChildWithSlot(int32_t slot,const RefPtr<Component> & newComponent)1392 void InspectorComposedElement::AddChildWithSlot(int32_t slot, const RefPtr<Component>& newComponent)
1393 {
1394 auto renderElement = GetRenderElement();
1395 CHECK_NULL_VOID(renderElement);
1396 renderElement->UpdateChildWithSlot(nullptr, newComponent, slot, slot);
1397 renderElement->MarkDirty();
1398 }
1399
UpdateChildWithSlot(int32_t slot,const RefPtr<Component> & newComponent)1400 void InspectorComposedElement::UpdateChildWithSlot(int32_t slot, const RefPtr<Component>& newComponent)
1401 {
1402 auto renderElement = GetRenderElement();
1403 CHECK_NULL_VOID(renderElement);
1404 auto child = GetElementChildBySlot(renderElement, slot);
1405 CHECK_NULL_VOID(child);
1406 // Deals with the case where the component to be updated is a custom component.
1407 auto rootElement = renderElement;
1408 auto childComposedElement = AceType::DynamicCast<ComposedElement>(child);
1409 if (childComposedElement && childComposedElement->GetName() == "view") {
1410 rootElement = child;
1411 child = rootElement->GetChildren().empty() ? nullptr : rootElement->GetChildren().front();
1412 }
1413
1414 // Replace the component with newComponent.
1415 auto context = rootElement->GetContext().Upgrade();
1416 auto needRebuildFocusElement = AceType::DynamicCast<Element>(rootElement->GetFocusScope());
1417 if (context && needRebuildFocusElement) {
1418 context->AddNeedRebuildFocusElement(needRebuildFocusElement);
1419 }
1420 ElementRegister::GetInstance()->RemoveItemSilently(child->GetElementId());
1421 rootElement->DeactivateChild(child);
1422 auto newChild = rootElement->InflateComponent(newComponent, child->GetSlot(), child->GetRenderSlot());
1423 ElementRegister::GetInstance()->AddElement(newChild);
1424 renderElement->MarkDirty();
1425 }
1426
DeleteChildWithSlot(int32_t slot)1427 void InspectorComposedElement::DeleteChildWithSlot(int32_t slot)
1428 {
1429 auto renderElement = GetRenderElement();
1430 CHECK_NULL_VOID(renderElement);
1431 auto child = GetElementChildBySlot(renderElement, slot);
1432 CHECK_NULL_VOID(child);
1433 renderElement->UpdateChildWithSlot(child, nullptr, slot, slot);
1434 renderElement->MarkDirty();
1435 }
1436
UpdateEventTarget(BaseEventInfo & info) const1437 void InspectorComposedElement::UpdateEventTarget(BaseEventInfo& info) const
1438 {
1439 auto area = GetCurrentRectAndOrigin();
1440 auto& target = info.GetTargetWithModify();
1441 target.area.SetOffset(area.first.GetOffset());
1442 target.area.SetHeight(Dimension(area.first.GetSize().Height()));
1443 target.area.SetWidth(Dimension(area.first.GetSize().Width()));
1444 target.origin = DimensionOffset(area.second);
1445 }
1446
GetCurrentRectAndOrigin() const1447 std::pair<Rect, Offset> InspectorComposedElement::GetCurrentRectAndOrigin() const
1448 {
1449 auto rectInLocal = GetRenderRectInLocal();
1450 auto rectInGlobal = GetRenderRect();
1451 auto marginLeft = GetMargin(AnimatableType::PROPERTY_MARGIN_LEFT).ConvertToPx();
1452 auto marginRight = GetMargin(AnimatableType::PROPERTY_MARGIN_RIGHT).ConvertToPx();
1453 auto marginTop = GetMargin(AnimatableType::PROPERTY_MARGIN_TOP).ConvertToPx();
1454 auto marginBottom = GetMargin(AnimatableType::PROPERTY_MARGIN_BOTTOM).ConvertToPx();
1455 auto LocalOffset = rectInLocal.GetOffset();
1456 auto offset = Offset(LocalOffset.GetX() + marginLeft, LocalOffset.GetY() + marginTop);
1457 auto size = Size(rectInLocal.Width() - marginLeft - marginRight, rectInLocal.Height() - marginTop - marginBottom);
1458 auto globalOffset = rectInGlobal.GetOffset();
1459 return { { offset, size }, { globalOffset.GetX() - LocalOffset.GetX(), globalOffset.GetY() - LocalOffset.GetY() } };
1460 }
1461
GetColorsAndRepeating(std::unique_ptr<JsonValue> & resultJson,const Gradient & gradient) const1462 void InspectorComposedElement::GetColorsAndRepeating(
1463 std::unique_ptr<JsonValue>& resultJson, const Gradient& gradient) const
1464 {
1465 auto jsoncolorArray = JsonUtil::CreateArray(true);
1466 auto colors = gradient.GetColors();
1467 for (size_t i = 0; i < colors.size(); ++i) {
1468 auto temp = JsonUtil::CreateArray(true);
1469 auto value = std::to_string(colors[i].GetDimension().Value() / 100.0);
1470 auto color = colors[i].GetColor().ColorToString();
1471 temp->Put("0", color.c_str());
1472 temp->Put("1", value.c_str());
1473 auto index = std::to_string(i);
1474 jsoncolorArray->Put(index.c_str(), temp);
1475 }
1476 resultJson->Put("colors", jsoncolorArray);
1477 auto repeat = ConvertBoolToString(gradient.GetRepeat());
1478 resultJson->Put("repeating", repeat.c_str());
1479 }
1480
GetLinearGradient() const1481 std::unique_ptr<JsonValue> InspectorComposedElement::GetLinearGradient() const
1482 {
1483 auto resultJson = JsonUtil::Create(true);
1484 auto node = GetRenderBox();
1485 if (!node) {
1486 return resultJson;
1487 }
1488 auto decoration = node->GetBackDecoration();
1489 if (decoration) {
1490 auto lineGradient = decoration->GetGradient();
1491 if (GradientType::LINEAR != lineGradient.GetType()) {
1492 return resultJson;
1493 }
1494 if (lineGradient.GetLinearGradient().angle) {
1495 resultJson->Put("angle", lineGradient.GetLinearGradient().angle->ToString().c_str());
1496 }
1497
1498 auto linearX = lineGradient.GetLinearGradient().linearX;
1499 auto linearY = lineGradient.GetLinearGradient().linearY;
1500 if (linearX == GradientDirection::LEFT) {
1501 if (linearY == GradientDirection::TOP) {
1502 resultJson->Put("direction", "GradientDirection.LeftTop");
1503 } else if (linearY == GradientDirection::BOTTOM) {
1504 resultJson->Put("direction", "GradientDirection.LeftBottom");
1505 } else {
1506 resultJson->Put("direction", "GradientDirection.Left");
1507 }
1508 } else if (linearX == GradientDirection::RIGHT) {
1509 if (linearY == GradientDirection::TOP) {
1510 resultJson->Put("direction", "GradientDirection.RightTop");
1511 } else if (linearY == GradientDirection::BOTTOM) {
1512 resultJson->Put("direction", "GradientDirection.RightBottom");
1513 } else {
1514 resultJson->Put("direction", "GradientDirection.Right");
1515 }
1516 } else {
1517 if (linearY == GradientDirection::TOP) {
1518 resultJson->Put("direction", "GradientDirection.Top");
1519 } else if (linearY == GradientDirection::BOTTOM) {
1520 resultJson->Put("direction", "GradientDirection.Bottom");
1521 } else {
1522 resultJson->Put("direction", "GradientDirection.None");
1523 }
1524 }
1525 GetColorsAndRepeating(resultJson, lineGradient);
1526 }
1527 return resultJson;
1528 }
1529
GetSweepGradient() const1530 std::unique_ptr<JsonValue> InspectorComposedElement::GetSweepGradient() const
1531 {
1532 auto resultJson = JsonUtil::Create(true);
1533 auto node = GetRenderBox();
1534 if (!node) {
1535 return resultJson;
1536 }
1537 auto decoration = node->GetBackDecoration();
1538 if (decoration) {
1539 auto sweepGradient = decoration->GetGradient();
1540 if (GradientType::SWEEP != sweepGradient.GetType()) {
1541 return resultJson;
1542 }
1543 auto radialCenterX = sweepGradient.GetSweepGradient().centerX;
1544 auto radialCenterY = sweepGradient.GetSweepGradient().centerY;
1545 if (radialCenterX && radialCenterY) {
1546 auto jsPoint = JsonUtil::CreateArray(true);
1547 jsPoint->Put("0", radialCenterX->ToString().c_str());
1548 jsPoint->Put("1", radialCenterY->ToString().c_str());
1549 resultJson->Put("center", jsPoint);
1550 }
1551
1552 auto startAngle = sweepGradient.GetSweepGradient().startAngle;
1553 auto endAngle = sweepGradient.GetSweepGradient().endAngle;
1554 if (startAngle) {
1555 resultJson->Put("start", startAngle->ToString().c_str());
1556 }
1557 if (endAngle) {
1558 resultJson->Put("end", endAngle->ToString().c_str());
1559 }
1560
1561 GetColorsAndRepeating(resultJson, sweepGradient);
1562 }
1563 return resultJson;
1564 }
1565
GetRadialGradient() const1566 std::unique_ptr<JsonValue> InspectorComposedElement::GetRadialGradient() const
1567 {
1568 auto resultJson = JsonUtil::Create(true);
1569 auto node = GetRenderBox();
1570 if (!node) {
1571 return resultJson;
1572 }
1573 auto decoration = node->GetBackDecoration();
1574 if (decoration) {
1575 auto radialGradient = decoration->GetGradient();
1576 if (GradientType::RADIAL != radialGradient.GetType()) {
1577 return resultJson;
1578 }
1579
1580 auto radialCenterX = radialGradient.GetRadialGradient().radialCenterX;
1581 auto radialCenterY = radialGradient.GetRadialGradient().radialCenterY;
1582 if (radialCenterX && radialCenterY) {
1583 auto jsPoint = JsonUtil::CreateArray(true);
1584 jsPoint->Put("0", radialCenterX->ToString().c_str());
1585 jsPoint->Put("1", radialCenterY->ToString().c_str());
1586 resultJson->Put("center", jsPoint);
1587 }
1588
1589 auto radius = radialGradient.GetRadialGradient().radialVerticalSize;
1590 if (radius) {
1591 resultJson->Put("radius", radius->ToString().c_str());
1592 }
1593
1594 GetColorsAndRepeating(resultJson, radialGradient);
1595 }
1596 return resultJson;
1597 }
1598
GetTag() const1599 const std::string& InspectorComposedElement::GetTag() const
1600 {
1601 auto iter = COMPONENT_TAG_TO_ETS_TAG_MAP.find(name_);
1602 return iter != COMPONENT_TAG_TO_ETS_TAG_MAP.end() ? iter->second : name_;
1603 }
1604
GetClickable() const1605 bool InspectorComposedElement::GetClickable() const
1606 {
1607 auto node = GetAccessibilityNode();
1608 if (!node) {
1609 return false;
1610 }
1611 return node->GetClickableState();
1612 }
GetCheckable() const1613 bool InspectorComposedElement::GetCheckable() const
1614 {
1615 auto node = GetAccessibilityNode();
1616 if (!node) {
1617 return false;
1618 }
1619 return node->GetCheckableState();
1620 }
GetFocusable() const1621 bool InspectorComposedElement::GetFocusable() const
1622 {
1623 auto focusableElement = GetContentElement<FocusableElement>(FocusableElement::TypeId(), false);
1624 if (!focusableElement) {
1625 return false;
1626 }
1627 return focusableElement->IsFocusable();
1628 }
GetScrollable() const1629 bool InspectorComposedElement::GetScrollable() const
1630 {
1631 auto node = GetAccessibilityNode();
1632 if (!node) {
1633 return false;
1634 }
1635 return node->GetScrollableState();
1636 }
GetLongClickable() const1637 bool InspectorComposedElement::GetLongClickable() const
1638 {
1639 auto node = GetAccessibilityNode();
1640 if (!node) {
1641 return false;
1642 }
1643 return node->GetLongClickableState();
1644 }
GetTouchable() const1645 bool InspectorComposedElement::GetTouchable() const
1646 {
1647 auto render = GetRenderBox();
1648 if (!render) {
1649 return false;
1650 }
1651 return render->IsTouchable();
1652 }
IsSelected() const1653 bool InspectorComposedElement::IsSelected() const
1654 {
1655 auto node = GetAccessibilityNode();
1656 if (!node) {
1657 return false;
1658 }
1659 return node->GetSelectedState();
1660 }
IsPassword() const1661 bool InspectorComposedElement::IsPassword() const
1662 {
1663 auto node = GetAccessibilityNode();
1664 if (!node) {
1665 return false;
1666 }
1667 return node->GetIsPassword();
1668 }
IsChecked() const1669 bool InspectorComposedElement::IsChecked() const
1670 {
1671 auto node = GetAccessibilityNode();
1672 if (!node) {
1673 return false;
1674 }
1675 return node->GetCheckedState();
1676 }
IsFocused() const1677 bool InspectorComposedElement::IsFocused() const
1678 {
1679 auto node = GetAccessibilityNode();
1680 if (!node) {
1681 return false;
1682 }
1683 return node->GetFocusedState();
1684 }
1685
TriggerVisibleAreaChangeCallback(std::list<VisibleCallbackInfo> & callbackInfoList)1686 void InspectorComposedElement::TriggerVisibleAreaChangeCallback(std::list<VisibleCallbackInfo>& callbackInfoList)
1687 {
1688 auto renderBox = GetRenderBox();
1689 if (!renderBox) {
1690 LOGE("TriggerVisibleAreaChangeCallback: Get render box failed.");
1691 return;
1692 }
1693
1694 auto context = context_.Upgrade();
1695 if (!context) {
1696 LOGW("get context failed");
1697 return;
1698 }
1699
1700 const auto& renderNode = GetRenderNode();
1701 if (!renderNode) {
1702 LOGE("Get render node failed!");
1703 return;
1704 }
1705
1706 if (!context->GetOnShow() || renderNode->GetHidden() || inspectorId_ == -1) {
1707 ProcessAllVisibleCallback(callbackInfoList, VISIBLE_RATIO_MIN);
1708 return;
1709 }
1710
1711 auto margin = renderBox->GetMargin();
1712 auto renderRect = GetRenderRect() + Offset(margin.LeftPx(), margin.TopPx());
1713 renderRect -= margin.GetLayoutSize();
1714
1715 Rect visibleRect = renderRect;
1716 Rect parentRect;
1717 auto parent = renderNode->GetParent().Upgrade();
1718 while (parent) {
1719 parentRect = parent->GetRectBasedWindowTopLeft();
1720 visibleRect = visibleRect.Constrain(parentRect);
1721 parent = parent->GetParent().Upgrade();
1722 }
1723
1724 double currentVisibleRatio =
1725 std::clamp(CalculateCurrentVisibleRatio(visibleRect, renderRect), VISIBLE_RATIO_MIN, VISIBLE_RATIO_MAX);
1726 ProcessAllVisibleCallback(callbackInfoList, currentVisibleRatio);
1727 }
1728
CalculateCurrentVisibleRatio(const Rect & visibleRect,const Rect & renderRect)1729 double InspectorComposedElement::CalculateCurrentVisibleRatio(const Rect& visibleRect, const Rect& renderRect)
1730 {
1731 if (!visibleRect.IsValid() || !renderRect.IsValid()) {
1732 return 0.0;
1733 }
1734
1735 return visibleRect.Width() * visibleRect.Height() / (renderRect.Width() * renderRect.Height());
1736 }
1737
ProcessAllVisibleCallback(std::list<VisibleCallbackInfo> & callbackInfoList,double currentVisibleRatio)1738 void InspectorComposedElement::ProcessAllVisibleCallback(
1739 std::list<VisibleCallbackInfo>& callbackInfoList, double currentVisibleRatio)
1740 {
1741 for (auto& nodeCallbackInfo : callbackInfoList) {
1742 if (GreatNotEqual(currentVisibleRatio, nodeCallbackInfo.visibleRatio) && !nodeCallbackInfo.isCurrentVisible) {
1743 OnVisibleAreaChangeCallback(nodeCallbackInfo, true, currentVisibleRatio);
1744 continue;
1745 }
1746
1747 if (LessNotEqual(currentVisibleRatio, nodeCallbackInfo.visibleRatio) && nodeCallbackInfo.isCurrentVisible) {
1748 OnVisibleAreaChangeCallback(nodeCallbackInfo, false, currentVisibleRatio);
1749 continue;
1750 }
1751
1752 if (NearEqual(currentVisibleRatio, nodeCallbackInfo.visibleRatio)) {
1753 if (NearEqual(nodeCallbackInfo.visibleRatio, VISIBLE_RATIO_MIN) && nodeCallbackInfo.isCurrentVisible) {
1754 OnVisibleAreaChangeCallback(nodeCallbackInfo, false, VISIBLE_RATIO_MIN);
1755 }
1756
1757 if (NearEqual(nodeCallbackInfo.visibleRatio, VISIBLE_RATIO_MAX) && !nodeCallbackInfo.isCurrentVisible) {
1758 OnVisibleAreaChangeCallback(nodeCallbackInfo, true, VISIBLE_RATIO_MAX);
1759 }
1760 }
1761 }
1762 }
1763
OnVisibleAreaChangeCallback(VisibleCallbackInfo & callbackInfo,bool visibleType,double currentVisibleRatio)1764 void InspectorComposedElement::OnVisibleAreaChangeCallback(
1765 VisibleCallbackInfo& callbackInfo, bool visibleType, double currentVisibleRatio)
1766 {
1767 callbackInfo.isCurrentVisible = visibleType;
1768 if (callbackInfo.callback) {
1769 callbackInfo.callback(visibleType, currentVisibleRatio);
1770 }
1771 }
1772
GetHitTestBehaviorStr() const1773 std::string InspectorComposedElement::GetHitTestBehaviorStr() const
1774 {
1775 auto node = GetInspectorNode(GetTargetTypeId());
1776 if (!node) {
1777 return HIT_TEST_BEHAVIOR[0];
1778 }
1779 auto hitTestMode = static_cast<int32_t>(node->GetHitTestMode());
1780 return HIT_TEST_BEHAVIOR[hitTestMode];
1781 }
1782
1783 } // namespace OHOS::Ace::V2
1784