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' /> 17class ArkToggleComponent extends ArkComponent implements ToggleAttribute { 18 constructor(nativePtr: KNode) { 19 super(nativePtr); 20 } 21 onGestureJudgeBegin(callback: (gestureInfo: GestureInfo, event: BaseGestureEvent) => GestureJudgeResult): this { 22 throw new Error('Method not implemented.'); 23 } 24 onChange(callback: (isOn: boolean) => void): this { 25 throw new Error('Method not implemented.'); 26 } 27 selectedColor(value: ResourceColor): this { 28 modifierWithKey(this._modifiersWithKeys, ToggleSelectedColorModifier.identity, ToggleSelectedColorModifier, value); 29 return this; 30 } 31 switchPointColor(value: ResourceColor): this { 32 modifierWithKey(this._modifiersWithKeys, ToggleSwitchPointColorModifier.identity, ToggleSwitchPointColorModifier, value); 33 return this; 34 } 35 height(value: Length): this { 36 modifierWithKey(this._modifiersWithKeys, ToggleHeightModifier.identity, ToggleHeightModifier, value); 37 return this; 38 } 39 responseRegion(value: Rectangle | Rectangle[]): this { 40 modifierWithKey(this._modifiersWithKeys, ToggleResponseRegionModifier.identity, ToggleResponseRegionModifier, value); 41 return this; 42 } 43 padding(value: Padding | Length): this { 44 modifierWithKey(this._modifiersWithKeys, TogglePaddingModifier.identity, TogglePaddingModifier, value); 45 return this; 46 } 47 backgroundColor(value: ResourceColor): this { 48 modifierWithKey(this._modifiersWithKeys, ToggleBackgroundColorModifier.identity, ToggleBackgroundColorModifier, value); 49 return this; 50 } 51 hoverEffect(value: HoverEffect): this { 52 modifierWithKey(this._modifiersWithKeys, ToggleHoverEffectModifier.identity, ToggleHoverEffectModifier, value); 53 return this; 54 } 55} 56class ToggleSelectedColorModifier extends ModifierWithKey<ResourceColor> { 57 constructor(value: ResourceColor) { 58 super(value); 59 } 60 static identity = Symbol('toggleSelectedColor'); 61 applyPeer(node: KNode, reset: boolean): void { 62 if (reset) { 63 getUINativeModule().toggle.resetSelectedColor(node); 64 } else { 65 getUINativeModule().toggle.setSelectedColor(node, this.value); 66 } 67 } 68 69 checkObjectDiff(): boolean { 70 return !isBaseOrResourceEqual(this.stageValue, this.value); 71 } 72} 73class ToggleSwitchPointColorModifier extends ModifierWithKey<ResourceColor> { 74 constructor(value: ResourceColor) { 75 super(value); 76 } 77 static identity = Symbol('toggleSwitchPointColor'); 78 applyPeer(node: KNode, reset: boolean): void { 79 if (reset) { 80 getUINativeModule().toggle.resetSwitchPointColor(node); 81 } else { 82 getUINativeModule().toggle.setSwitchPointColor(node, this.value); 83 } 84 } 85 86 checkObjectDiff(): boolean { 87 return !isBaseOrResourceEqual(this.stageValue, this.value); 88 } 89} 90class ToggleHeightModifier extends ModifierWithKey<Length> { 91 constructor(value: Length) { 92 super(value); 93 } 94 static identity = Symbol('toggleHeight'); 95 applyPeer(node: KNode, reset: boolean): void { 96 if (reset) { 97 getUINativeModule().toggle.resetHeight(node); 98 } else { 99 getUINativeModule().toggle.setHeight(node, this.value); 100 } 101 } 102 checkObjectDiff(): boolean { 103 return !isBaseOrResourceEqual(this.stageValue, this.value); 104 } 105} 106class ToggleResponseRegionModifier extends ModifierWithKey<Rectangle | Array<Rectangle>> { 107 constructor(value: Rectangle | Array<Rectangle>) { 108 super(value); 109 } 110 static identity = Symbol('toggleResponseRegion'); 111 applyPeer(node: KNode, reset: boolean): void { 112 if (reset) { 113 getUINativeModule().toggle.resetResponseRegion(node); 114 } else { 115 let responseRegion: (number | string | Resource)[] = []; 116 if (Array.isArray(this.value)) { 117 for (let i = 0; i < this.value.length; i++) { 118 responseRegion.push(this.value[i].x ?? 'PLACEHOLDER'); 119 responseRegion.push(this.value[i].y ?? 'PLACEHOLDER'); 120 responseRegion.push(this.value[i].width ?? 'PLACEHOLDER'); 121 responseRegion.push(this.value[i].height ?? 'PLACEHOLDER'); 122 } 123 } else { 124 responseRegion.push(this.value.x ?? 'PLACEHOLDER'); 125 responseRegion.push(this.value.y ?? 'PLACEHOLDER'); 126 responseRegion.push(this.value.width ?? 'PLACEHOLDER'); 127 responseRegion.push(this.value.height ?? 'PLACEHOLDER'); 128 } 129 getUINativeModule().toggle.setResponseRegion(node, responseRegion, responseRegion.length); 130 } 131 } 132 checkObjectDiff(): boolean { 133 if (Array.isArray(this.stageValue) && Array.isArray(this.value)) { 134 if (this.value.length !== this.stageValue.length) { 135 return true; 136 } else { 137 for (let i = 0; i < this.value.length; i++) { 138 if (!(isBaseOrResourceEqual(this.stageValue[i].x, this.value[i].x) && 139 isBaseOrResourceEqual(this.stageValue[i].y, this.value[i].y) && 140 isBaseOrResourceEqual(this.stageValue[i].width, this.value[i].width) && 141 isBaseOrResourceEqual(this.stageValue[i].height, this.value[i].height) 142 )) { 143 return true; 144 } 145 } 146 return false; 147 } 148 } else if (typeof this.stageValue === 'object' && typeof this.value === 'object') { 149 return !((this.stageValue as Rectangle).x === (this.value as Rectangle).x && 150 (this.stageValue as Rectangle).y === (this.value as Rectangle).y && 151 (this.stageValue as Rectangle).height === (this.value as Rectangle).height && 152 (this.stageValue as Rectangle).width === (this.value as Rectangle).width); 153 } else { 154 return true; 155 } 156 } 157} 158class TogglePaddingModifier extends ModifierWithKey<Padding | Length> { 159 constructor(value: Padding | Length) { 160 super(value); 161 } 162 static identity = Symbol('togglePadding'); 163 applyPeer(node: KNode, reset: boolean): void { 164 if (reset) { 165 getUINativeModule().toggle.resetPadding(node); 166 } else { 167 let top = undefined; 168 let right = undefined; 169 let bottom = undefined; 170 let left = undefined; 171 if (isLengthType(this.value) || isResource(this.value)) { 172 top = this.value; 173 right = this.value; 174 bottom = this.value; 175 left = this.value; 176 } else if (typeof this.value === 'object') { 177 top = (this.value as Padding).top; 178 right = (this.value as Padding).right; 179 bottom = (this.value as Padding).bottom; 180 left = (this.value as Padding).left; 181 } 182 getUINativeModule().toggle.setPadding(node, top, right, bottom, left); 183 } 184 } 185 checkObjectDiff(): boolean { 186 if (isResource(this.stageValue) && isResource(this.value)) { 187 return !isResourceEqual(this.stageValue, this.value); 188 } else if (!isResource(this.stageValue) && !isResource(this.value)) { 189 if (typeof this.stageValue === 'object' && typeof this.value === 'object') { 190 return !((this.stageValue as Padding).left === (this.value as Padding).left && 191 (this.stageValue as Padding).right === (this.value as Padding).right && 192 (this.stageValue as Padding).top === (this.value as Padding).top && 193 (this.stageValue as Padding).bottom === (this.value as Padding).bottom); 194 } else { 195 return !(this.stageValue === this.value); 196 } 197 } 198 return true; 199 } 200} 201class ToggleBackgroundColorModifier extends ModifierWithKey<ResourceColor> { 202 constructor(value: ResourceColor) { 203 super(value); 204 } 205 static identity = Symbol('toggleBackgroundColor'); 206 applyPeer(node: KNode, reset: boolean): void { 207 if (reset) { 208 getUINativeModule().toggle.resetBackgroundColor(node); 209 } else { 210 getUINativeModule().toggle.setBackgroundColor(node, this.value); 211 } 212 } 213 checkObjectDiff(): boolean { 214 return !isBaseOrResourceEqual(this.stageValue, this.value); 215 } 216} 217class ToggleHoverEffectModifier extends ModifierWithKey<HoverEffect> { 218 constructor(value: HoverEffect) { 219 super(value); 220 } 221 static identity = Symbol('toggleHoverEffect'); 222 applyPeer(node: KNode, reset: boolean): void { 223 if (reset) { 224 getUINativeModule().toggle.resetHoverEffect(node); 225 } else { 226 getUINativeModule().toggle.setHoverEffect(node, this.value); 227 } 228 } 229} 230// @ts-ignore 231globalThis.Toggle.attributeModifier = function (modifier) { 232 const elmtId = ViewStackProcessor.GetElmtIdToAccountFor(); 233 let nativeNode = getUINativeModule().getFrameNodeById(elmtId); 234 let component = this.createOrGetNode(elmtId, () => { 235 return new ArkToggleComponent(nativeNode); 236 }); 237 applyUIAttributes(modifier, nativeNode, component); 238 component.applyModifierPatch(); 239}; 240 241