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 MenuItemSelectedModifier extends ModifierWithKey<boolean> { 18 constructor(value: boolean) { 19 super(value); 20 } 21 static identity: Symbol = Symbol('menuItemSelected'); 22 applyPeer(node: KNode, reset: boolean) { 23 if (reset) { 24 getUINativeModule().menuitem.resetMenuItemSelected(node); 25 } else { 26 getUINativeModule().menuitem.setMenuItemSelected(node, this.value); 27 } 28 } 29 30 checkObjectDiff(): boolean { 31 return !isBaseOrResourceEqual(this.stageValue, this.value); 32 } 33} 34 35class LabelFontColorModifier extends ModifierWithKey<ResourceColor> { 36 constructor(value: ResourceColor) { 37 super(value); 38 } 39 static identity: Symbol = Symbol('labelfontColor'); 40 applyPeer(node: KNode, reset: boolean): void { 41 if (reset) { 42 getUINativeModule().menuitem.resetLabelFontColor(node); 43 } else { 44 getUINativeModule().menuitem.setLabelFontColor(node, this.value); 45 } 46 } 47 48 checkObjectDiff(): boolean { 49 if (isResource(this.stageValue) && isResource(this.value)) { 50 return !isResourceEqual(this.stageValue, this.value); 51 } else { 52 return true; 53 } 54 } 55} 56 57class ContentFontColorModifier extends ModifierWithKey<ResourceColor> { 58 constructor(value: ResourceColor) { 59 super(value); 60 } 61 static identity: Symbol = Symbol('contentfontColor'); 62 applyPeer(node: KNode, reset: boolean): void { 63 if (reset) { 64 getUINativeModule().menuitem.resetContentFontColor(node); 65 } else { 66 getUINativeModule().menuitem.setContentFontColor(node, this.value); 67 } 68 } 69 70 checkObjectDiff(): boolean { 71 if (isResource(this.stageValue) && isResource(this.value)) { 72 return !isResourceEqual(this.stageValue, this.value); 73 } else { 74 return true; 75 } 76 } 77} 78 79class LabelFontModifier extends ModifierWithKey<Font> { 80 constructor(value: Font) { 81 super(value); 82 } 83 static identity: Symbol = Symbol('labelFont'); 84 applyPeer(node: KNode, reset: boolean): void { 85 if (reset || !this.value) { 86 getUINativeModule().menuitem.resetLabelFont(node); 87 } else { 88 getUINativeModule().menuitem.setLabelFont(node, (this.value as Font).size, 89 (this.value as Font).weight, (this.value as Font).family, 90 (this.value as Font).style); 91 } 92 } 93 94 checkObjectDiff(): boolean { 95 let sizeEQ = isBaseOrResourceEqual(this.stageValue.size, this.value.size); 96 let weightEQ = this.stageValue.weight === this.value.weight; 97 let familyEQ = isBaseOrResourceEqual(this.stageValue.family, this.value.family); 98 let styleEQ = this.stageValue.style === this.value.style; 99 return !sizeEQ || !weightEQ || !familyEQ || !styleEQ; 100 } 101} 102 103class ContentFontModifier extends ModifierWithKey<Font> { 104 constructor(value: Font) { 105 super(value); 106 } 107 static identity: Symbol = Symbol('contentFont'); 108 applyPeer(node: KNode, reset: boolean): void { 109 if (reset || !this.value) { 110 getUINativeModule().menuitem.resetContentFont(node); 111 } else { 112 getUINativeModule().menuitem.setContentFont(node, (this.value as Font).size, 113 (this.value as Font).weight, (this.value as Font).family, 114 (this.value as Font).style); 115 } 116 } 117 118 checkObjectDiff(): boolean { 119 let sizeEQ = isBaseOrResourceEqual(this.stageValue.size, this.value.size); 120 let weightEQ = this.stageValue.weight === this.value.weight; 121 let familyEQ = isBaseOrResourceEqual(this.stageValue.family, this.value.family); 122 let styleEQ = this.stageValue.style === this.value.style; 123 return !sizeEQ || !weightEQ || !familyEQ || !styleEQ; 124 } 125} 126 127class MenuItemSelectIconModifier extends ModifierWithKey<boolean | ResourceStr> { 128 constructor(value: boolean | ResourceStr) { 129 super(value); 130 } 131 static identity: Symbol = Symbol('selectIcon'); 132 applyPeer(node: KNode, reset: boolean): void { 133 if (reset || !this.value) { 134 getUINativeModule().menuitem.resetSelectIcon(node); 135 } else { 136 getUINativeModule().menuitem.setSelectIcon(node, this.value); 137 } 138 } 139 checkObjectDiff(): boolean { 140 return !isBaseOrResourceEqual(this.stageValue, this.value); 141 } 142} 143 144class ArkMenuItemComponent extends ArkComponent implements MenuItemAttribute { 145 constructor(nativePtr: KNode, classType?: ModifierType) { 146 super(nativePtr, classType); 147 } 148 selected(value: boolean): this { 149 modifierWithKey(this._modifiersWithKeys, MenuItemSelectedModifier.identity, MenuItemSelectedModifier, value); 150 return this; 151 } 152 selectIcon(value: boolean | ResourceStr): this { 153 modifierWithKey(this._modifiersWithKeys, MenuItemSelectIconModifier.identity, MenuItemSelectIconModifier, value); 154 return this; 155 } 156 onChange(callback: (selected: boolean) => void): this { 157 throw new Error('Method not implemented.'); 158 } 159 contentFont(value: Font): this { 160 modifierWithKey(this._modifiersWithKeys, ContentFontModifier.identity, ContentFontModifier, value); 161 return this; 162 } 163 contentFontColor(value: ResourceColor): this { 164 modifierWithKey(this._modifiersWithKeys, ContentFontColorModifier.identity, ContentFontColorModifier, value); 165 return this; 166 } 167 labelFont(value: Font): this { 168 modifierWithKey(this._modifiersWithKeys, LabelFontModifier.identity, LabelFontModifier, value); 169 return this; 170 } 171 labelFontColor(value: ResourceColor): this { 172 modifierWithKey(this._modifiersWithKeys, LabelFontColorModifier.identity, LabelFontColorModifier, value); 173 return this; 174 } 175} 176// @ts-ignore 177globalThis.MenuItem.attributeModifier = function (modifier: ArkComponent): void { 178 attributeModifierFunc.call(this, modifier, (nativePtr: KNode) => { 179 return new ArkMenuItemComponent(nativePtr); 180 }, (nativePtr: KNode, classType: ModifierType, modifierJS: ModifierJS) => { 181 return new modifierJS.MenuItemModifier(nativePtr, classType); 182 }); 183}; 184