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'; 25 26class ArkNavigationComponent extends ArkComponent implements NavigationAttribute { 27 constructor(nativePtr: KNode) { 28 super(nativePtr); 29 } 30 navBarWidth(value: Length): NavigationAttribute { 31 modifierWithKey(this._modifiersWithKeys, NavBarWidthModifier.identity, NavBarWidthModifier, value); 32 return this; 33 } 34 navBarPosition(value: number): NavigationAttribute { 35 modifierWithKey(this._modifiersWithKeys, NavBarPositionModifier.identity, NavBarPositionModifier, value); 36 return this; 37 } 38 navBarWidthRange(value: [Dimension, Dimension]): NavigationAttribute { 39 modifierWithKey(this._modifiersWithKeys, NavBarWidthRangeModifier.identity, NavBarWidthRangeModifier, value); 40 return this; 41 } 42 minContentWidth(value: Dimension): NavigationAttribute { 43 modifierWithKey(this._modifiersWithKeys, MinContentWidthModifier.identity, MinContentWidthModifier, value); 44 45 return this; 46 } 47 mode(value: number): NavigationAttribute { 48 modifierWithKey(this._modifiersWithKeys, ModeModifier.identity, ModeModifier, value); 49 return this; 50 } 51 backButtonIcon(value: any): NavigationAttribute { 52 modifierWithKey(this._modifiersWithKeys, BackButtonIconModifier.identity, BackButtonIconModifier, value); 53 return this; 54 } 55 hideNavBar(value: boolean): NavigationAttribute { 56 modifierWithKey(this._modifiersWithKeys, HideNavBarModifier.identity, HideNavBarModifier, value); 57 return this; 58 } 59 title(value: any): NavigationAttribute { 60 throw new Error('Method not implemented.'); 61 } 62 subTitle(value: string): NavigationAttribute { 63 modifierWithKey(this._modifiersWithKeys, SubTitleModifier.identity, SubTitleModifier, value); 64 return this; 65 } 66 hideTitleBar(value: boolean): NavigationAttribute { 67 modifierWithKey(this._modifiersWithKeys, NavigationHideTitleBarModifier.identity, NavigationHideTitleBarModifier, value); 68 return this; 69 } 70 hideBackButton(value: boolean): NavigationAttribute { 71 modifierWithKey(this._modifiersWithKeys, HideBackButtonModifier.identity, HideBackButtonModifier, value); 72 return this; 73 } 74 titleMode(value: NavigationTitleMode): NavigationAttribute { 75 modifierWithKey(this._modifiersWithKeys, TitleModeModifier.identity, TitleModeModifier, value); 76 return this; 77 } 78 menus(value: any): NavigationAttribute { 79 throw new Error('Method not implemented.'); 80 } 81 toolBar(value: any): NavigationAttribute { 82 throw new Error('Method not implemented.'); 83 } 84 toolbarConfiguration(value: any): NavigationAttribute { 85 throw new Error('Method not implemented.'); 86 } 87 hideToolBar(value: boolean): NavigationAttribute { 88 modifierWithKey(this._modifiersWithKeys, HideToolBarModifier.identity, HideToolBarModifier, value); 89 return this; 90 } 91 onTitleModeChange(callback: (titleMode: NavigationTitleMode) => void): NavigationAttribute { 92 throw new Error('Method not implemented.'); 93 } 94 onNavBarStateChange(callback: (isVisible: boolean) => void): NavigationAttribute { 95 throw new Error('Method not implemented.'); 96 } 97 onNavigationModeChange(callback: (mode: NavigationMode) => void): NavigationAttribute { 98 throw new Error('Method not implemented.'); 99 } 100 navDestination(builder: (name: string, param: unknown) => void): NavigationAttribute { 101 throw new Error('Method not implemented.'); 102 } 103} 104 105class BackButtonIconModifier extends ModifierWithKey<boolean | object> { 106 constructor(value: boolean | object) { 107 super(value); 108 } 109 static identity: Symbol = Symbol('backButtonIcon'); 110 applyPeer(node: KNode, reset: boolean): void { 111 if (reset) { 112 getUINativeModule().navigation.resetBackButtonIcon(node); 113 } else { 114 getUINativeModule().navigation.setBackButtonIcon(node, this.value); 115 } 116 } 117 118 checkObjectDiff(): boolean { 119 return !isBaseOrResourceEqual(this.stageValue, this.value); 120 } 121} 122 123class NavBarWidthRangeModifier extends ModifierWithKey<[Dimension, Dimension]> { 124 constructor(value: [Dimension, Dimension]) { 125 super(value); 126 } 127 static identity: Symbol = Symbol('navBarWidthRange'); 128 applyPeer(node: KNode, reset: boolean): void { 129 if (reset) { 130 getUINativeModule().navigation.resetNavBarWidthRange(node); 131 } else { 132 getUINativeModule().navigation.setNavBarWidthRange(node, this.value); 133 } 134 } 135 136 checkObjectDiff(): boolean { 137 return !isBaseOrResourceEqual(this.stageValue, this.value); 138 } 139} 140 141class MinContentWidthModifier extends ModifierWithKey<Dimension> { 142 constructor(value: Dimension) { 143 super(value); 144 } 145 static identity: Symbol = Symbol('minContentWidth'); 146 147 applyPeer(node: KNode, reset: boolean): void { 148 if (reset) { 149 getUINativeModule().navigation.resetMinContentWidth(node); 150 } else { 151 getUINativeModule().navigation.setMinContentWidth(node, this.value); 152 } 153 } 154 155 checkObjectDiff(): boolean { 156 return !isBaseOrResourceEqual(this.stageValue, this.value); 157 } 158} 159 160class NavBarWidthModifier extends ModifierWithKey<Length> { 161 constructor(value: Length) { 162 super(value); 163 } 164 static identity: Symbol = Symbol('navBarWidth'); 165 166 applyPeer(node: KNode, reset: boolean): void { 167 if (reset) { 168 getUINativeModule().navigation.resetNavBarWidth(node); 169 } else { 170 getUINativeModule().navigation.setNavBarWidth(node, this.value); 171 } 172 } 173 174 checkObjectDiff(): boolean { 175 return !isBaseOrResourceEqual(this.stageValue, this.value); 176 } 177} 178 179class NavBarPositionModifier extends ModifierWithKey<number> { 180 constructor(value: number) { 181 super(value); 182 } 183 static identity: Symbol = Symbol('navBarPosition'); 184 185 applyPeer(node: KNode, reset: boolean): void { 186 if (reset) { 187 getUINativeModule().navigation.resetNavBarPosition(node); 188 } else { 189 getUINativeModule().navigation.setNavBarPosition(node, this.value); 190 } 191 } 192} 193 194class ModeModifier 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().navigation.resetMode(node); 203 } else { 204 getUINativeModule().navigation.setMode(node, this.value); 205 } 206 } 207} 208 209class HideToolBarModifier extends ModifierWithKey<boolean> { 210 constructor(value: boolean) { 211 super(value); 212 } 213 static identity: Symbol = Symbol('hideToolBar'); 214 215 applyPeer(node: KNode, reset: boolean): void { 216 if (reset) { 217 getUINativeModule().navigation.resetHideToolBar(node); 218 } else { 219 getUINativeModule().navigation.setHideToolBar(node, this.value); 220 } 221 } 222} 223 224class TitleModeModifier extends ModifierWithKey<number> { 225 constructor(value: number) { 226 super(value); 227 } 228 static identity: Symbol = Symbol('titleMode'); 229 230 applyPeer(node: KNode, reset: boolean): void { 231 if (reset) { 232 getUINativeModule().navigation.resetTitleMode(node); 233 } else { 234 getUINativeModule().navigation.setTitleMode(node, this.value); 235 } 236 } 237} 238 239class HideBackButtonModifier extends ModifierWithKey<boolean> { 240 constructor(value: boolean) { 241 super(value); 242 } 243 static identity: Symbol = Symbol('hideBackButton'); 244 245 applyPeer(node: KNode, reset: boolean): void { 246 if (reset) { 247 getUINativeModule().navigation.resetHideBackButton(node); 248 } else { 249 getUINativeModule().navigation.setHideBackButton(node, this.value); 250 } 251 } 252} 253 254class SubTitleModifier extends ModifierWithKey<string> { 255 constructor(value: string) { 256 super(value); 257 } 258 static identity: Symbol = Symbol('subTitle'); 259 260 applyPeer(node: KNode, reset: boolean): void { 261 if (reset) { 262 getUINativeModule().navigation.resetSubTitle(node); 263 } else { 264 getUINativeModule().navigation.setSubTitle(node, this.value); 265 } 266 } 267} 268 269class NavigationHideTitleBarModifier extends ModifierWithKey<boolean> { 270 constructor(value: boolean) { 271 super(value); 272 } 273 static identity: Symbol = Symbol('hideTitleBar'); 274 275 applyPeer(node: KNode, reset: boolean): void { 276 if (reset) { 277 getUINativeModule().navigation.resetHideTitleBar(node); 278 } else { 279 getUINativeModule().navigation.setHideTitleBar(node, this.value); 280 } 281 } 282} 283 284class HideNavBarModifier extends ModifierWithKey<boolean> { 285 constructor(value: boolean) { 286 super(value); 287 } 288 static identity: Symbol = Symbol('hideNavBar'); 289 290 applyPeer(node: KNode, reset: boolean): void { 291 if (reset) { 292 getUINativeModule().navigation.resetHideNavBar(node); 293 } else { 294 getUINativeModule().navigation.setHideNavBar(node, this.value); 295 } 296 } 297} 298 299// @ts-ignore 300globalThis.Navigation.attributeModifier = function (modifier) { 301 const elmtId = ViewStackProcessor.GetElmtIdToAccountFor(); 302 let nativeNode = getUINativeModule().getFrameNodeById(elmtId); 303 let component = this.createOrGetNode(elmtId, () => { 304 return new ArkNavigationComponent(nativeNode); 305 }); 306 applyUIAttributes(modifier, nativeNode, component); 307 component.applyModifierPatch(); 308}; 309