• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/navigation/nav_bar_node.h"
17 
18 #include "core/components_ng/pattern/navigation/nav_bar_layout_property.h"
19 #include "core/components_ng/pattern/navigation/navigation_pattern.h"
20 #include "core/components_ng/pattern/navigation/navigation_title_util.h"
21 
22 namespace OHOS::Ace::NG {
23 constexpr float CONTENT_OFFSET_PERCENT  = 0.2f;
24 constexpr float TITLE_OFFSET_PERCENT  = 0.02f;
25 
GetOrCreateNavBarNode(const std::string & tag,int32_t nodeId,const std::function<RefPtr<Pattern> (void)> & patternCreator)26 RefPtr<NavBarNode> NavBarNode::GetOrCreateNavBarNode(
27     const std::string& tag, int32_t nodeId, const std::function<RefPtr<Pattern>(void)>& patternCreator)
28 {
29     auto frameNode = GetFrameNode(tag, nodeId);
30     CHECK_NULL_RETURN(!frameNode, AceType::DynamicCast<NavBarNode>(frameNode));
31     auto pattern = patternCreator ? patternCreator() : MakeRefPtr<Pattern>();
32     auto navBarNode = AceType::MakeRefPtr<NavBarNode>(tag, nodeId, pattern);
33     navBarNode->InitializePatternAndContext();
34     ElementRegister::GetInstance()->AddUINode(navBarNode);
35     return navBarNode;
36 }
37 
AddChildToGroup(const RefPtr<UINode> & child,int32_t slot)38 void NavBarNode::AddChildToGroup(const RefPtr<UINode>& child, int32_t slot)
39 {
40     auto pattern = AceType::DynamicCast<NavigationPattern>(GetPattern());
41     CHECK_NULL_VOID(pattern);
42     auto contentNode = GetContentNode();
43     if (!contentNode) {
44         auto nodeId = ElementRegister::GetInstance()->MakeUniqueId();
45         contentNode = FrameNode::GetOrCreateFrameNode(
46             V2::NAVBAR_CONTENT_ETS_TAG, nodeId, []() { return AceType::MakeRefPtr<LinearLayoutPattern>(true); });
47         SetContentNode(contentNode);
48         AddChild(contentNode);
49 
50         if (Container::GreatOrEqualAPIVersion(PlatformVersion::VERSION_ELEVEN)) {
51             auto navBarContentNode = AceType::DynamicCast<FrameNode>(contentNode);
52             SafeAreaExpandOpts opts = { .type = SAFE_AREA_TYPE_SYSTEM | SAFE_AREA_TYPE_CUTOUT,
53                 .edges = SAFE_AREA_EDGE_ALL };
54             navBarContentNode->GetLayoutProperty()->UpdateSafeAreaExpandOpts(opts);
55         }
56     }
57     contentNode->AddChild(child);
58 }
59 
InitSystemTransitionPop()60 void NavBarNode::InitSystemTransitionPop()
61 {
62     // navabr do enter pop initialization
63     float isRTL = GetLanguageDirection();
64     SetTransitionType(PageTransitionType::ENTER_POP);
65     auto curFrameSize = GetGeometryNode()->GetFrameSize();
66     GetRenderContext()->RemoveClipWithRRect();
67     GetRenderContext()->UpdateTranslateInXY({ -curFrameSize.Width() * CONTENT_OFFSET_PERCENT * isRTL, 0.0f });
68     auto curTitleBarNode = AceType::DynamicCast<FrameNode>(GetTitleBarNode());
69     CHECK_NULL_VOID(curTitleBarNode);
70     curTitleBarNode->GetRenderContext()->UpdateTranslateInXY(
71         { curFrameSize.Width() * TITLE_OFFSET_PERCENT * isRTL, 0.0f });
72 }
73 
SystemTransitionPushAction(bool isStart)74 void NavBarNode::SystemTransitionPushAction(bool isStart)
75 {
76     // initialization or finish callBack
77     if (isStart) {
78         SetTransitionType(PageTransitionType::EXIT_PUSH);
79     } else {
80         GetRenderContext()->SetActualForegroundColor(Color::TRANSPARENT);
81     }
82     GetRenderContext()->UpdateTranslateInXY({ 0.0f, 0.0f });
83     auto titleNode = AceType::DynamicCast<FrameNode>(GetTitleBarNode());
84     CHECK_NULL_VOID(titleNode);
85     titleNode->GetRenderContext()->UpdateTranslateInXY({ 0.0f, 0.0f });
86 }
87 
StartSystemTransitionPush()88 void NavBarNode::StartSystemTransitionPush()
89 {
90     // start EXIT_PUSH transition animation
91     float isRTL = GetLanguageDirection();
92     auto frameSize = GetGeometryNode()->GetFrameSize();
93     GetRenderContext()->UpdateTranslateInXY(
94         { -frameSize.Width() * CONTENT_OFFSET_PERCENT * isRTL, 0.0f });
95     auto titleNode = AceType::DynamicCast<FrameNode>(GetTitleBarNode());
96     CHECK_NULL_VOID(titleNode);
97     titleNode->GetRenderContext()->UpdateTranslateInXY(
98         { frameSize.Width() * TITLE_OFFSET_PERCENT * isRTL, 0.0f });
99 }
100 
StartSystemTransitionPop()101 void NavBarNode::StartSystemTransitionPop()
102 {
103     // navabr start to do ENTER_POP animation
104     GetRenderContext()->UpdateTranslateInXY({ 0.0f, 0.0f });
105     auto titleBarNode = AceType::DynamicCast<FrameNode>(GetTitleBarNode());
106     CHECK_NULL_VOID(titleBarNode);
107     titleBarNode->GetRenderContext()->UpdateTranslateInXY({ 0.0f, 0.0f });
108 }
109 
IsNodeInvisible(const RefPtr<FrameNode> & node)110 bool NavBarNode::IsNodeInvisible(const RefPtr<FrameNode>& node)
111 {
112     auto navigation = DynamicCast<NavigationGroupNode>(node);
113     CHECK_NULL_RETURN(navigation, false);
114     auto lastStandardIndex = navigation->GetLastStandardIndex();
115     bool isInvisible = navigation->GetNavigationMode() == NavigationMode::STACK && lastStandardIndex >= 0;
116     return isInvisible;
117 }
118 
GetNavigationNode()119 RefPtr<UINode> NavBarNode::GetNavigationNode()
120 {
121     return GetParentFrameNode();
122 }
123 } // namespace OHOS::Ace::NG
124