• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_BASE_ELEMENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_ELEMENT_H
18 
19 #include <list>
20 
21 #include "base/utils/macros.h"
22 #include "core/focus/focus_node.h"
23 #include "core/gestures/gesture_recognizer.h"
24 #include "core/pipeline/base/component.h"
25 #include "core/pipeline/base/render_node.h"
26 
27 namespace OHOS::Ace {
28 
29 class PageElement;
30 class PipelineContext;
31 
32 // If no insertion location is specified, new child will be added to the end of children list by default.
33 constexpr int32_t DEFAULT_ELEMENT_SLOT = -1;
34 constexpr int32_t DEFAULT_RENDER_SLOT = -1;
35 
36 // Element is the key class in the UI framework, which presents a basic logic
37 // unit to construct a view hierarchy.
38 // There are two types inherited element: RenderElement and ComposedElement.
39 class ACE_EXPORT Element : public virtual AceType {
40     DECLARE_ACE_TYPE(Element, AceType);
41 
42 public:
43     Element() = default;
44     ~Element();
45 
46     void AddChild(const RefPtr<Element>& child, int32_t slot = DEFAULT_ELEMENT_SLOT);
47     void RemoveChild(const RefPtr<Element>& child);
48     RefPtr<Element> GetChildBySlot(int32_t slot);
49     void DeactivateChild(RefPtr<Element> child);
50     void Rebuild();
51 
52     // create a new child element and mount to element tree.
53     RefPtr<Element> InflateComponent(const RefPtr<Component>& newComponent, int32_t slot, int32_t renderSlot);
54     void Mount(
55         const RefPtr<Element>& parent, int32_t slot = DEFAULT_ELEMENT_SLOT, int32_t renderSlot = DEFAULT_RENDER_SLOT);
56     void AddToFocus();
57     virtual RefPtr<Element> UpdateChild(const RefPtr<Element>& child, const RefPtr<Component>& newComponent) = 0;
58     RefPtr<Element> UpdateChildWithSlot(
59         const RefPtr<Element>& child, const RefPtr<Component>& newComponent, int32_t slot, int32_t renderSlot);
60 
61     void DetachChild(const RefPtr<Element>&);
62     RefPtr<Element> RetakeDeactivateElement(const RefPtr<Component>& newComponent);
63 
OnMount()64     virtual void OnMount() {}
Detached()65     virtual void Detached() {}
Activate()66     virtual void Activate() {}
Deactivate()67     virtual void Deactivate() {}
UmountRender()68     virtual void UmountRender() {}
Prepare(const WeakPtr<Element> & parent)69     virtual void Prepare(const WeakPtr<Element>& parent) {}
PerformBuild()70     virtual void PerformBuild() {}
Update()71     virtual void Update() {}
72     virtual void DumpTree(int32_t depth);
73     virtual void DumpTree(int32_t depth, std::vector<std::string>& info);
74     virtual void Dump();
75     virtual bool CanUpdate(const RefPtr<Component>& newComponent);
76 
77     virtual void MarkDirty();
78 
NeedUpdateWithComponent(const RefPtr<Component> & newComponent)79     virtual bool NeedUpdateWithComponent(const RefPtr<Component>& newComponent)
80     {
81         return true;
82     }
83 
84     void SetUpdateComponent(const RefPtr<Component>& newComponent);
85 
NeedUpdate()86     bool NeedUpdate() const
87     {
88         return component_ != nullptr;
89     }
90 
SetDepth(int32_t depth)91     void SetDepth(int32_t depth)
92     {
93         depth_ = depth;
94     }
95 
GetDepth()96     int32_t GetDepth() const
97     {
98         return depth_;
99     }
100 
101     void SetPipelineContext(const WeakPtr<PipelineContext>& context);
102 
103     enum ElementType {
104         BASE_ELEMENT,
105         RENDER_ELEMENT,
106         COMPOSED_ELEMENT,
107     };
108 
GetType()109     virtual ElementType GetType() const
110     {
111         return type_;
112     }
113 
SetNewComponent(const RefPtr<Component> & newComponent)114     virtual void SetNewComponent(const RefPtr<Component>& newComponent)
115     {
116         component_ = newComponent;
117         if (newComponent) {
118             retakeId_ = newComponent->GetRetakeId();
119             componentTypeId_ = AceType::TypeId(component_);
120             ignoreInspector_ = newComponent->IsIgnoreInspector();
121             MarkNeedRebuild();
122         }
123     }
124 
125     RefPtr<FocusGroup> GetFocusScope();
126 
127     RefPtr<Element> GetFirstChild() const;
128     RefPtr<Element> GetLastChild() const;
129 
GetRenderNode()130     virtual RefPtr<RenderNode> GetRenderNode() const
131     {
132         if (children_.empty()) {
133             return nullptr;
134         } else {
135             return children_.front()->GetRenderNode();
136         }
137     }
138 
GetRenderRect()139     Rect GetRenderRect() const
140     {
141         auto renderNode = GetRenderNode();
142         if (renderNode) {
143             return renderNode->GetHidden() ? Rect() : renderNode->GetRectBasedWindowTopLeft();
144         }
145         return Rect();
146     }
147 
GetRenderRectInLocal()148     Rect GetRenderRectInLocal() const
149     {
150         auto renderNode = GetRenderNode();
151         if (renderNode) {
152             return renderNode->GetPaintRect();
153         }
154         return Rect();
155     }
156 
157     const std::list<RefPtr<Element>>& GetChildren() const;
158 
GetContext()159     const WeakPtr<PipelineContext>& GetContext() const
160     {
161         return context_;
162     }
163 
SetSlot(int32_t slot)164     void SetSlot(int32_t slot)
165     {
166         slot_ = slot;
167     }
168 
GetSlot()169     int32_t GetSlot() const
170     {
171         return slot_;
172     }
173 
SetRenderSlot(int32_t slot)174     void SetRenderSlot(int32_t slot)
175     {
176         renderSlot_ = slot;
177     }
178 
GetRenderSlot()179     int32_t GetRenderSlot() const
180     {
181         return renderSlot_;
182     }
183 
184     virtual int32_t CountRenderNode() const = 0;
185 
GetElementParent()186     const WeakPtr<Element>& GetElementParent() const
187     {
188         return parent_;
189     }
190 
191     RefPtr<PageElement> GetPageElement();
192 
IsAutoAccessibility()193     bool IsAutoAccessibility() const
194     {
195         return autoAccessibility_;
196     }
197 
SetAutoAccessibility(bool value)198     void SetAutoAccessibility(bool value)
199     {
200         autoAccessibility_ = value;
201     }
202 
SetRetakeId(int32_t retakeId)203     void SetRetakeId(int32_t retakeId)
204     {
205         retakeId_ = retakeId;
206     }
207 
GetRetakeId()208     int32_t GetRetakeId() const
209     {
210         return retakeId_;
211     }
212 
GetComponent()213     RefPtr<Component> GetComponent() const
214     {
215         return component_;
216     }
217 
218     void RebuildFocusTree();
219 
220     std::list<RefPtr<FocusNode>> RebuildFocusChild();
221 
SetParent(const WeakPtr<Element> & parent)222     void SetParent(const WeakPtr<Element>& parent)
223     {
224         parent_ = parent;
225     }
226 
227 #if defined(WINDOWS_PLATFORM) || defined(MAC_PLATFORM)
GetChildrenSize()228     int32_t GetChildrenSize() {
229         return children_.size();
230     }
231 #endif
232 
MarkNeedRebuild()233     void MarkNeedRebuild()
234     {
235         needRebuild_ = true;
236     }
237 
IsActive()238     bool IsActive() const
239     {
240         return active_;
241     }
242 
SetIgnoreInspector(bool ignoreInspector)243     void SetIgnoreInspector(bool ignoreInspector)
244     {
245         ignoreInspector_ = ignoreInspector;
246     }
247 
IsIgnoreInspector()248     bool IsIgnoreInspector() const
249     {
250         return ignoreInspector_;
251     }
252 
253     virtual void UnregisterForElementProxy();
254 
255 protected:
256     inline RefPtr<Element> DoUpdateChildWithNewComponent(
257         const RefPtr<Element>& child, const RefPtr<Component>& newComponent, int32_t slot, int32_t renderSlot);
258 
259     virtual void Apply(const RefPtr<Element>& child) = 0;
260 
OnContextAttached()261     virtual void OnContextAttached() {}
262 
263     void MarkActive(bool active);
264 
OnActive()265     virtual void OnActive() {}
266 
OnInactive()267     virtual void OnInactive() {}
268 
GetThemeManager()269     RefPtr<ThemeManager> GetThemeManager() const
270     {
271         auto context = context_.Upgrade();
272         if (!context) {
273             return nullptr;
274         }
275         return context->GetThemeManager();
276     }
277 
278     ElementType type_ = BASE_ELEMENT;
279     std::list<RefPtr<Element>> children_;
280     RefPtr<Component> component_;
281     WeakPtr<PipelineContext> context_;
282     IdType componentTypeId_ = 0;
283     bool active_ = false;
284 
285 private:
286     void ChangeChildSlot(const RefPtr<Element>& child, int32_t slot);
287     void ChangeChildRenderSlot(const RefPtr<Element>& child, int32_t renderSlot, bool effectDescendant);
288 
289     WeakPtr<Element> parent_;
290     int32_t depth_ = 0;
291     int32_t slot_ = DEFAULT_ELEMENT_SLOT;
292     int32_t renderSlot_ = DEFAULT_RENDER_SLOT;
293     bool autoAccessibility_ = true;
294     bool needRebuild_ = false;
295     // One-to-one correspondence with component through retakeId
296     int32_t retakeId_ = 0;
297     bool ignoreInspector_ = false;
298 };
299 
300 } // namespace OHOS::Ace
301 
302 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_ELEMENT_H
303