• 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' />
17const TITLE_MODE_RANGE = 2;
18const NAV_BAR_POSITION_RANGE = 1;
19const NAVIGATION_MODE_RANGE = 2;
20const DEFAULT_NAV_BAR_WIDTH = 240;
21const MIN_NAV_BAR_WIDTH_DEFAULT = '240vp';
22const MAX_NAV_BAR_WIDTH_DEFAULT = '40%';
23const NAVIGATION_TITLE_MODE_DEFAULT = 0;
24const DEFAULT_UNIT = 'vp';
25const NAV_SAFE_AREA_TYPE_LIMIT = 3;
26const NAV_SAFE_AREA_EDGE_LIMIT = 4;
27const NAV_SAFE_AREA_LOWER_LIMIT = 0;
28
29class ArkNavigationComponent extends ArkComponent implements NavigationAttribute {
30  constructor(nativePtr: KNode, classType?: ModifierType) {
31    super(nativePtr, classType);
32  }
33  navBarWidth(value: Length): NavigationAttribute {
34    modifierWithKey(this._modifiersWithKeys, NavBarWidthModifier.identity, NavBarWidthModifier, value);
35    return this;
36  }
37  navBarPosition(value: number): NavigationAttribute {
38    modifierWithKey(this._modifiersWithKeys, NavBarPositionModifier.identity, NavBarPositionModifier, value);
39    return this;
40  }
41  navBarWidthRange(value: [Dimension, Dimension]): NavigationAttribute {
42    modifierWithKey(this._modifiersWithKeys, NavBarWidthRangeModifier.identity, NavBarWidthRangeModifier, value);
43    return this;
44  }
45  minContentWidth(value: Dimension): NavigationAttribute {
46    modifierWithKey(this._modifiersWithKeys, MinContentWidthModifier.identity, MinContentWidthModifier, value);
47
48    return this;
49  }
50  mode(value: number): NavigationAttribute {
51    modifierWithKey(this._modifiersWithKeys, ModeModifier.identity, ModeModifier, value);
52    return this;
53  }
54  backButtonIcon(value: any): NavigationAttribute {
55    modifierWithKey(this._modifiersWithKeys, BackButtonIconModifier.identity, BackButtonIconModifier, value);
56    return this;
57  }
58  hideNavBar(value: boolean): NavigationAttribute {
59    modifierWithKey(this._modifiersWithKeys, HideNavBarModifier.identity, HideNavBarModifier, value);
60    return this;
61  }
62  title(value: any, options?: NavigationTitleOptions): NavigationAttribute {
63    let arkNavigationTitle = new ArkNavigationTitle();
64    if (!isUndefined(options) && !isNull(options) && isObject(options)) {
65      if (Object.keys(options).length !== 0) {
66        arkNavigationTitle.navigationTitleOptions = options;
67      }
68    }
69    modifierWithKey(this._modifiersWithKeys, TitleModifier.identity,
70      TitleModifier, arkNavigationTitle);
71    return this;
72  }
73  subTitle(value: string): NavigationAttribute {
74    modifierWithKey(this._modifiersWithKeys, SubTitleModifier.identity, SubTitleModifier, value);
75    return this;
76  }
77  hideTitleBar(isHide: boolean, animated?: boolean): NavigationAttribute {
78    let arkNavigationHideTitleBar = new ArkNavHideTitleBarOrToolBar();
79    if (!isUndefined(isHide) && !isNull(isHide)) {
80      arkNavigationHideTitleBar.isHide = isHide;
81    }
82    if (!isUndefined(animated) && !isNull(animated)) {
83      arkNavigationHideTitleBar.animated = animated;
84    }
85    if (arkNavigationHideTitleBar.isHide === undefined && arkNavigationHideTitleBar.animated === undefined) {
86        modifierWithKey(this._modifiersWithKeys, NavigationHideTitleBarModifier.identity, NavigationHideTitleBarModifier, undefined);
87    } else {
88        modifierWithKey(this._modifiersWithKeys, NavigationHideTitleBarModifier.identity, NavigationHideTitleBarModifier, arkNavigationHideTitleBar);
89    }
90    return this;
91  }
92  hideBackButton(value: boolean): NavigationAttribute {
93    modifierWithKey(this._modifiersWithKeys, HideBackButtonModifier.identity, HideBackButtonModifier, value);
94    return this;
95  }
96  titleMode(value: NavigationTitleMode): NavigationAttribute {
97    modifierWithKey(this._modifiersWithKeys, TitleModeModifier.identity, TitleModeModifier, value);
98    return this;
99  }
100  menus(value: any): NavigationAttribute {
101    throw new Error('Method not implemented.');
102  }
103  toolBar(value: any): NavigationAttribute {
104    throw new Error('Method not implemented.');
105  }
106  toolbarConfiguration(value: any): NavigationAttribute {
107    throw new Error('Method not implemented.');
108  }
109  hideToolBar(isHide: boolean, animated?: boolean): NavigationAttribute {
110    let arkNavigationHideToolBar = new ArkNavHideTitleBarOrToolBar();
111    if (!isUndefined(isHide) && !isNull(isHide)) {
112      arkNavigationHideToolBar.isHide = isHide;
113    }
114    if (!isUndefined(animated) && !isNull(animated)) {
115      arkNavigationHideToolBar.animated = animated;
116    }
117    if (arkNavigationHideToolBar.isHide === undefined && arkNavigationHideToolBar.animated === undefined) {
118        modifierWithKey(this._modifiersWithKeys, HideToolBarModifier.identity, HideToolBarModifier, undefined);
119    } else {
120        modifierWithKey(this._modifiersWithKeys, HideToolBarModifier.identity, HideToolBarModifier, arkNavigationHideToolBar);
121    }
122    return this;
123  }
124  onTitleModeChange(callback: (titleMode: NavigationTitleMode) => void): NavigationAttribute {
125    throw new Error('Method not implemented.');
126  }
127  onNavBarStateChange(callback: (isVisible: boolean) => void): NavigationAttribute {
128    throw new Error('Method not implemented.');
129  }
130  onNavigationModeChange(callback: (mode: NavigationMode) => void): NavigationAttribute {
131    throw new Error('Method not implemented.');
132  }
133  navDestination(builder: (name: string, param: unknown) => void): NavigationAttribute {
134    throw new Error('Method not implemented.');
135  }
136  ignoreLayoutSafeArea(types?: Array<SafeAreaType>, edges?: Array<SafeAreaEdge>): NavigationAttribute {
137    let opts = new ArkSafeAreaExpandOpts();
138    if (types && types.length >= 0) {
139      let safeAreaType: string | number = '';
140      for (let param of types) {
141        if (!isNumber(param) || param >= NAV_SAFE_AREA_TYPE_LIMIT || param < NAV_SAFE_AREA_LOWER_LIMIT) {
142          safeAreaType = undefined;
143          break;
144        }
145        if (safeAreaType) {
146          safeAreaType += '|';
147          safeAreaType += param.toString();
148        } else {
149          safeAreaType += param.toString();
150        }
151      }
152      opts.type = safeAreaType;
153    }
154    if (edges && edges.length >= 0) {
155      let safeAreaEdge: string | number = '';
156      for (let param of edges) {
157        if (!isNumber(param) || param >= NAV_SAFE_AREA_EDGE_LIMIT || param < NAV_SAFE_AREA_LOWER_LIMIT) {
158          safeAreaEdge = undefined;
159          break;
160        }
161        if (safeAreaEdge) {
162          safeAreaEdge += '|';
163          safeAreaEdge += param.toString();
164        } else {
165          safeAreaEdge += param.toString();
166        }
167      }
168      opts.edges = safeAreaEdge;
169    }
170    if (opts.type === undefined && opts.edges === undefined) {
171      modifierWithKey(this._modifiersWithKeys, IgnoreNavLayoutSafeAreaModifier.identity, IgnoreNavLayoutSafeAreaModifier, undefined);
172    } else {
173      modifierWithKey(this._modifiersWithKeys, IgnoreNavLayoutSafeAreaModifier.identity, IgnoreNavLayoutSafeAreaModifier, opts);
174    }
175    return this;
176  }
177}
178
179class BackButtonIconModifier extends ModifierWithKey<boolean | object> {
180  static identity: Symbol = Symbol('backButtonIcon');
181  applyPeer(node: KNode, reset: boolean): void {
182    if (reset) {
183      getUINativeModule().navigation.resetBackButtonIcon(node);
184    } else {
185      getUINativeModule().navigation.setBackButtonIcon(node, this.value);
186    }
187  }
188
189  checkObjectDiff(): boolean {
190    return !isBaseOrResourceEqual(this.stageValue, this.value);
191  }
192}
193
194class NavBarWidthRangeModifier extends ModifierWithKey<[Dimension, Dimension]> {
195  static identity: Symbol = Symbol('navBarWidthRange');
196  applyPeer(node: KNode, reset: boolean): void {
197    if (reset) {
198      getUINativeModule().navigation.resetNavBarWidthRange(node);
199    } else {
200      getUINativeModule().navigation.setNavBarWidthRange(node, this.value);
201    }
202  }
203
204  checkObjectDiff(): boolean {
205    return !isBaseOrResourceEqual(this.stageValue, this.value);
206  }
207}
208
209class MinContentWidthModifier extends ModifierWithKey<Dimension> {
210  static identity: Symbol = Symbol('minContentWidth');
211
212  applyPeer(node: KNode, reset: boolean): void {
213    if (reset) {
214      getUINativeModule().navigation.resetMinContentWidth(node);
215    } else {
216      getUINativeModule().navigation.setMinContentWidth(node, this.value);
217    }
218  }
219
220  checkObjectDiff(): boolean {
221    return !isBaseOrResourceEqual(this.stageValue, this.value);
222  }
223}
224
225class NavBarWidthModifier extends ModifierWithKey<Length> {
226  static identity: Symbol = Symbol('navBarWidth');
227
228  applyPeer(node: KNode, reset: boolean): void {
229    if (reset) {
230      getUINativeModule().navigation.resetNavBarWidth(node);
231    } else {
232      getUINativeModule().navigation.setNavBarWidth(node, this.value);
233    }
234  }
235
236  checkObjectDiff(): boolean {
237    return !isBaseOrResourceEqual(this.stageValue, this.value);
238  }
239}
240
241class NavBarPositionModifier extends ModifierWithKey<number> {
242  static identity: Symbol = Symbol('navBarPosition');
243
244  applyPeer(node: KNode, reset: boolean): void {
245    if (reset) {
246      getUINativeModule().navigation.resetNavBarPosition(node);
247    } else {
248      getUINativeModule().navigation.setNavBarPosition(node, this.value);
249    }
250  }
251}
252
253class ModeModifier extends ModifierWithKey<number> {
254  static identity: Symbol = Symbol('mode');
255
256  applyPeer(node: KNode, reset: boolean): void {
257    if (reset) {
258      getUINativeModule().navigation.resetMode(node);
259    } else {
260      getUINativeModule().navigation.setMode(node, this.value);
261    }
262  }
263}
264
265class HideToolBarModifier extends ModifierWithKey<ArkNavHideTitleBarOrToolBar | undefined> {
266  constructor(value: ArkNavHideTitleBarOrToolBar) {
267    super(value);
268  }
269
270  static identity: Symbol = Symbol('hideToolBar');
271
272  applyPeer(node: KNode, reset: boolean): void {
273    if (reset) {
274      getUINativeModule().navigation.resetHideToolBar(node);
275    } else {
276      getUINativeModule().navigation.setHideToolBar(node, this.value?.isHide, this.value?.animated);
277    }
278  }
279}
280
281class TitleModeModifier extends ModifierWithKey<number> {
282  static identity: Symbol = Symbol('titleMode');
283
284  applyPeer(node: KNode, reset: boolean): void {
285    if (reset) {
286      getUINativeModule().navigation.resetTitleMode(node);
287    } else {
288      getUINativeModule().navigation.setTitleMode(node, this.value);
289    }
290  }
291}
292
293class HideBackButtonModifier extends ModifierWithKey<boolean> {
294  static identity: Symbol = Symbol('hideBackButton');
295
296  applyPeer(node: KNode, reset: boolean): void {
297    if (reset) {
298      getUINativeModule().navigation.resetHideBackButton(node);
299    } else {
300      getUINativeModule().navigation.setHideBackButton(node, this.value);
301    }
302  }
303}
304
305class TitleModifier extends ModifierWithKey<ArkNavigationTitle | undefined> {
306  constructor(value: ArkNavigationTitle | undefined) {
307    super(value);
308  }
309  static identity: Symbol = Symbol('title');
310  applyPeer(node: KNode, reset: boolean): void {
311    if (reset) {
312      getUINativeModule().navigation.resetTitle(node);
313    } else {
314      getUINativeModule().navigation.setTitle(node, this.value?.navigationTitleOptions);
315    }
316  }
317  checkObjectDiff(): boolean {
318    return !this.value.isEqual(this.stageValue);
319  }
320}
321
322class SubTitleModifier extends ModifierWithKey<string> {
323  static identity: Symbol = Symbol('subTitle');
324
325  applyPeer(node: KNode, reset: boolean): void {
326    if (reset) {
327      getUINativeModule().navigation.resetSubTitle(node);
328    } else {
329      getUINativeModule().navigation.setSubTitle(node, this.value);
330    }
331  }
332}
333
334class NavigationHideTitleBarModifier extends ModifierWithKey<ArkNavHideTitleBarOrToolBar | undefined> {
335  constructor(value: ArkNavHideTitleBarOrToolBar) {
336    super(value);
337  }
338
339  static identity: Symbol = Symbol('hideTitleBar');
340
341  applyPeer(node: KNode, reset: boolean): void {
342    if (reset) {
343      getUINativeModule().navigation.resetHideTitleBar(node);
344    } else {
345      getUINativeModule().navigation.setHideTitleBar(node, this.value?.isHide, this.value?.animated);
346    }
347  }
348}
349
350class HideNavBarModifier extends ModifierWithKey<boolean> {
351  static identity: Symbol = Symbol('hideNavBar');
352
353  applyPeer(node: KNode, reset: boolean): void {
354    if (reset) {
355      getUINativeModule().navigation.resetHideNavBar(node);
356    } else {
357      getUINativeModule().navigation.setHideNavBar(node, this.value);
358    }
359  }
360}
361
362class IgnoreNavLayoutSafeAreaModifier extends ModifierWithKey<ArkSafeAreaExpandOpts | undefined> {
363  constructor(value: ArkSafeAreaExpandOpts | undefined) {
364    super(value);
365  }
366  static identity: Symbol = Symbol('ignoreLayoutSafeArea');
367  applyPeer(node: KNode, reset: boolean): void {
368    if (reset) {
369      getUINativeModule().navigation.resetIgnoreLayoutSafeArea(node);
370    } else {
371      getUINativeModule().navigation.setIgnoreLayoutSafeArea(node, this.value.type, this.value.edges);
372    }
373  }
374  checkObjectDiff(): boolean {
375    return !isBaseOrResourceEqual(this.stageValue.type, this.value.type) ||
376      !isBaseOrResourceEqual(this.stageValue.edges, this.value.edges);
377  }
378}
379
380// @ts-ignore
381globalThis.Navigation.attributeModifier = function (modifier: ArkComponent): void {
382  attributeModifierFunc.call(this, modifier, (nativePtr: KNode) => {
383    return new ArkNavigationComponent(nativePtr);
384  }, (nativePtr: KNode, classType: ModifierType, modifierJS: ModifierJS) => {
385    return new modifierJS.NavigationModifier(nativePtr, classType);
386  });
387};
388