• 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: ResourceStr | CustomBuilder | NavigationCommonTitle | NavigationCustomTitle | undefined,
26    options?: NavigationTitleOptions): this {
27    if (isUndefined(value) || isNull(value)) {
28      modifierWithKey(this._modifiersWithKeys, NavDestinationTitleModifier.identity,
29        NavDestinationTitleModifier, undefined);
30      return this;
31    }
32    let arkNavigationTitle = new ArkNavigationTitle();
33    arkNavigationTitle.value = value;
34    if (!isUndefined(options) && !isNull(options) && isObject(options)) {
35      if (Object.keys(options).length !== 0) {
36        arkNavigationTitle.navigationTitleOptions = options;
37      }
38    }
39    modifierWithKey(this._modifiersWithKeys, NavDestinationTitleModifier.identity,
40      NavDestinationTitleModifier, arkNavigationTitle);
41    return this;
42  }
43  menus(value: Array<NavigationMenuItem> | undefined): this {
44    if (isUndefined(value)) {
45      modifierWithKey(this._modifiersWithKeys, NavDestinationMenusModifier.identity,
46        NavDestinationMenusModifier, undefined);
47      return this;
48    }
49    modifierWithKey(this._modifiersWithKeys, NavDestinationMenusModifier.identity,
50        NavDestinationMenusModifier, value);
51    return this;
52  }
53  hideTitleBar(isHide: boolean, animated?: boolean): this {
54    let arkNavDestinationHideTitleBar = new ArkNavHideTitleBarOrToolBar();
55    if (!isUndefined(isHide) && !isNull(isHide)) {
56      arkNavDestinationHideTitleBar.isHide = isHide;
57    }
58    if (!isUndefined(animated) && !isNull(animated)) {
59      arkNavDestinationHideTitleBar.animated = animated;
60    }
61    if (arkNavDestinationHideTitleBar.isHide === undefined && arkNavDestinationHideTitleBar.animated === undefined) {
62        modifierWithKey(this._modifiersWithKeys, HideTitleBarModifier.identity, HideTitleBarModifier, undefined);
63    } else {
64        modifierWithKey(this._modifiersWithKeys, HideTitleBarModifier.identity, HideTitleBarModifier, arkNavDestinationHideTitleBar);
65    }
66    return this;
67  }
68  hideToolBar(isHide: boolean, animated?: boolean): this {
69    let arkNavDestinationHideToolBar = new ArkNavHideTitleBarOrToolBar();
70    if (!isUndefined(isHide) && !isNull(isHide)) {
71      arkNavDestinationHideToolBar.isHide = isHide;
72    }
73    if (!isUndefined(animated) && !isNull(animated)) {
74      arkNavDestinationHideToolBar.animated = animated;
75    }
76    if (arkNavDestinationHideToolBar.isHide === undefined && arkNavDestinationHideToolBar.animated === undefined) {
77        modifierWithKey(this._modifiersWithKeys, NavDestinationHideToolBarModifier.identity,
78          NavDestinationHideToolBarModifier, undefined);
79    } else {
80        modifierWithKey(this._modifiersWithKeys, NavDestinationHideToolBarModifier.identity,
81          NavDestinationHideToolBarModifier, arkNavDestinationHideToolBar);
82    }
83    return this;
84  }
85  toolbarConfiguration(value: any): this {
86    throw new Error('Method not implemented.');
87  }
88  backButtonIcon(value: any): this {
89    modifierWithKey(this._modifiersWithKeys, NavDestinationBackButtonIconModifier.identity,
90      NavDestinationBackButtonIconModifier, value);
91    return this;
92  }
93  mode(value: number): this {
94    modifierWithKey(this._modifiersWithKeys, NavDestinationModeModifier.identity,
95      NavDestinationModeModifier, value);
96    return this;
97  }
98  systemTransition(value: number): this {
99    modifierWithKey(this._modifiersWithKeys, NavDestinationSystemTransitionModifier.identity,
100      NavDestinationSystemTransitionModifier, value);
101    return this;
102  }
103  onShown(callback: () => void): this {
104    throw new Error('Method not implemented.');
105  }
106  onHidden(callback: () => void): this {
107    throw new Error('Method not implemented.');
108  }
109  onBackPressed(callback: () => boolean): this {
110    throw new Error('Method not implemented.');
111  }
112  ignoreLayoutSafeArea(types?: Array<SafeAreaType>, edges?: Array<SafeAreaEdge>): this {
113    let opts = new ArkSafeAreaExpandOpts();
114    if (types && types.length >= 0) {
115      let safeAreaType: string | number = '';
116      for (let param of types) {
117        if (!isNumber(param) || param >= NAVDES_SAFE_AREA_TYPE_LIMIT || param < NAVDES_SAFE_AREA_LOWER_LIMIT) {
118          safeAreaType = undefined;
119          break;
120        }
121        if (safeAreaType) {
122          safeAreaType += '|';
123          safeAreaType += param.toString();
124        } else {
125          safeAreaType += param.toString();
126        }
127      }
128      opts.type = safeAreaType;
129    }
130    if (edges && edges.length >= 0) {
131      let safeAreaEdge: string | number = '';
132      for (let param of edges) {
133        if (!isNumber(param) || param >= NAVDES_SAFE_AREA_EDGE_LIMIT || param < NAVDES_SAFE_AREA_LOWER_LIMIT) {
134          safeAreaEdge = undefined;
135          break;
136        }
137        if (safeAreaEdge) {
138          safeAreaEdge += '|';
139          safeAreaEdge += param.toString();
140        } else {
141          safeAreaEdge += param.toString();
142        }
143      }
144      opts.edges = safeAreaEdge;
145    }
146    if (opts.type === undefined && opts.edges === undefined) {
147      modifierWithKey(this._modifiersWithKeys, IgnoreLayoutSafeAreaModifier.identity, IgnoreLayoutSafeAreaModifier, undefined);
148    } else {
149      modifierWithKey(this._modifiersWithKeys, IgnoreLayoutSafeAreaModifier.identity, IgnoreLayoutSafeAreaModifier, opts);
150    }
151    return this;
152  }
153  recoverable(value: boolean | undefined): this {
154    modifierWithKey(this._modifiersWithKeys, NavDestinationRecoverableModifier.identity, NavDestinationRecoverableModifier, value);
155    return this;
156  }
157}
158
159class NavDestinationTitleModifier extends ModifierWithKey<ArkNavigationTitle | undefined> {
160  constructor(value: ArkNavigationTitle | undefined) {
161    super(value);
162  }
163  static identity: Symbol = Symbol('title');
164  applyPeer(node: KNode, reset: boolean): void {
165    if (reset) {
166      getUINativeModule().navDestination.resetTitle(node);
167    } else {
168      getUINativeModule().navDestination.setTitle(node, this.value?.value, this.value?.navigationTitleOptions);
169    }
170  }
171  checkObjectDiff(): boolean {
172    return !this.value.isEqual(this.stageValue);
173  }
174}
175
176class NavDestinationMenusModifier extends ModifierWithKey<Array<NavigationMenuItem> | undefined> {
177  constructor(value: Array<NavigationMenuItem> | undefined) {
178    super(value);
179  }
180  static identity: Symbol = Symbol('menus');
181
182  applyPeer(node: KNode, reset: boolean): void {
183    if (reset) {
184      getUINativeModule().navDestination.resetMenus(node);
185    } else {
186      getUINativeModule().navDestination.setMenus(node, this.value);
187    }
188  }
189  checkObjectDiff(): boolean {
190    if (!Array.isArray(this.value) || !Array.isArray(this.stageValue)) {
191      return true;
192    }
193    if (this.value.length !== this.stageValue.length) {
194      return true;
195    }
196    for (let i = 0; i < this.value.length; i++) {
197      if (!(isBaseOrResourceEqual(this.stageValue[i].value, this.value[i].value) &&
198        isBaseOrResourceEqual(this.stageValue[i].icon, this.value[i].icon) &&
199        isBaseOrResourceEqual(this.stageValue[i].isEnabled, this.value[i].isEnabled) &&
200        isBaseOrResourceEqual(this.stageValue[i].action, this.value[i].action) &&
201        isBaseOrResourceEqual(this.stageValue[i].symbolIcon, this.value[i].symbolIcon)
202      )) {
203        return true;
204      }
205    }
206    return false;
207  }
208}
209
210class HideTitleBarModifier extends ModifierWithKey<ArkNavHideTitleBarOrToolBar | undefined> {
211  constructor(value: ArkNavHideTitleBarOrToolBar | undefined) {
212    super(value);
213  }
214  static identity: Symbol = Symbol('hideTitleBar');
215
216  applyPeer(node: KNode, reset: boolean): void {
217    if (reset) {
218      getUINativeModule().navDestination.resetHideTitleBar(node);
219    } else {
220      getUINativeModule().navDestination.setHideTitleBar(node, this.value?.isHide, this.value?.animated);
221    }
222  }
223}
224
225class NavDestinationHideToolBarModifier extends ModifierWithKey<ArkNavHideTitleBarOrToolBar | undefined> {
226  constructor(value: ArkNavHideTitleBarOrToolBar | undefined) {
227    super(value);
228  }
229  static identity: Symbol = Symbol('hideToolBar');
230
231  applyPeer(node: KNode, reset: boolean): void {
232    if (reset) {
233      getUINativeModule().navDestination.resetHideToolBar(node);
234    } else {
235      getUINativeModule().navDestination.setHideToolBar(node, this.value?.isHide, this.value?.animated);
236    }
237  }
238}
239
240class NavDestinationBackButtonIconModifier extends ModifierWithKey<object> {
241  constructor(value: object) {
242    super(value);
243  }
244  static identity: Symbol = Symbol('backButtonIcon');
245  applyPeer(node: KNode, reset: boolean): void {
246    if (reset) {
247      getUINativeModule().navDestination.resetBackButtonIcon(node);
248    } else {
249      getUINativeModule().navDestination.setBackButtonIcon(node, this.value);
250    }
251  }
252}
253
254class NavDestinationModeModifier extends ModifierWithKey<number> {
255  constructor(value: number) {
256    super(value);
257  }
258  static identity: Symbol = Symbol('mode');
259
260  applyPeer(node: KNode, reset: boolean): void {
261    if (reset) {
262      getUINativeModule().navDestination.resetMode(node);
263    } else {
264      getUINativeModule().navDestination.setMode(node, this.value);
265    }
266  }
267}
268
269class NavDestinationSystemTransitionModifier extends ModifierWithKey<number> {
270  constructor(value: number) {
271    super(value);
272  }
273  static identity: Symbol = Symbol('systemTransition');
274
275  applyPeer(node: KNode, reset: boolean): void {
276    if (reset) {
277      getUINativeModule().navDestination.resetSystemTransition(node);
278    } else {
279      getUINativeModule().navDestination.setSystemTransition(node, this.value);
280    }
281  }
282}
283
284class IgnoreLayoutSafeAreaModifier extends ModifierWithKey<ArkSafeAreaExpandOpts | undefined> {
285  constructor(value: ArkSafeAreaExpandOpts | undefined) {
286    super(value);
287  }
288  static identity: Symbol = Symbol('ignoreLayoutSafeArea');
289  applyPeer(node: KNode, reset: boolean): void {
290    if (reset) {
291      getUINativeModule().navDestination.resetIgnoreLayoutSafeArea(node);
292    } else {
293      getUINativeModule().navDestination.setIgnoreLayoutSafeArea(node, this.value.type, this.value.edges);
294    }
295  }
296  checkObjectDiff(): boolean {
297    return !isBaseOrResourceEqual(this.stageValue.type, this.value.type) ||
298      !isBaseOrResourceEqual(this.stageValue.edges, this.value.edges);
299  }
300}
301
302class NavDestinationRecoverableModifier extends ModifierWithKey<boolean | undefined> {
303  constructor(value: boolean | undefined) {
304    super(value);
305  }
306  static identity: Symbol = Symbol('recoverable');
307
308  applyPeer(node: KNode, reset: boolean): void {
309    if (reset) {
310      getUINativeModule().navDestination.resetRecoverable(node);
311    } else {
312      getUINativeModule().navDestination.setRecoverable(node, this.value);
313    }
314  }
315}
316
317//@ts-ignore
318globalThis.NavDestination.attributeModifier = function (modifier: ArkComponent): void {
319  attributeModifierFunc.call(this, modifier, (nativePtr: KNode) => {
320    return new ArkNavDestinationComponent(nativePtr);
321  }, (nativePtr: KNode, classType: ModifierType, modifierJS: ModifierJS) => {
322    return new modifierJS.NavDestinationModifier(nativePtr, classType);
323  });
324};
325