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