• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
16/// <reference path='./import.ts' />
17
18const NAVDES_SAFE_AREA_TYPE_LIMIT = 3;
19const NAVDES_SAFE_AREA_EDGE_LIMIT = 4;
20const NAVDES_SAFE_AREA_LOWER_LIMIT = 0;
21class ArkNavDestinationComponent extends ArkComponent implements NavDestinationAttribute {
22  constructor(nativePtr: KNode, classType?: ModifierType) {
23    super(nativePtr, classType);
24  }
25  title(value: any, options?: NavigationTitleOptions): this {
26    let arkNavigationTitle = new ArkNavigationTitle();
27    if (!isUndefined(options) && !isNull(options) && isObject(options)) {
28      if (Object.keys(options).length !== 0) {
29        arkNavigationTitle.navigationTitleOptions = options;
30      }
31    }
32    modifierWithKey(this._modifiersWithKeys, NavDestinationTitleModifier.identity,
33      NavDestinationTitleModifier, arkNavigationTitle);
34    return this;
35  }
36  hideTitleBar(isHide: boolean, animated?: boolean): this {
37    let arkNavDestinationHideTitleBar = new ArkNavHideTitleBarOrToolBar();
38    if (!isUndefined(isHide) && !isNull(isHide)) {
39      arkNavDestinationHideTitleBar.isHide = isHide;
40    }
41    if (!isUndefined(animated) && !isNull(animated)) {
42      arkNavDestinationHideTitleBar.animated = animated;
43    }
44    if (arkNavDestinationHideTitleBar.isHide === undefined && arkNavDestinationHideTitleBar.animated === undefined) {
45        modifierWithKey(this._modifiersWithKeys, HideTitleBarModifier.identity, HideTitleBarModifier, undefined);
46    } else {
47        modifierWithKey(this._modifiersWithKeys, HideTitleBarModifier.identity, HideTitleBarModifier, arkNavDestinationHideTitleBar);
48    }
49    return this;
50  }
51  hideToolBar(isHide: boolean, animated?: boolean): this {
52    let arkNavDestinationHideToolBar = new ArkNavHideTitleBarOrToolBar();
53    if (!isUndefined(isHide) && !isNull(isHide)) {
54      arkNavDestinationHideToolBar.isHide = isHide;
55    }
56    if (!isUndefined(animated) && !isNull(animated)) {
57      arkNavDestinationHideToolBar.animated = animated;
58    }
59    if (arkNavDestinationHideToolBar.isHide === undefined && arkNavDestinationHideToolBar.animated === undefined) {
60        modifierWithKey(this._modifiersWithKeys, NavDestinationHideToolBarModifier.identity,
61          NavDestinationHideToolBarModifier, undefined);
62    } else {
63        modifierWithKey(this._modifiersWithKeys, NavDestinationHideToolBarModifier.identity,
64          NavDestinationHideToolBarModifier, arkNavDestinationHideToolBar);
65    }
66    return this;
67  }
68  toolbarConfiguration(value: any): this {
69    throw new Error('Method not implemented.');
70  }
71  backButtonIcon(value: any): this {
72    modifierWithKey(this._modifiersWithKeys, NavDestinationBackButtonIconModifier.identity,
73      NavDestinationBackButtonIconModifier, value);
74    return this;
75  }
76  mode(value: number): this {
77    modifierWithKey(this._modifiersWithKeys, NavDestinationModeModifier.identity,
78      NavDestinationModeModifier, value);
79    return this;
80  }
81  onShown(callback: () => void): this {
82    throw new Error('Method not implemented.');
83  }
84  onHidden(callback: () => void): this {
85    throw new Error('Method not implemented.');
86  }
87  onBackPressed(callback: () => boolean): this {
88    throw new Error('Method not implemented.');
89  }
90  ignoreLayoutSafeArea(types?: Array<SafeAreaType>, edges?: Array<SafeAreaEdge>): this {
91    let opts = new ArkSafeAreaExpandOpts();
92    if (types && types.length >= 0) {
93      let safeAreaType: string | number = '';
94      for (let param of types) {
95        if (!isNumber(param) || param >= NAVDES_SAFE_AREA_TYPE_LIMIT || param < NAVDES_SAFE_AREA_LOWER_LIMIT) {
96          safeAreaType = undefined;
97          break;
98        }
99        if (safeAreaType) {
100          safeAreaType += '|';
101          safeAreaType += param.toString();
102        } else {
103          safeAreaType += param.toString();
104        }
105      }
106      opts.type = safeAreaType;
107    }
108    if (edges && edges.length >= 0) {
109      let safeAreaEdge: string | number = '';
110      for (let param of edges) {
111        if (!isNumber(param) || param >= NAVDES_SAFE_AREA_EDGE_LIMIT || param < NAVDES_SAFE_AREA_LOWER_LIMIT) {
112          safeAreaEdge = undefined;
113          break;
114        }
115        if (safeAreaEdge) {
116          safeAreaEdge += '|';
117          safeAreaEdge += param.toString();
118        } else {
119          safeAreaEdge += param.toString();
120        }
121      }
122      opts.edges = safeAreaEdge;
123    }
124    if (opts.type === undefined && opts.edges === undefined) {
125      modifierWithKey(this._modifiersWithKeys, IgnoreLayoutSafeAreaModifier.identity, IgnoreLayoutSafeAreaModifier, undefined);
126    } else {
127      modifierWithKey(this._modifiersWithKeys, IgnoreLayoutSafeAreaModifier.identity, IgnoreLayoutSafeAreaModifier, opts);
128    }
129    return this;
130  }
131}
132
133class NavDestinationTitleModifier extends ModifierWithKey<ArkNavigationTitle | undefined> {
134  constructor(value: ArkNavigationTitle | undefined) {
135    super(value);
136  }
137  static identity: Symbol = Symbol('title');
138  applyPeer(node: KNode, reset: boolean): void {
139    if (reset) {
140      getUINativeModule().navDestination.resetTitle(node);
141    } else {
142      getUINativeModule().navDestination.setTitle(node, this.value?.navigationTitleOptions);
143    }
144  }
145  checkObjectDiff(): boolean {
146    return !this.value.isEqual(this.stageValue);
147  }
148}
149
150class HideTitleBarModifier extends ModifierWithKey<ArkNavHideTitleBarOrToolBar | undefined> {
151  constructor(value: ArkNavHideTitleBarOrToolBar | undefined) {
152    super(value);
153  }
154  static identity: Symbol = Symbol('hideTitleBar');
155
156  applyPeer(node: KNode, reset: boolean): void {
157    if (reset) {
158      getUINativeModule().navDestination.resetHideTitleBar(node);
159    } else {
160      getUINativeModule().navDestination.setHideTitleBar(node, this.value?.isHide, this.value?.animated);
161    }
162  }
163}
164
165class NavDestinationHideToolBarModifier extends ModifierWithKey<ArkNavHideTitleBarOrToolBar | undefined> {
166  constructor(value: ArkNavHideTitleBarOrToolBar | undefined) {
167    super(value);
168  }
169  static identity: Symbol = Symbol('hideToolBar');
170
171  applyPeer(node: KNode, reset: boolean): void {
172    if (reset) {
173      getUINativeModule().navDestination.resetHideToolBar(node);
174    } else {
175      getUINativeModule().navDestination.setHideToolBar(node, this.value?.isHide, this.value?.animated);
176    }
177  }
178}
179
180class NavDestinationBackButtonIconModifier extends ModifierWithKey<object> {
181  constructor(value: object) {
182    super(value);
183  }
184  static identity: Symbol = Symbol('backButtonIcon');
185  applyPeer(node: KNode, reset: boolean): void {
186    if (reset) {
187      getUINativeModule().navDestination.resetBackButtonIcon(node);
188    } else {
189      getUINativeModule().navDestination.setBackButtonIcon(node, this.value);
190    }
191  }
192}
193
194class NavDestinationModeModifier extends ModifierWithKey<number> {
195  constructor(value: number) {
196    super(value);
197  }
198  static identity: Symbol = Symbol('mode');
199
200  applyPeer(node: KNode, reset: boolean): void {
201    if (reset) {
202      getUINativeModule().navDestination.resetMode(node);
203    } else {
204      getUINativeModule().navDestination.setMode(node, this.value);
205    }
206  }
207}
208
209class IgnoreLayoutSafeAreaModifier extends ModifierWithKey<ArkSafeAreaExpandOpts | undefined> {
210  constructor(value: ArkSafeAreaExpandOpts | undefined) {
211    super(value);
212  }
213  static identity: Symbol = Symbol('ignoreLayoutSafeArea');
214  applyPeer(node: KNode, reset: boolean): void {
215    if (reset) {
216      getUINativeModule().navDestination.resetIgnoreLayoutSafeArea(node);
217    } else {
218      getUINativeModule().navDestination.setIgnoreLayoutSafeArea(node, this.value.type, this.value.edges);
219    }
220  }
221  checkObjectDiff(): boolean {
222    return !isBaseOrResourceEqual(this.stageValue.type, this.value.type) ||
223      !isBaseOrResourceEqual(this.stageValue.edges, this.value.edges);
224  }
225}
226
227//@ts-ignore
228globalThis.NavDestination.attributeModifier = function (modifier: ArkComponent): void {
229  attributeModifierFunc.call(this, modifier, (nativePtr: KNode) => {
230    return new ArkNavDestinationComponent(nativePtr);
231  }, (nativePtr: KNode, classType: ModifierType, modifierJS: ModifierJS) => {
232    return new modifierJS.NavDestinationModifier(nativePtr, classType);
233  });
234};
235