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