1 /* 2 * Copyright (c) 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_NG_BASE_UI_NODE_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_BASE_UI_NODE_H 18 19 #include <cstdint> 20 #include <list> 21 22 #include "base/geometry/ng/point_t.h" 23 #include "base/memory/ace_type.h" 24 #include "base/memory/referenced.h" 25 #include "base/utils/macros.h" 26 #include "core/components_ng/event/focus_hub.h" 27 #include "core/components_ng/event/gesture_event_hub.h" 28 #include "core/components_ng/layout/layout_wrapper.h" 29 #include "core/event/touch_event.h" 30 31 namespace OHOS::Ace::NG { 32 33 class PipelineContext; 34 constexpr int32_t DEFAULT_NODE_SLOT = -1; 35 36 // UINode is the base class of FrameNode and SyntaxNode. 37 class ACE_EXPORT UINode : public virtual AceType { 38 DECLARE_ACE_TYPE(UINode, AceType); 39 40 public: 41 UINode(const std::string& tag, int32_t nodeId, bool isRoot = false) tag_(tag)42 : tag_(tag), nodeId_(nodeId), accessibilityId_(currentAccessibilityId_++), isRoot_(isRoot) 43 {} 44 ~UINode() override; 45 46 // atomic node is like button, image, custom node and so on. 47 // In ets UI compiler, the atomic node does not Add Pop function, only have Create function. 48 virtual bool IsAtomicNode() const = 0; 49 50 virtual int32_t FrameCount() const; 51 52 RefPtr<LayoutWrapper> CreateLayoutWrapper(bool forceMeasure = false, bool forceLayout = false) const; 53 54 // Tree operation start. 55 void AddChild(const RefPtr<UINode>& child, int32_t slot = DEFAULT_NODE_SLOT, bool silently = false); 56 std::list<RefPtr<UINode>>::iterator RemoveChild(const RefPtr<UINode>& child); 57 int32_t RemoveChildAndReturnIndex(const RefPtr<UINode>& child); 58 void ReplaceChild(const RefPtr<UINode>& oldNode, const RefPtr<UINode>& newNode); 59 void MovePosition(int32_t slot); 60 void MountToParent(const RefPtr<UINode>& parent, int32_t slot = DEFAULT_NODE_SLOT, bool silently = false); 61 RefPtr<FrameNode> GetFocusParent() const; 62 RefPtr<FocusHub> GetFirstFocusHubChild() const; 63 void GetFocusChildren(std::list<RefPtr<FrameNode>>& children) const; 64 void Clean(); 65 void RemoveChildAtIndex(int32_t index); 66 RefPtr<UINode> GetChildAtIndex(int32_t index) const; 67 int32_t GetChildIndex(const RefPtr<UINode>& child) const; 68 void AttachToMainTree(); 69 void DetachFromMainTree(); 70 71 int32_t TotalChildCount() const; 72 73 // Returns index in the flattern tree structure 74 // of the node with given id and type 75 // Returns std::pair with 76 // boolean first - inidication of node is found 77 // int32_t second - index of the node 78 std::pair<bool, int32_t> GetChildFlatIndex(int32_t id); 79 GetChildren()80 const std::list<RefPtr<UINode>>& GetChildren() const 81 { 82 return children_; 83 } 84 GetLastChild()85 RefPtr<UINode> GetLastChild() 86 { 87 if (children_.empty()) { 88 return nullptr; 89 } 90 return children_.back(); 91 } 92 GetFirstChild()93 RefPtr<UINode> GetFirstChild() 94 { 95 if (children_.empty()) { 96 return nullptr; 97 } 98 return children_.front(); 99 } 100 101 void GenerateOneDepthVisibleFrame(std::list<RefPtr<FrameNode>>& visibleList); 102 void GenerateOneDepthAllFrame(std::list<RefPtr<FrameNode>>& allList); 103 104 int32_t GetChildIndexById(int32_t id); 105 GetParent()106 RefPtr<UINode> GetParent() const 107 { 108 return parent_.Upgrade(); 109 } 110 SetParent(const WeakPtr<UINode> & parent)111 void SetParent(const WeakPtr<UINode>& parent) 112 { 113 parent_ = parent; 114 } 115 // Tree operation end. 116 117 static RefPtr<PipelineContext> GetContext(); 118 119 // When FrameNode creates a layout task, the corresponding LayoutWrapper tree is created, and UINode needs to update 120 // the corresponding LayoutWrapper tree node at this time like add self wrapper to wrapper tree. 121 virtual void AdjustLayoutWrapperTree(const RefPtr<LayoutWrapper>& parent, bool forceMeasure, bool forceLayout); 122 123 // DFX info. 124 void DumpTree(int32_t depth); 125 GetTag()126 const std::string& GetTag() const 127 { 128 return tag_; 129 } 130 GetId()131 int32_t GetId() const 132 { 133 return nodeId_; 134 } 135 GetAccessibilityId()136 int32_t GetAccessibilityId() const 137 { 138 return accessibilityId_; 139 } 140 SetDepth(int32_t depth)141 void SetDepth(int32_t depth) 142 { 143 depth_ = depth; 144 for (auto& child : children_) { 145 child->SetDepth(depth_ + 1); 146 } 147 } 148 IsRootNode()149 bool IsRootNode() const 150 { 151 return isRoot_; 152 } 153 GetDepth()154 int32_t GetDepth() const 155 { 156 return depth_; 157 } 158 GetRootId()159 int32_t GetRootId() const 160 { 161 return hostRootId_; 162 } 163 GetPageId()164 int32_t GetPageId() const 165 { 166 return hostPageId_; 167 } 168 169 // TODO: SetHostRootId step on create node. SetHostRootId(int32_t id)170 void SetHostRootId(int32_t id) 171 { 172 hostRootId_ = id; 173 } 174 175 // TODO: SetHostPageId step on mount to page. SetHostPageId(int32_t id)176 void SetHostPageId(int32_t id) 177 { 178 hostPageId_ = id; 179 for (auto& child : children_) { 180 child->SetHostPageId(id); 181 } 182 } 183 SetRemoveSilently(bool removeSilently)184 void SetRemoveSilently(bool removeSilently) 185 { 186 removeSilently_ = removeSilently; 187 } 188 SetUndefinedNodeId()189 void SetUndefinedNodeId() 190 { 191 nodeId_ = -1; 192 } 193 SetInDestroying()194 void SetInDestroying() 195 { 196 isInDestroying_ = true; 197 } 198 IsInDestroying()199 bool IsInDestroying() const 200 { 201 return isInDestroying_; 202 } 203 204 void SetChildrenInDestroying(); 205 206 virtual HitTestResult TouchTest(const PointF& globalPoint, const PointF& parentLocalPoint, 207 const TouchRestrict& touchRestrict, TouchTestResult& result, int32_t touchId); GetHitTestMode()208 virtual HitTestMode GetHitTestMode() const 209 { 210 return HitTestMode::HTMDEFAULT; 211 } 212 213 virtual HitTestResult MouseTest(const PointF& globalPoint, const PointF& parentLocalPoint, 214 MouseTestResult& onMouseResult, MouseTestResult& onHoverResult, RefPtr<FrameNode>& hoverNode); 215 216 virtual HitTestResult AxisTest( 217 const PointF& globalPoint, const PointF& parentLocalPoint, AxisTestResult& onAxisResult); 218 219 // In the request to re-layout the scene, needs to obtain the changed state of the child node for the creation 220 // of parent's layout wrapper 221 virtual void UpdateLayoutPropertyFlag(); 222 223 virtual void AdjustParentLayoutFlag(PropertyChangeFlag& flag); 224 225 virtual void MarkDirtyNode(PropertyChangeFlag extraFlag = PROPERTY_UPDATE_NORMAL); 226 227 virtual void MarkNeedFrameFlushDirty(PropertyChangeFlag extraFlag = PROPERTY_UPDATE_NORMAL); 228 FlushUpdateAndMarkDirty()229 virtual void FlushUpdateAndMarkDirty() 230 { 231 for (const auto& child : children_) { 232 child->FlushUpdateAndMarkDirty(); 233 } 234 } 235 236 virtual void MarkNeedSyncRenderTree(); 237 238 virtual void RebuildRenderContextTree(); 239 OnWindowShow()240 virtual void OnWindowShow() {} 241 OnWindowHide()242 virtual void OnWindowHide() {} 243 virtual void Build(); 244 OnWindowFocused()245 virtual void OnWindowFocused() {} 246 OnWindowUnfocused()247 virtual void OnWindowUnfocused() {} 248 OnNotifyMemoryLevel(int32_t level)249 virtual void OnNotifyMemoryLevel(int32_t level) {} 250 251 virtual void SetActive(bool active); 252 253 virtual void OnVisibleChange(bool isVisible); 254 IsOnMainTree()255 bool IsOnMainTree() const 256 { 257 return onMainTree_; 258 } 259 ToJsonValue(std::unique_ptr<JsonValue> & json)260 virtual void ToJsonValue(std::unique_ptr<JsonValue>& json) const {} 261 262 ACE_DEFINE_PROPERTY_ITEM_FUNC_WITHOUT_GROUP(InspectorId, std::string); OnInspectorIdUpdate(const std::string &)263 void OnInspectorIdUpdate(const std::string& /*unused*/) {} 264 265 template<typename T> FindChildNodeOfClass()266 RefPtr<T> FindChildNodeOfClass() 267 { 268 const auto& children = GetChildren(); 269 for (auto iter = children.rbegin(); iter != children.rend(); ++iter) { 270 auto& child = *iter; 271 272 auto target = child->FindChildNodeOfClass<T>(); 273 if (target) { 274 return target; 275 } 276 } 277 278 RefPtr<UINode> uiNode = AceType::Claim<UINode>(this); 279 if (AceType::InstanceOf<T>(uiNode)) { 280 return AceType::DynamicCast<T>(uiNode); 281 } 282 return nullptr; 283 } 284 285 void ChildrenUpdatedFrom(int32_t index); GetChildrenUpdated()286 int32_t GetChildrenUpdated() const 287 { 288 return childrenUpdatedFrom_; 289 } 290 // These two interfaces are only used for fast preview. 291 // FastPreviewUpdateChild: Replace the old child at the specified slot with the new created node. 292 // FastPreviewUpdateChildDone: the new created node performs some special operations. FastPreviewUpdateChild(int32_t slot,const RefPtr<UINode> & newChild)293 virtual void FastPreviewUpdateChild(int32_t slot, const RefPtr<UINode>& newChild) 294 { 295 RemoveChildAtIndex(slot); 296 newChild->MountToParent(AceType::Claim(this), slot, false); 297 } FastPreviewUpdateChildDone()298 virtual void FastPreviewUpdateChildDone() {} 299 300 #ifdef PREVIEW SetDebugLine(const std::string & line)301 void SetDebugLine(const std::string& line) 302 { 303 debugLine_ = line; 304 } GetDebugLine()305 std::string GetDebugLine() const 306 { 307 return debugLine_; 308 } 309 SetViewId(const std::string & viewId)310 void SetViewId(const std::string& viewId) 311 { 312 viewId_ = viewId; 313 } 314 GetViewId()315 std::string GetViewId() const 316 { 317 return viewId_; 318 } 319 #endif 320 321 protected: ModifyChildren()322 std::list<RefPtr<UINode>>& ModifyChildren() 323 { 324 return children_; 325 } 326 OnGenerateOneDepthVisibleFrame(std::list<RefPtr<FrameNode>> & visibleList)327 virtual void OnGenerateOneDepthVisibleFrame(std::list<RefPtr<FrameNode>>& visibleList) 328 { 329 for (const auto& child : children_) { 330 child->OnGenerateOneDepthVisibleFrame(visibleList); 331 } 332 } 333 OnGenerateOneDepthAllFrame(std::list<RefPtr<FrameNode>> & allList)334 virtual void OnGenerateOneDepthAllFrame(std::list<RefPtr<FrameNode>>& allList) 335 { 336 for (const auto& child : children_) { 337 child->OnGenerateOneDepthAllFrame(allList); 338 } 339 } 340 OnContextAttached()341 virtual void OnContextAttached() {} 342 // dump self info. DumpInfo()343 virtual void DumpInfo() {} 344 345 // Mount to the main tree to display. 346 virtual void OnAttachToMainTree(); 347 virtual void OnDetachFromMainTree(); 348 349 private: 350 void OnRemoveFromParent(); 351 void DoAddChild(std::list<RefPtr<UINode>>::iterator& it, const RefPtr<UINode>& child, bool silently = false); 352 353 std::list<RefPtr<UINode>> children_; 354 WeakPtr<UINode> parent_; 355 std::string tag_ = "UINode"; 356 int32_t depth_ = 0; 357 int32_t hostRootId_ = 0; 358 int32_t hostPageId_ = 0; 359 int32_t nodeId_ = 0; 360 int32_t accessibilityId_ = -1; 361 bool isRoot_ = false; 362 bool onMainTree_ = false; 363 bool removeSilently_ = true; 364 bool isInDestroying_ = false; 365 366 int32_t childrenUpdatedFrom_ = -1; 367 static thread_local int32_t currentAccessibilityId_; 368 369 #ifdef PREVIEW 370 std::string debugLine_; 371 std::string viewId_; 372 #endif 373 ACE_DISALLOW_COPY_AND_MOVE(UINode); 374 }; 375 376 } // namespace OHOS::Ace::NG 377 378 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_BASE_UI_NODE_H 379