1 /*
2 * Copyright (c) 2023 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 #include "core/interfaces/native/node/nav_destination_modifier.h"
16
17 #include "core/components/common/layout/constants.h"
18 #include "core/components_ng/base/frame_node.h"
19 #include "core/components_ng/pattern/navigation/navigation_declaration.h"
20 #include "core/components_ng/pattern/navrouter/navdestination_model_ng.h"
21 #include "core/pipeline/base/element_register.h"
22
23 namespace OHOS::Ace::NG {
24 constexpr int32_t DEFAULT_SAFE_AREA_TYPE = 0b1;
25 constexpr int32_t DEFAULT_SAFE_AREA_EDGE = 0b1111;
SetHideTitleBar(ArkUINodeHandle node,ArkUI_Bool hideTitle,ArkUI_Bool animated)26 void SetHideTitleBar(ArkUINodeHandle node, ArkUI_Bool hideTitle, ArkUI_Bool animated)
27 {
28 auto* frameNode = reinterpret_cast<FrameNode*>(node);
29 CHECK_NULL_VOID(frameNode);
30 NavDestinationModelNG::SetHideTitleBar(frameNode, hideTitle, animated);
31 }
32
ResetHideTitleBar(ArkUINodeHandle node)33 void ResetHideTitleBar(ArkUINodeHandle node)
34 {
35 auto* frameNode = reinterpret_cast<FrameNode*>(node);
36 CHECK_NULL_VOID(frameNode);
37 NavDestinationModelNG::SetHideTitleBar(frameNode, false, false);
38 }
39
SetNavDestinationHideToolBar(ArkUINodeHandle node,ArkUI_Bool hide,ArkUI_Bool animated)40 void SetNavDestinationHideToolBar(ArkUINodeHandle node, ArkUI_Bool hide, ArkUI_Bool animated)
41 {
42 auto* frameNode = reinterpret_cast<FrameNode*>(node);
43 CHECK_NULL_VOID(frameNode);
44 NavDestinationModelNG::SetHideToolBar(frameNode, hide, animated);
45 }
46
ResetNavDestinationHideToolBar(ArkUINodeHandle node)47 void ResetNavDestinationHideToolBar(ArkUINodeHandle node)
48 {
49 auto* frameNode = reinterpret_cast<FrameNode*>(node);
50 CHECK_NULL_VOID(frameNode);
51 NavDestinationModelNG::SetHideToolBar(frameNode, false, false);
52 }
53
SetNavDestinationMode(ArkUINodeHandle node,int32_t value)54 void SetNavDestinationMode(ArkUINodeHandle node, int32_t value)
55 {
56 auto* frameNode = reinterpret_cast<FrameNode*>(node);
57 CHECK_NULL_VOID(frameNode);
58 NavDestinationModelNG::SetNavDestinationMode(frameNode, static_cast<NG::NavDestinationMode>(value));
59 }
60
ResetNavDestinationMode(ArkUINodeHandle node)61 void ResetNavDestinationMode(ArkUINodeHandle node)
62 {
63 auto* frameNode = reinterpret_cast<FrameNode*>(node);
64 CHECK_NULL_VOID(frameNode);
65 NavDestinationModelNG::SetNavDestinationMode(frameNode, NG::NavDestinationMode::STANDARD);
66 }
67
SetIgnoreLayoutSafeArea(ArkUINodeHandle node,const char * typeStr,const char * edgesStr)68 void SetIgnoreLayoutSafeArea(ArkUINodeHandle node, const char* typeStr, const char* edgesStr)
69 {
70 auto* frameNode = reinterpret_cast<FrameNode*>(node);
71 CHECK_NULL_VOID(frameNode);
72 NG::SafeAreaExpandOpts opts { .type = NG::SAFE_AREA_TYPE_SYSTEM, .edges = NG::SAFE_AREA_EDGE_ALL };
73 uint32_t safeAreaType = NG::SAFE_AREA_TYPE_NONE;
74 uint32_t safeAreaEdge = NG::SAFE_AREA_EDGE_NONE;
75 std::string safeAreaTypeStr = std::string(typeStr);
76 std::string safeAreaEdgeStr = std::string(edgesStr);
77 if (safeAreaTypeStr == "" || safeAreaEdgeStr == "") {
78 NG::SafeAreaExpandOpts opts { .type = NG::SAFE_AREA_TYPE_NONE, .edges = NG::SAFE_AREA_EDGE_NONE};
79 NavDestinationModelNG::SetIgnoreLayoutSafeArea(frameNode, opts);
80 return;
81 }
82 std::string delimiter = "|";
83 size_t pos = 0;
84 std::string type;
85 std::string edges;
86 while ((pos = safeAreaTypeStr.find(delimiter)) != std::string::npos) {
87 type = safeAreaTypeStr.substr(0, pos);
88 safeAreaType |= (1 << StringUtils::StringToUint(type));
89 safeAreaTypeStr.erase(0, pos + delimiter.length());
90 }
91 safeAreaType |= (1 << StringUtils::StringToUint(safeAreaTypeStr));
92 pos = 0;
93 while ((pos = safeAreaEdgeStr.find(delimiter)) != std::string::npos) {
94 edges = safeAreaEdgeStr.substr(0, pos);
95 safeAreaEdge |= (1 << StringUtils::StringToUint(edges));
96 safeAreaEdgeStr.erase(0, pos + delimiter.length());
97 }
98 safeAreaEdge |= (1 << StringUtils::StringToUint(safeAreaEdgeStr));
99 opts.type = safeAreaType;
100 opts.edges = safeAreaEdge;
101 NavDestinationModelNG::SetIgnoreLayoutSafeArea(frameNode, opts);
102 }
103
ResetIgnoreLayoutSafeArea(ArkUINodeHandle node)104 void ResetIgnoreLayoutSafeArea(ArkUINodeHandle node)
105 {
106 auto* frameNode = reinterpret_cast<FrameNode*>(node);
107 CHECK_NULL_VOID(frameNode);
108 NG::SafeAreaExpandOpts opts;
109 opts.type = DEFAULT_SAFE_AREA_TYPE;
110 opts.edges = DEFAULT_SAFE_AREA_EDGE;
111 NavDestinationModelNG::SetIgnoreLayoutSafeArea(frameNode, opts);
112 }
113
SetTitle(ArkUINodeHandle node,ArkUINavigationTitleInfo titleInfo,ArkUINavigationTitlebarOptions options)114 void SetTitle(ArkUINodeHandle node, ArkUINavigationTitleInfo titleInfo, ArkUINavigationTitlebarOptions options)
115 {
116 auto* frameNode = reinterpret_cast<FrameNode*>(node);
117 CHECK_NULL_VOID(frameNode);
118 NG::NavigationTitlebarOptions finalOptions;
119 if (options.enableHoverMode.isSet) {
120 finalOptions.enableHoverMode = options.enableHoverMode.value;
121 }
122 NavDestinationModelNG::SetTitlebarOptions(frameNode, std::move(finalOptions));
123 }
124
ResetTitle(ArkUINodeHandle node)125 void ResetTitle(ArkUINodeHandle node)
126 {
127 auto* frameNode = reinterpret_cast<FrameNode*>(node);
128 CHECK_NULL_VOID(frameNode);
129 NG::NavigationTitleInfo ngTitleInfo = { false, false, "", "" };
130 NavDestinationModelNG::ParseCommonTitle(frameNode, ngTitleInfo);
131 NavigationTitlebarOptions options;
132 NavDestinationModelNG::SetTitlebarOptions(frameNode, std::move(options));
133 }
134
135 namespace NodeModifier {
GetNavDestinationModifier()136 const ArkUINavDestinationModifier* GetNavDestinationModifier()
137 {
138 static const ArkUINavDestinationModifier modifier = {
139 SetHideTitleBar,
140 ResetHideTitleBar,
141 SetNavDestinationHideToolBar,
142 ResetNavDestinationHideToolBar,
143 SetNavDestinationMode,
144 ResetNavDestinationMode,
145 SetIgnoreLayoutSafeArea,
146 ResetIgnoreLayoutSafeArea,
147 SetTitle,
148 ResetTitle
149 };
150
151 return &modifier;
152 }
153
GetCJUINavDestinationModifier()154 const CJUINavDestinationModifier* GetCJUINavDestinationModifier()
155 {
156 static const CJUINavDestinationModifier modifier = {
157 SetHideTitleBar,
158 ResetHideTitleBar,
159 SetNavDestinationHideToolBar,
160 ResetNavDestinationHideToolBar,
161 SetNavDestinationMode,
162 ResetNavDestinationMode,
163 SetIgnoreLayoutSafeArea,
164 ResetIgnoreLayoutSafeArea
165 };
166
167 return &modifier;
168 }
169 }
170 } // namespace OHOS::Ace::NG
171