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 MenuFontColorModifier extends ModifierWithKey<ResourceColor> { 18 static identity: Symbol = Symbol('fontColor'); 19 applyPeer(node: KNode, reset: boolean): void { 20 if (reset) { 21 getUINativeModule().menu.resetMenuFontColor(node); 22 } else { 23 getUINativeModule().menu.setMenuFontColor(node, this.value); 24 } 25 } 26 27 checkObjectDiff(): boolean { 28 if (isResource(this.stageValue) && isResource(this.value)) { 29 return !isResourceEqual(this.stageValue, this.value); 30 } else { 31 return true; 32 } 33 } 34} 35 36class MenuFontModifier extends ModifierWithKey<Font> { 37 static identity: Symbol = Symbol('font'); 38 applyPeer(node: KNode, reset: boolean): void { 39 if (reset || !this.value) { 40 getUINativeModule().menu.resetFont(node); 41 } else { 42 getUINativeModule().menu.setFont(node, 43 this.value.size, 44 this.value.weight, 45 this.value.family, 46 this.value.style); 47 } 48 } 49 50 checkObjectDiff(): boolean { 51 let sizeEQ = isBaseOrResourceEqual(this.stageValue.size, this.value.size); 52 let weightEQ = this.stageValue.weight === this.value.weight; 53 let familyEQ = isBaseOrResourceEqual(this.stageValue.family, this.value.family); 54 let styleEQ = this.stageValue.style === this.value.style; 55 return !sizeEQ || !weightEQ || !familyEQ || !styleEQ; 56 } 57} 58 59class RadiusModifier extends ModifierWithKey<Dimension | BorderRadiuses> { 60 static identity: Symbol = Symbol('radius'); 61 applyPeer(node: KNode, reset: boolean): void { 62 if (reset) { 63 getUINativeModule().menu.resetRadius(node); 64 } else { 65 if (isNumber(this.value) || isString(this.value) || isResource(this.value)) { 66 getUINativeModule().menu.setRadius(node, this.value, this.value, this.value, this.value, false); 67 } else { 68 getUINativeModule().menu.setRadius(node, 69 (this.value as BorderRadiuses).topLeft, 70 (this.value as BorderRadiuses).topRight, 71 (this.value as BorderRadiuses).bottomLeft, 72 (this.value as BorderRadiuses).bottomRight, 73 true); 74 } 75 } 76 } 77 78 checkObjectDiff(): boolean { 79 if (isResource(this.stageValue) && isResource(this.value)) { 80 return !isResourceEqual(this.stageValue, this.value); 81 } else if (!isResource(this.stageValue) && !isResource(this.value)) { 82 return !((this.stageValue as BorderRadiuses).topLeft === (this.value as BorderRadiuses).topLeft && 83 (this.stageValue as BorderRadiuses).topRight === (this.value as BorderRadiuses).topRight && 84 (this.stageValue as BorderRadiuses).bottomLeft === (this.value as BorderRadiuses).bottomLeft && 85 (this.stageValue as BorderRadiuses).bottomRight === (this.value as BorderRadiuses).bottomRight); 86 } else { 87 return true; 88 } 89 } 90} 91 92class MenuWidthModifier extends ModifierWithKey<Length> { 93 constructor(value: Length) { 94 super(value); 95 } 96 static identity: Symbol = Symbol('menuWidth'); 97 applyPeer(node: KNode, reset: boolean): void { 98 if (reset) { 99 getUINativeModule().menu.resetWidth(node); 100 } else { 101 getUINativeModule().menu.setWidth(node, this.value); 102 } 103 } 104 105 checkObjectDiff(): boolean { 106 return !isBaseOrResourceEqual(this.stageValue, this.value); 107 } 108} 109 110class MenuItemDividerModifier extends ModifierWithKey<DividerStyleOptions> { 111 constructor(value: DividerStyleOptions) { 112 super(value); 113 } 114 static identity: Symbol = Symbol('menuItemDivider'); 115 applyPeer(node: KNode, reset: boolean): void { 116 if (reset || !this.value) { 117 getUINativeModule().menu.resetMenuItemDivider(node); 118 } else { 119 getUINativeModule().menu.setMenuItemDivider(node, this.value.strokeWidth, this.value.color, this.value.startMargin, this.value.endMargin); 120 } 121 } 122 123 checkObjectDiff(): boolean { 124 if (isResource(this.stageValue) && isResource(this.value)) { 125 return !isResourceEqual(this.stageValue, this.value); 126 } else if (!isResource(this.stageValue) && !isResource(this.value)) { 127 return !((this.stageValue as DividerStyleOptions).strokeWidth === (this.value as DividerStyleOptions).strokeWidth && 128 (this.stageValue as DividerStyleOptions).color === (this.value as DividerStyleOptions).color && 129 (this.stageValue as DividerStyleOptions).startMargin === (this.value as DividerStyleOptions).startMargin && 130 (this.stageValue as DividerStyleOptions).endMargin === (this.value as DividerStyleOptions).endMargin); 131 } else { 132 return true; 133 } 134 } 135} 136 137class MenuItemGroupDividerModifier extends ModifierWithKey<DividerStyleOptions> { 138 constructor(value: DividerStyleOptions) { 139 super(value); 140 } 141 static identity: Symbol = Symbol('menuItemGroupDivider'); 142 applyPeer(node: KNode, reset: boolean): void { 143 if (reset || !this.value) { 144 getUINativeModule().menu.resetMenuItemGroupDivider(node); 145 } else { 146 getUINativeModule().menu.setMenuItemGroupDivider(node, this.value.strokeWidth, this.value.color, this.value.startMargin, this.value.endMargin); 147 } 148 } 149 150 checkObjectDiff(): boolean { 151 if (isResource(this.stageValue) && isResource(this.value)) { 152 return !isResourceEqual(this.stageValue, this.value); 153 } else if (!isResource(this.stageValue) && !isResource(this.value)) { 154 return !((this.stageValue as DividerStyleOptions).strokeWidth === (this.value as DividerStyleOptions).strokeWidth && 155 (this.stageValue as DividerStyleOptions).color === (this.value as DividerStyleOptions).color && 156 (this.stageValue as DividerStyleOptions).startMargin === (this.value as DividerStyleOptions).startMargin && 157 (this.stageValue as DividerStyleOptions).endMargin === (this.value as DividerStyleOptions).endMargin); 158 } else { 159 return true; 160 } 161 } 162} 163 164class SubMenuExpandingModeModifier extends ModifierWithKey<number> { 165 constructor(value: number) { 166 super(value); 167 } 168 static identity: Symbol = Symbol('subMenuExpandingMode'); 169 applyPeer(node: KNode, reset: boolean): void { 170 if (reset) { 171 getUINativeModule().menu.resetSubMenuExpandingMode(node); 172 } else { 173 getUINativeModule().menu.setSubMenuExpandingMode(node, this.value); 174 } 175 } 176} 177 178class ArkMenuComponent extends ArkComponent implements MenuAttribute { 179 constructor(nativePtr: KNode, classType?: ModifierType) { 180 super(nativePtr, classType); 181 } 182 width(value: Length): this { 183 modifierWithKey(this._modifiersWithKeys, MenuWidthModifier.identity, MenuWidthModifier, value); 184 return this; 185 } 186 fontSize(value: any): this { 187 throw new Error('Method not implemented.'); 188 } 189 font(value: Font): this { 190 modifierWithKey(this._modifiersWithKeys, MenuFontModifier.identity, MenuFontModifier, value); 191 return this; 192 } 193 fontColor(value: ResourceColor): this { 194 modifierWithKey(this._modifiersWithKeys, MenuFontColorModifier.identity, MenuFontColorModifier, value); 195 return this; 196 } 197 radius(value: any): this { 198 modifierWithKey(this._modifiersWithKeys, RadiusModifier.identity, RadiusModifier, value); 199 return this; 200 } 201 menuItemDivider(value: DividerStyleOptions): this { 202 modifierWithKey(this._modifiersWithKeys, MenuItemDividerModifier.identity, MenuItemDividerModifier, value); 203 return this; 204 } 205 menuItemGroupDivider(value: DividerStyleOptions): this { 206 modifierWithKey(this._modifiersWithKeys, MenuItemGroupDividerModifier.identity, MenuItemGroupDividerModifier, value); 207 return this; 208 } 209 subMenuExpandingMode(value: SubMenuExpandingMode): this { 210 modifierWithKey(this._modifiersWithKeys, SubMenuExpandingModeModifier.identity, SubMenuExpandingModeModifier, value); 211 return this; 212 } 213} 214 215// @ts-ignore 216globalThis.Menu.attributeModifier = function (modifier: ArkComponent): void { 217 attributeModifierFunc.call(this, modifier, (nativePtr: KNode) => { 218 return new ArkMenuComponent(nativePtr); 219 }, (nativePtr: KNode, classType: ModifierType, modifierJS: ModifierJS) => { 220 return new modifierJS.MenuModifier(nativePtr, classType); 221 }); 222}; 223