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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_V2_INSPECTOR_INSPECTOR_COMPOSED_ELEMENT_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_V2_INSPECTOR_INSPECTOR_COMPOSED_ELEMENT_H 18 19 #include <tuple> 20 21 #include "base/geometry/matrix4.h" 22 #include "core/components/box/render_box.h" 23 #include "core/components/display/display_component.h" 24 #include "core/components/popup/popup_element_v2.h" 25 #include "core/components_v2/inspector/inspector_node.h" 26 #include "core/pipeline/base/composed_element.h" 27 28 namespace OHOS::Ace::V2 { 29 30 class ACE_EXPORT InspectorComposedElement : public ComposedElement, public InspectorNode { 31 DECLARE_ACE_TYPE(InspectorComposedElement, ComposedElement, InspectorNode) 32 33 public: 34 explicit InspectorComposedElement(const ComposeId& id); 35 ~InspectorComposedElement() override; 36 37 void Prepare(const WeakPtr<Element>& weakParent) override; 38 void Update() override; 39 bool CanUpdate(const RefPtr<Component>& newComponent) override; 40 template<class T> GetInspectorElement(IdType typeId)41 RefPtr<T> GetInspectorElement(IdType typeId) const 42 { 43 auto child = children_.empty() ? nullptr : children_.front(); 44 while (child) { 45 auto inspectorComposedElement = AceType::DynamicCast<InspectorComposedElement>(child); 46 if (inspectorComposedElement) { 47 return nullptr; 48 } 49 if (AceType::TypeId(child) == typeId) { 50 return AceType::DynamicCast<T>(child->GetRenderNode()); 51 } 52 53 child = child->GetChildren().empty() ? nullptr : child->GetChildren().front(); 54 } 55 return nullptr; 56 } 57 void AddComposedComponentId(); 58 void RemoveInspectorNode(int32_t id); 59 RefPtr<RenderNode> GetInspectorNode(IdType typeId, bool isForward = false) const; 60 61 void OnInactive() override; 62 void OnActive() override; 63 64 template<class T> 65 RefPtr<T> GetContentElement(IdType typeId, bool isFindAll = true) const 66 { 67 auto child = children_.empty() ? nullptr : children_.front(); 68 while (child) { 69 LOGD("GetContentElement: child = %{public}s", AceType::TypeName(child)); 70 auto inspectorComposedElement = AceType::DynamicCast<InspectorComposedElement>(child); 71 if (inspectorComposedElement && !isFindAll) { 72 return nullptr; 73 } 74 if (AceType::TypeId(child) == typeId) { 75 return AceType::DynamicCast<T>(child); 76 } 77 child = child->GetChildren().empty() ? nullptr : child->GetChildren().front(); 78 } 79 return nullptr; 80 } 81 82 template<class T> GetContentRender(IdType typeId)83 RefPtr<T> GetContentRender(IdType typeId) const 84 { 85 auto child = children_.empty() ? nullptr : children_.front(); 86 while (child) { 87 if (AceType::TypeId(child) == typeId) { 88 return AceType::DynamicCast<T>(child->GetRenderNode()); 89 } 90 child = child->GetChildren().empty() ? nullptr : child->GetChildren().front(); 91 } 92 return nullptr; 93 } 94 GetInspectorComposedElement()95 RefPtr<Element> GetInspectorComposedElement() 96 { 97 auto child = children_.empty() ? nullptr : children_.front(); 98 while (child) { 99 auto inspectorComposedElement = AceType::DynamicCast<InspectorComposedElement>(child); 100 if (inspectorComposedElement) { 101 return child; 102 } 103 child = child->GetChildren().empty() ? nullptr : child->GetChildren().front(); 104 } 105 return nullptr; 106 } 107 108 virtual RefPtr<Element> GetElementChildBySlot(const RefPtr<Element>& element, int32_t& slot) const; 109 RefPtr<Element> GetInspectorComposedElementParent(const RefPtr<Element>& element) const; 110 111 // dimension settings 112 std::string GetWidth() const override; 113 std::string GetHeight() const override; 114 std::unique_ptr<JsonValue> GetSize() const override; 115 std::string GetPadding() const override; 116 Dimension GetMargin(OHOS::Ace::AnimatableType type) const override; 117 std::string GetAllMargin() const override; 118 std::string GetConstraintSize() const override; 119 int32_t GetLayoutPriority() const override; 120 int32_t GetLayoutWeight() const override; 121 122 // position settings 123 std::string GetAlign() const override; 124 std::string GetDirectionStr() const override; 125 TextDirection GetDirection() const override; 126 std::unique_ptr<JsonValue> GetPosition() const override; 127 std::unique_ptr<JsonValue> GetMarkAnchor() const override; 128 std::unique_ptr<JsonValue> GetOffset() const override; 129 std::string GetRect() override; 130 Rect GetParentRect() const override; 131 132 // layout constraint 133 double GetAspectRatio() const override; 134 int32_t GetDisplayPriority() const override; 135 136 // flex layout 137 std::string GetFlexBasis() const override; 138 double GetFlexGrow() const override; 139 double GetFlexShrink() const override; 140 std::string GetAlignSelf() const override; 141 142 // border settings 143 Border GetBorder() const override; 144 std::unique_ptr<JsonValue> GetUnifyBorder() const override; 145 std::string GetBorderStyle() const override; 146 std::string GetBorderWidth() const override; 147 std::string GetBorderColor() const override; 148 std::string GetBorderRadius() const override; 149 150 // background settings 151 RefPtr<Decoration> GetBackDecoration() const override; 152 std::string GetBackgroundImage() const override; 153 std::string GetBackgroundColor() const override; 154 std::string GetBackgroundImageSize() const override; 155 std::string GetBackgroundImagePosition() const override; 156 157 // front decoration settings 158 RefPtr<Decoration> GetFrontDecoration() const override; 159 160 // opacity settings 161 double GetOpacity() const override; 162 163 // visibility settings 164 std::string GetVisibility() const override; 165 166 // enable settings 167 bool GetEnabled() const override; 168 169 // zindex settings 170 int32_t GetZIndex() const override; 171 172 // hit test behavior settings 173 std::string GetHitTestBehaviorStr() const override; 174 175 // graphical transformation 176 DimensionOffset GetOriginPoint() const override; 177 std::unique_ptr<JsonValue> GetRotate() const override; 178 std::unique_ptr<JsonValue> GetScale() const override; 179 std::unique_ptr<JsonValue> GetTransform() const override; 180 std::unique_ptr<JsonValue> GetTranslate() const override; 181 182 double GetBlur() const override; 183 double GetBackDropBlur() const override; 184 std::unique_ptr<JsonValue> GetWindowBlur() const override; 185 std::unique_ptr<JsonValue> GetShadow() const override; 186 double GetBrightness() const override; 187 double GetSaturate() const override; 188 double GetContrast() const override; 189 double GetInvert() const override; 190 double GetSepia() const override; 191 double GetGrayScale() const override; 192 double GetHueRotate() const override; 193 std::string GetColorBlend() const override; 194 195 // shape clip 196 std::string GetClip() const override; 197 bool GetClipFlag() const; 198 std::unique_ptr<JsonValue> GetMask() const override; 199 200 // grid setting 201 int32_t GetGridSpan() const override; 202 int32_t GetGridOffset() const override; 203 std::unique_ptr<JsonValue> GetUseSizeType() const override; 204 RefPtr<GridColumnInfo> GetGridColumnInfo() const override; 205 206 // useAlign seeting 207 std::unique_ptr<JsonValue> GetUseAlign() const override; 208 std::unique_ptr<JsonValue> ToJsonObject() const override; 209 210 std::unique_ptr<JsonValue> GetOverlay() const override; 211 212 // color gradient 213 std::unique_ptr<JsonValue> GetLinearGradient() const override; 214 std::unique_ptr<JsonValue> GetSweepGradient() const override; 215 std::unique_ptr<JsonValue> GetRadialGradient() const override; 216 void GetColorsAndRepeating(std::unique_ptr<JsonValue>& resultJson, const Gradient& gradient) const; 217 218 // bindpopup 219 std::string GetBindPopup() const override; GetTargetTypeId()220 virtual AceType::IdType GetTargetTypeId() const 221 { 222 return AceType::TypeId(this); 223 } 224 225 // bindcontextmenu 226 std::string GetBindContextMenu() const override; 227 GetRenderElement()228 virtual RefPtr<Element> GetRenderElement() const 229 { 230 return nullptr; 231 } 232 virtual void AddChildWithSlot(int32_t slot, const RefPtr<Component>& newComponent); 233 virtual void UpdateChildWithSlot(int32_t slot, const RefPtr<Component>& newComponent); 234 virtual void DeleteChildWithSlot(int32_t slot); 235 236 void UpdateEventTarget(BaseEventInfo& info) const override; 237 238 std::pair<Rect, Offset> GetCurrentRectAndOrigin() const override; GetLastRectAndOrigin()239 std::pair<Rect, Offset> GetLastRectAndOrigin() override 240 { 241 if (!lastRectOriginPtr_) { 242 lastRectOriginPtr_ = std::make_unique<std::pair<Rect, Offset>>(); 243 } 244 return *lastRectOriginPtr_; 245 } UpdateLastRectAndOrigin(const std::pair<Rect,Offset> & curRectOrigin)246 void UpdateLastRectAndOrigin(const std::pair<Rect, Offset>& curRectOrigin) override 247 { 248 if (!lastRectOriginPtr_) { 249 lastRectOriginPtr_ = std::make_unique<std::pair<Rect, Offset>>(); 250 } 251 *lastRectOriginPtr_ = curRectOrigin; 252 } 253 IsRectValid()254 bool IsRectValid() const 255 { 256 return isRectValid_; 257 } 258 SetKey(const std::string & key)259 void SetKey(const std::string& key) 260 { 261 key_ = key; 262 } 263 GetKey()264 const std::string& GetKey() const 265 { 266 return key_; 267 } 268 269 const std::string& GetTag() const; 270 bool GetClickable() const override; 271 bool GetCheckable() const override; 272 bool GetFocusable() const override; 273 bool GetScrollable() const override; 274 bool GetLongClickable() const override; 275 bool GetTouchable() const override; 276 bool IsSelected() const override; 277 bool IsPassword() const override; 278 bool IsChecked() const override; 279 bool IsFocused() const override; 280 void TriggerVisibleAreaChangeCallback(std::list<VisibleCallbackInfo>& callbackInfoList); 281 282 protected: 283 RefPtr<RenderBox> GetRenderBox() const; 284 285 RefPtr<AccessibilityNode> accessibilityNode_; 286 bool accessibilityEnabled_ = false; 287 RefPtr<AccessibilityNode> GetAccessibilityNode() const; 288 289 private: 290 RefPtr<PopupElementV2> GetPopupElement() const; 291 void ProcessAllVisibleCallback(std::list<VisibleCallbackInfo>& callbackInfoList, double currentVisibleRatio); 292 void OnVisibleAreaChangeCallback( 293 VisibleCallbackInfo& callbackInfo, bool visibleType, double currentVisibleRatio); 294 double CalculateCurrentVisibleRatio(const Rect& visibleRect, const Rect& renderRect); 295 bool isRectValid_; 296 std::string key_; 297 std::unique_ptr<std::pair<Rect, Offset>> lastRectOriginPtr_; 298 }; 299 300 } // namespace OHOS::Ace::V2 301 302 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_V2_INSPECTOR_INSPECTOR_COMPOSED_ELEMENT_H 303