• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2025 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
16import { logger } from './Logger';
17import { emitter } from '@kit.BasicServicesKit';
18import { display, window } from '@kit.ArkUI';
19import { DEFAULT_WINDOW_SIZE } from '../constants/Constants';
20
21// 路由跳转延时
22const DELAY_TIME: number = 20;
23
24export class RouterUtils {
25  public static pageStack: NavPathStack = new NavPathStack();
26  // 全屏子路由
27  private static fullScreenRouter: string[] = [];
28  private static timer: number = 0;
29
30  public static setFullScreenRouter(fullScreenRouter: string[]) {
31    RouterUtils.fullScreenRouter = fullScreenRouter;
32  }
33
34  // 通过获取页面栈并pop
35  public static popAppRouter(): void {
36
37    if (RouterUtils.pageStack.getAllPathName().length > 1) {
38      RouterUtils.pageStack.pop();
39    } else {
40      logger.info('RouterStack is only Home.');
41    }
42    // 定义emitter事件
43    let innerEvent: emitter.InnerEvent = {
44      eventId: 3
45    };
46    let eventData: emitter.EventData = {
47      data: {
48        navMode: NavMode.HomePageMode
49      }
50    };
51    let allPathName: string[] = RouterUtils.pageStack.getAllPathName();
52    // 查找到对应的路由栈进行pop
53    if (!RouterUtils.fullScreenRouter.includes(allPathName[allPathName.length-1]) &&
54      RouterUtils.pageStack.size() === 1) {
55      // 非全屏子路由宽屏条件下回到首页,Navigation的mode属性修改默认动画会与过场动画冲突,需关闭过场动画
56      if (display.getDefaultDisplaySync().width > DEFAULT_WINDOW_SIZE.width) {
57        RouterUtils.pageStack.disableAnimation(true);
58      }
59      RouterUtils.timer = setTimeout(() => {
60        // 触发EntryView下navMode改变
61        emitter.emit(innerEvent, eventData);
62      }, DELAY_TIME);
63      RouterUtils.pageStack.pop();
64    } else if (RouterUtils.fullScreenRouter.includes(allPathName[allPathName.length-1])) {
65      // 全屏子路由返回逻辑
66      RouterUtils.pageStack.pop();
67      // 触发EntryView下navMode改变
68      emitter.emit(innerEvent, eventData);
69    } else {
70      RouterUtils.pageStack.pop();
71    }
72  }
73
74  /**
75   * 兼容折叠屏下的路由跳转
76   * @param uri 路由名称
77   * @param param 路由参数
78   */
79  public static pushUri(uri: string, param?: ESObject) {
80    // 记录当前进入路由名称
81    AppStorage.setOrCreate('enterRouteName', uri);
82    // 定义emitter事件
83    let innerEvent: emitter.InnerEvent = {
84      eventId: 3
85    };
86    let eventData: emitter.EventData = {
87      data: {
88        navMode: NavMode.ChildPageMode
89      }
90    };
91    // 触发EntryView下navMode改变
92    emitter.emit(innerEvent, eventData);
93    // 获取当前窗口宽度
94    let displayInfo: display.Display = display.getDefaultDisplaySync();
95    let windowSize: window.Size | undefined =
96      AppStorage.get<window.Size>('windowSize') !== undefined ? AppStorage.get<window.Size>('windowSize') : {
97        width: displayInfo.width,
98        height: displayInfo.height
99      } as window.Size;
100    // 宽屏条件下跳转
101    if (windowSize!.width > DEFAULT_WINDOW_SIZE.width) {
102      RouterUtils.pageStack.clear();
103      if (RouterUtils.timer) {
104        clearTimeout(RouterUtils.timer);
105      }
106      // Navigation的mode属性修改会有一段响应时间,需延时跳转
107      RouterUtils.timer = setTimeout(() => {
108        RouterUtils.pageStack.pushPathByName(uri, param);
109      }, DELAY_TIME);
110    } else {
111      RouterUtils.pageStack.pushPathByName(uri, param);
112    }
113  }
114}
115
116
117// 设置导航栏显示改变模式枚举值
118export enum NavMode {
119  DefaultMode, // 默认模式
120  FoldMode, // 折叠模式
121  ChildPageMode, // 进入子页面模式
122  HomePageMode // 返回首页模式
123}