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/components_ng/pattern/navrouter/navdestination_group_node.h"
17
18 #include "base/memory/ace_type.h"
19 #include "base/memory/referenced.h"
20 #include "core/components_ng/pattern/linear_layout/linear_layout_pattern.h"
21 #include "core/components_ng/pattern/navrouter/navdestination_layout_property.h"
22 #include "core/components_ng/pattern/navrouter/navdestination_pattern.h"
23 #include "core/components_ng/pattern/text/text_layout_property.h"
24 #include "core/components_v2/inspector/inspector_constants.h"
25
26 namespace OHOS::Ace::NG {
27
GetOrCreateGroupNode(const std::string & tag,int32_t nodeId,const std::function<RefPtr<Pattern> (void)> & patternCreator)28 RefPtr<NavDestinationGroupNode> NavDestinationGroupNode::GetOrCreateGroupNode(
29 const std::string& tag, int32_t nodeId, const std::function<RefPtr<Pattern>(void)>& patternCreator)
30 {
31 auto frameNode = GetFrameNode(tag, nodeId);
32 CHECK_NULL_RETURN_NOLOG(!frameNode, AceType::DynamicCast<NavDestinationGroupNode>(frameNode));
33 auto pattern = patternCreator ? patternCreator() : MakeRefPtr<Pattern>();
34 auto navDestinationNode = AceType::MakeRefPtr<NavDestinationGroupNode>(tag, nodeId, pattern);
35 navDestinationNode->InitializePatternAndContext();
36 ElementRegister::GetInstance()->AddUINode(navDestinationNode);
37 return navDestinationNode;
38 }
39
AddChildToGroup(const RefPtr<UINode> & child,int32_t slot)40 void NavDestinationGroupNode::AddChildToGroup(const RefPtr<UINode>& child, int32_t slot)
41 {
42 auto pattern = AceType::DynamicCast<Pattern>(GetPattern());
43 CHECK_NULL_VOID(pattern);
44 auto contentNode = GetContentNode();
45 if (!contentNode) {
46 auto nodeId = ElementRegister::GetInstance()->MakeUniqueId();
47 contentNode = FrameNode::GetOrCreateFrameNode(V2::NAVDESTINATION_CONTENT_ETS_TAG, nodeId,
48 []() { return AceType::MakeRefPtr<LinearLayoutPattern>(true); });
49 SetContentNode(contentNode);
50 auto layoutProperty = GetLayoutProperty<NavDestinationLayoutProperty>();
51 CHECK_NULL_VOID(layoutProperty);
52 AddChild(contentNode);
53 }
54 contentNode->AddChild(child, slot);
55 }
56
DeleteChildFromGroup(int32_t slot)57 void NavDestinationGroupNode::DeleteChildFromGroup(int32_t slot)
58 {
59 auto navDestination = GetContentNode();
60 CHECK_NULL_VOID(navDestination);
61 navDestination->RemoveChildAtIndex(slot);
62 }
63
OnAttachToMainTree(bool recursive)64 void NavDestinationGroupNode::OnAttachToMainTree(bool recursive)
65 {
66 if (!UseOffscreenProcess()) {
67 ProcessShallowBuilder();
68 }
69 FrameNode::OnAttachToMainTree(recursive);
70 }
71
OnOffscreenProcess(bool recursive)72 void NavDestinationGroupNode::OnOffscreenProcess(bool recursive)
73 {
74 ProcessShallowBuilder();
75 FrameNode::OnOffscreenProcess(recursive);
76 }
77
ProcessShallowBuilder()78 void NavDestinationGroupNode::ProcessShallowBuilder()
79 {
80 auto navDestinationPattern = GetPattern<NavDestinationPattern>();
81 CHECK_NULL_VOID(navDestinationPattern);
82 auto shallowBuilder = navDestinationPattern->GetShallowBuilder();
83 if (shallowBuilder && !shallowBuilder->IsExecuteDeepRenderDone()) {
84 shallowBuilder->ExecuteDeepRender();
85 GetLayoutProperty()->UpdatePropertyChangeFlag(PROPERTY_UPDATE_MEASURE);
86 AceType::DynamicCast<FrameNode>(contentNode_)
87 ->GetLayoutProperty()
88 ->UpdatePropertyChangeFlag(PROPERTY_UPDATE_MEASURE);
89 }
90 }
91
UpdateTitleFontSize(bool showBackButton)92 void NavDestinationGroupNode::UpdateTitleFontSize(bool showBackButton)
93 {
94 // custom title
95 if (GetPrevTitleIsCustomValue(false)) {
96 return;
97 }
98 auto titleNode = AceType::DynamicCast<FrameNode>(title_);
99 CHECK_NULL_VOID(titleNode);
100 auto titleLayoutProperty = titleNode->GetLayoutProperty<TextLayoutProperty>();
101 CHECK_NULL_VOID(titleLayoutProperty);
102 auto theme = NavigationGetTheme();
103 CHECK_NULL_VOID(theme);
104 if (showBackButton) {
105 titleLayoutProperty->UpdateFontSize(theme->GetTitleFontSizeMin());
106 titleLayoutProperty->UpdateAdaptMaxFontSize(theme->GetTitleFontSizeMin());
107 } else {
108 titleLayoutProperty->UpdateFontSize(theme->GetTitleFontSize());
109 titleLayoutProperty->UpdateAdaptMaxFontSize(theme->GetTitleFontSize());
110 }
111 titleNode->MarkModifyDone();
112 titleNode->MarkDirtyNode(PROPERTY_UPDATE_MEASURE_SELF);
113 }
114 } // namespace OHOS::Ace::NG
115