• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 
16 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_NAVIGATION_NAVIGATION_DECLARATION_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_NAVIGATION_NAVIGATION_DECLARATION_H
18 
19 #include <string>
20 
21 #include "base/geometry/dimension.h"
22 #include "core/components/common/properties/color.h"
23 #include "core/components/declaration/common/declaration.h"
24 #include "core/components/navigation_bar/navigation_bar_theme.h"
25 #include "core/pipeline_ng/pipeline_context.h"
26 
27 namespace OHOS::Ace::NG {
28 
NavigationGetTheme()29 inline RefPtr<NavigationBarTheme> NavigationGetTheme()
30 {
31     auto pipeline = PipelineBase::GetCurrentContext();
32     CHECK_NULL_RETURN(pipeline, nullptr);
33     return pipeline->GetTheme<NavigationBarTheme>();
34 }
35 
36 
NavigationGetTheme(int32_t themeScopeId)37 inline RefPtr<NavigationBarTheme> NavigationGetTheme(int32_t themeScopeId)
38 {
39     auto pipeline = PipelineBase::GetCurrentContext();
40     CHECK_NULL_RETURN(pipeline, nullptr);
41     return pipeline->GetTheme<NavigationBarTheme>(themeScopeId);
42 }
43 
44 // TODO:move some items to theme
45 // title bar back button
46 constexpr const char* BACK_BUTTON = "Back";
47 constexpr InternalResource::ResourceId BACK_ICON = InternalResource::ResourceId::IC_BACK;
48 constexpr InternalResource::ResourceId MORE_ICON = InternalResource::ResourceId::IC_MORE;
49 // title bar and tool bar, bar item
50 constexpr float BAR_ITEM_WIDTH = 40.0f;
51 constexpr float BAR_ITEM_HEIGHT = 60.0f;
52 constexpr float BAR_TEXT_FONT_SIZE = 18.0f;
53 // title bar title and subtitle
54 constexpr float TITLE_WIDTH = 100.0f;
55 // single page maximum width
56 constexpr float SINGLE_PAGE_MAXIMUM_WIDTH = 720.0f;
57 
58 // title
59 constexpr Dimension MAX_TITLE_FONT_SIZE = 30.0_vp;
60 constexpr Dimension MIN_TITLE_FONT_SIZE = 20.0_vp;
61 constexpr Dimension MIN_ADAPT_TITLE_FONT_SIZE = 14.0_vp;
62 constexpr const char* TITLE_MAIN = "MainOnly";
63 constexpr const char* TITLE_MAIN_WITH_SUB = "MainWithSub";
64 
65 // subtitle
66 constexpr Dimension SUBTITLE_FONT_SIZE = 14.0_vp; // ohos_id_text_size_sub_title3
67 constexpr Dimension SUBTITLE_HEIGHT = 26.0_vp;
68 constexpr Dimension MIN_ADAPT_SUBTITLE_FONT_SIZE = 10.0_vp;
69 // back button
70 constexpr Dimension BACK_BUTTON_SIZE = 48.0_vp;
71 constexpr Dimension BACK_BUTTON_ICON_SIZE = 24.0_vp;
72 // title bar
73 constexpr Dimension TITLEBAR_HEIGHT_MINI = 56.0_vp;
74 constexpr Dimension TITLEBAR_HEIGHT_WITH_SUBTITLE = 137.0_vp;
75 constexpr Dimension TITLEBAR_HEIGHT_WITHOUT_SUBTITLE = 112.0_vp;
76 constexpr uint32_t TITLEBAR_MAX_LINES = 2;
77 // toolbar item
78 constexpr Dimension TEXT_FONT_SIZE = 10.0_vp;
79 constexpr Color TEXT_COLOR = Color(0xE6000000);
80 constexpr Color ICON_COLOR = Color(0xE6000000);
81 // toolbar
82 constexpr Dimension ICON_PADDING = 10.0_vp;
83 constexpr Dimension TEXT_TOP_PADDING = 2.0_vp;
84 
85 // divider
86 constexpr Dimension DIVIDER_WIDTH = 1.0_px;
87 constexpr Dimension DEFAULT_DIVIDER_START_MARGIN = 0.0_vp;
88 constexpr Dimension DEFAULT_DIVIDER_HOT_ZONE_HORIZONTAL_PADDING = 2.0_vp;
89 constexpr int32_t DIVIDER_HOT_ZONE_HORIZONTAL_PADDING_NUM = 2;
90 
91 // navigation content
92 constexpr Dimension SINGLE_LINE_TITLEBAR_HEIGHT = 56.0_vp;
93 constexpr Dimension DOUBLE_LINE_TITLEBAR_HEIGHT = 82.0_vp;
94 constexpr Dimension DEFAULT_MIN_CONTENT_WIDTH = 360.0_vp;
95 
96 // navBar
97 constexpr Dimension FULL_SINGLE_LINE_TITLEBAR_HEIGHT = 112.0_vp;
98 constexpr Dimension FULL_DOUBLE_LINE_TITLEBAR_HEIGHT = 138.0_vp;
99 constexpr Dimension NAV_HORIZONTAL_MARGIN_L = 16.0_vp; // ohos_id_elements_margin_horizontal_l
100 constexpr Dimension NAV_HORIZONTAL_MARGIN_M = 8.0_vp;  // ohos_id_elements_margin_horizontal_m
101 constexpr Dimension MENU_ITEM_PADDING = 24.0_vp;
102 constexpr Dimension MENU_ITEM_SIZE = 48.0_vp;
103 constexpr Dimension BUTTON_PADDING = 12.0_vp;
104 constexpr Dimension MENU_BUTTON_PADDING = 8.0_vp;
105 constexpr Dimension BACK_BUTTON_SYMBOL_PADDING = 14.0_vp;
106 constexpr Dimension BUTTON_RADIUS_SIZE = 5.0_vp;
107 constexpr Dimension MAX_OVER_DRAG_OFFSET = 180.0_vp;
108 constexpr Dimension DEFAULT_MIN_NAV_BAR_WIDTH = 240.0_vp;
109 constexpr Dimension DEFAULT_MAX_NAV_BAR_WIDTH = 432.0_vp;
110 constexpr Dimension MAX_MENU_CHANGE_SIZE = 600.0_vp;
111 constexpr int8_t MAX_MENU_NUM_SMALL = 3;
112 const int8_t MAX_MENU_NUM_LARGE = 5;
113 constexpr float MAX_NAV_BAR_WIDTH_SCALE = 0.4f;
114 
115 // more button
116 constexpr Dimension MORE_BUTTON_CORNER_RADIUS = 8.0_vp;
117 
118 // maximum number of toolbar items
119 constexpr uint32_t MAXIMUM_TOOLBAR_ITEMS_IN_BAR = 5;
120 constexpr uint32_t ONE_TOOLBAR_ITEM = 1;
121 
122 // navigation page info
123 constexpr char NAVIGATION_MODULE_NAME[] = "moduleName";
124 constexpr char NAVIGATION_PAGE_PATH[] = "pagePath";
125 constexpr char IS_USER_CREATE_STACK[] = "isUserCreateStack";
126 
127 // navigation/navdestination Field
128 constexpr const char* NAV_FIELD = "__NavigationField__";
129 constexpr const char* DES_FIELD = "__NavdestinationField__";
130 
131 // font scale
132 constexpr float STANDARD_FONT_SCALE = 1.0f;
133 
134 constexpr int32_t ROTATION_0 = 0;
135 constexpr int32_t ROTATION_90 = 90;
136 constexpr int32_t ROTATION_180 = 180;
137 constexpr int32_t ROTATION_270 = 270;
138 
139 constexpr uint32_t BAR_ITEM_MARGIN_NUM = 2;
140 
141 enum class NavToolbarItemStatus {
142     NORMAL = 0,
143     DISABLED,
144     ACTIVE,
145 };
146 
147 // appbar menu item and toolbar item configuration
148 struct BarItem {
149     std::optional<std::string> text;
150     std::optional<std::string> icon;
151     std::optional<std::function<void(WeakPtr<NG::FrameNode>)>> iconSymbol;
152     std::optional<bool> isEnabled;
153     std::function<void()> action;
154     NavToolbarItemStatus status;
155     std::optional<std::string> activeIcon;
156     std::optional<std::function<void(WeakPtr<NG::FrameNode>)>> activeIconSymbol;
157     struct resourceUpdater {
158         RefPtr<ResourceObject> resObj;
159         std::function<void(const RefPtr<ResourceObject>&, BarItem&)> updateFunc;
160     };
161     std::unordered_map<std::string, resourceUpdater> resMap_;
ToStringBarItem162     std::string ToString() const
163     {
164         std::string result;
165         result.append("text: ");
166         result.append(text.value_or("na"));
167         result.append(", icon: ");
168         result.append(icon.value_or("na"));
169         return result;
170     }
171 
AddResourceBarItem172     void AddResource(const std::string& key, const RefPtr<ResourceObject>& resObj,
173         std::function<void(const RefPtr<ResourceObject>&, BarItem&)>&& updateFunc)
174     {
175         if (resObj == nullptr || !updateFunc) {
176             return;
177         }
178         resMap_[key] = { resObj, std::move(updateFunc) };
179     }
180 
ReloadResourcesBarItem181     void ReloadResources()
182     {
183         for (const auto& [key, resourceUpdater] : resMap_) {
184             resourceUpdater.updateFunc(resourceUpdater.resObj, *this);
185         }
186     }
187 };
188 
189 struct NavigationTitleInfo {
190     bool hasSubTitle;
191     bool hasMainTitle;
192     std::string subtitle;
193     std::string title;
194 };
195 
196 enum class ToolbarIconStatus {
197     INITIAL = 0,
198     ACTIVE,
199 };
200 
201 enum class TitleBarChildType {
202     TITLE = 0,
203     SUBTITLE,
204     MENU,
205 };
206 
207 enum class NavigationTitleMode {
208     FREE = 0,
209     FULL,
210     MINI,
211 };
212 
213 enum class NavigationMode {
214     STACK = 0,
215     SPLIT,
216     AUTO,
217 };
218 
219 enum class NavBarPosition {
220     START = 0,
221     END,
222 };
223 
224 enum class NavDestinationMode {
225     STANDARD = 0,
226     DIALOG,
227 };
228 
229 enum class ChildNodeOperation {
230     ADD,
231     // remove case only used for back button
232     REMOVE,
233     REPLACE,
234     NONE
235 };
236 
237 enum class BarStyle {
238     STANDARD = 0,
239     STACK,
240     SAFE_AREA_PADDING,
241 };
242 
243 enum class TitleBarParentType { NAVBAR, NAV_DESTINATION };
244 
245 enum class NavRouteMode {
246     PUSH_WITH_RECREATE = 0,
247     PUSH,
248     REPLACE,
249 };
250 
251 enum class NavigationOperation {
252     PUSH = 1,
253     POP,
254     REPLACE,
255 };
256 
257 enum NavDestinationLifecycle {
258     ON_WILL_APPEAR = 0,
259     ON_APPEAR,
260     ON_WILL_SHOW,
261     ON_SHOW,
262     ON_ACTIVE,
263     ON_WILL_HIDE,
264     ON_INACTIVE,
265     ON_HIDE,
266     ON_WILL_DISAPPEAR,
267     ON_DISAPPEAR
268 };
269 
270 enum class NavDestinationActiveReason {
271     TRANSITION = 0,
272     CONTENT_COVER,
273     SHEET,
274     DIALOG,
275     OVERLAY,
276     APP_STATE_CHANGE
277 };
278 
279 enum class NavigationSystemTransitionType {
280     NONE = 0,
281     TITLE = 1,
282     CONTENT = 1 << 1,
283     DEFAULT = 1 | (1 << 1),
284     FADE = 1 << 2,
285     EXPLODE = 1 << 3,
286     SLIDE_RIGHT = 1 << 4,
287     SLIDE_BOTTOM = 1 << 5,
288 };
289 
290 enum class NavDestinationType {
291     DETAIL = 0,
292     HOME = 1,
293     PROXY = 2
294 };
295 
296 inline NavigationSystemTransitionType operator& (NavigationSystemTransitionType lv, NavigationSystemTransitionType rv)
297 {
298     return static_cast<NavigationSystemTransitionType>(static_cast<uint32_t>(lv) & static_cast<uint32_t>(rv));
299 }
300 struct NavSafeArea {
301     float top = 0.0f;
302     float bottom = 0.0f;
303 };
304 
305 struct NavDestinationTransition {
306     int32_t delay;
307     int32_t duration;
308     RefPtr<Curve> curve;
309     std::function<void()> event;
310     std::function<void()> onTransitionEnd;
311 };
312 
313 enum class LaunchMode {
314     STANDARD = 0,
315     MOVE_TO_TOP_SINGLETON,
316     POP_TO_TOP_SINGLETON,
317     NEW_INSTANCE,
318 };
319 
320 struct NavigationOptions {
321     LaunchMode launchMode = LaunchMode::STANDARD;
322     bool animated = true;
323 };
324 
325 using NavDestinationTransitionDelegate = std::function<std::optional<std::vector<NavDestinationTransition>>(
326     NavigationOperation operation, bool isEnter)>;
327 using NavDestinationOnNewParamCallback = std::function<void(napi_value param)>;
328 
329 } // namespace OHOS::Ace::NG
330 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_NAVIGATION_NAVIGATION_DECLARATION_H
331