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 #include "core/common/container.h"
17 #include "core/components_ng/pattern/navrouter/navdestination_group_node.h"
18 #include "core/components_ng/pattern/linear_layout/linear_layout_pattern.h"
19 #include "core/components_ng/pattern/navrouter/navdestination_context.h"
20 #include "core/components_ng/pattern/navrouter/navdestination_layout_property.h"
21 #include "core/components_ng/pattern/navrouter/navdestination_pattern.h"
22 #include "core/components_ng/pattern/text/text_layout_property.h"
23
24 namespace OHOS::Ace::NG {
25
~NavDestinationGroupNode()26 NavDestinationGroupNode::~NavDestinationGroupNode()
27 {
28 if (contentNode_) {
29 contentNode_->Clean();
30 }
31 }
32
GetOrCreateGroupNode(const std::string & tag,int32_t nodeId,const std::function<RefPtr<Pattern> (void)> & patternCreator)33 RefPtr<NavDestinationGroupNode> NavDestinationGroupNode::GetOrCreateGroupNode(
34 const std::string& tag, int32_t nodeId, const std::function<RefPtr<Pattern>(void)>& patternCreator)
35 {
36 auto frameNode = GetFrameNode(tag, nodeId);
37 CHECK_NULL_RETURN(!frameNode, AceType::DynamicCast<NavDestinationGroupNode>(frameNode));
38 auto pattern = patternCreator ? patternCreator() : MakeRefPtr<Pattern>();
39 auto navDestinationNode = AceType::MakeRefPtr<NavDestinationGroupNode>(tag, nodeId, pattern);
40 navDestinationNode->InitializePatternAndContext();
41 ElementRegister::GetInstance()->AddUINode(navDestinationNode);
42 return navDestinationNode;
43 }
44
AddChildToGroup(const RefPtr<UINode> & child,int32_t slot)45 void NavDestinationGroupNode::AddChildToGroup(const RefPtr<UINode>& child, int32_t slot)
46 {
47 auto pattern = AceType::DynamicCast<Pattern>(GetPattern());
48 CHECK_NULL_VOID(pattern);
49 auto contentNode = GetContentNode();
50 if (!contentNode) {
51 auto nodeId = ElementRegister::GetInstance()->MakeUniqueId();
52 ACE_LAYOUT_SCOPED_TRACE("Create[%s][self:%d]", V2::NAVDESTINATION_CONTENT_ETS_TAG, nodeId);
53 contentNode = FrameNode::GetOrCreateFrameNode(V2::NAVDESTINATION_CONTENT_ETS_TAG, nodeId,
54 []() { return AceType::MakeRefPtr<LinearLayoutPattern>(true); });
55 SetContentNode(contentNode);
56 AddChild(contentNode);
57
58 if (Container::GreatOrEqualAPIVersion(PlatformVersion::VERSION_ELEVEN)) {
59 auto navdestinationContentNode = AceType::DynamicCast<FrameNode>(contentNode);
60 SafeAreaExpandOpts opts = {.type = SAFE_AREA_TYPE_SYSTEM, .edges = SAFE_AREA_EDGE_ALL};
61 navdestinationContentNode->GetLayoutProperty()->UpdateSafeAreaExpandOpts(opts);
62 }
63 }
64 contentNode->AddChild(child, slot);
65 }
66
DeleteChildFromGroup(int32_t slot)67 void NavDestinationGroupNode::DeleteChildFromGroup(int32_t slot)
68 {
69 auto navDestination = GetContentNode();
70 CHECK_NULL_VOID(navDestination);
71 navDestination->RemoveChildAtIndex(slot);
72 }
73
OnAttachToMainTree(bool recursive)74 void NavDestinationGroupNode::OnAttachToMainTree(bool recursive)
75 {
76 if (!UseOffscreenProcess()) {
77 ProcessShallowBuilder();
78 }
79 FrameNode::OnAttachToMainTree(recursive);
80 }
81
OnOffscreenProcess(bool recursive)82 void NavDestinationGroupNode::OnOffscreenProcess(bool recursive)
83 {
84 ProcessShallowBuilder();
85 FrameNode::OnOffscreenProcess(recursive);
86 }
87
ProcessShallowBuilder()88 void NavDestinationGroupNode::ProcessShallowBuilder()
89 {
90 if (isCacheNode_) {
91 return;
92 }
93
94 TAG_LOGD(AceLogTag::ACE_NAVIGATION, "render navDestination content");
95 auto navDestinationPattern = GetPattern<NavDestinationPattern>();
96 CHECK_NULL_VOID(navDestinationPattern);
97 auto shallowBuilder = navDestinationPattern->GetShallowBuilder();
98 if (shallowBuilder && !shallowBuilder->IsExecuteDeepRenderDone()) {
99 auto eventHub = GetEventHub<NavDestinationEventHub>();
100 if (eventHub) {
101 auto ctx = navDestinationPattern->GetNavDestinationContext();
102 eventHub->FireOnReady(ctx);
103 }
104 shallowBuilder->ExecuteDeepRender();
105 GetLayoutProperty()->UpdatePropertyChangeFlag(PROPERTY_UPDATE_MEASURE);
106 AceType::DynamicCast<FrameNode>(contentNode_)
107 ->GetLayoutProperty()
108 ->UpdatePropertyChangeFlag(PROPERTY_UPDATE_MEASURE);
109 }
110 }
111
GetNavDestinationCustomNode()112 RefPtr<CustomNodeBase> NavDestinationGroupNode::GetNavDestinationCustomNode()
113 {
114 auto pattern = GetPattern<NavDestinationPattern>();
115 CHECK_NULL_RETURN(pattern, nullptr);
116 auto navDestinationNode = pattern->GetCustomNode();
117 CHECK_NULL_RETURN(navDestinationNode, nullptr);
118
119 auto child = navDestinationNode->GetFirstChild();
120 while (child) {
121 if (AceType::InstanceOf<NavDestinationGroupNode>(child)) {
122 break;
123 }
124
125 if (AceType::InstanceOf<CustomNodeBase>(child)) {
126 auto customNode = DynamicCast<CustomNodeBase>(child);
127 return customNode;
128 }
129 child = child->GetFirstChild();
130 }
131 return nullptr;
132 }
133
134 } // namespace OHOS::Ace::NG
135