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