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 ArkCounterComponent extends ArkComponent implements CounterAttribute { 18 constructor(nativePtr: KNode, classType?: ModifierType) { 19 super(nativePtr, classType); 20 } 21 onInc(event: () => void): this { 22 modifierWithKey(this._modifiersWithKeys, CounterOnIncModifier.identity, CounterOnIncModifier, event); 23 return this; 24 } 25 onDec(event: () => void): this { 26 modifierWithKey(this._modifiersWithKeys, CounterOnDecModifier.identity, CounterOnDecModifier, event); 27 return this; 28 } 29 enableDec(value: boolean): this { 30 modifierWithKey(this._modifiersWithKeys, EnableDecModifier.identity, EnableDecModifier, value); 31 return this; 32 } 33 enableInc(value: boolean): this { 34 modifierWithKey(this._modifiersWithKeys, EnableIncModifier.identity, EnableIncModifier, value); 35 return this; 36 } 37 backgroundColor(value: ResourceColor): this { 38 modifierWithKey(this._modifiersWithKeys, CounterBackgroundColorModifier.identity, CounterBackgroundColorModifier, value); 39 return this; 40 } 41 width(value: Length): this { 42 modifierWithKey( 43 this._modifiersWithKeys, CounterWidthModifier.identity, CounterWidthModifier, value); 44 return this; 45 } 46 height(value: Length): this { 47 modifierWithKey( 48 this._modifiersWithKeys, CounterHeightModifier.identity, CounterHeightModifier, value); 49 return this; 50 } 51 size(value: SizeOptions): this { 52 modifierWithKey(this._modifiersWithKeys, CounterSizeModifier.identity, CounterSizeModifier, value); 53 return this; 54 } 55} 56 57declare type OnIncCallback = () => void; 58class CounterOnIncModifier extends ModifierWithKey<OnIncCallback> { 59 constructor(value: OnIncCallback) { 60 super(value); 61 } 62 static identity: Symbol = Symbol('counterOnInc'); 63 applyPeer(node: KNode, reset: boolean): void { 64 if (reset) { 65 getUINativeModule().counter.resetCounterOnInc(node); 66 } else { 67 getUINativeModule().counter.setCounterOnInc(node, this.value); 68 } 69 } 70} 71 72declare type OnDecCallback = () => void; 73class CounterOnDecModifier extends ModifierWithKey<OnDecCallback> { 74 constructor(value: OnDecCallback) { 75 super(value); 76 } 77 static identity: Symbol = Symbol('counterOnDec'); 78 applyPeer(node: KNode, reset: boolean): void { 79 if (reset) { 80 getUINativeModule().counter.resetCounterOnDec(node); 81 } else { 82 getUINativeModule().counter.setCounterOnDec(node, this.value); 83 } 84 } 85} 86class CounterHeightModifier extends ModifierWithKey<Length> { 87 constructor(value: Length) { 88 super(value); 89 } 90 static identity: Symbol = Symbol('CounterHeight'); 91 applyPeer(node: KNode, reset: boolean): void { 92 if (reset) { 93 getUINativeModule().counter.resetCounterHeight(node); 94 } else { 95 getUINativeModule().counter.setCounterHeight(node, this.value); 96 } 97 } 98 99 checkObjectDiff(): boolean { 100 return !isBaseOrResourceEqual(this.stageValue, this.value); 101 } 102} 103class CounterWidthModifier extends ModifierWithKey<Length> { 104 constructor(value: Length) { 105 super(value); 106 } 107 static identity: Symbol = Symbol('CounterWidth'); 108 applyPeer(node: KNode, reset: boolean): void { 109 if (reset) { 110 getUINativeModule().counter.resetCounterWidth(node); 111 } else { 112 getUINativeModule().counter.setCounterWidth(node, this.value); 113 } 114 } 115 116 checkObjectDiff(): boolean { 117 return !isBaseOrResourceEqual(this.stageValue, this.value); 118 } 119} 120class CounterBackgroundColorModifier extends ModifierWithKey<ResourceColor> { 121 constructor(value: ResourceColor) { 122 super(value); 123 } 124 static identity: Symbol = Symbol('CounterBackgroundColor'); 125 applyPeer(node: KNode, reset: boolean): void { 126 if (reset) { 127 getUINativeModule().counter.resetCounterBackgroundColor(node); 128 } else { 129 getUINativeModule().counter.setCounterBackgroundColor(node, this.value); 130 } 131 } 132 133 checkObjectDiff(): boolean { 134 return !isBaseOrResourceEqual(this.stageValue, this.value); 135 } 136} 137class CounterSizeModifier extends ModifierWithKey<SizeOptions> { 138 constructor(value: SizeOptions) { 139 super(value); 140 } 141 static identity: Symbol = Symbol('CounterSize'); 142 applyPeer(node: KNode, reset: boolean): void { 143 if (reset) { 144 getUINativeModule().counter.resetCounterSize(node); 145 } else { 146 getUINativeModule().counter.setCounterSize(node, this.value.width, this.value.height); 147 } 148 } 149 150 checkObjectDiff(): boolean { 151 return !isBaseOrResourceEqual(this.stageValue.width, this.value.width) || 152 !isBaseOrResourceEqual(this.stageValue.height, this.value.height); 153 } 154} 155class EnableIncModifier extends ModifierWithKey<boolean> { 156 constructor(value: boolean) { 157 super(value); 158 } 159 static identity = Symbol('enableInc'); 160 applyPeer(node: KNode, reset: boolean): void { 161 if (reset) { 162 getUINativeModule().counter.resetEnableInc(node); 163 } else { 164 getUINativeModule().counter.setEnableInc(node, this.value); 165 } 166 } 167} 168class EnableDecModifier extends ModifierWithKey<boolean> { 169 constructor(value: boolean) { 170 super(value); 171 } 172 static identity = Symbol('enableDec'); 173 applyPeer(node: KNode, reset: boolean): void { 174 if (reset) { 175 getUINativeModule().counter.resetEnableDec(node); 176 } else { 177 getUINativeModule().counter.setEnableDec(node, this.value); 178 } 179 } 180} 181 182// @ts-ignore 183globalThis.Counter.attributeModifier = function (modifier: ArkComponent): void { 184 attributeModifierFunc.call(this, modifier, (nativePtr: KNode) => { 185 return new ArkCounterComponent(nativePtr); 186 }, (nativePtr: KNode, classType: ModifierType, modifierJS: ModifierJS) => { 187 return new modifierJS.CounterModifier(nativePtr, classType); 188 }); 189}; 190